Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Handle invalid date in Received header #593

Open
wants to merge 1 commit into from

1 participant

@okkez

Raise ArgumentError If invalid date in Received header.
On the other hand, nothing raised if invalid date in Date header.
So I think mail library should not raise ArgumentError if invalid date in Received header.

This patch can improve robustness.
I created this patch for 2-5-stable branch but it may be applied for master.
(I have not checked for master yet.)

Thanks!

Related issues: #564, #216

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 5, 2013
  1. @okkez
This page is out of date. Refresh to see the latest.
View
7 lib/mail/elements/received_element.rb
@@ -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)
View
2  lib/mail/fields/received_field.rb
@@ -44,7 +44,7 @@ def element
end
def date_time
- @datetime ||= ::DateTime.parse("#{element.date_time}")
+ @datetime ||= element.date_time
end
def info
View
10 spec/mail/fields/received_field_spec.rb
@@ -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
Something went wrong with that request. Please try again.