Skip to main content

ShipSafe

ShipSafe
ShipSafe
AI SecurityResearchVibe Coding

AI-Generated Code Security: The Risks Nobody Talks About

Stanford research shows 45% of AI-generated code ships with vulnerabilities. Here's why, what types of bugs AI creates, and what you can do about it.

9 min read

We are in the middle of the biggest shift in software development since open source. AI code assistants — Cursor, Copilot, Bolt.new, Lovable, v0 — are rewriting how software gets built. Developers are shipping full applications in hours instead of weeks. The productivity gains are real and extraordinary.

But there is a problem that the industry is not talking about loudly enough: the code these tools generate is often insecure. Not sometimes. Not in edge cases. Consistently, across tools, across languages, across frameworks.

A landmark study from Stanford University (Perry et al., 2024) found that developers using AI code assistants produced significantly less secure code than those writing manually, with approximately 45% of AI-assisted code containing vulnerabilities. Even more concerning: the developers using AI tools were more confident that their code was secure, even when it was not.

The Scale of the Problem

To understand why this matters, consider the numbers:

$4.88M

Average cost of a data breach (IBM, 2024)

45%

Of AI-generated code contains vulnerabilities (Stanford, 2024)

76%

Of developers now use AI coding tools (GitHub Survey, 2024)

258 days

Average time to identify and contain a breach (IBM, 2024)

Connect the dots: 76% of developers are now using AI tools that produce vulnerable code 45% of the time, and the average breach costs $4.88 million and takes 258 days to detect. The attack surface of the software industry is expanding faster than its defenses.

Why LLMs Generate Insecure Code

AI coding tools are not malicious. They are not trying to generate vulnerable code. The problem is structural, rooted in how large language models are trained and how they interpret prompts.

Training Data Reflects Reality

LLMs are trained on vast repositories of open-source code from GitHub, Stack Overflow, and other sources. The uncomfortable truth is that most of this code was not written with security in mind. Tutorial code skips error handling. Stack Overflow answers solve the functional problem without addressing security. Open-source projects often defer security hardening.

The model learns from what it sees, and what it sees is predominantly code that prioritizes making things work over making them secure.

Functional Optimization vs. Security

When you ask Cursor to “create a user profile API,” it generates code that creates a user profile API. It does not add rate limiting. It does not add input validation. It does not add authorization checks. Because you did not ask for those things.

LLMs optimize for the stated objective. Security is almost never the stated objective. The developer says “build a todo app with authentication,” and the LLM generates a todo app where users can log in — but it does not verify that User A cannot access User B's todos.

Context Window Limitations

Security is fundamentally a cross-cutting concern. Authentication needs to be consistent across dozens of files. Secret management has to work across environments. Input validation must happen at every boundary. LLMs generate code one file, one function, one block at a time. They cannot maintain a holistic security posture across an entire codebase the way a senior security engineer would.

The Five Categories of AI Code Vulnerabilities

Across our analysis of hundreds of AI-generated applications, vulnerabilities consistently fall into five categories. These map directly to the OWASP Top 10.

1. Injection Vulnerabilities

OWASP A03:2021 • CWE-79 (XSS), CWE-89 (SQLi)

AI tools generate code that concatenates user input directly into SQL queries, HTML output, and shell commands. This includes SQL injection via raw query builders, Cross-Site Scripting via dangerouslySetInnerHTML or unsanitized template literals, and command injection when calling system processes.

// AI-generated: SQL injection vulnerability
const results = await db.$queryRawUnsafe(
  `SELECT * FROM users WHERE email = '${req.body.email}'`
);

// AI-generated: XSS vulnerability
<div dangerouslySetInnerHTML={{ __html: userComment }} />

2. Broken Authentication and Authorization

OWASP A01:2021, A07:2021 • CWE-306, CWE-639, CWE-862

This is the most pervasive category. AI tools generate API endpoints without authentication middleware, implement role checks only in the frontend, create IDOR vulnerabilities by not scoping database queries to the authenticated user, and sometimes invert authentication conditions entirely. Our research found this in 67% of Cursor-built apps.

// AI-generated: missing auth check
export async function GET(req, { params }) {
  // No session check - anyone can access any user's data
  const data = await db.document.findUnique({
    where: { id: params.id },
  });
  return Response.json(data);
}

3. Hardcoded Secrets and Credentials

OWASP A02:2021 • CWE-798, CWE-259

AI tools frequently hardcode API keys, database credentials, JWT secrets, and encryption keys directly in source code. They also place server-side secrets in client-accessible environment variables (prefixed with VITE_ or NEXT_PUBLIC_), exposing them in the browser. See our deep dive on Lovable's secret exposure patterns.

// AI-generated: hardcoded JWT secret
const token = jwt.sign(payload, "my-super-secret-key-123");

// AI-generated: secret in client env var
const stripe = new Stripe(
  import.meta.env.VITE_STRIPE_SECRET_KEY // Exposed in browser!
);

4. Missing Input Validation

OWASP A03:2021 • CWE-20

AI-generated form handlers and API endpoints rarely validate or sanitize input. User-supplied data flows directly into database queries, file system operations, and API calls without type checking, length limits, or format validation. This enables injection attacks, denial-of-service via oversized payloads, and data corruption.

// AI-generated: no validation
router.post("/users", async (req, res) => {
  // req.body goes straight to the database
  // No type checking, no length limits, no sanitization
  const user = await db.user.create({ data: req.body });
  res.json(user);
});

5. Security Misconfiguration

OWASP A05:2021 • CWE-16

AI tools generate applications with permissive CORS policies (origin: "*"), missing security headers (no CSP, no HSTS), disabled RLS on Supabase tables, verbose error messages that leak stack traces, and debug mode enabled in production configs.

// AI-generated: overly permissive CORS
app.use(cors({ origin: "*" })); // Allows requests from ANY domain

// AI-generated: verbose error exposure
app.use((err, req, res, next) => {
  res.status(500).json({
    error: err.message,
    stack: err.stack, // Exposes internals to attackers
  });
});

The Confidence Gap: Why Developers Miss These Bugs

Perhaps the most alarming finding from the Stanford study is not that AI generates insecure code, but that developers who use AI tools are more confidenttheir code is secure — even when it is not. The researchers called this the “confidence gap.”

This makes intuitive sense. When you write code by hand, you are acutely aware of what you do not know. When an AI writes code for you, it looks professional, it compiles, it passes basic tests. There is an implicit trust: surely a tool trained on millions of repositories knows what it is doing.

But the tool is not making security decisions. It is making pattern-matching decisions. It produces the most statistically likely code given your prompt. And the most likely code is not the most secure code.

The Vibe Coding Problem

“Vibe coding” — using AI to build apps by describing what you want in natural language — has lowered the barrier to building software. This is good: more people can build more things. But it has also lowered the security bar.

Tools like Lovable, Bolt.new, and Cursor enable people without security expertise to build and deploy full-stack applications. The code works. Users can sign up, create data, and use the app. But the security holes are invisible to someone who does not know to look for them.

This is not a criticism of vibe coding. It is a recognition that the tooling needs to evolve. Just as CI/CD pipelines include automated testing, they need to include automated security scanning for AI-generated code.

What You Can Do About It

The solution is not to stop using AI coding tools. They are too valuable. The solution is to add a security verification step between generation and deployment.

1. Include Security in Your Prompts

Instead of “create a user API,” say “create a user API with authentication middleware, input validation using Zod, parameterized database queries, and rate limiting.” Explicit security requirements dramatically improve AI output quality.

2. Configure Your AI Tool's Rules

Add security directives to your .cursorrules, system prompts, or tool configuration files. Specify that all APIs must include auth checks, all secrets must use environment variables, and all inputs must be validated.

3. Review Auth Logic Manually

Authentication and authorization code is the highest-impact area for security bugs. Manually review every middleware file, every API route guard, and every role check. Verify that conditions are not inverted and that checks happen server-side.

4. Run Automated Security Scans

Use ShipSafe to scan your repository before every deployment. It identifies injection vulnerabilities, authentication gaps, exposed secrets, missing RLS policies, and configuration issues in under two minutes.

5. Follow a Security Checklist

Before every deployment, walk through a structured security checklist. We created a 20-item vibe coding security checklist covering secrets, auth, injection, XSS, and configuration. Use it as your pre-deploy gate.

The Future of AI Code Security

AI code generation is not going away. It is going to get better, faster, and more widely adopted. The question is not whether we use AI to write code. The question is whether we build the security infrastructure to match the speed of AI development.

We believe the future looks like this: AI writes the code, automated scanners verify it, and humans make the final security decisions. This is the same pattern that already exists in other domains. AI generates medical diagnoses, but doctors verify. AI drafts legal documents, but lawyers review. AI writes code, but security tools and human reviewers must validate it.

ShipSafe exists to close this gap. We built it specifically for the era of AI-generated code — fast scanning, focused on the vulnerability patterns that LLMs produce, with actionable fix recommendations that developers can implement immediately.

Conclusion

AI-generated code is transforming software development. It is also introducing security vulnerabilities at an unprecedented scale. The Stanford data, our own research, and the growing number of breach reports all point to the same conclusion: AI code needs automated security verification.

The developers and teams who recognize this early and build security checks into their AI-assisted workflow will ship faster and more securely than those who either reject AI tools or trust them blindly. The choice is not between speed and security. It is between thoughtful adoption and reckless deployment.

Want to check your own app?

Paste your GitHub URL and get a security report in under 2 minutes. Free scan, no credit card required.

Scan My App Free