Summary
A SQL injection vulnerability in Postgrex allows attacker-influenced values supplied through the :comment option of Postgrex.stream/4 to alter the streamed SQL statement. Because Ecto exposes this option through Ecto.Repo.stream/2, an attacker who can control the comment value may execute additional SQL clauses under the database connection's role.
Affected context
The issue occurs in applications using affected Postgrex releases when streamed queries accept attacker-influenced values for the :comment option. Postgrex appends this value directly to the statement text sent in the PostgreSQL Parse message, while the streaming execution path does not apply the validation used by other execution paths. The injection is confined to the streamed statement because PostgreSQL Parse accepts only one command.
Exploit preconditions
- Attack surface: Application code that invokes
Postgrex.stream/4orEcto.Repo.stream/2 - Required access: Ability to influence the stream comment value, directly or through application-controlled input
- Trigger point: Construction and parsing of the streamed SQL statement
- Database condition: The application must have an active PostgreSQL connection with permissions granted to its configured role
- Why exploitation is possible: The streaming path concatenates the comment into SQL text without rejecting comment terminators or applying the validation used elsewhere
Why this happens
The underlying bug is improper neutralization of special elements in an SQL command, mapping to CWE-89: SQL Injection. The streaming implementation treats a comment option as non-executable metadata but concatenates it into the statement before parsing. Because the stream path omits the existing comment validation routine, attacker-controlled text can escape the intended comment context and become part of the parsed SQL statement.
Potential impact
- An attacker may alter the streamed query's logic
- The application may return unauthorized database records
- Database data may be modified or deleted if the connection role permits it
- Sensitive information may be exposed through query results or side effects
- Database availability may be affected by resource-intensive or disruptive clauses
- The impact could extend to other systems reachable through database capabilities or trusted extensions
General mitigation guidance
Mitigation generally focuses on ensuring that metadata cannot become executable SQL and that all execution paths share the same validation guarantees.
Common defensive patterns include:
- Treating comment and annotation values as untrusted input
- Rejecting or safely encoding comment delimiters before statement construction
- Reusing centralized validation logic for streamed and non-streamed execution paths
- Prefering parameterized query values for data rather than interpolating input into SQL text
- Allowing only fixed, server-generated comments where comments are not required
- Applying least-privilege permissions to database roles used by streaming workloads
- Separating read-only streaming connections from roles that can modify sensitive data
How teams detect issues like this
Organizations can look for this class of issue through:
- Static analysis rules identifying string concatenation of user-controlled values into SQL statements
- Code review checks for comment, hint, annotation, or logging options appended to query text
- Comparisons of validation behavior across normal and streaming execution paths
- Tests that supply boundary characters and verify that metadata remains non-executable
- Database audit logging for unexpected clauses or operations issued by streaming roles
- Policy enforcement requiring parameterization or approved query-construction APIs
Automated code review and taint-analysis tools may help identify flows from request or tenant input into Postgrex and Ecto query options.

