|
| 1 | +"""Live PII and sensitive data masking for the proxy layer. |
| 2 | +
|
| 3 | +Extends redact.py with PII detection (emails, phone numbers, credit cards, |
| 4 | +SSNs, IP addresses, names in common patterns) applied in real-time as events |
| 5 | +flow through the proxy. Operates on raw JSON-RPC message bodies before they |
| 6 | +are stored as trace events. |
| 7 | +
|
| 8 | +Usage in proxy: |
| 9 | + from .masking import mask_event_data |
| 10 | + event.data = mask_event_data(event.data, config) |
| 11 | +""" |
| 12 | + |
| 13 | +from __future__ import annotations |
| 14 | + |
| 15 | +import re |
| 16 | +from dataclasses import dataclass, field |
| 17 | +from typing import Any |
| 18 | + |
| 19 | +from .redact import redact_data, REDACTED |
| 20 | + |
| 21 | +# --------------------------------------------------------------------------- |
| 22 | +# PII patterns |
| 23 | +# --------------------------------------------------------------------------- |
| 24 | + |
| 25 | +# Email addresses |
| 26 | +_EMAIL_RE = re.compile( |
| 27 | + r"\b[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}\b" |
| 28 | +) |
| 29 | + |
| 30 | +# Phone numbers (US and international formats) |
| 31 | +# Require a separator (space, dash, dot, or parens) to reduce false positives |
| 32 | +# on plain numeric sequences like IP addresses or version numbers. |
| 33 | +_PHONE_RE = re.compile( |
| 34 | + r"(?<!\d)(?:\+?1[\s\-.]?)?" |
| 35 | + r"(?:\(\d{3}\)[\s\-.]|\d{3}[\-.])" # area code must have separator |
| 36 | + r"\d{3}[\s\-.]?\d{4}" |
| 37 | + r"(?!\d)" |
| 38 | +) |
| 39 | + |
| 40 | +# Credit card numbers (Visa, MC, Amex, Discover — 13-16 digits with optional separators) |
| 41 | +_CC_RE = re.compile( |
| 42 | + r"\b(?:4[0-9]{12}(?:[0-9]{3})?" # Visa |
| 43 | + r"|5[1-5][0-9]{14}" # Mastercard |
| 44 | + r"|3[47][0-9]{13}" # Amex |
| 45 | + r"|6(?:011|5[0-9]{2})[0-9]{12}" # Discover |
| 46 | + r"|(?:\d{4}[\s\-]){3}\d{4})\b" # Generic 16-digit with separators |
| 47 | +) |
| 48 | + |
| 49 | +# US Social Security Numbers |
| 50 | +_SSN_RE = re.compile( |
| 51 | + r"\b(?!000|666|9\d{2})\d{3}[\s\-]" |
| 52 | + r"(?!00)\d{2}[\s\-]" |
| 53 | + r"(?!0000)\d{4}\b" |
| 54 | +) |
| 55 | + |
| 56 | +# IPv4 addresses (private ranges are not masked; public IPs are) |
| 57 | +_IPV4_RE = re.compile( |
| 58 | + r"\b(?!(?:10|127|172\.(?:1[6-9]|2\d|3[01])|192\.168)\.\d+\.\d+)" |
| 59 | + r"(?:\d{1,3}\.){3}\d{1,3}\b" |
| 60 | +) |
| 61 | + |
| 62 | +# AWS account IDs (12-digit numbers in ARNs) |
| 63 | +_AWS_ARN_RE = re.compile(r"arn:aws:[a-z0-9\-]+:[a-z0-9\-]*:(\d{12}):") |
| 64 | + |
| 65 | +# Generic UUIDs that look like user/account IDs (not session IDs) |
| 66 | +# Only mask when they appear as values of keys containing "user", "account", "customer" |
| 67 | +_UUID_RE = re.compile( |
| 68 | + r"\b[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\b", |
| 69 | + re.IGNORECASE, |
| 70 | +) |
| 71 | + |
| 72 | +_USER_KEYS = { |
| 73 | + "user_id", "userid", "user-id", "account_id", "accountid", |
| 74 | + "customer_id", "customerid", "person_id", "personid", |
| 75 | + "email", "phone", "mobile", "ssn", "dob", "date_of_birth", |
| 76 | + "credit_card", "card_number", "cvv", "billing_address", |
| 77 | +} |
| 78 | + |
| 79 | +# --------------------------------------------------------------------------- |
| 80 | +# Masking config |
| 81 | +# --------------------------------------------------------------------------- |
| 82 | + |
| 83 | +@dataclass |
| 84 | +class MaskingConfig: |
| 85 | + mask_emails: bool = True |
| 86 | + mask_phones: bool = True |
| 87 | + mask_credit_cards: bool = True |
| 88 | + mask_ssn: bool = True |
| 89 | + mask_public_ips: bool = False # off by default — too noisy |
| 90 | + mask_aws_arns: bool = True |
| 91 | + mask_user_id_keys: bool = True |
| 92 | + # Custom regex patterns provided by the user |
| 93 | + custom_patterns: list[str] = field(default_factory=list) |
| 94 | + # Replacement token (default: [MASKED]) |
| 95 | + replacement: str = "[MASKED]" |
| 96 | + |
| 97 | + @classmethod |
| 98 | + def from_dict(cls, d: dict) -> "MaskingConfig": |
| 99 | + return cls( |
| 100 | + mask_emails=d.get("emails", True), |
| 101 | + mask_phones=d.get("phones", True), |
| 102 | + mask_credit_cards=d.get("credit_cards", True), |
| 103 | + mask_ssn=d.get("ssn", True), |
| 104 | + mask_public_ips=d.get("public_ips", False), |
| 105 | + mask_aws_arns=d.get("aws_arns", True), |
| 106 | + mask_user_id_keys=d.get("user_id_keys", True), |
| 107 | + custom_patterns=d.get("custom_patterns", []), |
| 108 | + replacement=d.get("replacement", "[MASKED]"), |
| 109 | + ) |
| 110 | + |
| 111 | + @classmethod |
| 112 | + def default(cls) -> "MaskingConfig": |
| 113 | + return cls() |
| 114 | + |
| 115 | + |
| 116 | +# --------------------------------------------------------------------------- |
| 117 | +# String-level masking |
| 118 | +# --------------------------------------------------------------------------- |
| 119 | + |
| 120 | +def _mask_string(value: str, config: MaskingConfig) -> str: |
| 121 | + """Apply all enabled PII patterns to a string value.""" |
| 122 | + result = value |
| 123 | + repl = config.replacement |
| 124 | + |
| 125 | + if config.mask_emails: |
| 126 | + result = _EMAIL_RE.sub(repl, result) |
| 127 | + |
| 128 | + if config.mask_phones: |
| 129 | + result = _PHONE_RE.sub(repl, result) |
| 130 | + |
| 131 | + if config.mask_credit_cards: |
| 132 | + result = _CC_RE.sub(repl, result) |
| 133 | + |
| 134 | + if config.mask_ssn: |
| 135 | + result = _SSN_RE.sub(repl, result) |
| 136 | + |
| 137 | + if config.mask_public_ips: |
| 138 | + result = _IPV4_RE.sub(repl, result) |
| 139 | + |
| 140 | + if config.mask_aws_arns: |
| 141 | + # Replace only the account ID portion (group 1) within the ARN |
| 142 | + def _mask_arn(m: re.Match) -> str: |
| 143 | + return m.group(0).replace(m.group(1), repl, 1) |
| 144 | + result = _AWS_ARN_RE.sub(_mask_arn, result) |
| 145 | + |
| 146 | + for pat_str in config.custom_patterns: |
| 147 | + try: |
| 148 | + result = re.sub(pat_str, repl, result) |
| 149 | + except re.error: |
| 150 | + pass |
| 151 | + |
| 152 | + return result |
| 153 | + |
| 154 | + |
| 155 | +# --------------------------------------------------------------------------- |
| 156 | +# Recursive data masking |
| 157 | +# --------------------------------------------------------------------------- |
| 158 | + |
| 159 | +def mask_data(data: Any, config: MaskingConfig, parent_key: str = "") -> Any: |
| 160 | + """Recursively apply PII masking to event data. |
| 161 | +
|
| 162 | + Runs *after* redact_data (which handles secrets/tokens). This layer |
| 163 | + handles PII: emails, phones, credit cards, SSNs, etc. |
| 164 | + """ |
| 165 | + if isinstance(data, dict): |
| 166 | + result = {} |
| 167 | + for k, v in data.items(): |
| 168 | + k_lower = k.lower().strip() |
| 169 | + if config.mask_user_id_keys and k_lower in _USER_KEYS: |
| 170 | + result[k] = config.replacement if isinstance(v, str) else v |
| 171 | + else: |
| 172 | + result[k] = mask_data(v, config, parent_key=k) |
| 173 | + return result |
| 174 | + |
| 175 | + if isinstance(data, list): |
| 176 | + return [mask_data(item, config, parent_key=parent_key) for item in data] |
| 177 | + |
| 178 | + if isinstance(data, str): |
| 179 | + return _mask_string(data, config) |
| 180 | + |
| 181 | + return data |
| 182 | + |
| 183 | + |
| 184 | +# --------------------------------------------------------------------------- |
| 185 | +# Combined masking entry point (secrets + PII) |
| 186 | +# --------------------------------------------------------------------------- |
| 187 | + |
| 188 | +def mask_event_data( |
| 189 | + data: Any, |
| 190 | + config: MaskingConfig | None = None, |
| 191 | + redact_secrets: bool = True, |
| 192 | +) -> Any: |
| 193 | + """Apply secret redaction and PII masking to event data. |
| 194 | +
|
| 195 | + This is the single entry point used by the proxy layer. It runs |
| 196 | + redact_data first (for API keys, tokens, etc.) then mask_data (for PII). |
| 197 | + """ |
| 198 | + if config is None: |
| 199 | + config = MaskingConfig.default() |
| 200 | + |
| 201 | + result = data |
| 202 | + if redact_secrets: |
| 203 | + result = redact_data(result) |
| 204 | + result = mask_data(result, config) |
| 205 | + return result |
0 commit comments