Skip to content

Commit

Permalink
Fix multipart parsing of binary content in UTF8 mode
Browse files Browse the repository at this point in the history
The boundary regexp should use the 'n' multibyte switch to disable multibyte processing on the multipart content bodies.
  • Loading branch information
Jonathan del Strother authored and josh committed Jan 17, 2009
1 parent 0f99727 commit 7377caf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rack/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ def self.parse_multipart(env)
status = input.read(boundary_size)
raise EOFError, "bad content body" unless status == boundary + EOL

rx = /(?:#{EOL})?#{Regexp.quote boundary}(#{EOL}|--)/
rx = /(?:#{EOL})?#{Regexp.quote boundary}(#{EOL}|--)/n

loop {
head = nil
Expand Down
27 changes: 27 additions & 0 deletions test/spec_rack_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,33 @@
lambda { req.POST }.should.raise(EOFError)
end

specify "shouldn't try to interpret binary as utf8" do
begin
original_kcode = $KCODE
$KCODE='UTF8'

input = <<EOF
--AaB03x\r
content-disposition: form-data; name="fileupload"; filename="junk.a"\r
content-type: application/octet-stream\r
\r
#{[0x36,0xCF,0x0A,0xF8].pack('c*')}\r
--AaB03x--\r
EOF

req = Rack::Request.new Rack::MockRequest.env_for("/",
"CONTENT_TYPE" => "multipart/form-data, boundary=AaB03x",
"CONTENT_LENGTH" => input.size,
:input => input)

lambda{req.POST}.should.not.raise(EOFError)
req.POST["fileupload"][:tempfile].size.should.equal 4
ensure
$KCODE = original_kcode
end
end


specify "should work around buggy 1.8.* Tempfile equality" do
input = <<EOF
--AaB03x\r
Expand Down

0 comments on commit 7377caf

Please sign in to comment.