Skip to content

Commit

Permalink
Only increment the opened_files counter for file inputs. If you have a
Browse files Browse the repository at this point in the history
large form with only one file, you can quickly hit the MultipartPartLimitError
Add test for not hitting the multipart limit
  • Loading branch information
johnnaegle committed Mar 9, 2015
1 parent 1a43de5 commit 7331561
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/rack/multipart/parser.rb
Expand Up @@ -54,14 +54,15 @@ def parse

opened_files = 0
loop do
if Utils.multipart_part_limit > 0
raise MultipartPartLimitError, 'Maximum file multiparts in content reached' if opened_files >= Utils.multipart_part_limit
opened_files += 1
end

head, filename, content_type, name, body =
get_current_head_and_filename_and_content_type_and_name_and_body

if Utils.multipart_part_limit > 0
opened_files += 1 if filename
raise MultipartPartLimitError, 'Maximum file multiparts in content reached' if opened_files >= Utils.multipart_part_limit
end

# Save the rest.
if i = @buf.index(rx)
body << @buf.slice!(0, i)
Expand Down
31 changes: 31 additions & 0 deletions test/multipart/three_files_three_fields
@@ -0,0 +1,31 @@
--AaB03x
content-disposition: form-data; name="reply"

yes
--AaB03x
content-disposition: form-data; name="to"

people
--AaB03x
content-disposition: form-data; name="from"

others
--AaB03x
content-disposition: form-data; name="fileupload1"; filename="file1.jpg"
Content-Type: image/jpeg
Content-Transfer-Encoding: base64

/9j/4AAQSkZJRgABAQAAAQABAAD//gA+Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcg
--AaB03x
content-disposition: form-data; name="fileupload2"; filename="file2.jpg"
Content-Type: image/jpeg
Content-Transfer-Encoding: base64

/9j/4AAQSkZJRgABAQAAAQABAAD//gA+Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcg
--AaB03x
content-disposition: form-data; name="fileupload3"; filename="file3.jpg"
Content-Type: image/jpeg
Content-Transfer-Encoding: base64

/9j/4AAQSkZJRgABAQAAAQABAAD//gA+Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcg
--AaB03x--
27 changes: 27 additions & 0 deletions test/spec_multipart.rb
Expand Up @@ -476,6 +476,33 @@ def rd.length
end
end

should "not reach a multi-part limit" do
begin
previous_limit = Rack::Utils.multipart_part_limit
Rack::Utils.multipart_part_limit = 4

env = Rack::MockRequest.env_for '/', multipart_fixture(:three_files_three_fields)
params = Rack::Multipart.parse_multipart(env)
params['reply'].should.equal 'yes'
params['to'].should.equal 'people'
params['from'].should.equal 'others'
ensure
Rack::Utils.multipart_part_limit = previous_limit
end
end

should "reach a multipart limit" do
begin
previous_limit = Rack::Utils.multipart_part_limit
Rack::Utils.multipart_part_limit = 3

env = Rack::MockRequest.env_for '/', multipart_fixture(:three_files_three_fields)
lambda { Rack::Multipart.parse_multipart(env) }.should.raise(Rack::Multipart::MultipartPartLimitError)
ensure
Rack::Utils.multipart_part_limit = previous_limit
end
end

should "return nil if no UploadedFiles were used" do
data = Rack::Multipart.build_multipart("people" => [{"submit-name" => "Larry", "files" => "contents"}])
data.should.equal nil
Expand Down

0 comments on commit 7331561

Please sign in to comment.