genai
SECURITY LAB
IncidentsImproper output handling

LangChain SQLDatabaseChain: Prompt-to-SQL Injection

High severityJuly 2023LLM10: Improper Output HandlingLLM01: Prompt Injection
Status: CVE-2023-36189 (CVSS 7.5); component later moved to langchain-experimental (fixed 0.0.247)
On this page

Ask in English, run in SQL — with the model's query executed verbatim against your database.

WhenJuly 2023
TargetLangChain SQLDatabaseChain
VendorLangChain
TechniqueUnvalidated LLM-generated SQL executed live
CVECVE-2023-36189 — CVSS 7.5 (High)
Attack flow
  1. 1SQLDatabaseChain turns a natural-language question into SQL via the LLM
  2. 2It executes that SQL directly against the database, unvalidated
  3. 3A crafted question steers the model into emitting malicious SQL

What happened

LangChain's SQLDatabaseChain turned a question into SQL via the LLM and then executed that query directly against the database with no validation or parameterisation. A crafted question could steer the model to emit SQL that reads or alters unauthorised data, which the chain ran verbatim — CVE-2023-36189. The component was later moved to langchain-experimental due to this class of risk.

How it worked

The malicious SQL originates in the model's output, so input-layer defences don't see it. The unvalidated, executed LLM-generated query is the injection.

Root cause

Executing model-generated SQL against a live database with no validation, parameterisation, or privilege limits.

What a test would have caught

Prompt-to-SQL injection testing — asking questions designed to produce destructive or out-of-scope SQL and checking what runs — surfaces this class.

How to prevent it

  • Don't execute LLM-generated SQL unvalidated; constrain and review it.
  • Use least-privilege, read-only DB accounts for query features.
  • Treat generated queries as untrusted input to the database.

Feel it yourselfThe replay lab turns a model's generated query into a database injection — the improper-output-handling class.

FAQ

How is this different from classic SQL injection?

The malicious SQL originates in the model's output, not the raw user input — so input-layer defences miss it. The unvalidated LLM-generated query is the injection.

What could it do?

Steer the model to emit SQL that reads or modifies data it shouldn't — the query was run verbatim against the live database.

What's the fix?

Never execute LLM-generated SQL unvalidated; use parameterisation, least-privilege DB accounts, and treat generated queries as untrusted (the component was moved out of core for this risk).

Replay this attack
Turn a model's generated query into a database injection — the improper-output-handling class.
Open the live lab
Runs as a live, sandboxed lab. Sign-in required — this replay is a Pro lab. Recreates the attack class, not this exact branded bot.