Back
+30 XP
Concept

Sensitive String Scrubber for Tracebacks

Uncaught exceptions dumping to stdout or APM platforms like Sentry often contain API keys, Bearer tokens, or credit cards in local frame variables. A sensitive string scrubber scans raw text with regular expressions, replacing detected credentials with redacted placeholders before export.

python
import re

# Mask credit card numbers:
scrubbed = re.sub(r'\b(?:\d[ -]*?){13,16}\b', '***CARD_REDACTED***', text)

# Sensitive String Scrubber for Exception Tracebacks When an unhandled exception blows up in production, your logging framework helpfully prints the stack trace and all local variables: ```python HTTPError: Request to Stripe failed with Authorization: Bearer sk_live_998877665544 ``` If that log entry flows into Datadog, Sentry, or Slack alert channels, your live Stripe secret key is now visible to the entire organization, contractors, and third-party SaaS providers! 🛑🔒 A **Sensitive String Scrubber** acts as a security filter between exception handlers and your logging sinks. It scans error messages and tracebacks with precompiled regex patterns to redact sensitive credentials before they ever hit the disk or wire. ```python import re PATTERNS = [ (re.compile(r"Bearer\s+[A-Za-z0-9_\-\.]+", re.IGNORECASE), "Bearer ***REDACTED***"), ] ``` ## Pop Quiz Why should sensitive data scrubbing occur at the logging format stage rather than waiting for downstream SIEM systems? - [ ] Downstream systems may persist unredacted logs in intermediate buffers, disk queues, and error notifications. - [ ] SIEM systems require uncompressed ASCII text to generate database indexes. - [ ] The Python interpreter crashes if tracebacks contain more than 10 secret tokens. - [ ] Because regex substitution operates 10x faster inside client web browsers. ## Real-World Challenge Implement `scrub_sensitive_text(raw_text: str) -> str`. It must redact: 1. Keyword assignments (`password=...`, `token=...`, `api_key=...`, `secret=...`) replacing values with `***REDACTED***`. 2. Bearer tokens (`Bearer <token>`) replacing the token with `Bearer ***REDACTED***`. 3. 13-16 digit payment card numbers replacing digits with `***CARD_REDACTED***`. <details> <summary>Solution</summary> ```python import re SCRUB_RULES = [ # 1. Keyword value pairs (e.g. password=secret, token: "xyz") ( re.compile(r"(password|token|secret|api_key)\s*[:=]\s*['\"]?([^\s'\",;]+)['\"]?", re.IGNORECASE), r"\1=***REDACTED***" ), # 2. HTTP Authorization Bearer tokens ( re.compile(r"Bearer\s+[A-Za-z0-9_\-\.]+", re.IGNORECASE), "Bearer ***REDACTED***" ), # 3. Credit card number sequences (13 to 16 digits with optional spaces or dashes) ( re.compile(r"\b(?:\d[ -]*?){13,16}\b"), "***CARD_REDACTED***" ) ] def scrub_sensitive_text(raw_text: str) -> str: cleaned = raw_text for pattern, replacement in SCRUB_RULES: cleaned = pattern.sub(replacement, cleaned) return cleaned # Verification raw_log = ( "Exception in worker-04: Failed connecting with token=live_sec_9999 " "Header Authorization: Bearer eyJhbGciOiJIUzI1Ni. User billing card: 4111-2222-3333-4444." ) sanitized = scrub_sensitive_text(raw_log) assert "live_sec_9999" not in sanitized assert "eyJhbGciOiJIUzI1Ni" not in sanitized assert "4111-2222-3333-4444" not in sanitized assert "token=***REDACTED***" in sanitized assert "Bearer ***REDACTED***" in sanitized assert "***CARD_REDACTED***" in sanitized ``` </details>