Summary
pgEdge Postgres MCP Server's custom pl-do tools can be vulnerable to SQL injection when they interpolate JSON arguments into PostgreSQL DO blocks using fixed dollar-quote tags. A client able to invoke an affected tool and supply crafted arguments may inject statements that run with the connecting database role's privileges, including when read_only is enabled.
Affected context
The issue occurs when an MCP server has at least one custom pl-do tool that constructs a PostgreSQL DO block by interpolating JSON arguments into text delimited by fixed dollar-quote tags. Arguments containing those tags can break out of the intended block structure. The described injection mechanism does not affect sql or pl-func tools, which pass arguments as bind parameters.
Exploit preconditions
- Attack surface: An MCP client can invoke a custom pl-do tool.
- Required access: The caller must be able to supply arguments to that tool; resulting database actions are limited by the connecting role's privileges.
- Trigger point: The tool builds and submits a DO block containing caller-controlled arguments.
- Why exploitation is possible: Fixed dollar-quote delimiters and string interpolation allow arguments to alter the generated SQL. The simple query protocol can accept additional statements and transaction control, so read_only does not reliably prevent the injected actions.
Why this happens
The tool treats caller-controlled JSON values as SQL source by interpolating them into a DO block delimited with fixed tags. An argument can terminate the expected quoting context or alter procedural code, changing the meaning of the generated statement. This is CWE-89: SQL Injection—untrusted input is incorporated into a query without preserving the intended separation between data and SQL syntax.
Potential impact
- An attacker may read data available to the connecting database role.
- Injected statements could modify or delete accessible database resources.
- Transaction-control statements could undermine assumptions made by read_only protections.
- The database role's privileges could be used to affect other accessible resources.
General mitigation guidance
Defensive designs keep untrusted values separate from SQL syntax. Prefer parameterized interfaces and avoid constructing executable SQL by interpolating caller-controlled values into procedural blocks. Where dynamic SQL is necessary, use database-supported safe quoting and strict allowlists for any identifiers or syntax that cannot be bound as values. Treat read_only as an additional safeguard rather than a substitute for safe query construction, and grant MCP-connected database roles only the privileges their tools require.
How teams detect issues like this
- Use static analysis to identify SQL strings or DO blocks built with concatenation, formatting, or interpolation of tool arguments.
- Review custom pl-do tool implementations for fixed quoting delimiters and confirm that caller-controlled values cannot alter SQL structure.
- Enforce code-review or CI policies that require parameterized query APIs for untrusted values and least-privilege database roles.
- Audit tool definitions to verify that only intended clients can invoke database-affecting operations.

