Summary
pgjdbc can incorrectly quote parameter values in simple query mode when the session's standard_conforming_strings setting changes or a cached parse is reused under a different setting. If an attacker can influence a parameter containing the relevant quote and backslash characters, the value may escape its intended SQL literal and be interpreted as SQL.
Affected context
The issue affects pgjdbc when preferQueryMode=simple is used and parameter values are rendered into SQL statements. Risk arises when standard_conforming_strings changes during a session or a statement cache reuses a parse made under a different setting. With allowEncodingChanges=true, the EUC_JP and SJIS client encodings can also make the yen sign act as a backslash. The description identifies versions before 42.7.15 as affected; earlier behavior also depended on the session setting and query conditions.
Exploit preconditions
- Attack surface: SQL statements issued through pgjdbc in simple query mode.
- Required access: An attacker must be able to influence a value supplied as a query parameter; the description does not establish that database access is unauthenticated.
- Trigger point: The driver quotes the parameter while the session setting or cached parse state differs from the state used for quoting.
- Why exploitation is possible: The value can end its intended SQL literal, causing subsequent content to be parsed as SQL.
- Additional condition: The encoding-related case involves
allowEncodingChanges=truewith EUC_JP or SJIS client encoding.
Why this happens
The driver's quoting behavior can rely on a stale or incorrect understanding of standard_conforming_strings. In simple query mode, parameter values are inserted into SQL text, so incorrect escaping can change the boundary between data and SQL syntax. This is a CWE-89: SQL Injection flaw: input intended as data can instead affect the structure of a database query.
Potential impact
- An attacker who can influence affected parameter values could cause unintended SQL statements to run.
- Depending on database permissions and query context, this could expose, alter, or delete data.
- Database-side effects could disrupt application workflows or affect other users.
General mitigation guidance
Defensive patterns include:
- Prefer protocol modes that transmit parameter values separately from SQL text rather than relying on client-side literal quoting.
- Keep session settings that affect parsing and quoting consistent, and avoid changing them unexpectedly during a session.
- Treat statement-cache entries as valid only for the session state under which they were created.
- Restrict encoding changes and use configurations that avoid the described encoding ambiguity.
- Apply least-privilege database roles so that query execution has only the permissions required by the application.
How teams detect issues like this
- Use static analysis and code review to identify simple query mode and code paths that interpolate or quote parameter values into SQL text.
- Review session-setting changes, statement-cache behavior, and configurations that enable encoding changes with EUC_JP or SJIS.
- Enforce application policies that prefer separately bound parameters and restrict risky driver or connection settings.
- Test query handling across relevant session-setting and encoding states; automated security review tools may help identify suspicious patterns.

