Sub topic 2
Redacting What You Paste and What Tools Echo Back
Assume you know basic Git and command-line use, understand API keys and passwords, and can set environment variables. This chapter focuses on reducing exposure while using an AI coding tool.
Redact prompts and output
Treat prompts, pasted logs, stack traces, and command output as data leaving your machine. Remove:
- API keys, passwords, session cookies, and private keys
- Authorization headers and database connection strings
- Customer records, personal data, and proprietary algorithms
- Tool output that repeats environment variables or configuration files
Redaction means replacing a sensitive value with a safe placeholder while preserving enough structure for diagnosis.
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 textThis filter catches only known shapes. Inspect the result yourself before sending it. Also check the assistant's response, terminal transcripts, and generated patches for echoed secrets.
Move secrets into the environment
Keep credentials out of source files that an assistant can read. Store them in a secret manager or local environment, and read them at runtime.
import os
api_key = os.environ["PAYMENTS_API_KEY"]Add secret-bearing paths to an ignore file, while keeping a safe example configuration under version control:
.env
.env.*
!.env.example
*.pem
*.key
terraform.tfstate
terraform.tfstate.*Limit credentials and agent access
Use a credential that can perform only the required operation, on only the required resources. Prefer short-lived tokens over permanent keys. An agent that can edit code does not need production database access.
Control the agent's file, command, and network permissions. Start with read-only access, require approval for shell commands and external requests, and use a sandbox for untrusted work.
Prompt injection is untrusted text that tries to change an agent's instructions, such as a malicious line in a repository file. Do not let repository content grant new permissions. Review requests to read secrets, run commands, or contact unfamiliar services.
Exercise
You need help debugging a production error log that contains an Authorization header. What should you do first?
Show answer
Remove or replace the credential, inspect the remaining text for other sensitive values, and then paste only the minimum useful log.