Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions lib/rollbar/request_data_extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'rollbar/scrubbers'
require 'rollbar/scrubbers/url'
require 'rollbar/scrubbers/params'
require 'rollbar/util'
require 'rollbar/util/ip_obfuscator'
require 'rollbar/util/ip_anonymizer'
require 'rollbar/json'
Expand Down Expand Up @@ -91,14 +92,23 @@ def scrub_params(params, sensitive_params)
def mergeable_raw_body_params(rack_req)
raw_body_params = rollbar_raw_body_params(rack_req)

case raw_body_params
when Hash
raw_body_params
when Array
{ 'body.multi' => raw_body_params }
else
{ 'body.value' => raw_body_params }
end
params = case raw_body_params
when Hash
raw_body_params
when Array
{ 'body.multi' => raw_body_params }
else
{ 'body.value' => raw_body_params }
end

# The request body is serialized to JSON right here in the extractor,
# before the payload-wide UTF-8 pass in Rollbar::Item runs. A parsed
# body carrying invalid UTF-8 (e.g. a malformed request) would make that
# serialization raise JSON::GeneratorError and mask the original error
# being reported, so normalize the encoding up front.
Rollbar::Util.enforce_valid_utf8(params)

params
end

def rollbar_request_method(env)
Expand Down
21 changes: 21 additions & 0 deletions spec/rollbar/request_data_extractor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,27 @@ class ExtractorDummy
end
end

context 'with JSON POST body containing invalid UTF-8' do
let(:body) { "{\"key\":\"\xAE\"}".b }
let(:env) do
Rack::MockRequest.env_for('/?foo=bar',
'CONTENT_TYPE' => 'application/json',
:input => body,
:method => 'POST')
end

it 'does not raise and serializes a valid body' do
result = nil

expect { result = subject.extract_request_data_from_rack(env) }
.not_to raise_error

expect(result[:body]).to be_kind_of(String)
expect(result[:body].encoding).to be_eql(Encoding::UTF_8)
expect(result[:body]).to be_valid_encoding
end
end

context 'with JSON DELETE body' do
let(:params) { { 'key' => 'value' } }
let(:body) { params.to_json }
Expand Down
Loading