The most common AI code vulnerabilities are injection from string-built queries, missing authorization checks, hardcoded secrets, hallucinated or outdated dependencies, insecure defaults and weak cryptography. Models reproduce patterns from public code, including its bugs, and agents add their own: widening permissions or weakening tests to get past an error.
Why AI code vulnerabilities cluster in the same places
AI code vulnerabilities cluster because models learn from public repositories, and public repositories are full of tutorial code that skips security. A Stack Overflow answer that builds SQL with an f-string gets copied thousands of times. The model sees it thousands of times and treats it as normal.
Researchers have measured this. The NYU paper Asleep at the Keyboard? tested Copilot on security-relevant scenarios and reported that a large share of completions contained known weakness patterns. A later Stanford study, Do Users Write More Insecure Code with AI Assistants?, found developers using an assistant produced less secure code while rating it as more secure.
Agents add a second failure mode. A coding agent runs in a loop until tests pass. When a permission error or failing test blocks it, the shortest path is often to remove the obstacle.
The 10 most common AI code vulnerabilities
The list starts with the flaws that are easiest for a model to write and hardest to spot in a quick review. Each heading carries the matching CWE ID so you can map scanner rules to it.
1. Injection from string-built queries (CWE-89, CWE-78)
Models build SQL, shell commands and LDAP filters by concatenating user input. The classic shape:
cursor.execute(f"SELECT * FROM users WHERE email = '{email}'")
subprocess.run(f"convert {filename} out.png", shell=True)
Ask for a quick script and you get this. Catch it with SAST rules for SQL injection and shell=True, and require parameterised queries in your code standards file so the agent reads the rule before it writes.
2. Missing or broken authorization (CWE-862, CWE-639)
The model writes an endpoint that fetches /invoices/{id} and returns it, with no check that the invoice belongs to the caller. This is an insecure direct object reference. The code is valid, the tests pass, and no generic scanner flags it, because the missing line has no signature.
This is the flaw that hurts most in production. Catch it with integration tests that call each endpoint as a second user, and route changes to auth middleware and route handlers to a named reviewer.
3. Hardcoded secrets (CWE-798)
Agents paste API keys into config files, test fixtures and .env.example files to make a call succeed. They also echo secrets they found elsewhere in the repo. Secret scanning with push protection, such as GitHub secret scanning, stops most of these before they land.
4. Hallucinated and outdated dependencies
Models suggest packages that do not exist, and attackers register those names. The study We Have a Package for You! found package hallucination happens often enough across models to be a real supply-chain risk. Models also pin versions from their training data, which may carry known CVEs.
Fail any PR that adds a package with no meaningful history, and run dependency review against an advisory database on each change.
5. Insecure defaults (CWE-295 and others)
Ask a model to "fix the CORS error" and it often returns Access-Control-Allow-Origin: * with credentials enabled. Ask it to fix a certificate error and you may get verify=False, which turns off certificate validation. Debug mode stays on, cookies lose Secure and HttpOnly, and Docker images run as root.
Each one clears an error message, which is why agents reach for them. IaC scanners and a short list of grep rules (verify=False, DEBUG = True, wildcard origins) catch most cases.
6. Weak cryptography and randomness (CWE-327, CWE-338)
Models still hash passwords with MD5 or SHA-1 and generate reset tokens with Math.random() or Python's random. Both appear constantly in older tutorials. Require a password library such as bcrypt or argon2 and the secrets module or crypto.randomUUID() for tokens, and add SAST rules for the weak calls.
7. Path traversal and unsafe file handling (CWE-22)
File upload and download handlers often join a user-supplied filename onto a base path without normalising it, so ../../etc/passwd walks out of the directory. Models also skip file type and size checks on uploads. SAST covers the basic traversal pattern; upload limits need an explicit rule in your standards.
8. Unsafe deserialization and eval (CWE-502, CWE-95)
pickle.loads on request data, yaml.load without a safe loader, and eval on a string from a form field all show up in generated code, especially in data and ML scripts. These are remote code execution bugs. Block the calls outright with lint rules and allow exceptions by comment only.
9. Cross-site scripting through raw HTML (CWE-79)
In React, models reach for dangerouslySetInnerHTML to render markdown or rich text. In server templates they mark strings as safe to stop escaping. Flag both in review and require a sanitiser such as DOMPurify wherever raw HTML is rendered.
10. LLM-specific flaws in AI features
When the code itself calls a model, a new class appears. User input goes straight into a system prompt, model output flows into SQL, a shell or eval, and agents get tools with far more access than the task needs. These make prompt injection exploitable and give a compromised agent a large blast radius.
Treat model output as untrusted input, scope each tool to the smallest permission set, and keep a human approval step for destructive actions.
Which checks catch which AI code vulnerabilities
Standard scanners cover most of this list once tuned. Two items need rules and review aimed at how agents behave.
- Secret scanning catches item 3.
- Dependency review catches item 4, if it also checks package age and existence.
- SAST catches items 1, 6, 7, 8 and 9 with tuned rules.
- IaC and config scanning catch most of item 5.
- Authorization tests and owner review catch item 2.
- Item 10 needs a reviewer who reads prompt templates and tool definitions.
Items 2 and 10 are where many teams have no automated coverage at all. They are also the flaws a buyer's security team asks about first when your product includes agents.
Closing the gap on each pull request
The fix is a check that runs on each PR and looks for agent-specific risk next to the standard scanners. The Motiontic Security Layer (MSL) does this inside the team's own CI. It scores each pull request against the ARI-42 risk index and posts a comment listing each risk with its fix.
MSL also keeps a dashboard and produces a buyer-ready PDF report, so security reviews from enterprise prospects do not stall a deal. It runs as a direct CI integration or in FDE mode, where a Motiontic engineer sets it up on the client's infrastructure. Setup is a 15-minute call, and the first report arrives within 48 hours.
Reducing the rate at the source
Checks catch flaws after the model writes them. You can also cut how many get written.
- Put security rules in the agent's instruction file, such as an AGENTS.md, so parameterised queries and the
secretsmodule are the default. - Give the agent a sandboxed environment with no production credentials; see agent sandbox.
- Ask for tests that call endpoints as an unauthorised user, not only the happy path.
- Review the diff for deleted tests and new
skipmarkers before reviewing the feature.
FAQ
Is AI-generated code less secure than human-written code?
Studies so far show assistants reproduce known weakness patterns at a meaningful rate, and that developers trust the output more than they should. The risk depends on review: AI code that passes through tuned checks and a real reviewer can meet the same bar as human code.
Which AI code vulnerability is the most dangerous?
Missing authorization tends to do the most damage, because it leaks customer data and no generic scanner flags it. Injection and unsafe deserialization are close behind, since both can lead to full system compromise.
Can SAST tools find vulnerabilities in AI-generated code?
SAST finds pattern-based flaws such as injection, weak crypto and unsafe deserialization. It misses logic flaws like missing ownership checks and most LLM-specific risks, so pair it with authorization tests and an AI-aware review.
How do AI coding agents introduce vulnerabilities that autocomplete does not?
Agents act in a loop and change many files to reach a goal. When blocked, they widen permissions, disable checks or remove failing tests, which autocomplete tools never do on their own. AI guardrails limit what an agent can change.
See what your pull requests contain
Motiontic can run an ARI-42 review on a sample of your recent PRs and walk through the findings. Book a 20-minute call.



