Skip to content

Commit

Permalink
use a fallback date if Date header is not valid RFC2822
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
danc86 committed May 12, 2024
1 parent 7a035e6 commit 7085ec5
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions Manifest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/sup/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
4 changes: 3 additions & 1 deletion test/dummy_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/invalid-date.eml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
To: [email protected]
Subject: pre-RFC2822 Date header
Content-type: text/plain
From: [email protected]
Message-Id: <[email protected]>
Date: ons, 26 maj 1999 11:00:34 +0200 (CEST)

A message body.
13 changes: 13 additions & 0 deletions test/test_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7085ec5

Please sign in to comment.