Skip to content

Commit

Permalink
Skip header value with non-convertable encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
st0012 committed Aug 26, 2021
1 parent 740ed8c commit 9de377f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
14 changes: 13 additions & 1 deletion sentry-ruby/lib/sentry/interfaces/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Sentry
class RequestInterface < Interface
REQUEST_ID_HEADERS = %w(action_dispatch.request_id HTTP_X_REQUEST_ID).freeze
CONTENT_HEADERS = %w(CONTENT_TYPE CONTENT_LENGTH).freeze
INVALID_ENCODING_MESSAGE = "[Filtered due to invalid encoding]"
IP_HEADERS = [
"REMOTE_ADDR",
"HTTP_CLIENT_IP",
Expand Down Expand Up @@ -76,7 +77,18 @@ def filter_and_format_headers(env)
# Rack stores headers as HTTP_WHAT_EVER, we need What-Ever
key = key.sub(/^HTTP_/, "")
key = key.split('_').map(&:capitalize).join('-')
memo[key] = value.to_s

value = value.to_s

if value.respond_to?(:force_encoding)
value = value.force_encoding(Encoding::UTF_8)
end

if !value.valid_encoding?
value = INVALID_ENCODING_MESSAGE
end

memo[key] = value
rescue StandardError => e
# Rails adds objects to the Rack env that can sometimes raise exceptions
# when `to_s` is called.
Expand Down
11 changes: 11 additions & 0 deletions sentry-ruby/spec/sentry/interfaces/request_interface_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@
end
end

context 'with special characters' do
let(:additional_headers) { { "HTTP_FOO" => "\xC4" } }

it "doesn't cause any issue" do
interface = described_class.build(env: env)
json = JSON.generate(interface.to_hash)

expect(JSON.parse(json)["headers"]).to eq({"Content-Length"=>"0", "Foo"=>"[Filtered due to invalid encoding]"})
end
end

context 'with additional env variables' do
let(:mock) { double }
let(:env) { { "some.variable" => mock } }
Expand Down

0 comments on commit 9de377f

Please sign in to comment.