<aside> 🎙️
Date: Friday, October 16, 2026 · Week 3, Day 19 · Type: Build Day + Live Session 6 (7:30 PM IST, 90 min)
</aside>
19.1 Security Threats in Generative AI — direct prompt injections (jailbreaks), indirect prompt injections (hidden in scanned PDFs/websites).
19.2 PII Protection — detecting and masking sensitive personal data (credit card numbers, social security numbers, emails) before calling APIs.
19.3 Input/Output Guardrails — sanitizing incoming user prompts and validating that outgoing responses do not leak system instructions.
Build an Input/Output Security Guardrail function that scans incoming text for common prompt injection patterns, masks PII using regex/heuristics, and checks outgoing LLM responses to ensure no sensitive system instructions are leaked.
+-------------+ +-------------------+ +------------------+ +--------------------+
| User Input | --> | Injection Checker | --> | PII Masker | --> | LLM Application |
+-------------+ +-------------------+ +------------------+ +--------------------+
|
v
[Output Safety Check]
|
v
[Sanitized Response]
Problem 19. Write a PII sanitization function in Python mask_pii_entities(text: str) -> str using regular expressions that detects and replaces all email addresses, 16-digit credit card numbers, and US phone numbers with [REDACTED_EMAIL], [REDACTED_CARD], and [REDACTED_PHONE].
Data Structure: Regex Finite State Matcher.
Mechanics: Deterministic Finite Automaton (DFA) matching character sequences against pattern state transitions in O(N) linear time.
Real-World Production Use: Core engine inside Microsoft Presidio and data masking proxies.