Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Split on an exact \r\n\r\n sequence if it exists #501

Merged
merged 1 commit into from

3 participants

@ConradIrwin
Collaborator

While the optional whitespace thing is kind of nice, that shouldn't cause us to ignore the spec sometimes.

@jeremy
Collaborator

:+1: to this. Using an example mail is a pretty big hammer though. Could you do a more precise spec in message_spec?

@ConradIrwin
Collaborator

I've updated the branch with some smaller specs that cover a range of splitting behaviour. Is that better?

@jeremy jeremy merged commit 1d515f7 into mikel:master
@jeremy jeremy referenced this pull request from a commit
@jeremy jeremy Update CHANGELOG for #501 2cf92b8
@leifbladt

@jeremy Any chance to see this in a new release soon, since Rails 3.2.13 requires mail-0.5.3?

@jeremy
Collaborator
@jeremy

This means we'll split headers/body on certain pathological folded header values:

diff --git a/spec/mail/message_spec.rb b/spec/mail/message_spec.rb
index 259482d..444f4ae 100644
--- a/spec/mail/message_spec.rb
+++ b/spec/mail/message_spec.rb
@@ -216,6 +216,11 @@ describe Mail::Message do
         message.decoded.should == " Hello there\n"
       end

+      it 'should not split the body at an empty folded header value' do
+        message = Mail::Message.new("To: foo\r\n \r\nFrom: bar\r\n\r\nbody\r\n")
+        message.decoded.should == "body\n"
+      end
+
       it 'should split only once if there are "\r\n\r\n"s in the body' do
         message = Mail::Message.new("To: Example <example@cirw.in>\r\n\r\nHello\r\n\r\nthere\r\n")
         message.decoded.should == "Hello\n\nthere\n"
  1) Mail::Message initialization splitting should not split the body at an empty folded header value
     Failure/Error: message.decoded.should == "body\n"
       expected: "body\n"
            got: "From: bar\n\nbody\n" (using ==)
       Diff:
       @@ -1,2 +1,4 @@
       +From: bar
       +
        body
Collaborator

This was preserving previous behaviour of the mail gem. I'd be happy to drop it, but wanted to err on the side of minimal change.

Collaborator

Hmm yeah. Hard to tell whether the previous behavior was accidental or intentional.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 10, 2013
  1. @ConradIrwin

    Split on an exact \r\n\r\n sequence if it exists

    ConradIrwin authored
    This has been broken since 17783f8
This page is out of date. Refresh to see the latest.
Showing with 39 additions and 1 deletion.
  1. +1 −1  lib/mail/message.rb
  2. +38 −0 spec/mail/message_spec.rb
View
2  lib/mail/message.rb
@@ -1889,7 +1889,7 @@ def text?
# Additionally, I allow for the case where someone might have put whitespace
# on the "gap line"
def parse_message
- header_part, body_part = raw_source.lstrip.split(/#{CRLF}#{WSP}*#{CRLF}(?!#{WSP})/m, 2)
+ header_part, body_part = raw_source.lstrip.split(/#{CRLF}#{CRLF}|#{CRLF}#{WSP}*#{CRLF}(?!#{WSP})/m, 2)
self.header = header_part
self.body = body_part
end
View
38 spec/mail/message_spec.rb
@@ -189,6 +189,44 @@ def basic_email
deserialized.parts.map(&:body).should == ['body', '<b>body</b>']
end
end
+
+ describe "splitting" do
+ it "should split the body from the header" do
+ message = Mail::Message.new("To: Example <example@cirw.in>\r\n\r\nHello there\r\n")
+ message.decoded.should == "Hello there\n"
+ end
+
+ it "should split when the body starts with a space" do
+ message = Mail::Message.new("To: Example <example@cirw.in>\r\n\r\n Hello there\r\n")
+ message.decoded.should == " Hello there\n"
+ end
+
+ it "should split if the body starts with an empty line" do
+ message = Mail::Message.new("To: Example <example@cirw.in>\r\n\r\n\r\nHello there\r\n")
+ message.decoded.should == "\nHello there\n"
+ end
+
+ it "should split if the body starts with a blank line" do
+ message = Mail::Message.new("To: Example <example@cirw.in>\r\n\r\n\t\r\nHello there\r\n")
+ message.decoded.should == "\t\nHello there\n"
+ end
+
+ it 'should split after headers that contain "\r\n "' do
+ message = Mail::Message.new("To: Example\r\n <example@cirw.in>\r\n\r\n Hello there\r\n")
+ message.decoded.should == " Hello there\n"
+ end
+
+ it 'should split only once if there are "\r\n\r\n"s in the body' do
+ message = Mail::Message.new("To: Example <example@cirw.in>\r\n\r\nHello\r\n\r\nthere\r\n")
+ message.decoded.should == "Hello\n\nthere\n"
+ end
+
+ # N.B. this is not in any RFCs
+ it "should split on a line with whitespace on it" do
+ message = Mail::Message.new("To: Example <example@cirw.in>\r\n \r\nHello there\r\n")
+ message.decoded.should == "Hello there\n"
+ end
+ end
end
describe "envelope line handling" do
Something went wrong with that request. Please try again.