Summary
A SQL injection flaw in the pgAdmin 4 AI Assistant allows LLM-generated queries to escape a supposed read-only transaction wrapper and execute arbitrary SQL with the pgAdmin user's database privileges.
If an attacker can influence any database content that the assistant reads, they can deliver prompt-injection payloads that commit the read-only transaction and run write or privileged statements, potentially reaching remote code execution on the database server host.
Affected context
This vulnerability arises in pgAdmin 4 deployments that have the AI Assistant enabled and configured to run SQL against a PostgreSQL database using the execute_sql_query tool.
It specifically affects environments where:
- The AI Assistant is allowed to inspect database content such as rows, column values, or comments
- Users or applications with ordinary write access can store text that the assistant may later read
- The pgAdmin user's database role has write privileges, elevated privileges, or superuser-like capabilities
Under these conditions, the assumption that a BEGIN TRANSACTION READ ONLY wrapper prevents data modification is invalid, because multi-statement payloads can terminate the read-only transaction and execute follow-up statements with full privileges.
Exploit preconditions
- Attack surface: The pgAdmin 4 AI Assistant interface and its backend
execute_sql_querytool that forwards LLM-generated SQL to PostgreSQL. - Required attacker access:
- Ability to write or modify database content that the AI Assistant may read (tables, views, comments, or other objects surfaced to the assistant).
- The attacker does not need direct SQL execution through pgAdmin; they only need to influence what the assistant sees.
- Trigger point:
- The AI Assistant is prompted (directly or indirectly) in a way that causes the LLM to inspect attacker-controlled content.
- The attacker-controlled content contains instructions or patterns (prompt injection) that cause the LLM to emit a multi-statement SQL tool call starting with
COMMIT,END,ROLLBACK, orABORT, followed by arbitrary SQL. - Why exploitation is possible:
- The assistant wraps the LLM-supplied SQL in
BEGIN TRANSACTION READ ONLY, assuming PostgreSQL will prevent writes. - The backend forwards the LLM query to the database driver without enforcing a single-statement constraint or restricting statement types to read-only operations.
- A leading transaction-control statement ends the read-only transaction; subsequent statements run in autocommit mode, bypassing the read-only safeguard.
- A trailing
ROLLBACKis ineffective because there is no active transaction at that point.
Why this happens
The root cause is the unsafe execution of dynamically generated SQL without robust validation of its structure and allowed verbs, combined with an incomplete reliance on transaction-level read-only semantics.
This aligns with CWE-89: SQL Injection, where untrusted input is incorporated into SQL commands executed by the database, and CWE-77: Improper Neutralization of Special Elements, because multi-statement payloads and transaction-control keywords are not constrained or sanitized.
At an abstract level, the system trusted an LLM as if it were a safe code generator and attempted to constrain it only via a database-level wrapper (BEGIN TRANSACTION READ ONLY), instead of enforcing that:
- Only a single SQL statement is executed per tool call, and
- The statement belongs to a restricted set of read-only operations.
The missing validation allowed attackers, via prompt injection, to smuggle state-changing or privileged statements behind an initial transaction-control command, breaking the security boundary the wrapper was intended to provide.
Potential impact
- An attacker can cause arbitrary INSERT, UPDATE, DELETE, and MERGE operations using the pgAdmin user's database role.
- An attacker can perform unauthorized DDL, such as creating or dropping tables, altering schemas, or manipulating privileges.
- When the pgAdmin user's role is a PostgreSQL superuser or has
pg_execute_server_program, an attacker can potentially achieve remote code execution on the database server host (for example, viaCOPY ... TO PROGRAM). - Integrity and confidentiality of application data can be compromised by unauthorized modifications or exfiltration.
- Audit trails and security logs stored in the database can be tampered with, hampering incident response and forensics.
- In multi-tenant or shared database environments, compromise of one tenant's content viewed by the assistant could lead to broader database compromise under the pgAdmin role's privileges.
General mitigation guidance
Effective mitigation focuses on constraining what the AI Assistant can execute, reducing trust in LLM-generated SQL, and hardening privilege boundaries:
- Enforce strict query validation and whitelisting:
- Require that assistant-generated SQL parse to exactly one non-empty, non-comment statement.
- Restrict allowed leading tokens to read-only constructs (e.g.,
SELECT,WITH,EXPLAIN,SHOW,VALUES,TABLE) and reject transaction-control, DML, DDL,CALL,COPY,DO,SET/RESET, and any other modifying or side-effect-capable commands. - Rely on defense in depth:
- Continue to use database-level
READ ONLYtransactions to backstop edge cases like data-modifying CTEs orEXPLAIN ANALYZEon writes, rather than treating them as the sole protection. - Minimize assistant privileges:
- Run the AI Assistant under a dedicated database role with least-privilege, typically read-only access to the relevant schema.
- Avoid using superuser or highly privileged roles for interactive or AI-driven tooling, and avoid granting capabilities like
pg_execute_server_programwhere not strictly required. - Constrain accessible data and prompts:
- Limit what tables, comments, and metadata the assistant is allowed to read, especially where untrusted users can write data.
- Treat all content that the AI Assistant consumes as untrusted input and design prompts and tool interfaces accordingly.
- Architect for safe tool usage:
- Separate "query generation" from "query approval," optionally adding human-in-the-loop review for high-risk operations.
- Consider alternative non-SQL tools for write operations, with explicit parameterized interfaces instead of free-form SQL.
How teams detect issues like this
Detection strategies can combine code-centric and runtime techniques:
- Static analysis and code review:
- Flag any paths where LLM- or user-generated SQL is passed directly to the database driver without structural validation or verb whitelisting.
- Look for multi-statement execution settings, disabled single-statement enforcement, or APIs that permit stacked queries.
- Review AI tool integration code for assumptions that transaction-level
READ ONLYis sufficient to prevent writes. - Configuration and policy checks:
- Enforce policies requiring least-privilege database roles for assistants and tooling, and alert when highly privileged roles are used.
- Use schema reviews to identify where untrusted users can write text that is later consumed by the assistant.
- Runtime monitoring:
- Monitor database logs for suspicious patterns, such as:
- AI Assistant sessions issuing transaction-control statements followed by DML/DDL.
- Unexpected use of
COPY ... TO PROGRAMor other high-risk commands from pgAdmin-associated roles. - Use anomaly detection or query whitelisting/blacklisting to spot unexpected write or privileged operations coming from assistant sessions.
- Automated tooling:
- Integrate security scanners or custom linters into CI/CD pipelines to detect unsafe SQL execution patterns and missing validation logic in AI integration layers.

