Fix the log redactor for login events
Your task
An AI added logging to your login function, and it printed the plaintext password and the new session token. Logs flow to aggregators, third-party tools and backups, where many more people can read them. Your team now runs every log line through a redactor before it is written.
An assistant wrote redact_sensitive_log(line). It is in the editor. It has a bug. Find it and fix it.
Input and output:
line: a log line made of parts separated by spaces. Many parts arekey=value.- Return the line with sensitive values replaced.
Rules the tests check:
- Split the line at each space character, keeping empty parts, and join the parts back with single spaces. The spacing does not change.
- A part with no
=stays as it is. - Otherwise the part's key is the text before its first
=. Judge the key alone, never the value. - Normalize the key: lowercase it and keep only ASCII letters and digits, dropping
_,-and every other character. - If the normalized key contains
password,passwd,token,secret,apikey,credentialorauthorization, the part becomes the key as written, then=[REDACTED]. - Every other part stays as it is.
Examples
redact_sensitive_log(line: str) -> strExample 1
Input
redact_sensitive_log("user=alice@example.com password=hunter2")Output
"user=alice@example.com password=[REDACTED]"Example 2
Input
redact_sensitive_log("Login attempt email=a@b.com sessionToken=abc123")Output
"Login attempt email=a@b.com sessionToken=[REDACTED]"Example 3
Input
redact_sensitive_log("status=ok latency_ms=42")Output
"status=ok latency_ms=42"