Skip to content

Commit b69cce5

Browse files
committed
Detect content type correctly if raised any low level errors\n
If Rack level errors raise, content type of the request couldn't be detected correctly. Basically there are two types of errors might happen in Rack level, Rack::Utils::ParameterTypeError and Rack::Utils::InvalidParameterError.\n Passing query parameters like `x[y]=1&x[y]z=2` and `foo%81E=1` will raise the Rack level errors and the content type couldn't be detected correctly.
1 parent 864568d commit b69cce5

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#### Fixes
1313

14+
* [#1427](https://github.com/ruby-grape/grape/pull/1427): Detect content type in case of raising Rack level errors - [@iCEAGE](https://github.com/iCEAGE)
1415
* [#1405](https://github.com/ruby-grape/grape/pull/1405): Fix priority of `rescue_from` clauses applying - [@hedgesky](https://github.com/hedgesky).
1516
* [#1365](https://github.com/ruby-grape/grape/pull/1365): Fix finding exception handler in error middleware - [@ktimothy](https://github.com/ktimothy).
1617
* [#1380](https://github.com/ruby-grape/grape/pull/1380): Fix `allow_blank: false` for `Time` attributes with valid values causes `NoMethodError` - [@ipkes](https://github.com/ipkes).

Diff for: lib/grape/middleware/formatter.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'grape/middleware/base'
2+
require 'rack/utils'
23

34
module Grape
45
module Middleware
@@ -139,7 +140,11 @@ def format_from_extension
139140
end
140141

141142
def format_from_params
142-
fmt = Rack::Utils.parse_nested_query(env[Grape::Http::Headers::QUERY_STRING])[Grape::Http::Headers::FORMAT]
143+
fmt = begin
144+
Rack::Utils.parse_nested_query(env[Grape::Http::Headers::QUERY_STRING])[Grape::Http::Headers::FORMAT]
145+
rescue Rack::Utils::ParameterTypeError, Rack::Utils::InvalidParameterError
146+
nil
147+
end
143148
# avoid symbol memory leak on an unknown format
144149
return fmt.to_sym if content_type_for(fmt)
145150
fmt

Diff for: spec/grape/middleware/formatter_spec.rb

+18
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,24 @@ def to_xml
117117
expect(subject.env['api.format']).to eq(:json)
118118
end
119119

120+
it 'uses the requested format with invalid parameter type if provided in headers' do
121+
_, headers, = subject.call(
122+
'PATH_INFO' => '/info',
123+
'QUERY_STRING' => 'id=12&id[]=12',
124+
'HTTP_ACCEPT' => 'application/json'
125+
)
126+
expect(headers['Content-type']).to eq('application/json')
127+
end
128+
129+
it 'uses the requested format with invalid byte sequence in UTF-8 if provided in headers' do
130+
_, headers, = subject.call(
131+
'PATH_INFO' => '/info',
132+
'QUERY_STRING' => 'foo%81E=1',
133+
'HTTP_ACCEPT' => 'application/json'
134+
)
135+
expect(headers['Content-type']).to eq('application/json')
136+
end
137+
120138
it 'handles quality rankings mixed with nothing' do
121139
subject.call('PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/json,application/xml; q=1.0')
122140
expect(subject.env['api.format']).to eq(:xml)

0 commit comments

Comments
 (0)