Skip to content

Commit 6a21f80

Browse files
authored
Handle EOFError raised by Rack (#2161)
1 parent 52ed5d5 commit 6a21f80

File tree

8 files changed

+72
-3
lines changed

8 files changed

+72
-3
lines changed

.github/workflows/test.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ jobs:
3333
- Gemfile
3434
- gemfiles/rack1.gemfile
3535
- gemfiles/rack2.gemfile
36+
- gemfiles/rack2_2.gemfile
3637
- gemfiles/rack_edge.gemfile
3738
- gemfiles/rails_5.gemfile
3839
- gemfiles/rails_6.gemfile
@@ -76,15 +77,15 @@ jobs:
7677

7778
- name: Run tests
7879
run: bundle exec rake spec
79-
80+
8081
- name: Run tests (spec/integration/eager_load)
8182
if: ${{ matrix.gemfile == 'Gemfile' }}
8283
run: bundle exec rspec spec/integration/eager_load
8384

8485
- name: Run tests (spec/integration/multi_json)
8586
if: ${{ matrix.gemfile == 'gemfiles/multi_json.gemfile' }}
8687
run: bundle exec rspec spec/integration/multi_json
87-
88+
8889
- name: Run tests (spec/integration/multi_xml)
8990
if: ${{ matrix.gemfile == 'gemfiles/multi_xml.gemfile' }}
9091
run: bundle exec rspec spec/integration/multi_xml

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
* Your contribution here.
1010

11+
* [#2161](https://github.com/ruby-grape/grape/pull/2157): Handle EOFError from Rack when given an empty multipart body - [@bschmeck](https://github.com/bschmeck).
12+
1113
### 1.5.2 (2021/02/06)
1214

1315
#### Features

gemfiles/rack2_2.gemfile

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# frozen_string_literal: true
2+
3+
# This file was generated by Appraisal
4+
5+
source 'https://rubygems.org'
6+
7+
gem 'rack', '~> 2.2'
8+
9+
group :development, :test do
10+
gem 'bundler'
11+
gem 'hashie'
12+
gem 'rake'
13+
gem 'rubocop', '1.7.0'
14+
gem 'rubocop-ast', '1.3.0'
15+
gem 'rubocop-performance', '1.9.1', require: false
16+
end
17+
18+
group :development do
19+
gem 'appraisal'
20+
gem 'benchmark-ips'
21+
gem 'benchmark-memory'
22+
gem 'guard'
23+
gem 'guard-rspec'
24+
gem 'guard-rubocop'
25+
end
26+
27+
group :test do
28+
gem 'cookiejar'
29+
gem 'coveralls_reborn'
30+
gem 'grape-entity', '~> 0.6'
31+
gem 'maruku'
32+
gem 'mime-types'
33+
gem 'rack-jsonp', require: 'rack/jsonp'
34+
gem 'rack-test', '~> 1.1.0'
35+
gem 'rspec', '~> 3.0'
36+
gem 'ruby-grape-danger', '~> 0.2.0', require: false
37+
end
38+
39+
gemspec path: '../'

lib/grape.rb

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ module Exceptions
7676
autoload :InvalidVersionHeader
7777
autoload :MethodNotAllowed
7878
autoload :InvalidResponse
79+
autoload :EmptyMessageBody
7980
end
8081
end
8182

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
module Grape
4+
module Exceptions
5+
class EmptyMessageBody < Base
6+
def initialize(body_format)
7+
super(message: compose_message(:empty_message_body, body_format: body_format), status: 400)
8+
end
9+
end
10+
end
11+
end

lib/grape/locale/en.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ en:
4444
"when specifying %{body_format} as content-type, you must pass valid
4545
%{body_format} in the request's 'body'
4646
"
47+
empty_message_body: 'Empty message body supplied with %{body_format} content-type'
4748
invalid_accept_header:
4849
problem: 'Invalid accept header'
4950
resolution: '%{message}'
5051
invalid_version_header:
5152
problem: 'Invalid version header'
5253
resolution: '%{message}'
5354
invalid_response: 'Invalid response'
54-

lib/grape/request.rb

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ def initialize(env, **options)
1515

1616
def params
1717
@params ||= build_params
18+
rescue EOFError
19+
raise Grape::Exceptions::EmptyMessageBody, content_type
1820
end
1921

2022
def headers

spec/grape/endpoint_spec.rb

+13
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,19 @@ def app
420420
expect(last_response.status).to eq(201)
421421
expect(last_response.body).to eq('Bob')
422422
end
423+
424+
# Rack swallowed this error until v2.2.0
425+
it 'returns a 400 if given an invalid multipart body', if: Gem::Version.new(Rack.release) >= Gem::Version.new('2.2.0') do
426+
subject.params do
427+
requires :file, type: Rack::Multipart::UploadedFile
428+
end
429+
subject.post '/upload' do
430+
params[:file][:filename]
431+
end
432+
post '/upload', { file: '' }, 'CONTENT_TYPE' => 'multipart/form-data; boundary=foobar'
433+
expect(last_response.status).to eq(400)
434+
expect(last_response.body).to eq('Empty message body supplied with multipart/form-data; boundary=foobar content-type')
435+
end
423436
end
424437

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

0 commit comments

Comments
 (0)