Summary
A SQL injection flaw in the Elixir.Postgrex.Notifications module allows attacker-controlled channel names to be interpolated directly into PostgreSQL LISTEN and UNLISTEN statements without proper escaping of quotes.
When an application lets untrusted input influence notification channel names, an attacker can craft payloads that break out of the quoted identifier and execute arbitrary SQL, including multi-statement DDL and DML commands, over the PostgreSQL simple query protocol.
Affected context
This vulnerability appears in Elixir applications that use the postgrex library's Elixir.Postgrex.Notifications module to subscribe to PostgreSQL notification channels.
It becomes relevant when:
- Notification channels are constructed from dynamic values, especially user-controlled or externally influenced data.
listen/3andunlisten/3are invoked with channel names that are not validated, escaped, or constrained to safe patterns.- The notification connection uses PostgreSQL's simple query protocol, which accepts multi-statement SQL payloads.
The same unsafe interpolation occurs when reconnect logic replays LISTEN commands in handle_connect/1, so risks persist across connection resets.
Exploit preconditions
- Attack surface: Application features that derive PostgreSQL notification channel names from request parameters, headers, message payloads, tenant identifiers, or other untrusted sources.
- Required access: Ability to influence or control the string passed as the
channelargument toPostgrex.Notifications.listen/3orunlisten/3(directly or via higher-level abstractions that wrap these APIs). - Trigger point: Execution of a
LISTENorUNLISTENoperation on the notifications connection, including automatic re-subscription duringhandle_connect/1after reconnect. - Why exploitation is possible: The
channelvalue is interpolated directly into SQLLISTEN "..."/UNLISTEN "..."without escaping the"character, allowing an attacker to close the quoted identifier and append arbitrary SQL. Because the connection uses the PostgreSQL simple query protocol, the backend accepts chained statements separated by semicolons.
Why this happens
The underlying bug is an instance of CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection').
Instead of using parameterized queries or a dedicated identifier-quoting routine, the Elixir.Postgrex.Notifications implementation constructs LISTEN and UNLISTEN statements by string interpolation, embedding the channel value directly between double quotes.
No escaping is applied to the " character, so crafted channel names can terminate the quoted identifier context and inject additional SQL tokens, operators, and statements.
Because the notifications connection relies on PostgreSQL's simple query protocol, the database executes every statement in the constructed string, turning what should be a metadata subscription operation into a general-purpose SQL execution vector.
Potential impact
- Arbitrary SQL commands may be executed on the backend database within the privileges of the notifications connection.
- Attackers could perform destructive operations, such as
DROP TABLEorTRUNCATE, leading to loss of critical data. - Data manipulation (INSERT, UPDATE, DELETE) could be used to corrupt application state, tamper with business records, or escalate logical privileges.
- Sensitive data could be exfiltrated if the attacking context can observe query results or use injected SQL to influence subsequent application flows.
- Compromised database integrity can undermine audit trails, reporting accuracy, and downstream analytics.
General mitigation guidance
Effective mitigation focuses on preventing untrusted input from being directly incorporated into SQL, especially for identifiers such as channel names.
Pattern-based hardening approaches include:
- Strict input constraints: Treat notification channel names as configuration or internal identifiers where possible. If they must be dynamic, restrict them to a conservative character set (e.g., alphanumerics, underscores) and validate against a strict pattern before use.
- Safe construction of SQL identifiers: Use library-provided mechanisms or well-reviewed helper functions for safely quoting identifiers, rather than manual string concatenation. Separate data from SQL structure whenever possible.
- Abstraction boundaries: Encapsulate
LISTEN/UNLISTENinvocations in a small, well-audited module that never accepts raw external input as a channel name. - Privilege minimization: Run the notifications connection under a database role with least privilege—able to
LISTEN/UNLISTENon needed channels but restricted from dangerous DDL/DML operations. This reduces blast radius if injection occurs. - Configuration hardening: Where feasible, keep channel names static or environment-configured rather than request-derived, so they remain outside user control.
How teams detect issues like this
Detection efforts can focus on both code patterns and runtime behavior:
- Static analysis: Configure SAST rules or custom linters for Elixir/Erlang projects to flag string interpolation in SQL statement construction, especially around
LISTEN,UNLISTEN, and other meta-commands. Look for concatenation of variables into SQL snippets rather than parameterization. - Code review signals: During manual reviews, pay special attention to:
- Direct use of untrusted input in
Postgrex.Notifications.listen/3andunlisten/3. - Helper functions that build channel names from request data or user identifiers.
- Any reconnection logic that replays
LISTENstatements by reconstructing SQL from in-memory state. - Policy enforcement: Adopt coding standards that disallow raw SQL string construction for control statements where parameters are not supported, and mandate validated, whitelisted identifiers for notification channels.
- Runtime and log analysis: Monitor database logs for unexpected
LISTEN/UNLISTENpatterns, suspicious multi-statement queries initiated from notification connections, or DDL statements executed by roles intended only for listening.

