10 Most Common Security Mistakes in AI-Generated Code
AI coding assistants are changing how we build software, but they often introduce subtle security flaws. Learn the top 10 vulnerabilities they create and how to fix them.
AI coding assistants have revolutionized the speed at which we can build and ship software. Tools like GitHub Copilot, Cursor, and Claude are incredible companions for "vibe coding"—allowing developers to construct complex applications primarily through natural language prompts. However, this speed comes with a significant trade-off: security blind spots.
Because LLMs are trained on vast amounts of public code (much of which is outdated or insecure), they frequently suggest implementations that look correct but contain subtle vulnerabilities. In this guide, we break down the 10 most common security mistakes in AI-generated code and show you how to identify and fix them.
1. Missing Input Validation on API Routes
One of the most frequent errors AI makes is assuming that incoming data is well-formed and safe. When asked to "create an endpoint to update user profiles," an AI will often generate the database update logic but completely skip validation.
typescript
// AI-generated (Unsafe)
app.post('/api/profile', async (req, res) => {
const user = await db.users.update({ id: req.user.id }, req.body);
res.json(user);
});
The Fix: Always enforce a strict schema using libraries like Zod or Joi. Never pass req.body directly into a database query.
2. Hardcoded Secrets and API Keys
AI models love to generate code that "just works." When prompting for an integration with a third-party service like Stripe or AWS, the AI will frequently hardcode placeholder keys directly into the source file. If you aren't careful, you might replace those placeholders with real keys and commit them to version control.
The Fix: Use environment variables (process.env) exclusively. Use tools like CodeAudit to scan for exposed secrets before they reach your repository.
3. SQL Injection via String Interpolation
While ORMs have reduced the prevalence of SQL injection, AI models will still suggest raw SQL queries using string interpolation when asked to perform complex joins or dynamic filtering.
javascript
// AI-generated (Unsafe)
const query = `SELECT * FROM users WHERE email = '${req.body.email}'`;
db.execute(query);
The Fix: Always use parameterized queries or stick to your ORM's built-in query builders.
4. Insecure Direct Object References (IDOR)
When an AI builds a "fetch user data" endpoint, it typically relies entirely on the parameters provided in the request without verifying ownership.
typescript
// AI-generated (Unsafe)
app.get('/api/documents/:id', async (req, res) => {
const doc = await db.documents.find(req.params.id);
res.json(doc);
});
The Fix: Always verify that the currently authenticated user has permission to access the requested resource. Check the document's owner_id against req.user.id.
5. Over-Permissive CORS Configurations
When an AI encounters a CORS error during development, its default solution is often the nuclear option: allowing all origins.
javascript
// AI-generated (Unsafe)
app.use(cors({ origin: '*' }));
The Fix: Explicitly define the allowed origins. Only allow your production and staging domains.
6. Exposing Sensitive Data in Responses
AI-generated endpoints often return entire database rows directly to the client, inadvertently leaking password hashes, internal IDs, or other users' private data.
The Fix: Create specific Data Transfer Objects (DTOs) or use selection parameters to ensure only necessary fields are sent to the client.
7. Improper Error Handling Leaking Internal Logic
When prompted to add error handling, AI often catches exceptions and returns the raw error message to the client. This can expose stack traces, database schema details, or underlying infrastructure paths.
The Fix: Log the raw error internally, but return a generic, user-friendly error message to the client.
8. Missing Rate Limiting on Authentication Routes
Authentication endpoints generated by AI almost never include rate limiting, leaving your application vulnerable to credential stuffing and brute-force attacks.
The Fix: Implement strict rate limiting on all login, registration, and password reset endpoints.
9. Weak Cryptography and Hashing
When asked to hash a password or generate a token, AI models sometimes reach for outdated algorithms like MD5 or SHA-1 because they are heavily represented in older training data.
The Fix: Use bcrypt or Argon2 for password hashing, and rely on secure, standard libraries for token generation.
10. Disabled Row-Level Security (RLS)
If you use tools like Supabase, AI will often provide instructions or SQL to create tables without explicitly enabling and configuring Row-Level Security, leaving your database entirely open.
The Fix: Always enable RLS on every table and define explicit policies for read and write operations.
Want CodeAudit to check your repo for these automatically? Join the waitlist and secure your AI-generated code before launch.
Frequently Asked Questions
Q: Are AI coding tools safe to use? A: Yes, but they should be treated as junior developers. Their output must be reviewed, tested, and audited before being deployed to production.
Q: How can I catch these vulnerabilities automatically? A: CodeAudit.dev specializes in scanning repositories for these exact patterns, providing automated security reviews tailored for modern development workflows.
Q: Does GitHub Copilot have built-in security features? A: Copilot includes some basic filtering, but it does not perform a comprehensive architectural or contextual security audit of your codebase.
Want CodeAudit to check your repo for this automatically? Join the waitlist.
System Check Required
RUN CODEAUDIT BEFORE DEPLOYMENT. AVOID CRITICAL FAILURES. JOIN THE QUEUE.
[ INITIALIZE AUDIT ]