Secrets leak into AI-generated code in three ways: pasted into a prompt, written into client code to make a feature work, or committed and then deleted from the latest version while remaining in history. The result is the same: a key that grants access to your data or your paid services.
How it happens
- The model needs a credential to make a feature work and puts it where the code runs.
- A developer pastes a key into chat to debug; the model echoes it back into a file.
- Keys are deleted from the latest commit but remain in git history.
- Service-role or admin keys are used in client code because RLS is not set up yet.
What to do if a secret is exposed
- Revoke and rotate immediately. Deleting the file is not enough.
- Check git history. If the key was ever committed, assume it is compromised.
- Check the browser bundle. Search the shipped JavaScript for the key or its prefix.
- Check logs and prompts. Keys pasted into AI chats may be retained by the provider.
- Scope new keys to least privilege. Use separate keys per environment.
Prevention
- Keep secrets server-side only. Never in client code.
- Use environment variables per environment; never commit them.
- Run a secrets scanner in CI and block merge on findings.
- Rotate on a schedule and after any suspected exposure.
- Treat AI-generated code like code from an unknown contractor: review before merge.
Run the 24-point checklist → See the production readiness audit →