lib/PublicInbox.pm | 19 ++++++++++++++++--- t/precheck.t | 9 ++++++--- diff --git a/lib/PublicInbox.pm b/lib/PublicInbox.pm index b05fd8c5607853af1050054162818b6ecbdd4706..cfa9d4bf9d9ca6dd8baf06eaf918fbb3a379e150 100644 --- a/lib/PublicInbox.pm +++ b/lib/PublicInbox.pm @@ -4,6 +4,7 @@ package PublicInbox; use strict; use warnings; use Email::Address; +use Date::Parse qw(strptime); use constant MAX_SIZE => 1024 * 500; # same as spamc default # drop plus addressing for matching @@ -17,11 +18,23 @@ # do not allow Bcc, only Cc and To if recipient is set sub precheck { my ($klass, $filter, $recipient) = @_; my $simple = $filter->simple; - return 0 unless $simple->header("Message-ID"); - return 0 unless defined($filter->from); - return 0 unless $simple->header("Subject"); + my $mid = $simple->header("Message-ID"); + return 0 unless usable_str(length(''), $mid) && $mid =~ /\@/; + return 0 unless usable_str(length('u@h'), $filter->from); + return 0 unless usable_str(length(':o'), $simple->header("Subject")); + return 0 unless usable_date($simple->header("Date")); return 0 if length($simple->as_string) > MAX_SIZE; recipient_specified($filter, $recipient); +} + +sub usable_str { + my ($len, $str) = @_; + defined($str) && length($str) >= $len; +} + +sub usable_date { + my @t = eval { strptime(@_) }; + scalar @t; } sub recipient_specified { diff --git a/t/precheck.t b/t/precheck.t index 1bfa4c9f644f5d13028922485838d5b700896004..acfd5e8b46f0530ccbc1672fdc7687d8204af7e8 100644 --- a/t/precheck.t +++ b/t/precheck.t @@ -36,7 +36,8 @@ To => 'b@example.com', Cc => 'c@example.com', 'Content-Type' => 'text/plain', Subject => 'this is a subject', - 'Message-ID' => '', + 'Message-ID' => '', + Date => 'Wed, 09 Apr 2014 01:28:34 +0000', ], body => "hello world\n", )); @@ -50,7 +51,8 @@ To => 'b+plus@example.com', Cc => 'John Doe ', 'Content-Type' => 'text/plain', Subject => 'this is a subject', - 'Message-ID' => '', + 'Message-ID' => '', + Date => 'Wed, 09 Apr 2014 01:28:34 +0000', ], body => "hello world\n", )); @@ -64,7 +66,8 @@ To => 'b@example.com', Cc => 'c@example.com', 'Content-Type' => 'text/plain', Subject => 'this is a subject', - 'Message-ID' => '', + 'Message-ID' => '', + Date => 'Wed, 09 Apr 2014 01:28:34 +0000', ], body => "hello world\n", );