Class: Sentry::Scrubbers::PIISanitizer

Inherits:
Object
  • Object
show all
Defined in:
lib/sentry/scrubbers/pii_sanitizer.rb

Constant Summary collapse

SANITIZED_FIELDS =
%w[
  accountNumber
  accountType
  address_line1
  address_line2
  address_line3
  bankName
  birth_date
  city
  common_name
  country
  dslogon_idvalue
  fileNumber
  firstName
  fname
  gender
  lastName
  lname
  mname
  participant_id
  phone
  postalCode
  routingNumber
  social
  ssn
  state
  street
  va_eauth_authorization
  va_eauth_birlsfilenumber
  va_eauth_gcIds
  vaEauthPnid
  zipCode
].freeze
SANITIZER_EXCEPTIONS =
%w[
  relaystate
].freeze
PATTERN =
Regexp.union(SANITIZED_FIELDS.map { |field| field.downcase.tr('_', '') }).freeze
JSON_STARTS_WITH =
['[', '{'].freeze
FILTER_MASK =
'FILTERED-CLIENTSIDE'
FILTER_MASK_NIL =
"#{FILTER_MASK}-NIL".freeze
FILTER_MASK_BLANK =
"#{FILTER_MASK}-BLANK".freeze

Instance Method Summary collapse

Instance Method Details

#filter(key, unsanitized_value) ⇒ Object (private)



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/sentry/scrubbers/pii_sanitizer.rb', line 83

def filter(key, unsanitized_value)
  if filter_pattern(key)
    if unsanitized_value.is_a?(Array)
      unsanitized_value.map { |element| filter(key, element) }
    else
      return FILTER_MASK_NIL if unsanitized_value.nil?
      return FILTER_MASK_BLANK if unsanitized_value.blank?

      FILTER_MASK
    end
  else
    unsanitized_value
  end
end

#filter_pattern(key) ⇒ Object (private)



98
99
100
101
# File 'lib/sentry/scrubbers/pii_sanitizer.rb', line 98

def filter_pattern(key)
  normalized_key = key.to_s.tr('_', '').downcase
  normalized_key.match(PATTERN) && SANITIZER_EXCEPTIONS.exclude?(normalized_key)
end

#parse_json_or_nil(string) ⇒ Object (private)



103
104
105
106
107
108
109
# File 'lib/sentry/scrubbers/pii_sanitizer.rb', line 103

def parse_json_or_nil(string)
  return unless string.start_with?(*JSON_STARTS_WITH)

  JSON.parse(string)
rescue JSON::ParserError, NoMethodError
  nil
end

#process(unsanitized_object) ⇒ Object



56
57
58
# File 'lib/sentry/scrubbers/pii_sanitizer.rb', line 56

def process(unsanitized_object)
  sanitize(unsanitized_object.deep_dup)
end

#sanitize(object) ⇒ Object (private)



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/sentry/scrubbers/pii_sanitizer.rb', line 62

def sanitize(object)
  case object
  when Hash
    object.each do |k, v|
      object[k] = filter(k, sanitize(v))
    end
  when Array
    object.each_with_index do |value, index|
      object[index] = sanitize(value)
    end
  when String
    if object.match(PATTERN) && (json = parse_json_or_nil(object))
      object = sanitize(json).to_json
    else
      object
    end
  else
    object
  end
end