Sub topic 6
Redacting what you paste and what tools echo back
This chapter assumes you know Git basics, have used an AI assistant, understand what an API key is, and can work with environment variables. Earlier chapters covered which files count as secrets and how to exclude them from AI context. Ignore files do not help here. This chapter covers text you copy into a prompt yourself, and text a tool reads from a terminal and sends back to the model.
Why pasted text is a leak path
Logs, stack traces, HTTP traces, and config dumps often contain credentials. A failing request may print its Authorization header. A crash report may print the whole environment. When you paste that output into a chat, the values leave your machine along with the rest of the prompt.
The same thing happens without your involvement. An agent that runs env, docker inspect, or cat config.yml receives the output as context. A secret that was never in your source tree can still enter the conversation this way.
The dotted line is the path to avoid. Place the redaction step on every route into the prompt.
Redact before you paste
Treat redaction as a habit with three steps:
- Replace secret values with a placeholder that keeps the structure, such as
[REDACTED]. The model can still see that a token was present. - Check for values that identify you or your systems: internal hostnames, customer emails, account IDs.
- Read the result once before you send it.
A small helper can do the first step for well-known credential shapes. This one handles AWS access key IDs, bearer tokens, and key=value pairs.
import re
PATTERNS = [
(re.compile(r"AKIA[0-9A-Z]{16}"), "[REDACTED_AWS_KEY_ID]"),
(re.compile(r"(?i)(authorization:\s*bearer\s+)[^\s]+"), r"\1[REDACTED]"),
(re.compile(r"(?i)((?:password|secret|token|api[_-]?key)\s*[=:]\s*)\S+"), r"\1[REDACTED]"),
]
def redact(text: str) -> str:
for pattern, replacement in PATTERNS:
text = pattern.sub(replacement, text)
return text
print(redact("password=hunter2 Authorization: Bearer abc123"))The output is password=[REDACTED] Authorization: Bearer [REDACTED]. Each pattern keeps the label in a capture group and replaces only the value.
Common errors
- Redacting only the line that looks wrong. Secrets often appear several lines above or below the error, in request headers or environment dumps.
- Trusting a pattern list as complete. Add patterns for the providers you use, and still read the output.
- Replacing a secret with a similar fake. A realistic placeholder can be mistaken for a real value later. Use an obvious marker.
- Forgetting that a pasted secret is already exposed. Redacting after the fact does not undo a send. If a real value went into a prompt, treat it as leaked and rotate it, as covered in the incident response chapter.
Limit what tools echo back
You can also reduce what an agent sees:
- Prefer commands that print less. Ask for
printenv VARIABLE_NAMEof a non-secret variable, not a bareenv. - Do not let the agent run commands that dump credentials, such as
cat .envoraws configure list, without your approval. Approval settings are covered in the agent permissions chapter. - Pipe verbose output through a filter first. For example, run
./deploy.sh 2>&1 | python redact.pyif you saved the helper with a loop that reads standard input.
Redaction is a second layer. The first layer is keeping secrets out of logs and out of source, so there is less to remove.
Exercise
A teammate pastes a failing curl -v trace into an AI chat after running the redact helper. The trace contains the line X-Api-Key: 9f8e7d6c. Will the helper remove that value, and what should the teammate do?
Show answer
No. The pattern for api_key requires an underscore, a hyphen, or nothing between "api" and "key", and then = or : right after "key". X-Api-Key: 9f8e7d6c has a hyphen between "Api" and "Key", so it does match api[_-]?key followed by :, and the value is replaced. However, the helper would miss a header such as X-Auth: 9f8e7d6c. The teammate should read the redacted output before sending, add patterns for the headers their services use, and if any real value was already pasted, rotate that credential.
Exercise
Name two ways an AI agent can receive a secret that is not in your source code, and one control for each.
Show answer
Examples: the agent runs env or printenv and reads credentials from the environment, controlled by requiring approval for commands and avoiding bare env. Or the agent runs cat on a config file or reads verbose logs, controlled by filtering output through a redaction step and keeping secrets out of logs.