Reviewing AI-generated code securely
AI assistants write code fast. How to keep security reviews, testing and accountability in the loop.
I use AI coding assistants every day. They refactor, explain legacy code, write tests and find bugs faster than I can. I even used one to audit and clean up my own legacy websites. But speed changes the economics of security: if code is produced ten times faster, review becomes the bottleneck, and skipped reviews become vulnerabilities.
The rule I follow is simple: AI writes, a human owns. Here is what that means in practice.
Typical risks in AI-generated code
- Plausible but insecure patterns. String-concatenated SQL, missing output encoding, weak randomness, disabled certificate checks "to make it work".
- Outdated knowledge. Suggestions based on deprecated APIs or old library versions with known vulnerabilities.
- Invented dependencies. A suggested package name that doesn't exist can be registered by an attacker. This is sometimes called "slopsquatting".
- Secrets in prompts and code. API keys pasted into a chat, or example credentials that end up in production.
- Over-broad permissions. AI agents that can run commands, change infrastructure or push to production without limits.
Guardrails that work
Treat AI output like a pull request from a new colleague
Clever, fast, and unfamiliar with your threat model. Review it with the same standard: does it validate input, encode output, handle errors safely and follow your architecture?
Automate the boring checks
Static analysis (SAST), dependency and licence scanning (SCA), secret scanning and tests run on every change, whoever wrote it. Automation scales with AI; human attention doesn't.
Verify every new dependency
Check that the package exists, is maintained, and is the one you think it is. Pin versions and use a lock file.
Keep secrets out of the conversation
Use placeholders in prompts and a secret manager in code. Configure your tools so that credential files are never sent to the model.
Limit what agents can do
Agents should work on a copy, ask before irreversible actions and never deploy to production on their own. Apply least privilege, exactly as you would for a service account.
Ask the AI to attack its own code
A useful habit: after generating code, ask the assistant to review it as an attacker against the OWASP Top 10. It often finds its own mistakes. It doesn't replace human review, but it improves the input to it.
Accountability doesn't change
If AI-generated code causes a data breach, "the assistant wrote it" is not an answer the regulator, the customer or the data subject will accept. The developer who merges the code and the organisation that ships it remain responsible. Used with that mindset, AI is one of the best security tools we have ever had.