class Bugno::Filter::Params

Constants

ATTACHMENT_CLASSES
SCRUB_ALL
SKIPPED_CLASSES

Public Class Methods

call(*args) click to toggle source
# File lib/bugno/filter/params.rb, line 12
def self.call(*args)
  new.call(*args)
end
scrub_value() click to toggle source
# File lib/bugno/filter/params.rb, line 16
def self.scrub_value
  '[FILTERED]'
end

Public Instance Methods

call(options = {}) click to toggle source
# File lib/bugno/filter/params.rb, line 20
def call(options = {})
  params = options[:params]
  return {} unless params

  @scrubbed_object_ids = {}

  config = options[:config]
  extra_fields = options[:extra_fields]
  whitelist = options[:whitelist] || []

  scrub(params, build_scrub_options(config, extra_fields, whitelist))
end

Private Instance Methods

bugno_filtered_param_value(value) click to toggle source
# File lib/bugno/filter/params.rb, line 100
def bugno_filtered_param_value(value)
  if ATTACHMENT_CLASSES.include?(value.class.name)
    begin
      attachment_value(value)
    rescue StandardError
      'Uploaded file'
    end
  else
    value
  end
end
build_fields_regex(config, extra_fields) click to toggle source
# File lib/bugno/filter/params.rb, line 45
def build_fields_regex(config, extra_fields)
  fields = config.find_all { |f| f.is_a?(String) || f.is_a?(Symbol) }
  fields += Array(extra_fields)

  return unless fields.any?

  Regexp.new(fields.map { |val| Regexp.escape(val.to_s).to_s }.join('|'), true)
end
build_scrub_options(config, extra_fields, whitelist) click to toggle source
# File lib/bugno/filter/params.rb, line 35
def build_scrub_options(config, extra_fields, whitelist)
  ary_config = Array(config)

  {
    fields_regex: build_fields_regex(ary_config, extra_fields),
    scrub_all: ary_config.include?(SCRUB_ALL),
    whitelist: build_whitelist_regex(whitelist)
  }
end
build_whitelist_regex(whitelist) click to toggle source
# File lib/bugno/filter/params.rb, line 54
def build_whitelist_regex(whitelist)
  fields = whitelist.find_all { |f| f.is_a?(String) || f.is_a?(Symbol) }
  return unless fields.any?

  Regexp.new(fields.map { |val| /\A#{Regexp.escape(val.to_s)}\z/ }.join('|'))
end
scrub(params, options) click to toggle source
# File lib/bugno/filter/params.rb, line 61
def scrub(params, options)
  return params if @scrubbed_object_ids[params.object_id]

  @scrubbed_object_ids[params.object_id] = true

  fields_regex = options[:fields_regex]
  scrub_all = options[:scrub_all]
  whitelist_regex = options[:whitelist]

  return scrub_array(params, options) if params.is_a?(Array)

  params.to_hash.each_with_object({}) do |(key, value), result|
    encoded_key = Bugno::Encoding.encode(key).to_s
    result[key] = if (fields_regex === encoded_key) && !(whitelist_regex === encoded_key)
                    scrub_value
                  elsif value.is_a?(Hash)
                    scrub(value, options)
                  elsif scrub_all && !(whitelist_regex === encoded_key)
                    scrub_value
                  elsif value.is_a?(Array)
                    scrub_array(value, options)
                  elsif skip_value?(value)
                    "Skipped value of class '#{value.class.name}'"
                  else
                    bugno_filtered_param_value(value)
                  end
  end
end
scrub_array(array, options) click to toggle source
# File lib/bugno/filter/params.rb, line 90
def scrub_array(array, options)
  array.map do |value|
    value.is_a?(Hash) ? scrub(value, options) : bugno_filtered_param_value(value)
  end
end
scrub_value() click to toggle source
# File lib/bugno/filter/params.rb, line 96
def scrub_value
  '[FILTERED]'
end
skip_value?(value) click to toggle source
# File lib/bugno/filter/params.rb, line 112
def skip_value?(value)
  SKIPPED_CLASSES.any? { |klass| value.is_a?(klass) }
end