Class: GuardrailsRuby::Checks::PII

Inherits:
GuardrailsRuby::Check show all
Defined in:
lib/guardrails_ruby/checks/pii.rb

Constant Summary collapse

PATTERNS =
{
  ssn: /\b\d{3}-\d{2}-\d{4}\b/,
  credit_card: /\b(?:\d{4}[- ]?){3}\d{4}\b/,
  email: /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/,
  phone_us: /\b(?:\+?1[-.]?)?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}\b/,
  ip_address: /\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/,
  date_of_birth: /\b(?:DOB|date of birth|born)[:\s]*\d{1,2}[\/\-]\d{1,2}[\/\-]\d{2,4}\b/i,

  # International formats
  nik_indonesia: /\b\d{16}\b/,
  phone_international: /\b\+(?:62|44|49|33|81|86|91|61|64|65)\d{8,12}\b/,
  iban: /\b[A-Z]{2}\d{2}[A-Z0-9]{4}\d{7}(?:[A-Z0-9]{0,16})\b/,
  passport: /\b[A-Z]{1,2}\d{6,9}\b/
}.freeze
REDACT_MAP =
{
  ssn: "[SSN REDACTED]",
  credit_card: "[CC REDACTED]",
  email: "[EMAIL REDACTED]",
  phone_us: "[PHONE REDACTED]",
  ip_address: "[IP REDACTED]",
  date_of_birth: "[DOB REDACTED]",
  nik_indonesia: "[NIK REDACTED]",
  phone_international: "[PHONE REDACTED]",
  iban: "[IBAN REDACTED]",
  passport: "[PASSPORT REDACTED]"
}.freeze

Instance Attribute Summary

Attributes inherited from GuardrailsRuby::Check

#options

Instance Method Summary collapse

Methods inherited from GuardrailsRuby::Check

check_name, direction, #initialize, lookup

Constructor Details

This class inherits a constructor from GuardrailsRuby::Check

Instance Method Details

#call(text, context: {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/guardrails_ruby/checks/pii.rb', line 37

def call(text, context: {})
  enabled = @options.fetch(:patterns, PATTERNS.keys)
  found = {}

  PATTERNS.each do |type, pattern|
    next unless enabled.include?(type)
    matches = text.scan(pattern)
    found[type] = matches if matches.any?
  end

  if found.any?
    fail! "PII detected: #{found.keys.join(', ')}",
      matches: found,
      sanitized: redact(text, found)
  else
    pass!
  end
end