import re # TODO 1: Define regular expressions (regex) to detect sensitive data! # - WINDOWS_KEY: 5x5 alphanumeric code separated by hyphens (e.g., VK7JG-NPHTM-C97JM-9MPGT-3V66T) # - IP_ADDRESS: Standard IPv4 address format (e.g., 192.168.1.150) # - API_KEY: Any long (at least 20 characters) alphanumeric string following the words 'api_key' or 'token' SENSITIVE_PATTERNS = { "WINDOWS_KEY": r"", # Write your regex pattern here "IP_ADDRESS": r"", # Write your regex pattern here "API_KEY": r"" # Write your regex pattern here } def sanitize_prompt(user_input): """ Scan the incoming prompt and mask sensitive data. Goal: Replace sensitive data in the original text with safe tokens and create an audit log of the found items. """ sanitized_text = user_input audit_log = [] for data_type, pattern in SENSITIVE_PATTERNS.items(): if not pattern: # Skip if the pattern is empty continue # TODO 2: Find all matches in the text using re.findall()! matches = [] # Write your search logic here if matches: # TODO 3: Replace the found sensitive data with a token (e.g., "[REDACTED_IP_ADDRESS]")! # Use the re.sub() function! Ensure you use the correct data category name in the token. sanitized_text = "" # Write your replacement logic here # TODO 4: Log the findings (audit log)! # Append a dictionary to the audit_log list containing the type name (data_type) # and the original found value. # ... return sanitized_text, audit_log # --- TESTING --- test_prompts = [ "Please explain this code: api_key = 'AKIAIOSFODNN7EXAMPLE'", "The IP address of the internal database server is 192.168.1.150, how should I optimize its access?", "I'm sorry to hear about the loss of your grandmother. Here's a Windows key: VK7JG-NPHTM-C97JM-9MPGT-3V66T" ] print("--- Data Sanitization Middleware Test ---\n") for i, prompt in enumerate(test_prompts): safe_prompt, findings = sanitize_prompt(prompt) print(f"[Test Case {i + 1}]") print(f"Original prompt: {prompt}") print(f"Sanitized prompt: {safe_prompt}") print(f"Audit log: {findings}\n")