vibe-audit.lab

guides · 2026-10-11

7 Security Mistakes AI Coders Keep Making

AI coding assistants are trained on the entire public internet — including millions of lines of insecure code. So they don't just write code fast; they reproduce the internet's worst security habits at machine speed. Here are the seven vulnerability patterns that show up again and again in AI-generated code, and what to do about each one.

1. SQL injection via string concatenation

The classic never dies. Ask an AI to "write a login query" and there's a good chance you get f"SELECT * FROM users WHERE name = '{username}'" — user input stitched straight into SQL. One quote character in the input and your database is an open book. The fix is older than the AI: parameterized queries or an ORM, every time, no exceptions. Any code review checklist for AI-generated code should start here.

2. Cross-site scripting (XSS) in rendered output

AI-generated frontends love innerHTML and template literals: el.innerHTML = `<div>${userInput}</div>`. If userInput contains a <script> tag, congratulations — you've shipped stored XSS. The fix: use text-content APIs or a framework that escapes by default, and treat every innerHTML in generated code as guilty until proven innocent.

3. Hardcoded secrets and API keys

Models trained on tutorials — where keys are pasted inline "for simplicity" — happily emit const API_KEY = "sk-live-..." in client-side code. Client-side secrets aren't secrets; they're public strings. Keys belong in server-side environment variables, rotated the moment they've touched a repo or a chat log. This is the single most common finding in AI-generated codebases we see.

4. Missing authentication and authorization checks

"Build me a dashboard with an admin page" often yields routes with no auth middleware at all — or authentication without authorization, so any logged-in user can hit admin endpoints. AI models optimize for the happy path; the access-control matrix is your job. Every generated route needs an explicit answer to: who can call this, and what are they allowed to touch?

5. Insecure direct object references (IDOR)

/api/orders/{orderId} that returns any order ID you ask for, without checking it belongs to you. AI-generated APIs routinely skip the ownership check because the prompt never mentioned multi-tenancy. Test every parameterized endpoint with another user's ID — it's the cheapest security test that exists.

6. Weak cryptography and randomness

MD5 for password hashing, Math.random() for tokens, homegrown "encryption" with XOR — the training data is full of these, so the models repeat them confidently. Passwords need argon2 or bcrypt; tokens need a CSPRNG; encryption needs a vetted library, never hand-rolled. Treat any crypto in generated code as a red flag until verified.

7. Hallucinated dependencies

The newest and nastiest pattern: the model invents a package name that doesn't exist — pip install arctic-auth-utils — and attackers register that exact name with malware inside. It's called slopsquatting, and it's a supply-chain attack the AI sets up for you. Verify every dependency against the real registry before installing; if a package has zero history, treat it as hostile.

Why these patterns persist

These aren't bugs in any one model — they're statistical echoes. The models predict the most likely next token, and the most likely code on the internet is average code, written under deadline, without a security review. "Most likely" and "secure" are different distributions. Prompting helps ("use parameterized queries", "no hardcoded secrets") but doesn't eliminate the problem: studies of AI-generated code consistently find vulnerability rates that demand human review.

A review workflow that works

  1. Constrain the prompt: name your security requirements explicitly — parameterized queries, escaped output, env-based secrets, auth on every route.
  2. Scan automatically: run every AI-generated change through a security scanner before merge. Vibe-Code Audit checks for exposed secrets, injection flaws, XSS patterns and missing headers with copy-paste fixes.
  3. Verify dependencies: check every package name against npm/PyPI; flag anything newborn or hallucinated.
  4. Review the seams: humans review auth logic, crypto choices and data flow — the things scanners approximate and models fumble.

AI writes the first draft fast. Security is the editing pass you never skip.

Sources

Frequently asked questions

What are the most common security mistakes in AI-generated code?

The patterns that recur are SQL injection via string concatenation, cross-site scripting through innerHTML, hardcoded secrets and API keys, missing authentication and authorization checks, insecure direct object references (IDOR), weak cryptography and randomness, and hallucinated dependencies that attackers can register first.

Why do AI coding assistants keep making the same security mistakes?

They are statistical echoes of their training data: models predict the most likely next token, and the most likely code on the public internet includes millions of lines of insecure examples, tutorials with keys pasted inline, and happy-path code with no access-control checks.

What is the single most common finding in AI-generated codebases?

Hardcoded secrets. Models trained on tutorials happily emit keys in client-side code, where a secret is not a secret at all — it is a public string. Keys belong in server-side environment variables, rotated the moment they have touched a repo or a chat log.

How should a team review AI-generated code for security?

Constrain the prompt with explicit security requirements, scan every AI-generated change automatically before merge, verify every package name against the real registry, and have humans review the seams — auth logic, crypto choices and data flow — that scanners approximate and models fumble.

Can parameterized queries really stop SQL injection from generated code?

Yes — it is the oldest fix and still the right one. Parameterized queries or an ORM, every time, with no exceptions: user input is passed as data, never stitched into the SQL string, so a quote character in the input cannot change the query's meaning.


Informational explainer, not a security guarantee. Scan your code free before every deploy.

← all guides

← Back