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

Handle Rack errors when too many files are uploaded #2256

Merged
merged 12 commits into from
Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* [#2227](https://github.com/ruby-grape/grape/pull/2222): Rename `MissingGroupType` and `UnsupportedGroupType` exceptions - [@ericproulx](https://github.com/ericproulx).
* [#2244](https://github.com/ruby-grape/grape/pull/2244): Fix a breaking change in `Grape::Validations` provided in 1.6.1 - [@dm1try](https://github.com/dm1try).
* [#2250](https://github.com/ruby-grape/grape/pull/2250): Add deprecation warning for `UnsupportedGroupTypeError` and `MissingGroupTypeError` - [@ericproulx](https://github.com/ericproulx).
* [#2256](https://github.com/ruby-grape/grape/pull/2256): Raise Grape::Exceptions::MultipartPartLimitError from Rack when too many files are uploaded - [@bschmeck](https://github.com/bschmeck).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Back-tick quote Grape::Exceptions::MultipartPartLimitError

* Your contribution here.

### 1.6.2 (2021/12/30)
Expand Down
1 change: 1 addition & 0 deletions lib/grape.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ module Exceptions
autoload :MethodNotAllowed
autoload :InvalidResponse
autoload :EmptyMessageBody
autoload :TooManyMultipartFiles
autoload :MissingGroupTypeError, 'grape/exceptions/missing_group_type'
autoload :UnsupportedGroupTypeError, 'grape/exceptions/unsupported_group_type'
end
Expand Down
11 changes: 11 additions & 0 deletions lib/grape/exceptions/too_many_multipart_files.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module Grape
module Exceptions
class TooManyMultipartFiles < Base
def initialize
super(message: compose_message(:too_many_multipart_files), status: 413)
end
end
end
end
1 change: 1 addition & 0 deletions lib/grape/locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ en:
%{body_format} in the request's 'body'
"
empty_message_body: 'Empty message body supplied with %{body_format} content-type'
too_many_multipart_files: "The number of uploaded files exceeded the system's configured limit"
invalid_accept_header:
problem: 'Invalid accept header'
resolution: '%{message}'
Expand Down
2 changes: 2 additions & 0 deletions lib/grape/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def params
@params ||= build_params
rescue EOFError
raise Grape::Exceptions::EmptyMessageBody.new(content_type)
rescue Rack::Multipart::MultipartPartLimitError
raise Grape::Exceptions::TooManyMultipartFiles
end

def headers
Expand Down
21 changes: 21 additions & 0 deletions spec/grape/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,27 @@ def app
end
end

context 'when the limit on multipart files is exceeded' do
around do |example|
limit = Rack::Utils.multipart_part_limit
Rack::Utils.multipart_part_limit = 1
example.run
Rack::Utils.multipart_part_limit = limit
end

it 'returns a 413 if given too many multipart files' do
subject.params do
requires :file, type: Rack::Multipart::UploadedFile
end
subject.post '/upload' do
params[:file][:filename]
end
post '/upload', { file: Rack::Test::UploadedFile.new(__FILE__, 'text/plain'), extra: Rack::Test::UploadedFile.new(__FILE__, 'text/plain') }
expect(last_response.status).to eq(413)
expect(last_response.body).to eq("The number of uploaded files exceeded the system's configured limit")
end
end

it 'responds with a 415 for an unsupported content-type' do
subject.format :json
# subject.content_type :json, "application/json"
Expand Down