Skip to main content
AI Found the Flaw, Then Created It AgainIncident
4 min readFor Security Engineers

AI Found the Flaw, Then Created It Again

The Issue at Hand

Between 2023 and 2024, organizations using AI code generation noticed a troubling trend: the same AI models that excel at finding vulnerabilities were also introducing security flaws. Veracode found that 44% of AI-generated code contains at least one known OWASP Top 10 vulnerability. No model achieved better than a 68% security pass rate when generating code.

This isn't an isolated incident. It's a widespread issue affecting thousands of codebases as teams adopt AI without adequate security measures.

Timeline of Events

Q4 2023: Organizations start using AI coding assistants widely, viewing them as productivity tools without implementing specific security review processes.

Q1 2024: Security teams begin identifying clusters of similar vulnerabilities in code reviews, SQL injection, missing input validation, hardcoded secrets, all traced to AI-generated functions.

Q2 2024: Veracode publishes findings showing that while AI models can identify zero-day vulnerabilities, they consistently fail to generate secure code when creating new functions or patches.

Present: The gap remains. AI is effective at finding flaws but fails at writing secure code.

Missing or Failed Controls

Code Review Processes

Teams often treat AI-generated code like junior developer output, trusted by default and reviewed casually. The key control that failed: mandatory security review for all AI-generated code before merging.

Your CI/CD pipeline likely includes SAST scanning, but it runs after code reaches the repository. AI-generated code needs pre-commit validation, not post-merge detection.

Training Data Quality

AI models trained on public repositories learned from decades of vulnerable code. GitHub, Stack Overflow, and open-source projects contain examples of SQL injection, XSS, and authentication bypasses. The models reproduced these patterns because there was no control to filter training data for security quality.

Contextual Prompts

AI assistants received prompts without security requirements. A developer asking for "a login function" got syntax-correct code with no password hashing, rate limiting, or session management. The missing control: security requirements in every code generation prompt.

What Standards Demand

OWASP ASVS v4.0.3

Requirement 1.14.2: "Verify that all components are up to date with proper security configuration(s) and version(s)."

AI-generated code doesn't come with a version number or security configuration. You need a process that treats each AI output as a new component requiring verification against your security baseline.

Requirement 5.1.1: "Verify that the application has defenses against HTTP parameter pollution attacks."

AI models consistently fail this. They generate code that accepts user input without validation because the training data contained thousands of examples doing exactly that.

PCI DSS v4.0.1

Requirement 6.2.4: "Software engineering techniques or other methods are defined and in use by software development personnel to prevent or mitigate common software attacks and related vulnerabilities."

If your developers use AI to generate payment processing code, you're responsible for ensuring the output meets this requirement. The AI doesn't know PCI scope. Your validation process must.

Requirement 6.5.3: "All payment page scripts that are loaded and executed in the consumer's browser are managed as follows..."

AI-generated JavaScript for payment forms needs the same integrity controls as human-written code. Most teams haven't extended their script management policies to cover AI outputs.

ISO/IEC 27001:2022

Control 8.25 (Secure development lifecycle): "Rules for the secure development of software and systems shall be established and applied."

Your SDLC documentation probably doesn't mention AI-generated code. That's the gap. You need explicit rules: which AI tools are approved, what validation steps apply, who reviews the output.

Actionable Steps for Your Team

1. Implement Pre-Commit AI Code Validation

Create a validation gate that runs before AI-generated code enters your repository:

  • Tag all AI-generated commits (GitHub Copilot adds metadata; enforce this)
  • Run focused SAST scans on AI code with zero-tolerance thresholds
  • Require human security review for any AI-generated authentication, authorization, or data handling logic

You can implement this today using branch protection rules and required status checks.

2. Build Security-First Prompts

Don't ask AI for "a login function." Ask for "a login function that implements bcrypt password hashing with work factor 12, rate limiting at 5 attempts per minute, and secure session token generation per OWASP Session Management Cheat Sheet."

The more security context you provide, the better the output. Make this a documented standard for your team.

3. Create Harnesses for Common Patterns

Chris Wysopal suggests harnesses as a solution: pre-built, security-reviewed frameworks that constrain AI code generation. Instead of letting AI write database queries from scratch, provide a harness that enforces parameterized queries and input validation.

Build harnesses for your most common patterns:

  • Database access (parameterized queries only)
  • Authentication flows (with required MFA hooks)
  • API endpoints (with input validation schemas)

4. Audit Your Training Data Strategy

If you're fine-tuning models on your internal codebase, audit that codebase first. Don't train AI on code that failed your last penetration test. Curate a "secure examples" dataset and use that for training.

This requires coordination between security and ML teams, a conversation that hasn't happened at most organizations.

5. Update Your Secure SDLC Documentation

Add explicit sections covering:

  • Approved AI coding tools
  • Required validation steps for AI-generated code
  • Prohibited use cases (AI must not generate crypto implementations, authentication logic without review)
  • Responsibility assignment (who owns security validation of AI code)

Your auditors will ask about this. Have an answer ready.

6. Measure the Gap

Track two metrics monthly:

  • Percentage of AI-generated code that passes security review on first submission
  • Types of vulnerabilities found in AI code versus human code

If you're seeing the same OWASP Top 10 categories repeatedly in AI code, that's your signal to refine prompts and harnesses.

AI will continue finding vulnerabilities faster than humans while simultaneously introducing them at scale. Your job is to build the controls that let you benefit from the first capability without suffering the second.

Topics:Incident

You Might Also Like