Definition · AI security
SQL injection
SQL injection is a vulnerability where software makes an SQL command from external input and does not keep that input as data. The database then runs part of the input as SQL. An attacker can read, change or erase data. MITRE tracks it as CWE-89. It also occurs in AI tools that write SQL or send SQL to a database.
Last reviewed
Key points
- SQL injection occurs when external input becomes part of the SQL syntax of a query. The database then runs the input as a command.
- The primary defence is a parameterized query (a prepared statement). OWASP calls manual escaping "strongly discouraged".
- CWE-89 is number 2 in the 2025 CWE Top 25. MITRE made the list from 39,080 CVE records of June 2024 to June 2025.
- AI tools have the same vulnerability. OWASP lists it under LLM05:2025, and injected SQL got through the read-only checks of MCP database servers.
- A read-only check in the tool is not a security boundary. The permissions of the database account are the boundary.
How SQL injection works
Software makes a query from a fixed SQL text and an external value. If the software joins the two as one text, the database cannot find the difference between code and data. OWASP says that SQL injection occurs when untrusted data “becomes part of” the SQL syntax of the query.
Three frequent types of input, from OWASP and the advisories on this page:
| Input form | What it does |
|---|---|
x' OR 'a'='a |
Makes the WHERE clause always true, so all rows come back |
-- |
Starts a comment, so the database ignores the rest of the statement |
; DROP ... |
Adds a second statement after the first one (a chained statement) |
A parameterized query stops this. The SQL text goes to the database first, and the value goes in a different slot. The value cannot change the command.
Why SQL injection matters
SQL injection gives the attacker the rights of the database account. OWASP says an attack can read and change data and “execute administration operations on the database (such as shutdown the DBMS)”.
SQL injection continues to be frequent:
- Rank 2 of the 2025 CWE Top 25 (MITRE). The data was 39,080 CVE records published 1 June 2024 to 1 June 2025.
- MOVEit Transfer, CVE-2023-34362: CISA says the CL0P ransomware gang started to exploit this SQL injection vulnerability on 27 May 2023.
- March 2024: CISA and the FBI gave a joint Secure by Design Alert against SQL injection. The attacks on a managed file transfer product had hit “thousands of organizations”.
SQL injection in AI tools
AI tools add new paths for the same vulnerability. There are two primary paths.
- The model writes the SQL. OWASP LLM05:2025 names “LLM-generated SQL queries” that run “without proper parameterization” as one cause. Pedro and colleagues call the prompt form prompt-to-SQL (P2SQL) injection. They tested 7 LLMs in Langchain applications (arXiv, August 2023).
- The tool joins the strings. An MCP server gives a model access to a database. If its own code joins model output into SQL, any text the model passes on can become a command.
Why read-only checks in MCP servers did not stop SQL injection
Many database tools for AI have a “read-only” mode. In each server below, the read-only control did not stop the injected SQL. Two servers checked the query text, and one used a read-only transaction.
| Server | Date | How the check was bypassed |
|---|---|---|
| Anthropic Postgres MCP (reference, archived) | Aug 2025, Datadog | COMMIT; ended the read-only transaction, then DROP SCHEMA ran |
| awslabs.mysql-mcp-server 1.0.21 and earlier | Sep 2026, AWS, CVE-2026-85788 | SQL inline comments got the statement through the check. Fixed in 1.0.23 |
| StacklokLabs sqlite-mcp before 0.1.2 | Oct 2026, GHSA-q433-mp6c-8v4f, CVE-2026-130008 | The only check was “starts with SELECT”. Chained statements ran. A second flaw was in describe_table |
Read-only mode “is a best-effort safeguard and is not a substitute for correctly scoped database permissions.” AWS, bulletin 2026-103-AWS, September 2026
How to stop SQL injection
The defences in CWE-89, the OWASP cheat sheet and OWASP LLM05:2025. The OWASP cheat sheet makes items 1 and 2 the primary defences, and items 3 and 4 added layers.
- Use parameterized queries for each value, also for values that a model gives.
- Use a strict allow list for parts that cannot be a parameter, such as a table name.
- Give the database account least privilege. OWASP says: “DO NOT ASSIGN DBA OR ADMIN TYPE ACCESS TO YOUR APPLICATION ACCOUNTS.” For an AI tool that must only read, use an account with read access only.
- Validate input as an added layer, not as the primary defence.
What does not work alone:
- Manual escaping. OWASP calls it “error-prone” and “strongly discouraged”.
- Stored procedures. MITRE says they stop some types of SQL injection, “they do not protect against many others.”
- Keyword blocklists and text checks. In the AWS MySQL server, SQL inline comments went through the check. In sqlite-mcp, chained statements went through it.
- A read-only transaction in the tool. In the Postgres server, a chained
COMMIT;ended the transaction.
Questions and answers
Does SQL injection apply to AI agents and chatbots?
Yes. OWASP LLM05:2025 lists one cause: SQL from a language model that runs with no parameterization. Researchers name the prompt form prompt-to-SQL (P2SQL) injection. In 2025, the reference MCP database servers for SQLite and Postgres also had SQL injection vulnerabilities.
Is a read-only mode in a database tool enough to stop SQL injection?
No. A check on the text of a query can be tricked by chained statements or SQL comments. AWS calls the read-only mode of its MySQL MCP server "a best-effort safeguard" and "not a substitute for correctly scoped database permissions". Give the database account only read access.
Does escaping user input stop SQL injection?
Not reliably. The OWASP cheat sheet calls manual escaping "error-prone" and "strongly discouraged". Use parameterized queries. Some parts of a query cannot be a parameter, for example a table name. For these, map the input to a strict allow list of fixed values.
Sources
- CWE-89: Improper Neutralization of Special Elements used in an SQL CommandMITRE
- SQL Injection Prevention Cheat SheetOWASP
- SQL InjectionOWASP
- 2025 CWE Top 25 Most Dangerous Software WeaknessesMITRE, Dec 2025
- 2025 CWE Top 25 MethodologyMITRE, Dec 2025
- #StopRansomware: CL0P Ransomware Gang Exploits CVE-2023-34362 MOVEit Vulnerability (AA23-158A)CISA and FBI, 7 Jun 2023
- Secure by Design Alert: Eliminating SQL Injection Vulnerabilities in SoftwareCISA and FBI, 25 Mar 2024
- LLM05:2025 Improper Output HandlingOWASP GenAI Security Project, Nov 2024
- From Prompt Injections to SQL Injection Attacks: How Protected is Your LLM-Integrated Web Application?arXiv (Pedro et al.), 3 Aug 2023
- Why a Classic MCP Server Vulnerability Can Undermine Your Entire AI AgentTrend Micro, 24 Jun 2025
- MCP vulnerability case study: SQL injection in the Postgres MCP serverDatadog Security Labs, 21 Aug 2025
- CVE-2026-85788: Issue with awslabs mysql-mcp-server (2026-103-AWS)AWS, 9 Sep 2026
- sqlite-mcp: Read-only mode bypass via statement chaining and SQL injection in describe_table (GHSA-q433-mp6c-8v4f)GitHub Advisory Database, 19 Oct 2026