diff --git a/Manifest.txt b/Manifest.txt index da43167d..99826ef4 100644 --- a/Manifest.txt +++ b/Manifest.txt @@ -128,6 +128,7 @@ test/fixtures/binary-content-transfer-encoding-2.eml test/fixtures/blank-header-fields.eml test/fixtures/contacts.txt test/fixtures/embedded-message.eml +test/fixtures/invalid-date.eml test/fixtures/mailing-list-header.eml test/fixtures/malicious-attachment-names.eml test/fixtures/missing-from-to.eml diff --git a/lib/sup/message.rb b/lib/sup/message.rb index 0a0bcc6e..3354507c 100644 --- a/lib/sup/message.rb +++ b/lib/sup/message.rb @@ -103,7 +103,7 @@ def parse_header encoded_header when Time date when String - Time.parse date rescue nil + Time.rfc2822 date rescue nil end @date = location.fallback_date if @date.nil? @date = Time.utc 1970, 1, 1 if @date.nil? diff --git a/test/dummy_source.rb b/test/dummy_source.rb index d8515313..1e2492a0 100644 --- a/test/dummy_source.rb +++ b/test/dummy_source.rb @@ -10,10 +10,12 @@ module Redwood class DummySource < Source attr_accessor :messages + attr_writer :fallback_date def initialize uri, last_date=nil, usual=true, archived=false, id=nil, labels=[] super uri, usual, archived, id @messages = nil + @fallback_date = Time.utc 2001, 2, 3, 4, 56, 57 end def start_offset @@ -61,7 +63,7 @@ def each_raw_message_line id end def fallback_date_for_message id - Time.utc 2001, 2, 3, 4, 56, 57 + @fallback_date end end diff --git a/test/fixtures/invalid-date.eml b/test/fixtures/invalid-date.eml new file mode 100644 index 00000000..0ab49962 --- /dev/null +++ b/test/fixtures/invalid-date.eml @@ -0,0 +1,8 @@ +To: person@example.invalid +Subject: pre-RFC2822 Date header +Content-type: text/plain +From: anotherperson@example.invalid +Message-Id: <6965470922807932796@example.invalid> +Date: ons, 26 maj 1999 11:00:34 +0200 (CEST) + +A message body. diff --git a/test/test_message.rb b/test/test_message.rb index d009a01e..3a8a1f48 100644 --- a/test/test_message.rb +++ b/test/test_message.rb @@ -359,6 +359,19 @@ def test_malicious_attachment_names fn = chunks[3].safe_filename assert_equal(fn, File.basename(fn)) end + + def test_invalid_date_header + fallback_date = Time.utc 2024, 5, 12, 15, 5, 56 + source = DummySource.new("sup-test://test_invalid_date_header") + source.messages = [ fixture_path("invalid-date.eml") ] + source.fallback_date = fallback_date + + sup_message = Message.build_from_source(source, 0) + sup_message.load_from_source! + + assert_equal(fallback_date, sup_message.date) + end + # TODO: test different error cases, malformed messages etc. # TODO: test different quoting styles, see that they are all divided