Summary
Langroid's SQLChatAgent previously executed raw SQL generated by a large language model, allowing attackers to steer queries via prompt injection and indirect data poisoning.
When the backing database role had OS-level capabilities (such as COPY ... FROM PROGRAM in PostgreSQL), this could be abused to execute arbitrary commands on the database host, leading to full remote code execution.
Affected context
This vulnerability affects applications built with Langroid that:
- Use
SQLChatAgentto let an LLM construct and execute SQL queries, and - Connect to a database account with elevated privileges that enable code execution or filesystem access
(e.g., PostgreSQL pg_execute_server_program/COPY ... FROM PROGRAM, MySQL FILE, MSSQL xp_cmdshell).
The risk is most pronounced when untrusted or semi-trusted input (including natural-language prompts and data retrieved from the database) can influence the LLM's reasoning and the resulting SQL it generates, without a strict allowlist or query-shaping layer.
Exploit preconditions
- Attack surface: Any interface that feeds text into the LLM-backed
SQLChatAgent(web UI, chatbot, API endpoint, or background agent that processes external data). - Required access: Ability to supply or influence prompts or contextual data consumed by the agent; this may be direct (user prompts) or indirect (prompt injection embedded in database rows, documents, or tool outputs).
- Trigger point: The moment the SQLChatAgent submits LLM-generated SQL directly to the database connection without enforcing a safe statement allowlist or robust validation.
- Why exploitation is possible:
- The LLM is treated as a trusted query author, so its SQL output is executed as-is.
- Prompt injection can override developer instructions and cause the LLM to produce non-SELECT, privileged SQL.
- Database roles may expose dialect-specific features that bridge from SQL to operating system commands or arbitrary file system access.
Why this happens
The core issue is delegating full query construction authority to an LLM and executing its SQL output directly, treating it as trustworthy system logic.
This aligns with CWE-89: Improper Neutralization of Special Elements used in an SQL Command, but here the "untrusted input" is the model's text output, shaped by hostile prompts and injected instructions.
Because the agent did not constrain statements to a safe subset or apply dialect-aware validation, an attacker could cause the LLM to emit SQL that uses high-privilege, database-specific primitives to cross the boundary from database operations to OS-level execution.
In configurations where the database account is powerful, this design flaw effectively extended prompt injection into a remote code execution channel on the database host.
Potential impact
- Remote execution of arbitrary operating system commands on the database server host.
- Reading, writing, or exfiltrating sensitive files accessible to the database process.
- Complete compromise of the database instance, including modification or deletion of data.
- Use of the database host as a pivot point for lateral movement within the infrastructure.
- Supply-chain style data poisoning by altering records that future LLM workflows will consume.
General mitigation guidance
Effective mitigation focuses on constraining what the LLM is allowed to execute and minimizing the power of the database role:
- Constrain query capabilities:
- Restrict LLM-executable queries to a safe subset (e.g.,
SELECT-only) using a parser-backed allowlist. - Apply dialect-aware validation to block known dangerous constructs (e.g.,
COPY ... FROM PROGRAM,xp_cmdshell,LOAD DATA INFILE, or genericCALLto risky procedures). - Introduce an explicit "query planning" layer that maps high-level intents to vetted SQL templates instead of free-form SQL generation.
- Limit database privileges:
- Use dedicated, minimally privileged database roles for the agent that lack code execution and filesystem-related privileges.
- Separate read-only access for LLM use from higher-privilege accounts used for administration or migrations.
- Reduce input trust:
- Treat both user prompts and retrieved context (rows, documents, tool outputs) as attacker-controlled for purposes of prompt injection.
- Avoid enabling dangerous database features unless strictly necessary, and prefer safer alternatives when possible.
- Contain the blast radius:
- Run database services and associated processes with least-privilege OS accounts.
- Apply network and host-level controls to limit lateral movement should a database host be compromised.
How teams detect issues like this
Organizations can detect and monitor for this class of vulnerability through a combination of code-level and runtime strategies:
- Static and configuration analysis:
- Flag usage of LLM agents that are permitted to emit and execute arbitrary SQL strings.
- Use static analysis or linters to detect agent configurations that allow non-SELECT queries or disable safety checks.
- Review ORM or database access layers to ensure that an LLM cannot bypass them with raw, unvalidated SQL.
- Code review signals:
- Look for patterns where LLM outputs are passed directly into
execute()/query()APIs without parsing or allowlisting. - Scrutinize any explicit enabling of "dangerous operations" flags or settings that relax SQL safety.
- Check that database credentials wired into LLM agents correspond to least-privilege roles.
- Runtime and logging-based detection:
- Enable detailed database logging and alerts for:
- Use of OS execution primitives (
COPY ... FROM PROGRAM,xp_cmdshell,pg_execute_server_program, etc.). - Sudden appearance of DDL, DCL, or maintenance commands via LLM-driven connections.
- Monitor LLM agent behavior for unusual patterns, such as the agent attempting to run system-level operations or escalating its own capabilities.
Automated security testing tools and policy-as-code systems can help enforce these patterns in CI/CD pipelines, but should complement, not replace, human review of LLM-integrated data access paths.

