Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only count files (not all form elements) against the Multipart File Limit #814

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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