Skip to content

Commit 9e945a6

Browse files
committed
Validates response processed by exception handler
If block of return nil, the response would be nil. That causes system information leak. Add a validation to prevent it.
1 parent fd53891 commit 9e945a6

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* [#1758](https://github.com/ruby-grape/grape/pull/1758): Fix expanding load_path in gemspec - [@2maz](https://github.com/2maz).
2222
* [#1765](https://github.com/ruby-grape/grape/pull/1765): Use 415 when request body is of an unsupported media type - [@jdmurphy](https://github.com/jdmurphy).
2323
* [#1771](https://github.com/ruby-grape/grape/pull/1771): Fix param aliases with 'given' blocks - [@jereynolds](https://github.com/jereynolds).
24+
* [#1776](https://github.com/ruby-grape/grape/pull/1776): Validates response processed by exception handler - [@darren987469](https://github.com/darren987469).
2425

2526
### 1.0.3 (4/23/2018)
2627

lib/grape/middleware/error.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,13 @@ def run_rescue_handler(handler, error)
127127
handler = public_method(handler)
128128
end
129129

130-
handler.arity.zero? ? instance_exec(&handler) : instance_exec(error, &handler)
130+
response = handler.arity.zero? ? instance_exec(&handler) : instance_exec(error, &handler)
131+
valid_response?(response) ? response : error!('Internal Server Error(Invalid Response)')
132+
end
133+
134+
def valid_response?(response)
135+
# Rack::Response.new(...).finish generates an array with size 3
136+
response.is_a?(Array) && response.size == 3
131137
end
132138
end
133139
end

spec/grape/api_spec.rb

+20
Original file line numberDiff line numberDiff line change
@@ -1723,6 +1723,26 @@ class CustomError < Grape::Exceptions::Base; end
17231723
expect(last_response.status).to eql 500
17241724
expect(last_response.body).to eq('Formatter Error')
17251725
end
1726+
1727+
it 'validates response processed by exception handler' do
1728+
subject.rescue_from ArgumentError do
1729+
error!('rain!')
1730+
nil # invalid response caused by return nil
1731+
end
1732+
subject.rescue_from :all do
1733+
error!('rain!')
1734+
end
1735+
subject.get('/invalid_response') { raise ArgumentError }
1736+
subject.get('/valid_response') { raise 'rain!' }
1737+
1738+
get '/invalid_response'
1739+
expect(last_response.status).to eql 500
1740+
expect(last_response.body).to eq('Internal Server Error(Invalid Response)')
1741+
1742+
get '/valid_response'
1743+
expect(last_response.status).to eql 500
1744+
expect(last_response.body).to eq('rain!')
1745+
end
17261746
end
17271747

17281748
describe '.rescue_from klass, block' do

0 commit comments

Comments
 (0)