Skip to content

Commit cd89d4e

Browse files
committed
fix(rails): filter the context emitted by ErrorSubscriber
1 parent 880a941 commit cd89d4e

3 files changed

Lines changed: 41 additions & 5 deletions

File tree

sentry-rails/lib/sentry/rails/error_reporter_context.rb

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,26 @@ module Rails
88
module ErrorReporterContext
99
SUPPORTS_EXECUTION_CONTEXT = Gem::Version.new(::Rails.version) >= Gem::Version.new("7.0.0")
1010

11+
# Prepare a context hash for the "rails.error" context entry, so that user data attached
12+
# via `Rails.error.set_context` gets the same treatment regardless of whether the
13+
# exception was handled.
14+
#
15+
# Serialization runs first: the serializer expands Enumerables and Ranges into arrays the
16+
# filter can descend into, and it preserves hash keys, so filtering its output is strictly
17+
# more thorough than filtering the raw context.
18+
#
19+
# @param context [Hash] the raw context
20+
# @return [Hash] the serialized context with sensitive values redacted
21+
def error_context(context)
22+
Sentry::Rails::ParameterFilter.filter_sensitive_params(Sentry::Rails::Serializer.serialize(context))
23+
end
24+
1125
if SUPPORTS_EXECUTION_CONTEXT
1226
def execution_context
1327
context = ::ActiveSupport::ExecutionContext.to_h
1428
return {} if context.empty?
1529

16-
# Serialize first: the serializer expands Enumerables and Ranges into arrays the
17-
# filter can descend into, and it preserves hash keys, so filtering its output is
18-
# strictly more thorough than filtering the raw context.
19-
{ "rails.error" => Sentry::Rails::ParameterFilter.filter_sensitive_params(Sentry::Rails::Serializer.serialize(context)) }
30+
{ "rails.error" => error_context(context) }
2031
end
2132
else
2233
def execution_context

sentry-rails/lib/sentry/rails/error_subscriber.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
# frozen_string_literal: true
22

3+
require "sentry/rails/error_reporter_context"
4+
35
module Sentry
46
module Rails
57
# This is not a user-facing class. You should use it with Rails 7.0's error reporter feature and its interfaces.
68
# See https://github.com/rails/rails/blob/main/activesupport/lib/active_support/error_reporter.rb to learn more about reporting APIs.
79
# If you want Sentry to subscribe to the error reporter, please set `config.rails.register_error_subscriber` to `true`.
810
class ErrorSubscriber
11+
include ErrorReporterContext
12+
913
SKIP_SOURCES = Regexp.union([/.*_cache_store.active_support/])
1014

1115
def report(error, handled:, severity:, context:, source: nil)
@@ -29,7 +33,7 @@ def report(error, handled:, severity:, context:, source: nil)
2933

3034
hint[:mechanism] ||= Sentry::Mechanism.new(type: Sentry::Rails.integration_name, handled: handled)
3135

32-
Sentry::Rails.capture_exception(error, level: severity, contexts: { "rails.error" => context }, tags: tags, hint: hint)
36+
Sentry::Rails.capture_exception(error, level: severity, contexts: { "rails.error" => error_context(context) }, tags: tags, hint: hint)
3337
end
3438
end
3539
end

sentry-rails/spec/sentry/rails_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,27 @@ def capture_in_separate_process(exit_code:)
366366
expect(event.contexts).to include({ "rails.error" => { foo: "bar" } })
367367
end
368368

369+
it "filters Rails.error.set_context data attached before a handled exception" do
370+
Rails.error.set_context(
371+
debug_key: "important_value",
372+
api_key: "secret-api-key",
373+
nested: { password: "hunter2" }
374+
)
375+
376+
Rails.error.handle(severity: :info) { 1/0 }
377+
378+
expect(transport.events.count).to eq(1)
379+
380+
event = transport.events.first
381+
expect(event.contexts).to include(
382+
"rails.error" => hash_including(
383+
debug_key: "important_value",
384+
api_key: "[FILTERED]",
385+
nested: { password: "[FILTERED]" }
386+
)
387+
)
388+
end
389+
369390
it "skips cache storage sources", skip: Rails.version.to_f < 7.1 do
370391
Rails.error.handle(severity: :info, source: "mem_cache_store.active_support") do
371392
1/0

0 commit comments

Comments
 (0)