In short: treat AI-generated code like a pull request from a fast junior developer who sounds confident and has never seen your production system. Read the diff, run it, check every dependency, test the edge cases, look for security holes, and only then merge. Generation is cheap now; verification is where your time should go.
AI assistants produce code that looks right. It compiles, it follows a familiar pattern and it comes with a convincing explanation. That is exactly why it is risky: plausible code lowers your guard. This guide gives you a repeatable checklist to review AI-generated code, with concrete examples. It works for output from any assistant, and it also works for code you copy from a forum or an old project. If you want better output in the first place, see our tips on getting more from ChatGPT.
Prerequisites
- A Git repository, so every change is a diff you can read and revert
- A way to run the code locally and some automated tests (even a few)
- A linter for your language, such as ESLint, Ruff or
golangci-lint - Basic knowledge of common web vulnerabilities (the OWASP Top 10 is a good reference)
The mindset: verify, do not trust
An assistant predicts likely code. It does not know your database schema, your deployment, your users or your threat model, and it can invent functions, options and packages that do not exist. Responsibility for the result stays with you. A useful rule is that you must be able to explain every line you merge. If you cannot, ask for a simpler version or write it yourself.
The 10-point checklist
1. Review the diff, not just the final file
Ask for small, focused changes and read them as a diff. Large rewrites hide unrelated edits: a removed check, a changed default, a renamed variable. If the assistant touched files you did not mention, find out why.
2. Run it and run the tests
Execute the code, then run the full test suite and not only the new tests. Reproduce the original bug or feature in the real application, since a snippet that works in isolation can fail inside your project.
3. Verify every dependency
Assistants sometimes suggest packages that do not exist, or that have a name close to a real one. Attackers can register such names with malicious code. Before you install anything:
- Check the package on its registry page: age, maintainers, download count, repository link
- Confirm the exact spelling against the official documentation
- Prefer libraries you already use, or the standard library
bashnpm view some-package time.created maintainers repository.url
npm audit
4. Look for injection and unsafe input handling
Every input from a user, an API or a file is untrusted. The most common serious mistake is building a query from strings. Compare these two versions:
js// Unsafe: user input becomes part of the SQL
const rows = await db.query(
`SELECT * FROM users WHERE email = '${email}'`
);
// Safe: the value is passed as a parameter
const rows = await db.query(
'SELECT * FROM users WHERE email = $1',
[email]
);
Apply the same thinking to shell commands, file paths, HTML output (cross-site scripting) and URLs your server fetches on behalf of a user.
5. Check authentication and authorization
Confirm that every route checks who the user is and whether they are allowed to do the action. Assistants often implement the login but forget that user A must not read user B’s data. Test with two accounts. For design guidance, read JWT vs sessions vs OAuth2.
6. Hunt for secrets and unsafe defaults
Search the diff for hard-coded keys, tokens and passwords, and for permissive settings such as cors: *, disabled TLS verification or debug mode. Run a secret scanner before you commit:
bashgitleaks detect --source . --verbose # recent versions: gitleaks git .
7. Test the edge cases and failure paths
Happy-path code is easy to generate. Check what happens with empty input, very large input, special characters, a missing file, a timeout, a failed network call and two simultaneous requests. Make sure errors are handled without leaking internal details to the user.
8. Look at performance and resource use
Typical problems include a query inside a loop (the N+1 problem), loading a whole table into memory, missing pagination and missing indexes. Check the query plan for any new database query and test with realistic data volumes.
9. Check licenses and provenance
Generated code can resemble open-source code under a license with conditions. For anything sizable or unusual, search for a distinctive line to see whether it is copied. Keep your company policy in mind if you work for a client.
10. Review the tests the assistant wrote
Assistants can produce tests that always pass because they assert the wrong thing or mock too much. Break the code on purpose and confirm that a test fails. A test that cannot fail is not a test.
Make the process automatic
Do not rely on discipline alone. Put checks in your pipeline so unsafe code is caught early:
- A linter and a type checker on every commit
- Dependency scanning with
npm auditor Dependabot alerts - Secret scanning with
gitleaksor GitHub’s built-in scanning - A static analysis tool such as Semgrep for common vulnerability patterns
- Required tests before merge, as in our GitHub Actions deployment pipeline
Using an assistant as a second reviewer
A second pass can help, if you ask for criticism instead of approval. A good prompt is: “Review this diff for security issues, race conditions and missing error handling. List concrete problems with line numbers and do not rewrite the code.” Treat the answer as hints to verify, not as a verdict. The same blind spots can appear twice.
Team rules that work
- Keep pull requests small, so a human can really read them
- Say in the description when a change was AI-assisted and what you verified
- Never paste secrets, customer data or private code into tools your company has not approved
- Require at least one human review for authentication, payments and data deletion
Common mistakes
- Merging because “the tests pass” when the tests were also generated
- Installing a suggested package without checking that it exists and is maintained
- Accepting a large rewrite instead of asking for a minimal change
- Skipping authorization checks because the login works
- Assuming the explanation is correct because it is well written
FAQ
Is AI-generated code less secure than human code?
Not necessarily, but it is often unreviewed, and it repeats common insecure patterns from its training data. The risk comes from trusting it without checking.
How long should a review take?
Proportional to the risk. A small utility function needs minutes. Code that handles authentication, money or personal data deserves a careful, slower review and a second person.
Can I automate the whole review?
You can automate a lot, including linting, scanning and tests, but not judgment about business logic and threat models. Keep a human in the loop.
Which tests matter most?
Tests that cover the behaviour users depend on and the cases that would hurt most if they failed: permissions, money, deletion and input validation.
Next steps
Pick one item from this list and add it to your pipeline this week: a secret scanner or dependency audit is a quick win. For deeper reading, see the OWASP Top 10 and strengthen session handling with our guide to session idle timeout in JavaScript.