Topic 4 of 6 · 4 min read
Hardcoded secrets and credential leaks
Goal: Locate every place an AI-written change exposes a credential and choose the right fix: environment, secret manager or rotation.
A secret that has been committed, logged or pasted into a chat must be treated as leaked, and the only real fix is to rotate it.
Check yourself · 1 of 3
An AI agent committed an API key, and you removed it in the next commit. What else is required?
Why it matters
An assistant wants the code to run now. If you paste an API key into the prompt, or the key sits in a .env file the agent can read, it may copy the value into source code, a test fixture, a Dockerfile, a README example or a log line. Git history keeps it forever, and public repositories are scanned for keys by bots within minutes. GitHub runs secret scanning for exactly this reason, and many providers revoke leaked keys they detect.
Where AI changes leak secrets
- Source code.
const client = new Stripe("sk_live_...")written directly into a file. - Config and fixtures. A
.envfile committed, a copieddocker-compose.yml, a test that uses a real key. - Logs and errors. Printing the full request, headers or config object while debugging.
- Transcripts and prompts. Keys pasted into a chat or a prompt file that is saved or shared.
- Client bundles. A server key placed in front-end code, which every visitor can read.
The fix, in order
- Rotate the secret first. Removing it from the latest commit does not undo exposure.
- Load it from the environment or a secret manager, and fail at startup when it is missing.
- Keep
.envout of git with.gitignore, and commit a.env.examplewith empty values. - Turn on secret scanning and a pre-commit check so the next leak is caught before it is pushed.
- Scope the key. Give it the least permissions and a short life.
Before and after
- const stripe = new Stripe("sk_live_...redacted");
+ const key = process.env.STRIPE_SECRET_KEY;
+ if (!key) throw new Error("STRIPE_SECRET_KEY is not set");
+ const stripe = new Stripe(key);
The diff is only half the repair. The original key is in history, so it needs to be revoked and replaced.
What goes wrong
- Deleting the line and calling it fixed. The key remains in earlier commits.
- Moving the secret to a config file that is committed. Same leak, new filename.
- Logging the config object at startup "for debugging".
- Giving the agent production credentials when a sandbox key would do the job.
Examples
The fixture with a real key
def test_charge():
client = PaymentClient(api_key="sk_live_...redacted")
assert client.charge(100).ok
The agent used the key you pasted into the chat so the test would pass. It is now in the repository. Revoke it, use the provider's test-mode key from an environment variable, and mock the network call in unit tests.
The helpful debug log
console.log("Starting with config", config);
If config holds a database URL with a password, it now sits in your log system. Log only the non-secret fields, or redact known keys before printing.
Key points
- A leaked secret is compromised until it is rotated; deleting it from code is not enough.
- Secrets belong in environment variables or a secret manager, with startup failure when one is missing.
- Check source, fixtures, config files, logs and client bundles, not just the main code file.
- Commit a .env.example with empty values and keep the real .env in .gitignore.
- Give agents scoped sandbox keys and enable secret scanning and pre-commit checks.
Common mistakes
- Removing a committed secret in a new commit and skipping rotation.
- Checking only source files and missing keys in test fixtures, Docker files and logs.
- Pasting production keys into an agent prompt when a restricted test key would work.
- Putting a server-side key into front-end code because "it is just a build variable".
Check yourself · 2 of 3
Where should an application get its database password at runtime?
Check yourself · 3 of 3
Which file is safe to commit for configuration that needs an API key?
Practice items, not counted towards your mastery.
Start here
- CWE Top 25 Most Dangerous Software Weaknesses (MITRE) (opens in a new tab)DocsFree
The ranked list of weakness classes, including injection and broken access control, that generated code should be checked against.
- Do Users Write More Insecure Code with AI Assistants? (opens in a new tab)PaperFree
A user study showing assistant users wrote less secure code while feeling more sure of it.
- OWASP Top Ten (opens in a new tab)StandardFree
The standard awareness list of web application risks, from access control to misconfiguration.
More for this topic
- Secrets Management Cheat Sheet (OWASP) (opens in a new tab)ArticleFree
Storage, rotation, least privilege and auditing for secrets in applications.
- About secret scanning (GitHub Docs) (opens in a new tab)DocsFree
How repositories are scanned for leaked credentials and what to do when one is found.