Summary
An SQL injection vulnerability in the pgAdmin 4 Maintenance Tool allowed four user-controlled JSON fields to be concatenated directly into VACUUM, ANALYZE, and REINDEX commands passed to psql --command.
An authenticated user with the tools_maintenance permission could escape the intended option syntax, execute arbitrary SQL on the connected PostgreSQL server, and potentially reach operating-system command execution via features like COPY ... TO PROGRAM.
Affected context
This vulnerability arises in pgAdmin 4 deployments using the Maintenance Tool to run VACUUM, ANALYZE, and REINDEX operations against a PostgreSQL server.
It specifically affects code paths where:
- Maintenance operations are constructed from JSON request fields such as
buffer_usage_limit,vacuum_parallel,vacuum_index_cleanup, andreindex_tablespace - These fields are concatenated into SQL command strings or
psql --commandparameters without strict server-side validation - The caller has authenticated access and the
tools_maintenancepermission within pgAdmin
The risk materializes when these maintenance commands are generated from user-supplied data and executed with the privileges of the connected database role.
Exploit preconditions
- Attack surface: Authenticated web interface / API of pgAdmin 4 (Maintenance Tool endpoint).
- Required access: A valid pgAdmin user session with
tools_maintenancecapability for the target server or database. - Trigger point: Submission of a Maintenance Tool action (VACUUM, ANALYZE, or REINDEX) where one or more of:
buffer_usage_limitvacuum_parallelvacuum_index_cleanupreindex_tablespace
is crafted to break out of the intended option syntax.
- Why exploitation is possible: The values of these four fields were interpolated directly into the SQL or CLI command line string without strict allow-listing or robust identifier/value quoting, enabling arbitrary SQL fragments to be injected into the command executed by
psql.
Why this happens
The underlying issue is a classic SQL Injection problem (CWE-89) caused by unsafe construction of SQL statements from user-supplied input.
Instead of treating JSON fields as strictly validated, typed parameters, the application:
- Concatenated the values of
buffer_usage_limit,vacuum_parallel,vacuum_index_cleanup, andreindex_tablespaceinto maintenance commands - Relied on manual quoting for certain fields (notably
reindex_tablespace), which is fragile and error-prone - Did not enforce an explicit allow-list of permissible values or formats for these options
Because SQL strings were built through string concatenation rather than parameterization or rigorous filtering, attackers could inject additional SQL tokens and control the final command executed on the PostgreSQL server.
Potential impact
- Arbitrary SQL execution on the connected PostgreSQL server under the privileges of the configured database role
- Creation, modification, or deletion of database objects and data corruption or exfiltration
- Abuse of PostgreSQL's
COPY ... TO PROGRAMor similar capabilities to execute operating-system commands on the database host - Escalation from application-layer compromise to full database server compromise
- Lateral movement to other applications or services that share the same database or host environment
General mitigation guidance
Defensive strategies for this kind of issue center on eliminating direct string concatenation of user-controlled values into SQL and limiting what maintenance options can contain:
- Apply strict allow-list validation for all maintenance options: only accept known-safe enumerated values, numeric ranges, and boolean flags for fields like
buffer_usage_limit,vacuum_parallel, andvacuum_index_cleanup. - Treat identifiers such as tablespaces and relation names as identifiers, not arbitrary strings, and pass them through robust quoting/escaping helpers (e.g., centralized identifier-quoting filters) rather than bespoke manual quoting.
- Avoid building SQL via string concatenation; use structured query generation or parameterized APIs where possible, even for maintenance operations.
- Centralize command construction logic so that maintenance SQL is composed from trusted templates and constrained parameters, not arbitrary fragments.
- Apply least-privilege principles to the database roles used by pgAdmin: limit their ability to execute dangerous constructs like
COPY ... TO PROGRAMor superuser-only features where operationally feasible.
How teams detect issues like this
Organizations can detect and prevent similar vulnerabilities using a combination of code-level and runtime techniques:
- Static analysis: Configure rules to flag direct string concatenation when building SQL or
psqlcommand strings, especially when they incorporate web/API request parameters or JSON fields. - Code review practices: Require explicit justification when new code constructs SQL from user input, check for allow-list validation, and confirm the use of standard quoting/escaping utilities for identifiers and literals.
- Security linters and SAST tools: Integrate tools into CI/CD pipelines that recognize unsafe query-building patterns, missing parameterization, or manual quoting in database-related code.
- Runtime monitoring: Enable database logging of executed statements and review for unexpected patterns within maintenance operations (e.g., VACUUM commands followed by unrelated DDL/DML or
COPY ... TO PROGRAM). - Policy enforcement: Establish internal guidelines that prohibit manual SQL string construction for administrative tools and require the use of vetted helper functions and templates.

