AI generated code security review
Learn a concrete procedure to catch the three most common security mistakes in AI generated code before they reach a pull request.
the short answer
Follow a short review pass to catch three common security mistakes in AI generated code before they reach a pull request.

You will finish a short, repeatable pass that catches three common security mistakes in AI generated code before a pull request is even opened.
Gather the generated changes and their context
Start with the diff the agent produced and the prompt it received. If the agent edited multiple files, pick the primary change and open its diff. Then open the prompt the agent used, because the prompt often states what the agent thought it was doing and what it assumed about inputs.
Read the prompt first
Look for assumptions the agent made about inputs, such as “the user is authenticated” or “the input is sanitized.” If the prompt says the agent should trust a parameter it never saw, that is a red flag. Agents that assume inputs are safe without evidence tend to trust them in code as well.
Open the diff next
Scan for functions that take parameters and use them without validation. Focus on public entry points like HTTP handlers, CLI arguments, and message queue consumers. If a function takes userId and immediately passes it to a database query without a check, you have a trust issue.
Note any place where the agent copied an auth or validation pattern from another part of the codebase. Copying a pattern without the context that made it safe is a frequent cause of broken security assumptions.
Check for untrusted input used directly
Look at every public function the agent touched. Confirm that each one validates or sanitizes its inputs before using them. If the agent added a new endpoint that takes a filePath and immediately reads the file, that is a clear violation unless the agent also added validation to ensure the path stays inside an allowed directory.
Ask the agent for context if the prompt is unclear
If the prompt does not state whether inputs are trusted, ask the agent directly in its chat or in the task description. A short clarification can prevent a security mistake from slipping through. Agents that lack this context often make implicit trust assumptions that are easy to overlook.
Mark functions that should validate but don’t
For each function that takes user-controlled input, write a comment if validation is missing. Common patterns to catch are raw SQL queries with string concatenation, file reads with unchecked paths, and shell commands built from user input. If the agent copied a function that already had validation, verify that the context is the same and that the copied checks still apply.
A function that trusts an input it never saw is a bug waiting to surface.
Verify copied auth patterns carry their original context
Auth patterns are fragile. Copying a middleware or a helper function without the surrounding context often removes critical parts, such as rate limiting, IP allowlists, or user authorization checks. If the agent copied a JWT validation snippet, confirm that the same signing key and algorithm are still in use and that the token is still required for the endpoint.
Compare the copied code to its origin
Open the file where the pattern originally lived and compare it line by line with what the agent produced. Look for differences in the order of checks, the presence of additional validation steps, or the removal of error handling. If the original pattern checked for a specific role before allowing access and the copied version omitted that check, the copy is incomplete.
Check for missing middleware or wrappers
If the agent added a new endpoint and copied an auth helper, verify that the endpoint is wrapped by the same middleware as the original. Middleware that enforces authentication or authorization can be accidentally omitted when copying patterns between files or services.
If the agent copied a pattern that relied on a configuration value that no longer exists, the copy is broken. Missing configuration often means missing environment variables, which in turn means authentication defaults to weak or disabled settings.
A copied auth pattern without its original context is a security hole in disguise.
Audit dependencies the agent pulled in
Agents often introduce new dependencies to solve small tasks. A dependency that looks innocent can carry vulnerabilities, especially if it is obscure or has a history of security advisories. Scan the diff for package.json, pyproject.toml, go.mod, or equivalent files and list every new dependency the agent added.
Check the dependency’s recent advisories
For each new dependency, open its repository or package registry page and review the last six months of security advisories. Focus on high and critical severity issues. If the dependency has a known vulnerability that is not patched in the version the agent installed, remove it or upgrade to a safe version.
Pay special attention to dependencies that handle parsing, serialization, or network requests. These are common vectors for injection or denial of service. If the agent added a lightweight CSV parser and it has a published vulnerability for path traversal, replace it with a maintained alternative.
Prefer standard library or well-maintained alternatives
If the dependency is small and solves a trivial task, consider whether the standard library or a widely used alternative can do the same without the risk. For example, many agents add a lightweight HTTP client that ends up being less secure than the built-in client in the language runtime. Replace it if the risk is not justified by the benefit.
A small dependency can carry a large security surface if it is not maintained.
Decide what to fix before the pull request
After you finish the three checks, you will have a list of functions to fix, patterns to restore, and dependencies to replace or remove. Group the fixes by severity: critical issues that must be addressed before merging, and non-critical issues that can be addressed in a follow-up. If the agent copied an auth pattern without the original context and the endpoint now allows unauthenticated access, that is a critical fix.
Write the minimal fix for each issue
For untrusted input used directly, add validation or sanitization at the entry point. If the agent copied an incomplete auth pattern, restore the missing checks or wrap the endpoint with the correct middleware. If the dependency is unsafe, remove it or upgrade to a patched version. Keep each fix minimal and test it immediately.
Document the assumptions for future agents
After you fix an issue, add a comment or a small note in the prompt template explaining why the validation or auth pattern is necessary. This helps future agents avoid repeating the same mistake. If the prompt for the endpoint states that the user must be authenticated, the next agent is less likely to copy the pattern without the context that enforces it.
If you coordinate multiple agents on the same task, track the fixes in a shared workspace so every agent sees the same context. Overlap warnings arrive when an agent claims work, which the five tools each contribute to in a different way.
When to stop and escalate
Stop the review if you find an issue that requires a larger architectural change, such as moving auth logic into a shared library or replacing a core dependency. These issues cannot be fixed in a single diff and should be escalated to the team. If the agent pulled in a dependency that changes the data model or introduces a new network layer, create a separate task for the platform team.
If the agent’s prompt or the surrounding context is unclear, ask for clarification before proceeding. Security decisions cannot be made on assumptions. If the prompt does not state whether inputs are trusted, pause the review and request a clarification from the agent or the prompt author.
If the diff is large or touches many files, split the review into smaller chunks. Focus on the primary change and its immediate context first. Large diffs are hard to reason about and often hide subtle security issues. Once the primary change is secure, move to the secondary changes.
A security review is not complete until every assumption is explicit and every input is validated.