Summary
A SQL injection vulnerability in the Go pgx PostgreSQL driver allows crafted input to break out of intended query boundaries when the non-default simple protocol is used together with dollar-quoted string literals.
Under specific conditions where user-controllable values are embedded in SQL containing dollar-quoted literals, an attacker can influence query structure and potentially execute arbitrary SQL commands on the database.
Affected context
This vulnerability affects Go applications that:
- Use the pgx PostgreSQL driver with the simple protocol enabled (non-default mode), and
- Construct SQL statements containing dollar-quoted string literals (e.g., $$...$$ or $tag$...$tag$), and
- Incorporate values influenced by external input into those queries in a way that pgx misinterprets parameter placeholders inside or around the dollar-quoted segments.
It typically manifests in code paths where developers expect pgx to safely handle parameters but the combination of protocol mode and query structure leads to user-controlled data being treated as part of the executable SQL.
Exploit preconditions
- Attack surface: Any application endpoint, RPC handler, or background job that:
- Accepts attacker-controlled input, and
- Uses that input in SQL executed via pgx with simple protocol and dollar-quoted literals.
- Required access: Ability to provide or manipulate the input that becomes associated with the affected SQL parameter(s) (e.g., HTTP request fields, form data, API payloads).
- Trigger point: Execution of a SQL statement where:
- The query text includes a dollar-quoted string literal, and
- The text inside that literal, combined with pgx's parsing in simple protocol mode, causes parts of the attacker-controlled value to be interpreted as a SQL placeholder or control token outside of a string literal.
- Why exploitation is possible: In the simple protocol, pgx's handling of dollar-quoted strings and placeholders allows ambiguity about where literals end and where parameters apply. This mis-parsing can let attacker-controlled data escape intended literal boundaries and alter the structure of the SQL command, aligning with CWE-89.
Why this happens
The underlying issue is a subtle query parsing and parameterization flaw in pgx's implementation of the non-default simple protocol. When SQL statements contain dollar-quoted string literals, pgx must determine where literals and parameter placeholders begin and end.
Due to incorrect or incomplete handling of this interaction, text that would normally be safely treated as data can be interpreted as if it were a placeholder outside a string literal. This breaks the core safety guarantee of parameterized queries and converts what should be data into executable SQL.
Conceptually, this is an instance of CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection'), where the driver's query processing layer fails to reliably separate user-controlled data from SQL syntax under certain protocol and query-structure combinations.
Potential impact
- Attackers could execute arbitrary SQL statements within the privileges of the application's database role.
- Attackers may read sensitive data from arbitrary tables, including personal or confidential records.
- Attackers could modify or delete data, causing integrity loss or application malfunction.
- Attackers might create or alter database objects (e.g., roles, functions) if permissions allow, potentially leading to further privilege escalation.
- In some environments, attackers could leverage database features (e.g., stored procedures, extensions) for broader system compromise.
General mitigation guidance
Effective mitigation focuses on reducing ambiguity between data and SQL syntax and avoiding fragile query construction patterns:
- Prefer parameterized queries with the default, safer protocol where feasible, and avoid relying on protocol modes that change placeholder semantics unless strictly necessary.
- Minimize or avoid embedding attacker-controlled text in contexts that interact with dollar-quoted literals or complex SQL constructs. Consider alternative patterns or explicit escaping for such cases.
- Centralize SQL construction in well-reviewed data-access layers and avoid ad-hoc concatenation or dynamic query assembly around dollar-quoted blocks.
- Apply least privilege to the database account used by the application so that, even if a query is compromised, the blast radius is reduced.
- Use defense in depth at the input boundary (validation, whitelisting for identifiers, rejecting unexpected control characters) to limit the space of harmful payloads, while not relying solely on validation as a security control.
How teams detect issues like this
Detection typically involves both code-focused and behavior-focused approaches:
- Static analysis & code scanning:
- Search for pgx usage where the simple protocol is enabled or configured explicitly.
- Flag SQL strings that combine dollar-quoted literals with dynamic or user-derived variables.
- Identify data-access layers that bypass standard parameterization or perform custom query assembly.
- Code review signals:
- Pay attention to complex SQL with $$...$$ or $tag$...$tag$ blocks where developer comments or logic suggest that attacker-influenced data is interpolated.
- Look for helper functions wrapping pgx calls that change how parameters are bound or switch protocol modes.
- Runtime and logging analysis:
- Enable verbose SQL logging in non-production environments to review queries derived from user input for unexpected extra clauses or structure.
- Monitor for anomalous queries (e.g., unexpected UNIONs, subselects, or DDL statements originating from typical read-only operations).
- Automated review tools:
- Integrate SAST/linters into CI that understand Go/pgx patterns and can alert on risky SQL construction or usage of non-default protocol options.

