Redact nested data without mutating it
Logging a whole decoded request can expose credentials nested below innocent-looking fields. Traverse dictionaries and lists recursively and replace values of sensitive keys. Matching key names without regard to case avoids obvious bypasses. Build new containers so redaction for logging does not silently change the data the application is processing.
data = {"profile": {"token": "secret"}}
print(data["profile"].keys())Think before coding
Recursion follows the data structure rather than a fixed nesting depth. Replacing the whole sensitive value also hides nested credentials under that key. This key-based rule is limited: it cannot detect secrets stored under unrelated names. Work through the example by hand before changing the starter. Decide what should happen for an empty input, a single value and a repeated value where those cases apply. The checks use several inputs so a hard-coded answer cannot stand in for the rule.
Your task
Write redact(value). Recursively copy dicts and lists; replace values whose string key lowercases to password, token or secret with "[redacted]". Other scalar values stay unchanged. Do not modify the original.
The explanation opens here once every check passes.