Mask PII (Credit Cards & Emails) in Log Payloads
Leaking raw credit card numbers or email addresses into centralized log sinks breaches GDPR and PCI-DSS compliance, incurring massive regulatory fines. Production log sanitizers intercept outgoing text payloads, masking PANs to the last 4 digits and scrubbing email usernames 🛡️💳🔒.
import re
CARD_RE = re.compile(r"\b(?:\d{4}[ -]?){3}(\d{4})\b")
def scrub_card(text):
# Preserve last 4 digits for customer support auditing
return CARD_RE.sub(r"****-****-****-\1", text)
raw_log = "Payment processed for card 4532-1111-2222-9876 successfully."
print("Safe Log:", scrub_card(raw_log))
# 5. Mask PII (Credit Cards & Emails) in Log Payloads Accidentally logging sensitive user information (Personally Identifiable Information - PII) is one of the most common ways organizations get hit with multimillion-dollar data privacy penalties 🛑. ```python # The Compliance Mandate: # Raw Log: "Checkout user customer@domain.com charged $49 on 4111 2222 3333 4444" # Compliant Log: "Checkout user c******r@domain.com charged $49 on ****-****-****-4444" ``` ### Engineering Masking Pipelines 1. **Preserve Last 4 Digits for Auditing**: When a customer calls support asking why a charge was declined, customer support must be able to ask "Are you using the card ending in 4444?". Stripping the entire number breaks support workflows. 2. **Word Boundary Anchors (`\b`)**: Always use word boundaries so ordinary numbers (like database serial IDs or Unix epoch timestamps) are not mistakenly matched as credit cards. 3. **Scrub Before Network I/O**: Masking must happen inside your application's logging pipeline *before* the payload is serialized over the wire to Datadog, CloudWatch, or Elasticsearch 🛡️💳🔒. --- ### Interactive Pop-Quiz 🧠 #### Why does PCI-DSS compliance allow retaining the last 4 digits of a Primary Account Number (PAN) while masking the rest? - [ ] The last 4 digits cannot be used for fraud on their own, but enable customer service agents to confirm card identity with users 🛡️ - [ ] Because the last 4 digits are encrypted with AES - [ ] Because credit card companies require 4 digits to compute sales tax - [ ] The last 4 digits determine the client's country of birth --- ### Practical Challenge 🎯 Implement `mask_pii(text: str) -> str` using regular expressions. Mask 16-digit credit cards (with optional spaces or hyphens) into `'****-****-****-XXXX'` (preserving last 4 digits). Mask emails by replacing all username characters between the first and last letter with asterisks (e.g. `'alice@example.com'` -> `'a***e@example.com'`). Test with `'User alice@example.com paid with 4111-2222-3333-4444.'`, assert masked values, and print result. <details> <summary>Solution</summary> ```python import re CARD_RE = re.compile(r"\b(?:\d{4}[ -]?){3}(\d{4})\b") EMAIL_RE = re.compile(r"\b([a-zA-Z0-9_.+-]+)@([a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)\b") def _mask_email_match(match): user, domain = match.group(1), match.group(2) if len(user) <= 2: masked_user = user[0] + "*" else: masked_user = user[0] + ("*" * (len(user) - 2)) + user[-1] return f"{masked_user}@{domain}" def mask_pii(text: str) -> str: text = CARD_RE.sub(r"****-****-****-\1", text) text = EMAIL_RE.sub(_mask_email_match, text) return text log_entry = "User alice@example.com paid with 4111-2222-3333-4444." cleaned = mask_pii(log_entry) assert "alice@example.com" not in cleaned assert "4111-2222-3333-4444" not in cleaned assert "a***e@example.com" in cleaned assert "****-****-****-4444" in cleaned print(f"Sanitized PII Log: {cleaned}") ``` </details>