|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Sentry |
| 4 | + module Rails |
| 5 | + module LogSubscribers |
| 6 | + # Shared utility module for filtering sensitive parameters in log subscribers. |
| 7 | + # |
| 8 | + # This module provides consistent parameter filtering across all Sentry Rails |
| 9 | + # log subscribers, leveraging Rails' built-in parameter filtering when available. |
| 10 | + # It automatically detects the correct Rails parameter filtering API based on |
| 11 | + # the Rails version and includes the appropriate implementation module. |
| 12 | + # |
| 13 | + # @example Usage in a log subscriber |
| 14 | + # class MySubscriber < Sentry::Rails::LogSubscriber |
| 15 | + # include Sentry::Rails::LogSubscribers::ParameterFilter |
| 16 | + # |
| 17 | + # def my_event(event) |
| 18 | + # if Sentry.configuration.send_default_pii && event.payload[:params] |
| 19 | + # filtered_params = filter_sensitive_params(event.payload[:params]) |
| 20 | + # attributes[:params] = filtered_params unless filtered_params.empty? |
| 21 | + # end |
| 22 | + # end |
| 23 | + # end |
| 24 | + module ParameterFilter |
| 25 | + EMPTY_HASH = {}.freeze |
| 26 | + |
| 27 | + def self.included(base) |
| 28 | + # Determine which parameter filter implementation to use based on Rails version |
| 29 | + # Try to require ActiveSupport::ParameterFilter first (Rails 6.0+) |
| 30 | + begin |
| 31 | + require "active_support/parameter_filter" |
| 32 | + base.include(ActiveSupportImplementation) |
| 33 | + rescue LoadError |
| 34 | + # Fall back to ActionDispatch::Http::ParameterFilter (Rails 5.0-5.2) |
| 35 | + base.include(ActionDispatchImplementation) |
| 36 | + end |
| 37 | + end |
| 38 | + |
| 39 | + # Implementation for Rails 6.0+ using ActiveSupport::ParameterFilter |
| 40 | + module ActiveSupportImplementation |
| 41 | + # Filter sensitive parameters from a hash, respecting Rails configuration. |
| 42 | + # |
| 43 | + # @param params [Hash] The parameters to filter |
| 44 | + # @return [Hash] Filtered parameters with sensitive data removed |
| 45 | + def filter_sensitive_params(params) |
| 46 | + return EMPTY_HASH unless params.is_a?(Hash) |
| 47 | + |
| 48 | + filter_parameters = ::Rails.application.config.filter_parameters |
| 49 | + parameter_filter = ActiveSupport::ParameterFilter.new(filter_parameters) |
| 50 | + |
| 51 | + parameter_filter.filter(params) |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + # Implementation for Rails 5.0-5.2 using ActionDispatch::Http::ParameterFilter |
| 56 | + module ActionDispatchImplementation |
| 57 | + def self.included(base) |
| 58 | + require "action_dispatch" |
| 59 | + end |
| 60 | + |
| 61 | + # Filter sensitive parameters from a hash, respecting Rails configuration. |
| 62 | + # |
| 63 | + # @param params [Hash] The parameters to filter |
| 64 | + # @return [Hash] Filtered parameters with sensitive data removed |
| 65 | + def filter_sensitive_params(params) |
| 66 | + return EMPTY_HASH unless params.is_a?(Hash) |
| 67 | + |
| 68 | + filter_parameters = ::Rails.application.config.filter_parameters |
| 69 | + parameter_filter = ActionDispatch::Http::ParameterFilter.new(filter_parameters) |
| 70 | + |
| 71 | + parameter_filter.filter(params) |
| 72 | + end |
| 73 | + end |
| 74 | + end |
| 75 | + end |
| 76 | + end |
| 77 | +end |
0 commit comments