CWE-89: SQL Injection
Untrusted input is concatenated into a database query, letting an attacker change what the query does, read, modify, or destroy data they should never reach.
Also known as: SQLi, SQL injection, Query injection, Database injection
Risk
Typically critical. A single injectable parameter can expose or destroy an entire database, bypass authentication, and in some configurations lead to remote code execution on the database host.
Definition
What it is
CWE-89 occurs when user-controlled input is placed into a SQL statement without being separated from the query's code. Because the database cannot tell the developer's intended query apart from the attacker's injected fragment, a value like an id, a search term, or a login field can alter the query's logic, returning other users' rows, skipping an authentication check, dumping whole tables, or running destructive statements. The fix is structural: the query text and the data must travel to the database separately.
Use-cases
How it shows up in real code
Login bypass
A login query builds `WHERE email = '<input>' AND password = '<input>'` by string concatenation. An attacker submits `' OR '1'='1` and the condition is always true, logging them in as the first user without a password.
Data exfiltration through a search box
A product search concatenates the search term into the query. A crafted term uses UNION to append rows from the users table, and the results page quietly returns password hashes alongside products.
A numeric id that isn't really numeric
An endpoint trusts that an `id` parameter is a number and interpolates it directly. The caller sends SQL instead, and the query runs with the application's full database privileges.
Destructive stacked query
Where the driver allows multiple statements, an injected `; DROP TABLE orders; --` is executed after the intended query, destroying data in a single request.
AI risk
Why AI-generated code has increased the exposure
- Asked to 'build a query that filters by X', assistants often produce string-concatenated or template-literal SQL because it reads naturally and demos correctly, rather than a parameterized statement.
- Models were trained on large volumes of tutorial and legacy code that concatenate SQL, so the insecure pattern is well represented and reproduced by default.
- Generated code frequently trusts that a value is 'just an id' or 'just a name' and skips the parameter binding, because the prompt described intent, not a threat model.
- When a developer asks the assistant to 'make this query dynamic' (variable columns, optional filters, sorting), the easiest generated solution is to build the string, which is exactly where injection creeps in.
- More generated endpoints means more query sites; each one is an independent chance for a concatenated query to slip through.
Vibe coding
How vibe coding amplifies the risk
- Vibe coding ships whatever query the model wrote, unread. If that query concatenates input, the injection point goes straight to production without anyone noticing the missing parameter binding.
- Prompt-and-ship skips the code review where a senior engineer would flag string-built SQL on sight.
- Iterating by prompt ('now let me filter by date too') tends to extend the existing concatenated string rather than refactor to parameters, compounding the flaw.
- Demo data makes injection invisible during vibe-coded testing, the query returns the right rows for honest input, so nothing looks wrong until a malicious input arrives in production.
- Rapid feature growth multiplies query sites faster than any manual review can keep up, so the probability that at least one is injectable rises with every prompt.
Detection
How to find it
- Look for queries assembled with string concatenation or template literals that include request data, rather than parameter placeholders.
- Trace every request parameter that reaches a data-access call and confirm it is passed as a bound parameter, not spliced into the query text.
- Watch for 'dynamic' query builders (variable column names, ORDER BY from input), these often bypass parameterization and need an allow-list instead.
- Test endpoints with inputs containing quotes and SQL metacharacters and confirm they are treated as literal data, never as query syntax.
Remediation
How to fix it
- Use parameterized queries or prepared statements everywhere; the query text is fixed and the values are bound separately so input can never change the query's structure.
- Prefer a well-maintained query builder or ORM that parameterizes by default, and avoid dropping to raw concatenated SQL.
- For parts that genuinely must be dynamic (column or table names), validate them against a fixed allow-list rather than passing input through.
- Apply least-privilege database accounts so even a successful injection cannot drop tables or read unrelated schemas.
- Add a static analysis rule and CI gate that flags concatenated SQL so new injection sites fail the build.
Frequently asked questions
- What is CWE-89 (SQL injection)?
- CWE-89 is when untrusted input is placed into a SQL query without being separated from the query's code, letting an attacker change what the query does, reading other users' data, bypassing login, or destroying tables.
- How have AI assistants and vibe coding increased SQL injection risk?
- Assistants often generate string-concatenated SQL because it reads naturally and demos correctly, and they were trained on a lot of code that does this. Vibe coding ships that query unread, skipping the review that would normally catch a missing parameter binding, and rapid prompt-driven iteration tends to extend the concatenated string rather than refactor to parameters.
- How do I fix SQL injection?
- Use parameterized queries or prepared statements so the query text and the input travel to the database separately, use an ORM or query builder that parameterizes by default, allow-list any genuinely dynamic identifiers, apply least-privilege database accounts, and add a CI rule that flags concatenated SQL.
Related weaknesses
Catch CWE-89 before you ship
Tethlyn Audit scans your codebase and its git history for CWE-89 and the rest of the injection & code vulnerabilities class, then ranks each finding by real exploitability and data sensitivity.