Class: LangsmithrbRails::Redactor

Inherits:
Object
  • Object
show all
Defined in:
lib/langsmithrb_rails/redactor.rb

Overview

PII redaction utility for LangSmith traces

Constant Summary collapse

PATTERNS =

Common PII patterns

{
  email: /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/,
  phone: /\b(\+\d{1,2}\s?)?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}\b/,
  credit_card: /\b(?:\d{4}[-\s]?){3}\d{4}\b|\b\d{13,16}\b/,
  ssn: /\b\d{3}[-\s]?\d{2}[-\s]?\d{4}\b/,
  ip_address: /\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/
}.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.allowlistObject

Allowlist of patterns that should not be redacted



17
18
19
# File 'lib/langsmithrb_rails/redactor.rb', line 17

def allowlist
  @allowlist
end

.custom_patternsObject

Custom patterns to redact beyond the defaults



20
21
22
# File 'lib/langsmithrb_rails/redactor.rb', line 20

def custom_patterns
  @custom_patterns
end

Class Method Details

.scrub(input) ⇒ Object

Scrub PII from the input

Parameters:

  • input (Object)

    Input to scrub (String, Hash, Array, etc.)

Returns:

  • (Object)

    Redacted copy of the input



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/langsmithrb_rails/redactor.rb', line 31

def scrub(input)
  return input unless Config[:redact_by_default]
  
  case input
  when String
    scrub_string(input)
  when Hash
    input.transform_values { |v| scrub(v) }
  when Array
    input.map { |item| scrub(item) }
  else
    input
  end
end

.setupObject

Initialize the class variables



23
24
25
26
# File 'lib/langsmithrb_rails/redactor.rb', line 23

def setup
  @allowlist = []
  @custom_patterns = {}
end