Summary
pgjdbc can leave a PostgreSQL protocol message incomplete when an unchecked exception interrupts a request, allowing later bytes sent on the same connection to be interpreted as SQL. If an attacker can influence data sent over a connection that is subsequently reused after such an interruption, this may enable SQL injection.
Affected context
The issue affects pgjdbc versions before 42.7.15 when an unchecked exception interrupts communication and the connection remains in use. The described triggers include certain repeated executions involving a setBinaryStream() value without a length, driver retries after SQLState 0A000 or 26000, and application-provided streams that throw unchecked exceptions. The resulting risk depends on whether the connection is reused and whether an attacker can influence later data sent through it.
Exploit preconditions
- Attack surface: Application database requests sent through a pgjdbc connection
- Required access: Ability to influence data or operations sent over a connection that is reused; the exact access depends on the application
- Trigger point: An unchecked exception interrupts a request, including the described stream or driver-retry cases
- Why exploitation is possible: The connection may retain part of a Bind message in its send buffer, so subsequent bytes can be interpreted by the server as the remainder of that message and potentially as SQL
Why this happens
An unchecked exception can interrupt request processing without the connection being safely closed or otherwise prevented from reuse. This leaves the PostgreSQL protocol stream out of sync: bytes from a later value may be consumed as part of an earlier Bind message. If those bytes can be interpreted as SQL, the behavior can lead to CWE-89: SQL Injection. The failure to handle exceptional conditions safely is also associated with CWE-755: Improper Handling of Exceptional Conditions.
Potential impact
- A later value sent on the affected connection could be interpreted as SQL.
- An attacker who can influence that value may be able to alter database operations.
- Database data may be exposed, modified, or deleted, depending on the application's database privileges.
- Application operations relying on the affected connection may fail or behave unexpectedly.
General mitigation guidance
Defensive patterns focus on preserving protocol integrity and reducing the trust and authority of database requests:
- Treat a connection as unusable after an unchecked exception may have interrupted protocol communication; avoid returning potentially desynchronized connections to a pool.
- Make error handling and retry logic ensure that retries cannot reuse a connection with uncertain protocol state.
- Prefer APIs and stream-handling patterns that make input length and failure behavior explicit where practical.
- Use parameterized queries and keep SQL structure separate from data.
- Limit database account privileges to the operations the application requires.
- Validate and constrain externally influenced data before it reaches database operations.
How teams detect issues like this
- Review exception-handling and connection-pool paths for connections being reused after unchecked failures during request transmission.
- Inspect retry logic, especially around database error states, for assumptions that a failed request leaves the connection synchronized.
- Use static analysis or code review checks to identify stream-based parameter handling and SQL construction that mixes data with query structure.
- Enforce policies for parameterized queries, safe connection lifecycle handling, and least-privilege database accounts.
- Add tests that simulate unchecked stream failures and verify that affected connections are discarded rather than reused.

