AI Coding Assistants

AI Coding Assistant Security Review

AI assistants can write fast, but they do not write with a security checklist in mind. This guide shows how to review AI-generated code for vulnerabilities before it reaches production, with repeatable prompts, common failure modes, and CI enforcement.

FreeLast tested: 2026-09-04Audience: Developers, security leads, engineering managers

Why security review is different for AI-generated code

Most teams treat AI-generated code as already reviewed. The assistant produced the code quickly, it passes lint, and the tests are green. That assumption is the problem. AI coding assistants optimize for plausible output, not for threat modeling. They can concatenate SQL strings, echo credentials into comments, or skip null checks without any warning. The result is code that looks correct but carries the same class of bugs that junior engineers introduce in their first month: injection risks, unsafe deserialization, missing validation, and hardcoded secrets. The fix is not to stop using assistants. It is to apply the same security checklist to generated code that you would apply to code written by a new contractor.

In practice, AI-generated code often skips the security gate because it feels "already reviewed." That bias is dangerous. Generated code should go through the same diff review, secret scanning, and test coverage checks as any human-written patch.

Prompt pattern for security review

Security review prompts should be explicit and repeatable. The model needs to know which vulnerability classes to prioritize and which to ignore. A good base prompt specifies the categories, asks for file and line context, and requires severity.

Base prompt

You are reviewing a pull request for security issues. Rules: - Do not rewrite the code. - Return findings only as a concise review comment. - Prioritize: SQL injection, XSS, CSRF, hardcoded secrets, unsafe deserialization, missing input validation, auth bypass, IDOR, insecure direct object references. - If nothing is wrong, say "No blocking findings."

Diff input

Context: {{issue_or_pr_title}} Success criteria: {{success_criteria}} Diff: ```diff {{unified_diff_or_selected_hunks}} ```

Keep the diff short. Models degrade after a few thousand tokens of patch content. If the change is large, split it into logical hunks and review each hunk separately.

Common vulnerabilities the assistant misses

The most common misses are predictable. They are the same issues that show up in every OWASP top-ten review, but they appear more often in AI-generated code because the assistant has seen insecure patterns in training data and reproduces them as plausible output.

VulnerabilityWhat the assistant often doesWhat to check
SQL injectionBuilds queries with string concatenation instead of parameterized statements.Verify all DB queries use parameterized inputs or an ORM.
Unsafe deserializationUses pickle, yaml.load without SafeLoader, or eval on user input.Flag any eval, exec, pickle, or unsafe YAML parse on external data.
Hardcoded secretsEchoes API keys, tokens, or passwords from context into comments or config blocks.Scan for high-entropy strings and common key prefixes.
Missing input validationAssumes callers provide valid data; no null, empty, or length checks.Require explicit validation for every new parameter or endpoint.
Auth bypassForgets permission checks on new routes or endpoints.Verify new routes enforce auth and authorization before action.
IDORUses sequential IDs without ownership checks in API responses.Require ownership or scope checks for every object fetch.

Redact before you review

Redaction happens before the assistant sees the request and after it returns the output. On the input side, remove secrets from the request before it reaches the assistant. Use placeholder values like VITE_SUPABASE_KEY instead of the real key, and keep a local .env.example with fake values. If the assistant needs to know the format, give it the shape, not the credential.

On the output side, scan for common patterns before the code touches version control. A simple first pass is to grep for high-entropy strings and common prefixes:

grep -R -E "(sk|pk|key|token|secret|password)\s*=\s*['\"][^'\"]{20,}['\"]" .

That command will not catch every leak, but it catches the low-hanging fruit quickly. For a more robust workflow, see AI Coding Assistant Secrets Management for redaction and scanning patterns.

CI enforcement

The best security control is automatic. A pre-commit hook or CI job makes scanning non-optional. The pattern is simple: if the scanner finds a secret, the pipeline fails. That keeps the team from accidentally merging leaked credentials. If the assistant injects a secret into a branch, the scanner catches it before a human reviewer opens the diff.

For CI pipeline design, see AI Coding Assistant CI/CD Pipelines for automation patterns. For the broader review workflow, AI Coding Assistant Code Review covers diff review, checklist design, and routing findings to the right owner.

# Example: GitHub Actions secret scan step - name: Secret scan run: gitleaks protect --source=. --staged

Related reading

These articles cover adjacent workflows for AI-assisted development with security in mind.