Reviewing AI-Generated Code for Security Before It Ships
Review AI-generated code for permissions, injection, secrets, dependencies and business rules. Use negative tests and deployment evidence before approving a change.
AI-generated code should pass the same engineering and security review as any other contribution. The ability to compile or satisfy a happy-path test does not establish that the implementation respects permissions, protects data or handles hostile input.
Review the resulting diff in the context of the application. An assistant can propose a plausible local fix while overlooking a shared helper, alternate route or business rule that is not present in its context.
Establish the intended behaviour
Write down the actor, action, resource and permitted conditions before judging the implementation. For an invoice download, this means more than “return the PDF”: identify tenant membership, role permissions, record state and the fields the user may receive.
Check whether the generated change weakens an existing control to make a test pass. Removed permission checks, broader CORS rules, disabled certificate verification and permissive fallback paths deserve particular attention.
Follow untrusted data to sensitive operations
Trace request fields, retrieved documents, uploaded files and external responses into queries, templates, paths, commands and network calls. Use the protection suited to each operation, such as parameterised database queries and context-appropriate output encoding.
An allowlist at one boundary does not automatically make the data safe in every later context. Confirm the actual library API rather than accepting a generated comment that describes it as secure.
Review trust and dependency changes
Inspect new packages, external services, build scripts and configuration. Confirm that the package exists in the intended registry, is appropriate for the job and has an accountable maintenance path. Review lockfile and pipeline changes alongside application code.
Check for test credentials, embedded tokens, debug endpoints and verbose errors. A harmless-looking example value can be copied into production or become a shared default across deployments.
Add tests for the ways it should fail
| Change | Useful negative test |
|---|---|
| Record lookup | Another user's or tenant's record is refused. |
| Administrative action | An ordinary account cannot invoke it directly. |
| File operation | Supplied paths cannot escape the permitted storage area. |
| Callback handler | Invalid signatures and repeated events cannot alter state. |
| External URL fetch | Disallowed destinations and redirect targets are refused. |
| Permission cache | Changed or revoked access is reflected as intended. |
Create the assertions from the requirements, not only from the generated implementation. Ask what should remain unchanged after a rejected request and test that state explicitly.
Keep review accountable
A person with the relevant application knowledge should own the decision to merge. Record material assumptions, test results and any deferred work. Asking the same model whether its own output is secure can provide suggestions, but it does not replace independent evidence.
For sensitive changes, combine source review with runtime investigation of the deployed behaviour. Verify the actual build and configuration rather than assuming that a correct patch reached every environment.
Use findings to improve the development process
Turn recurring failures into maintained permission tests, coding patterns and review questions. Improve the context supplied to future tasks, while keeping secrets and unnecessary sensitive information out of prompts.
Related resources
Read our testing methodology or discuss your assessment.