Skip to main content
Menu
Advisories

pgjdbc parses quoted text as SQL in JDBC escapes, dollar quotes and E strings

GHSA-4743-5RFH-F7MPPublished Updated GitHub

Description

The pgjdbc query parser misread some quoted text. Text that the application quoted correctly could then run as SQL.

Details

A } inside a string literal, quoted identifier, dollar quote or comment ended a {d}, {t}, {ts}, {oj} or {escape} escape. The driver then translated the rest of the literal as SQL. Doubling the quotes did not help.

The parser did not see a dollar quote whose tag had a non-ASCII symbol or space, such as U+00A7. It found placeholders and statement breaks in the quoted text. Escape processing could also create a closing tag.

After a doubled quote in an E'' string, the driver read the rest as a standard string. A \' then ended the string for the driver but not for the server. In simple query mode a parameter value could then run as SQL.

{fn right(a, b)} and {fn timestampdiff(t, b, c)} wrote a minus sign before an argument. A negative argument made --, which commented out the rest of the line. Text after the next newline then ran as SQL, even inside a literal.

Proof of concept

String since = "2024-01-01 }{fn locate($$ */ $$,$$ is null)::int))) UNION SELECT token FROM api_keys --$$,{oj (/*";
stmt.executeQuery("SELECT 1 WHERE now() >= {ts '" + since.replace("'", "''") + "'}");
// The escape ends at the } inside the literal, and the UNION SELECT runs.

Impact

An attacker who controls a value that the application quotes can run SQL as the application's database role. The value must be inside a JDBC escape, a dollar quote with such a tag, or an E'' string with a doubled quote, or follow a negative right or timestampdiff argument. Escape processing is on by default. The E'' case with parameters needs preferQueryMode=simple.

Affected versions

References

GitHub · pgjdbc/pgjdbc