Skip to content

Commit

Permalink
Handle EOFError raised by Rack
Browse files Browse the repository at this point in the history
In v2.2.0, Rack started raising an EOFError when given an empty body with a
multipart upload - rack/rack#1572  Previously, Rack had
swallowed this error.
  • Loading branch information
bschmeck committed Feb 9, 2021
1 parent 0e0ac10 commit 2676741
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

* Your contribution here.

* [#2161](https://github.com/ruby-grape/grape/pull/2157): Handle EOFError from Rack when given an empty multipart body - [@bschmeck](https://github.com/bschmeck).

### 1.5.2 (2021/02/06)

#### Features
Expand Down
2 changes: 2 additions & 0 deletions lib/grape/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ def initialize(env, **options)

def params
@params ||= build_params
rescue EOFError
raise Grape::Exceptions::InvalidMessageBody, 'multipart/form-data'
end

def headers
Expand Down
12 changes: 12 additions & 0 deletions spec/grape/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,18 @@ def app
expect(last_response.status).to eq(201)
expect(last_response.body).to eq('Bob')
end

it 'returns a 400 if given an invalid multipart body' do
subject.params do
requires :file, type: Rack::Multipart::UploadedFile
end
subject.post '/upload' do
params[:file][:filename]
end
post '/upload', { file: '' }, 'CONTENT_TYPE' => 'multipart/form-data; boundary=foobar'
expect(last_response.status).to eq(400)
expect(last_response.body).to include('multipart/form-data')
end
end

it 'responds with a 415 for an unsupported content-type' do
Expand Down

0 comments on commit 2676741

Please sign in to comment.