From a11002ba1a3e651f7c71235f733efbf650d8ce7d Mon Sep 17 00:00:00 2001 From: Alex Castillo Date: Tue, 8 Sep 2026 20:16:07 -0400 Subject: [PATCH] Don't crash on an impossible date in a resource filename `date_from_filename` matches `\d{4}-\d{2}-\d{2}-` and passes the parts straight to `Date.new`, so a file named `2024-99-99-post.md` raises `Date::Error: invalid date`. That propagates through `publication_date` and `published?`, so a single typo'd filename takes down the whole collection load. Rescue `Date::Error` and treat the prefix as not-a-date, the same as a filename with no date prefix at all. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01GtYgwYNfJ3wHrw9xrooVoB --- lib/perron/resource/publishable.rb | 10 +++++++++- test/perron/resource/resource_publishable_test.rb | 12 ++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/perron/resource/publishable.rb b/lib/perron/resource/publishable.rb index bfe5b55..b107aed 100644 --- a/lib/perron/resource/publishable.rb +++ b/lib/perron/resource/publishable.rb @@ -54,7 +54,15 @@ def date_from_filename return @date_from_filename if defined?(@date_from_filename) match = File.basename(file_path).match(DATE_REGEX) - @date_from_filename = match ? Date.new(match[:year].to_i, match[:month].to_i, match[:day].to_i).in_time_zone : nil + + @date_from_filename = + if match + begin + Date.new(match[:year].to_i, match[:month].to_i, match[:day].to_i).in_time_zone + rescue Date::Error + nil + end + end end end end diff --git a/test/perron/resource/resource_publishable_test.rb b/test/perron/resource/resource_publishable_test.rb index fd11d95..6f56cd3 100644 --- a/test/perron/resource/resource_publishable_test.rb +++ b/test/perron/resource/resource_publishable_test.rb @@ -1,4 +1,5 @@ require "test_helper" +require "tmpdir" class Perron::Site::Resource::PublishableTest < ActiveSupport::TestCase include ActiveSupport::Testing::TimeHelpers @@ -74,4 +75,15 @@ class Perron::Site::Resource::PublishableTest < ActiveSupport::TestCase refute public_feature.draft? end + + test "#publication_date ignores a structurally valid but impossible date in the filename" do + Dir.mktmpdir do |dir| + path = File.join(dir, "2024-99-99-typo.md") + File.write(path, "---\ntitle: Typo\n---\nBody") + resource = Content::Post.new(path) + + assert_nil resource.publication_date + assert resource.published? + end + end end