From 7085ec55ef5bd7d770fead5c6e16000ebc1a2629 Mon Sep 17 00:00:00 2001 From: Dan Callaghan Date: Sun, 12 May 2024 16:21:47 +1000 Subject: [PATCH] use a fallback date if Date header is not valid RFC2822 Use Time.rfc2822 to parse the Date header in RFC2822 format, rather than letting Time.parse try heuristics to guess the format. If parsing fails, use the fallback date (mtime or From line timestamp) instead of letting Time.parse fill in the missing pieces from the current time. Fixes #252. --- Manifest.txt | 1 + lib/sup/message.rb | 2 +- test/dummy_source.rb | 4 +++- test/fixtures/invalid-date.eml | 8 ++++++++ test/test_message.rb | 13 +++++++++++++ 5 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/invalid-date.eml 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