class Sentry::Sanitizer::Cleaner

Constants

DEFAULT_MASK
DEFAULT_SENSITIVE_HEADERS

Attributes

do_cookies[R]
fields[R]
http_headers[R]

Public Class Methods

new(config) click to toggle source
# File lib/sentry/sanitizer/cleaner.rb, line 12
def initialize(config)
  @fields = config.fields || []
  @http_headers = config.http_headers || DEFAULT_SENSITIVE_HEADERS
  @do_cookies = config.cookies || false
end

Public Instance Methods

call(event) click to toggle source
# File lib/sentry/sanitizer/cleaner.rb, line 18
def call(event)
  if event.is_a?(Sentry::Event)
    sanitize_request(event, :object) if event.request
    event.extra = sanitize_data(event.extra)
  elsif event.is_a?(Hash)
    sanitize_request(event, :stringified_hash) if event['request']
    sanitize_request(event, :symbolized_hash) if event[:request]
    event['extra'] = sanitize_data(event['extra']) if event['extra']
    event[:extra] = sanitize_data(event[:extra]) if event[:extra]
  end
end
sanitize_data(hash) click to toggle source
# File lib/sentry/sanitizer/cleaner.rb, line 47
def sanitize_data(hash)
  return hash unless hash.is_a? Hash
  return hash unless fields.size.positive?

  sanitize_value(hash, nil)
end
sanitize_request(event, type) click to toggle source
# File lib/sentry/sanitizer/cleaner.rb, line 30
def sanitize_request(event, type)
  case type
  when :object
    event.request.data = sanitize_data(event.request.data)
    event.request.headers = sanitize_headers(event.request.headers)
    event.request.cookies = sanitize_cookies(event.request.cookies)
  when :stringified_hash
    event['request']['data'] = sanitize_data(event['request']['data'])
    event['request']['headers'] = sanitize_headers(event['request']['headers'])
    event['request']['cookies'] = sanitize_cookies(event['request']['cookies'])
  when :symbolized_hash
    event[:request][:data] = sanitize_data(event[:request][:data])
    event[:request][:headers] = sanitize_headers(event[:request][:headers])
    event[:request][:cookies] = sanitize_cookies(event[:request][:cookies])
  end
end

Private Instance Methods

sanitize_array(key, value) click to toggle source
# File lib/sentry/sanitizer/cleaner.rb, line 108
def sanitize_array(key, value)
  if value.frozen?
    value.map { |val| sanitize_value(val, key) }
  else
    value.map! { |val| sanitize_value(val, key) }
  end
end
sanitize_cookies(cookies) click to toggle source

Sanitize all cookies

# File lib/sentry/sanitizer/cleaner.rb, line 78
def sanitize_cookies(cookies)
  return cookies unless cookies.is_a? Hash
  return cookies unless do_cookies

  cookies.transform_values { DEFAULT_MASK }
end
sanitize_hash(key, value) click to toggle source
# File lib/sentry/sanitizer/cleaner.rb, line 98
def sanitize_hash(key, value)
  if key&.match?(sensitive_fields)
    DEFAULT_MASK
  elsif value.frozen?
    value.merge(value) { |k, v| sanitize_value(v, k) }
  else
    value.merge!(value) { |k, v| sanitize_value(v, k) }
  end
end
sanitize_headers(headers) click to toggle source

Sanitize specified headers

# File lib/sentry/sanitizer/cleaner.rb, line 59
def sanitize_headers(headers)
  case http_headers
  when TrueClass
    headers.transform_values { DEFAULT_MASK }
  when Array
    return headers unless http_headers.size.positive?
    http_headers_regex = sensitive_regexp(http_headers)

    headers.keys.select { |key| key.match?(http_headers_regex) }.each do |key|
      headers[key] = DEFAULT_MASK
    end

    headers
  else
    headers
  end
end
sanitize_string(key, value) click to toggle source
# File lib/sentry/sanitizer/cleaner.rb, line 116
def sanitize_string(key, value)
  key&.match?(sensitive_fields) ? DEFAULT_MASK : value
end
sanitize_value(value, key) click to toggle source
# File lib/sentry/sanitizer/cleaner.rb, line 85
def sanitize_value(value, key)
  case value
  when Hash
    sanitize_hash(key, value)
  when Array
    sanitize_array(key, value)
  when String
    sanitize_string(key, value)
  else
    value
  end
end
sensitive_fields() click to toggle source
# File lib/sentry/sanitizer/cleaner.rb, line 120
def sensitive_fields
  @sensitive_fields ||= sensitive_regexp(fields)
end
sensitive_regexp(fields) click to toggle source
# File lib/sentry/sanitizer/cleaner.rb, line 124
def sensitive_regexp(fields)
  Regexp.new(fields.map { |field| "\\b#{field}\\b" }.join('|'), 'i')
end