Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ACCEPT header to check json requests #333

Merged
merged 8 commits into from
Nov 2, 2015
7 changes: 6 additions & 1 deletion lib/rollbar/request_data_extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def rollbar_raw_body_params(rack_req)
correct_method = rack_req.post? || rack_req.put? || rack_req.patch?

return {} unless correct_method
return {} unless rack_req.env['CONTENT_TYPE'] =~ %r{application/json}i
return {} unless json_request?(rack_req)

Rollbar::JSON.load(rack_req.body.read)
rescue
Expand All @@ -146,6 +146,11 @@ def rollbar_raw_body_params(rack_req)
rack_req.body.rewind
end

def json_request?(rack_req)
!!(rack_req.env['CONTENT_TYPE'] =~ %r{application/json} ||
rack_req.env['ACCEPT'] =~ /\bjson\b/)
end

def rollbar_request_params(env)
env['action_dispatch.request.parameters'] || {}
end
Expand Down
12 changes: 12 additions & 0 deletions spec/controllers/home_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,18 @@
end
end

context 'with json ACCEPT header', :type => 'request' do
let(:params) { { :foo => :bar } }

it 'parses the correct headers' do
expect do
post '/cause_exception', params, { 'ACCEPT' => 'application/vnd.github.v3+json' }
end.to raise_exception

expect(Rollbar.last_report[:request][:params]['foo']).to be_eql('bar')
end
end

context 'with params to be scrubed from URL', :type => :request do
next unless Rollbar::LanguageSupport.can_scrub_url?

Expand Down
2 changes: 1 addition & 1 deletion spec/dummyapp/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
member { post :start_session }
end

get '/cause_exception' => 'home#cause_exception'
match '/cause_exception' => 'home#cause_exception', :via => [:get, :post]
put '/deprecated_report_exception' => 'home#deprecated_report_exception'
match '/report_exception' => 'home#report_exception',
:via => [:get, :post, :put]
Expand Down
10 changes: 9 additions & 1 deletion spec/rollbar/middleware/sinatra_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,21 @@ def app
}
end

it 'appear in the sent payload' do
it 'appears in the sent payload when application/json is the content type' do
expect do
post '/crash_post', params.to_json, { 'CONTENT_TYPE' => 'application/json' }
end.to raise_error(exception)

expect(Rollbar.last_report[:request][:params]).to be_eql(params)
end

it 'appears in the sent payload when the accepts header contains json' do
expect do
post '/crash_post', params, { 'ACCEPT' => 'application/vnd.github.v3+json' }
end.to raise_error(exception)

expect(Rollbar.last_report[:request][:params]).to be_eql(params)
end
end

it 'resets the notifier in every request' do
Expand Down