Skip to content

Commit 19a763e

Browse files
committed
Filter event reporter payloads
1 parent f5d9c02 commit 19a763e

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

activesupport/lib/active_support.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ def self.eager_load!
113113
@event_reporter = ActiveSupport::EventReporter.new
114114
singleton_class.attr_accessor :event_reporter # :nodoc:
115115

116+
cattr_accessor :filter_parameters, default: [] # :nodoc:
117+
116118
def self.cache_format_version
117119
Cache.format_version
118120
end

activesupport/lib/active_support/event_reporter.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# frozen_string_literal: true
33

44
require "active_support/core_ext/hash/indifferent_access"
5+
require "active_support/parameter_filter"
56
require_relative "event_reporter/encoders"
67

78
module ActiveSupport
@@ -459,6 +460,13 @@ def context_store
459460
self.class.context_store
460461
end
461462

463+
def payload_filter
464+
@payload_filter ||= begin
465+
mask = ActiveSupport::ParameterFilter::FILTERED
466+
ActiveSupport::ParameterFilter.new(ActiveSupport.filter_parameters, mask: mask)
467+
end
468+
end
469+
462470
def resolve_name(name_or_object)
463471
case name_or_object
464472
when String, Symbol
@@ -473,9 +481,9 @@ def resolve_payload(name_or_object, payload, **kwargs)
473481
when String, Symbol
474482
handle_unexpected_args(name_or_object, payload, kwargs) if payload && kwargs.any?
475483
if kwargs.any?
476-
kwargs.with_indifferent_access
484+
payload_filter.filter(kwargs.with_indifferent_access)
477485
elsif payload
478-
payload.with_indifferent_access
486+
payload_filter.filter(payload.with_indifferent_access)
479487
end
480488
else
481489
handle_unexpected_args(name_or_object, payload, kwargs) if payload || kwargs.any?

activesupport/lib/active_support/railtie.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ class Railtie < Rails::Railtie # :nodoc:
7474
app.executor.to_run { ActiveSupport.event_reporter.clear_context }
7575
end
7676

77+
initializer "active_support.set_filter_parameters" do |app|
78+
app.after_initialize do
79+
ActiveSupport.filter_parameters += Rails.application.config.filter_parameters
80+
end
81+
end
82+
7783
initializer "active_support.deprecation_behavior" do |app|
7884
if app.config.active_support.report_deprecations == false
7985
app.deprecators.silenced = true

activesupport/test/event_reporter_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,19 @@ def emit(event)
224224
assert_equal("Uh oh!", error.message)
225225
end
226226

227+
test "#notify with filtered parameters" do
228+
previous_filter_parameters = ActiveSupport.filter_parameters
229+
ActiveSupport.filter_parameters = [:zomg]
230+
231+
assert_called_with(@subscriber, :emit, [
232+
event_matcher(name: "test_event", payload: { key: "value", zomg: "[FILTERED]" })
233+
]) do
234+
@reporter.notify(:test_event, { key: "value", zomg: "secret" })
235+
end
236+
ensure
237+
ActiveSupport.filter_parameters = previous_filter_parameters
238+
end
239+
227240
test "#with_debug" do
228241
@reporter.with_debug do
229242
assert_predicate @reporter, :debug_mode?

0 commit comments

Comments
 (0)