LlmGuard.Detectors.DataLeakage.PIIRedactor (LlmGuard v0.3.1)

View Source

Redacts PII from text using various strategies.

Supports multiple redaction strategies:

  • :mask - Replace with asterisks (default)
  • :partial - Show partial information (e.g., last 4 digits)
  • :hash - Replace with deterministic hash
  • :placeholder - Use descriptive placeholders like [EMAIL]
  • {:custom, function} - Custom redaction function

Strategies

Mask Strategy

Replaces PII with asterisks or custom character:

"john@example.com" -> "*****************"

Partial Strategy

Shows partial information while hiding sensitive parts:

"john@example.com" -> "j***@example.com"
"555-123-4567" -> "***-***-4567"
"123-45-6789" -> "***-**-6789"

Hash Strategy

Replaces with deterministic hash for anonymization:

"john@example.com" -> "HASH_a1b2c3d4"

Placeholder Strategy

Uses descriptive type-based placeholders:

"john@example.com" -> "[EMAIL]"
"555-1234" -> "[PHONE]"

Examples

# Simple redaction with default mask strategy
iex> text = "Email: user@example.com"
iex> PIIRedactor.redact_text(text)
"Email: *****************"

# Partial redaction
iex> entities = PIIScanner.scan("Card: 4532015112830366")
iex> PIIRedactor.redact("Card: 4532015112830366", entities, strategy: :partial)
"Card: ************0366"

# Hash strategy with mapping
iex> {redacted, mapping} = PIIRedactor.redact_with_mapping(text, entities, strategy: :hash)
iex> is_map(mapping)
true

Summary

Functions

Redacts PII from text based on detected entities.

Scans and redacts PII in one convenient call.

Redacts PII and returns both redacted text and a mapping.

Types

redaction_options()

@type redaction_options() :: [
  strategy: redaction_strategy(),
  mask_char: String.t(),
  placeholder_format: :square_brackets | :angle_brackets
]

redaction_strategy()

@type redaction_strategy() ::
  :mask
  | :partial
  | :hash
  | :placeholder
  | {:custom, (map() -> String.t())}
  | %{optional(atom()) => redaction_strategy()}

Functions

redact(text, entities, opts \\ [])

@spec redact(String.t(), [map()], redaction_options()) :: String.t()

Redacts PII from text based on detected entities.

Parameters

  • text - Original text containing PII
  • entities - List of PII entities from PIIScanner
  • opts - Redaction options

Options

  • :strategy - Redaction strategy (default: :mask)
  • :mask_char - Character for masking (default: "*")
  • :placeholder_format - Format for placeholders (default: :square_brackets)

Returns

Redacted text string.

redact_text(text, opts \\ [])

@spec redact_text(String.t(), redaction_options()) :: String.t()

Scans and redacts PII in one convenient call.

Parameters

  • text - Text to scan and redact
  • opts - Redaction options (same as redact/3)

Returns

Redacted text string.

Examples

iex> PIIRedactor.redact_text("Email: user@example.com")
"Email: *****************"

redact_with_mapping(text, entities, opts \\ [])

@spec redact_with_mapping(String.t(), [map()], redaction_options()) ::
  {String.t(), map()}

Redacts PII and returns both redacted text and a mapping.

Useful for hash strategy where you want to maintain a lookup table.

Parameters

  • text - Original text
  • entities - PII entities
  • opts - Redaction options

Returns

Tuple of {redacted_text, mapping} where mapping is a map from original values to redacted values.

Examples

{redacted, mapping} = PIIRedactor.redact_with_mapping(text, entities, strategy: :hash)
# mapping = %{"user@example.com" => "HASH_abc123", ...}