SQL injection should be an old problem. Modern frameworks support parameterized queries, ORMs handle escaping, and developers have been taught the risk for years.
We still find injectable queries in AI-built applications, but usually not in the obvious CRUD code.
The inconsistency shows up in the less routine paths: dynamic search, reporting, administrative filters and other queries that are assembled from several inputs. A project can be mostly correct and still contain one unsafe query that compromises the database.
The consistency problem
AI coding tools know that database queries should be parameterized. When you prompt them to "build a function that fetches a user by ID," the resulting query will almost certainly use parameters. The tool has seen thousands of examples of this exact pattern, and it follows them.
The issues appear in the less common queries, the ones that aren't direct CRUD operations. Search endpoints with multiple optional filters. Reporting queries that aggregate across tables. Admin tools that let operators query by arbitrary fields. Dynamic sorting and ordering. These are the queries where the AI is more likely to fall back on string concatenation, because the examples in its training data are more varied and the "correct" pattern is less clearly defined.
In one recent audit, a Cursor-built application had 31 database queries. Twenty-seven were properly parameterized through the ORM. Four (a search endpoint, a report generator, an admin filter, and a CSV export function) used string concatenation to build dynamic queries. Each of those four was injectable.
The developer had no idea. The main CRUD operations looked correct, and those are the ones most developers (and most automated scanners) check first.
How it happens in practice
A typical failure starts with a reasonable prompt: "Add a search feature that lets users search projects by name, status, and date range. All filters are optional."
The AI generates a function that builds a query dynamically. If a filter is provided, it appends a WHERE clause. If multiple filters are provided, it chains them with AND. The resulting query looks reasonable. It handles the optional nature of each filter correctly.
But the filter values are interpolated directly into the query string rather than passed as parameters. The code might look something like this:
let query = 'SELECT * FROM projects WHERE 1=1'; if (name) query += AND name LIKE '%${name}%'; if (status) query += AND status = '${status}';
The function works perfectly in testing. Search by name? Works. Filter by status? Works. Combine both? Works. But the name and status values are user input spliced directly into the SQL string, which means an attacker can input a value like '; DROP TABLE projects; -- and the database will execute it.
The ORM false sense of security
Many AI-built apps use ORMs (Prisma, Sequelize, TypeORM, Drizzle) which provide built-in protection against SQL injection for standard operations. Developers (and AI tools) correctly assume that using an ORM makes them safe.
The assumption breaks down in two scenarios.
Raw queries inside ORM codebases. When the ORM can't express a complex query naturally, the AI falls back to raw SQL: prisma.$queryRaw, sequelize.query, knex.raw. These functions accept raw SQL strings, and the AI frequently constructs those strings with concatenation rather than parameterized templates. The developer sees "Prisma" and assumes safety. The raw query inside is just as injectable as any other string-concatenated SQL.
Dynamic query building. ORMs handle static queries well. Dynamic queries (where the fields being queried, the sort order, or the grouping change based on user input) are harder to express idiomatically. AI tools often generate code that constructs the ORM query using user input in ways that bypass the ORM's protection. Column names passed as raw strings, sort directions interpolated without validation, table names selected based on user input.
Beyond SELECT: the destructive potential
SQL injection gets discussed primarily as a data exfiltration risk: an attacker reading data they shouldn't. But the destructive potential is broader.
Data modification. An attacker can UPDATE any record in the database. In an application without audit logging (which, as we've covered, is most AI-built apps), these modifications are undetectable.
Data deletion. DROP TABLE, DELETE FROM, TRUNCATE. If the database user has the permission (and in AI-built apps, it almost always does), the entire dataset can be destroyed.
Privilege escalation. If the application stores user roles in the database, an attacker can update their own role to admin. Combined with the authorization gaps we find in most AI-built apps, this grants complete control.
Server-side access. Depending on the database system and configuration, SQL injection can be used to read files from the server, execute operating system commands, or establish outbound network connections. The database becomes a gateway to the entire server.
Why AI tools keep getting this wrong
The root cause is that SQL injection prevention isn't a single technique. It's a discipline applied consistently across every database interaction. AI tools can learn the technique (parameterized queries) but struggle with the discipline (applying it everywhere, including in edge cases and dynamic scenarios).
Training data reinforces this. Tutorials and examples typically show parameterized queries for simple CRUD operations, the cases the AI handles correctly. Complex, dynamic queries are less standardized, and the training data contains a mix of safe and unsafe patterns. The AI generates whichever pattern it finds most applicable, and for dynamic queries, that's often string concatenation.
How to eliminate the risk
Audit every raw query. Search your codebase for .query(, .$queryRaw, .raw(, and any function that accepts raw SQL. Every instance should use parameterized syntax ($1 placeholders in PostgreSQL, ? in MySQL), not string interpolation.
Validate dynamic inputs against allowlists. If users can specify sort columns, filter fields, or table names, validate those values against a hardcoded list of allowed options. Never pass user input as a column name, table name, or SQL keyword.
Use the ORM's query builder for dynamic queries. Instead of dropping to raw SQL for complex queries, use the ORM's chainable query builder (.where(), .orderBy(), .select()). These methods handle parameterization internally.
The remediation is methodical rather than exotic: inventory every raw query, validate dynamic values against allowlists, use parameterized query builders wherever possible, and run the application with a database account that has only the permissions it needs.
Then test the unusual paths, not just the main create/read/update/delete flows. Search, sorting, reporting and administrative features deserve the same scrutiny as the code that gets exercised every day.
What makes this frustrating is that the technical solution has been known for years. AI-assisted development can reintroduce the problem when safe practices are applied inconsistently across a codebase.
The review goal is therefore consistency. One unsafe query is enough to turn an otherwise well-written data layer into a serious risk.