Why AI-generated code needs a security review
AI coding assistants can speed up prototyping, tests, migrations, and routine development. But generated code can also be incomplete, rely on outdated packages, mishandle authorization, or look convincing while quietly missing important checks. The right approach is not to avoid AI coding tools; it is to treat their output like a contribution from a fast assistant whose work still needs review.
The OWASP Top 10 for Web Application Security is a useful baseline for common application risks. AI-generated code should be reviewed against the same requirements as code written by a person.
The AI code security checklist
1. Verify authorization on the server
Do not rely on hiding a button or checking a role only in the browser. Every sensitive server action must verify the authenticated user's permission to access the specific record or operation. Test with a low-privilege account and attempt to access another user's object by changing an identifier.
2. Validate all untrusted input
Treat form values, URL parameters, uploaded files, webhook payloads, and third-party API responses as untrusted. Use framework-supported validation, parameterized database queries, context-aware output encoding, and appropriate file-type and size checks.
Do not accept generated SQL, shell commands, filesystem paths, or dynamic code as safe simply because an AI assistant produced them.
3. Inspect dependencies before installing them
AI tools may suggest packages that are unnecessary, abandoned, misspelled, or less trustworthy than a standard library alternative. Check the package's official source, maintenance activity, version history, license, and known vulnerabilities. Pin versions where appropriate and use a lockfile plus automated dependency scanning.
Avoid installing a package based only on its name in a chat response.
4. Search for secrets and sensitive data
Look for API keys, database credentials, private keys, access tokens, production URLs, and personal data in code, logs, test fixtures, and prompts. Keep secrets in an approved secret manager or environment configuration, and ensure they are not committed to source control.
If a secret was exposed, remove it from the code and rotate it. Deleting it from the latest commit alone may not remove it from repository history or logs.
5. Review authentication and session handling
Confirm password reset flows, session expiration, cookie flags, multi-factor authentication, rate limits, and account recovery behavior. Use established framework or identity-provider implementations rather than inventing custom cryptography or home-grown password storage.
6. Check error handling and logging
Errors should help operators debug without exposing secrets, stack traces, internal hostnames, SQL statements, or user data to visitors. Logs should support investigation but should not become a second copy of sensitive information.
7. Review file, network, and command access
Code that reads files, fetches URLs, processes uploads, or invokes operating-system commands can introduce path traversal, server-side request forgery, unsafe deserialization, or command injection. Restrict allowed paths and destinations, use safe APIs, apply timeouts, and run services with the least privileges they need.
8. Test the security properties, not just the happy path
Ask for tests covering unauthenticated access, cross-user access, malformed input, oversized requests, missing fields, unexpected types, and failure states. Run unit tests, static analysis, dependency checks, secret scanning, and targeted dynamic tests in CI before deployment.
A passing test suite proves only what the tests cover. It does not guarantee the application is secure.
A safer workflow for AI-assisted development
1. Define constraints first. Specify framework versions, trusted libraries, data-handling requirements, and security expectations. 2. Generate a small change. Prefer incremental code that is easier to inspect over a large, unexplained rewrite. 3. Review the diff. Understand every new dependency, permission check, database query, and network request. 4. Run automated checks. Include linting, tests, type checks, dependency auditing, and secret scanning. 5. Use human review for sensitive paths. Authentication, payments, access control, cryptography, and data export deserve extra scrutiny. 6. Deploy gradually. Use staging, backups, monitoring, and a rollback plan.
How to prompt an AI assistant more safely
Instead of asking for “production-ready secure code,” give concrete requirements. For example:
> Implement this endpoint using the project's existing framework and dependencies. Enforce authorization on the server for each requested record. Validate all input, use parameterized queries, avoid logging secrets, include negative tests for unauthorized access and malformed input, and explain assumptions or unresolved security concerns. Do not invent package names or claim tests passed unless they were run.
A detailed prompt improves the review target, but it does not transfer accountability to the model. Verify package versions and independently inspect the result.
What about AI code review tools?
Automated reviewers can flag likely vulnerabilities and explain risky patterns, but they can miss flaws, produce false positives, or overlook business-specific authorization rules. Use them as one layer in a secure development process—not as a security certification.
For broader guidance, consult OWASP's security resources and the security documentation for your language and framework.
Final takeaway
AI can make development faster, but speed increases the value of a repeatable review process. Validate inputs, enforce server-side permissions, audit dependencies, protect secrets, test negative cases, and keep a human responsible for the final change. The safest AI-generated code is code whose behavior has been understood, tested, and reviewed before release.
