Skip to main content
AI-Generated Code Review: A Security Engineer's ReferenceGeneral
5 min readFor Security Engineers

AI-Generated Code Review: A Security Engineer's Reference

You're reviewing more pull requests than ever. Your team's throughput has doubled, and you're catching issues you've never seen before: logic errors that compile cleanly, API calls that look right but leak data, and authentication checks that pass tests but fail under load.

This is the new normal. According to Jellyfish's recent study, 64% of companies now generate most of their code with AI assistance. Development teams in the top quartile of AI adoption have doubled their pull request throughput. The 90th percentile of companies have increased AI code tool adoption by approximately seven times in the past year.

The productivity gains are real, but so are the risks.

Scope

This guide addresses the security review process for AI-generated code. It covers:

  • Code review procedures specific to AI-assisted development
  • Security validation steps for LLM-generated code
  • Common vulnerability patterns in AI output
  • Integration with existing security gates (SAST, SCA, manual review)

This guide does NOT cover:

  • General code review practices (see OWASP Code Review Guide)
  • AI tool selection or procurement
  • Developer training on AI tools

Key Concepts

AI-assisted code: Code where a developer uses an AI tool for suggestions, completions, or scaffolding but retains full editorial control.

AI-generated code: Code blocks produced entirely by an AI tool with minimal human modification. May include complete functions, classes, or configuration files.

Verification depth: The level of scrutiny required based on code sensitivity. Authentication logic requires deeper verification than UI formatting.

Hallucinated dependencies: When AI tools reference libraries, APIs, or patterns that don't exist or don't work as described.

Requirements Breakdown

PCI DSS v4.0.1 Implications

Requirement 6.2.4: All system components must be protected from known vulnerabilities by installing applicable security patches/updates.

AI-generated code often references outdated library versions or deprecated APIs. Your review process must verify:

  • Dependency versions are current
  • No known CVEs in suggested libraries
  • Patch levels match your baseline

Requirement 6.4.3: All custom software is reviewed prior to release to address security vulnerabilities.

This hasn't changed, but the volume has. When your team doubles PR throughput, you need automated gates to maintain coverage without doubling headcount.

OWASP ASVS v4.0.3 Controls

V1.14.2: Verify that all code paths through an application are protected by authentication and authorization checks.

AI tools frequently generate "happy path" code. They'll implement the success case cleanly but skip edge cases, error handling, and authorization checks. Your review checklist must explicitly verify:

  • Authentication checks on every protected endpoint
  • Authorization validation for data access
  • Input validation on all user-controlled parameters

Implementation Guidance

Stage 1: Automated Scanning

Run these checks before human review:

  1. SAST scan (Semgrep, CodeQL, or your existing tool)

    • Flag any CWE in OWASP Top 10 2021
    • Block on HIGH severity findings
    • Review MEDIUM findings same-day
  2. SCA scan (Snyk, Dependabot, or equivalent)

    • Check all new dependencies
    • Verify license compatibility
    • Flag deprecated packages
  3. Secret detection (GitGuardian, TruffleHog)

    • AI tools sometimes include example API keys or credentials in generated code
    • Run on every commit, not just PR

Stage 2: Human Review Protocol

Your reviewers need a different checklist for AI-heavy PRs:

For authentication/authorization code:

  • Verify the AI didn't skip authorization checks
  • Confirm error messages don't leak information
  • Check session handling matches your framework
  • Validate token expiration is enforced

For data access code:

  • Confirm parameterized queries (no string concatenation)
  • Verify least-privilege database credentials
  • Check for proper connection pooling
  • Validate data sanitization on output

For API integrations:

  • Verify the API endpoint exists and matches documentation
  • Check TLS configuration (minimum TLS 1.2)
  • Confirm timeout and retry logic
  • Validate error handling doesn't expose stack traces

Stage 3: Verification Testing

Don't assume AI-generated tests are comprehensive:

  • Run the suggested tests, then add negative cases
  • Test with malformed input, not just valid data
  • Verify rate limiting and resource constraints
  • Check behavior under concurrent access

Common Pitfalls

The Plausible-But-Wrong Pattern

AI tools generate code that looks correct and compiles cleanly but contains subtle security flaws:

  • Authentication middleware that checks tokens but doesn't validate claims
  • Input validation that sanitizes display output but not database queries
  • Error handlers that log sensitive data
  • Race conditions in state management

Mitigation: Require security-focused unit tests for all authentication, authorization, and data access code. The developer writes these tests, not the AI.

Dependency Drift

AI training data is always outdated. Tools suggest libraries that were current 18 months ago but now have known vulnerabilities.

Mitigation: Your SCA scan catches this, but only if you run it on every PR. Don't trust the AI's version suggestions.

The Copy-Paste Security Model

AI tools learn from public repositories. If your team uses AI to generate OAuth flows, you're likely getting patterns copied from GitHub examples, which often skip production-hardening steps.

Mitigation: Maintain security templates for common patterns (authentication, API clients, data access). Require AI-generated code to match these templates.

Test Coverage Illusion

AI-generated tests focus on happy paths. They'll test that valid input succeeds but skip boundary conditions, error cases, and security scenarios.

Mitigation: Require minimum coverage thresholds AND manual security test cases for sensitive code paths.

Quick Reference Table

Code Type Required Checks Automated Tools Manual Review Focus
Authentication SAST, secret scan, unit tests Semgrep auth rules, GitGuardian Session handling, token validation, error messages
Data Access SAST, SCA, query analysis SQLMap, CodeQL Parameterization, privilege level, connection security
API Integration SCA, TLS check, endpoint validation Dependency scanner, SSL Labs Error handling, timeout logic, credential management
Business Logic Unit tests, integration tests Coverage tools Edge cases, state management, concurrency
Infrastructure Config Policy-as-code, secret scan Checkov, KICS Least privilege, network policies, logging

What Changes Tomorrow

Your review process needs to scale with AI-driven throughput without sacrificing security depth. That means:

  1. Automate the mechanical checks, dependency versions, secret detection, basic SAST
  2. Focus human review on logic, does this code do what it claims, handle errors properly, enforce authorization correctly?
  3. Require security test cases, don't accept AI-generated tests as sufficient coverage
  4. Maintain security templates, give AI tools good patterns to learn from

The productivity gains from AI tools are real. The 90th percentile of companies have increased adoption by approximately seven times in the past year. Your security review process needs to scale at the same rate, or you're just moving faster toward incidents.

CWE in OWASP Top 10 2021

Topics:General

You Might Also Like