The root cause of the SQL injection vulnerability (GHSA-j88v-2chj-qfwx) in the jackc/pgx library is the improper handling of PostgreSQL-specific dollar-quoted string literals within the library's SQL sanitizer. This sanitizer is invoked when using the non-default simple query protocol.
The vulnerability resides in the lexer used for sanitization, specifically within the rawState function of the sqlLexer in internal/sanitize/sanitize.go. This function failed to recognize dollar-quoted strings (e.g., $$...$$ or $tag$...$tag$). As a result, if a placeholder like $1 appeared inside such a string, the sanitizer would incorrectly treat it as a parameter to be substituted, rather than as part of the literal string.
An attacker could exploit this by providing a specially crafted value for a query parameter. This value would be designed to close the dollar-quoted string from within, and then inject malicious SQL commands. For example, given a query like SELECT $tag$ $1 $tag$ and an attacker-controlled value for $1 of $tag$; DROP TABLE important_data; --, the sanitizer would produce a query that executes the malicious DROP TABLE command.
The patch addresses this by enhancing the lexer. It introduces a new state (dollarQuoteState) and logic (scanDollarQuoteTag) to correctly identify and parse dollar-quoted strings, ensuring their contents are treated as a single literal and not scanned for placeholders.
Additionally, the analysis of the patch revealed a secondary hardening improvement in the placeholderState function. This change prevents an integer overflow when parsing large placeholder numbers, which could have otherwise led to unpredictable behavior by aliasing arguments.