rollbar-gem icon indicating copy to clipboard operation
rollbar-gem copied to clipboard

Support scrubbing values in arrays

Open ajw725 opened this issue 4 years ago • 1 comments

This is a question/feature request, not a bug.

I'm trying to scrub a portion of my payload. I am adding this proc to transform the payload:

body_scrubber = proc do |options|
  req_body = options.dig(:payload, 'data', :request, :body)
  if req_body
    begin
      parsed_body = JSON.parse(req_body)
      scrubbed = Rollbar::Scrubbers::Params.call(params: parsed_body, config: [:scrub_all])
      options[:payload]['data'][:request][:body] = scrubbed.to_json
    rescue => e
      options[:payload]['data'][:request][:body] = '****'
    end
  end
end

I still want to see the structure of the body, i.e. all the keys, but I want all of the values to be scrubbed. This mostly works, except that it does not scrub values in arrays. For example, if my request body is like this:

{
    "key1": "value1",
    "key2": ["value 2", "value 3"]
}

Then my scrubbed body will look like this:

{
    "key1": "*******",
    "key2": ["value 2", "value 3"]

This is not what I want; I would like "value 2" and "value 3" to be scrubbed also. However, it looks like Rollbar::Scrubbers::Params#scrub_array doesn't attempt to scrub values, even when scrub_all is true. Is there some option I'm missing to make this happen? If not, is that a reasonable feature request?

ajw725 avatar Jun 02 '20 00:06 ajw725

FWIW, monkeypatching like this does what i want:

require 'rollbar/scrubbers/params
Rollbar::Scrubbers::Params.class_eval do
  private
  def scrub_array(array, options)
    scrub_all = options[:scrub_all]
    array.map do |value|
      if value.is_a?(Hash)
        scrub value, options
      elsif Rollbar::Scrubbers::Params::ATTACHMENT_CLASSES.include?(value.class.name)
        attachment_value(value) rescue "Uploaded file"
      elsif scrub_all
        scrub_value(value)
      else
        value
      end
    end
  end
end

ajw725 avatar Jun 02 '20 01:06 ajw725