Automation

Secret redaction, partly written

Remove sensitive values from nested JSON logs before exporting them to an observability backend.

In production

Logs leaving the box can carry passwords. Missing a nested key leaks a secret; over-redacting hides useful fields.

What it must return

Recursively copy structures, replacing values under case-insensitive keys password, token, and authorization with "[REDACTED]".

  • Recursively handle dictionaries, lists and JSON scalars. Replace values of case-insensitive keys password, token and authorization with "[REDACTED]". Preserve key spelling and all other values. Input keys are strings.
  • Do not mutate input values. Use the Python standard library only.

Edge cases

  • Handle dicts, lists, and scalars separately.
  • Redact on the key before recursing into that value.
  • Preserve key spelling for non-sensitive fields.

Signature

redact(value)

Part of this function is already written. The TODO comments mark what is left.

Hints

  1. Second hint

  2. A partial sketch

The reference solution opens after the last hint.

solution.py · Python

Loading editor…

Nested fields

value

{
  "user": "a",
  "Password": "secret",
  "nested": [
    {
      "token": "x",
      "ok": true
    }
  ]
}

Expected

{
  "user": "a",
  "Password": "[REDACTED]",
  "nested": [
    {
      "token": "[REDACTED]",
      "ok": true
    }
  ]
}

Each run uses a fresh Python runtime in this browser, with a 3-second limit and a Stop button. The first run downloads the runtime. 0 of 8 in Automation passed in Python on this device.

Next: Secret redaction, with a bug