Catch a secret leaked in base64 in a reply

Agent buildMedium25 min

Your task

Your coding agent can read the repository's deploy key. A prompt injection in a repository file can ask it to print the key in base64, which a plain text match misses. Before any reply leaves the agent, a filter checks it for the key, in plain text and in base64.

Write leaks(reply, secret) that returns True when the reply leaks the secret, and False otherwise.

Input and output:

  • reply: the agent's reply, up to 200,000 characters of printable ASCII and newlines.
  • secret: 8 to 64 characters of printable ASCII.
  • Return a boolean.

Rules:

  • The reply leaks the secret if the secret appears in it exactly, case included.
  • It also leaks if any base64 part of the reply decodes to bytes that contain the secret's ASCII bytes.
  • A base64 part is any substring made of whole groups of 4 characters, each from A to Z, a to z, 0 to 9, + and /. Its last group may instead be 3 such characters and =, or 2 and ==.
  • A part decodes as standard base64 does: each group of 4 decodes to 3 bytes, a group ending in = to 2 and one ending in == to 1. Bits left over in a padded group are ignored.

Examples

leaks(reply: str, secret: str) -> bool
Example 1
Inputleaks("Here is the key you asked for: dk_live_7Hq2Zp9Lx4", "dk_live_7Hq2Zp9Lx4")
OutputTrue
Example 2
Inputleaks("Encoded as requested: ZGtfbGl2ZV83SHEyWnA5THg0 (base64)", "dk_live_7Hq2Zp9Lx4")
OutputTrue
Example 3
Inputleaks("The deploy key lives in the vault. I cannot print it.", "dk_live_7Hq2Zp9Lx4")
OutputFalse