Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle invalid date in Received header #593

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/mail/elements/received_element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ class ReceivedElement
def initialize( string )
parser = Mail::ReceivedParser.new
if tree = parser.parse(string)
@date_time = ::DateTime.parse("#{tree.date_time.date.text_value} #{tree.date_time.time.text_value}")
begin
@date_time = ::DateTime.parse("#{tree.date_time.date.text_value} #{tree.date_time.time.text_value}")
rescue ArgumentError => ex
@date_time = nil
raise ex unless ex.message == "invalid date"
end
@info = tree.name_val_list.text_value
else
raise Mail::Field::ParseError.new(ReceivedElement, string, parser.failure_reason)
Expand Down
2 changes: 1 addition & 1 deletion lib/mail/fields/received_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def element
end

def date_time
@datetime ||= ::DateTime.parse("#{element.date_time}")
@datetime ||= element.date_time
end

def info
Expand Down
10 changes: 9 additions & 1 deletion spec/mail/fields/received_field_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,13 @@
t.decoded.should eq ''
t.encoded.should eq "Received: \r\n"
end


it "should handle invalid date" do
t = Mail::ReceivedField.new('Received: from mail.example.com (192.168.1.1) by mail.example.com with (esmtp) id (qid) for <foo@example.com>; Mon, 29 Jul 2013 25:12:46 +0900')
t.name.should eq 'Received'
t.value.should eq 'from mail.example.com (192.168.1.1) by mail.example.com with (esmtp) id (qid) for <foo@example.com>; Mon, 29 Jul 2013 25:12:46 +0900'
t.info.should eq 'from mail.example.com (192.168.1.1) by mail.example.com with (esmtp) id (qid) for <foo@example.com>'
t.date_time.should eq nil
end

end