Skip to content

Commit bba1a9d

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 bba1a9d

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

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): Handling an invalid query string when trying to detect the response format - [@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).

lib/grape/middleware/formatter.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,11 @@ def format_from_extension
139139
end
140140

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

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)