Summary
An OS command injection flaw in pgAdmin 4's Import/Export feature allowed user-controlled query fragments and options to be interpolated directly into a psql \copy metacommand template without proper sanitization.
An authenticated user could break out of the \copy (...) context to execute arbitrary commands on the pgAdmin server or write arbitrary files by crafting malicious query and option values.
Affected context
This vulnerability arises in pgAdmin 4 deployments where the Import/Export query export feature constructs a psql \copy command using user-supplied input.
The flaw manifests when:
- pgAdmin is configured to run server-side operations that invoke psql
- Query text and Import/Export options (such as format, on_error, log_verbosity) flow directly into a
\copymetacommand template - The server process has OS-level permissions to execute commands and access the filesystem
The issue affects pgAdmin 4 versions prior to 9.15.
Exploit preconditions
- Attack surface: pgAdmin 4 Import/Export query export functionality exposed via the web UI/API.
- Required access: Authenticated access to a pgAdmin 4 session with permission to run Import/Export operations.
- Trigger point: Submission of a crafted export configuration where:
- The SQL query is crafted to close the
\copyparenthesis and appendTO PROGRAM 'cmd'orTO '/path'. - Additional options (format, on_error, log_verbosity) may also be manipulated to inject content.
- Why exploitation is possible: User-controlled data is interpolated directly into a psql
\copytemplate without robust parsing or escaping, allowing an attacker to: - Break out of the intended
\copy (SELECT ...) TO STDOUTstructure. - Introduce
TO PROGRAMor file path targets, which psql executes or writes to on behalf of pgAdmin.
Why this happens
The core issue is the construction of shell-interpreted or metacommand-interpreted strings by concatenating unsanitized user input into a psql \copy command template.
Instead of using a structured representation of the command and strictly validating each field, the implementation treated the query and options as opaque strings, relying on naive interpolation.
This maps to CWE-78: Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection'), where attacker-controlled input alters the structure of a command executed by a subprocess.
Additional contributing factors include:
- Lack of parentheses-balancing enforcement on the
\copy ( ... )query body. - Absence of tight allow-lists and type checks on
format,on_error, andlog_verbosityparameters. - Insufficient rejection of control characters such as null bytes and other metacommand delimiters.
Potential impact
- Arbitrary command execution on the host running the pgAdmin server process.
- Arbitrary file write via
TO '/path', potentially leading to: - Overwriting configuration files.
- Dropping web shell scripts or other backdoors.
- Lateral movement within the environment, depending on the pgAdmin server's OS privileges.
- Exfiltration or destruction of data accessible from the compromised host.
- Use of the pgAdmin server as a pivot point for further attacks within the network.
General mitigation guidance
Effective mitigation focuses on preventing untrusted data from altering the structure of commands passed to psql:
- Prefer structured command construction over string concatenation; treat each logical field (query, format, options) as a typed parameter, not raw text.
- Enforce strict parsing and validation for the query portion used inside
\copy ( ... ), including: - Balanced parentheses checks.
- Rejection of metacommand escape patterns and control characters (such as null bytes) that can break context.
- Apply allow-lists for all mode-like parameters (e.g., format, on_error, log_verbosity), restricting them to known-safe literals and enumerated values.
- Avoid allowing user input to control
PROGRAM-like features; if such capabilities are required, gate them behind elevated trust or administrator-only flows. - Run the pgAdmin server and its subprocesses under least-privilege OS accounts with constrained filesystem and execution rights to limit blast radius.
- Introduce defense-in-depth by separating the component that invokes psql from the main application, minimizing what user input can reach that boundary.
How teams detect issues like this
Detection can combine code-level analysis and runtime monitoring:
- Static analysis and code review:
- Search for string templates building psql or shell commands that interpolate user-controlled fields.
- Flag any use of
\copyor similar metacommands where the query body or options are derived from HTTP parameters or UI inputs without normalization. - Review for absence of allow-lists, type checks, or parser-based validation on Import/Export-related parameters.
- Configuration and policy checks:
- Enforce internal guidelines that disallow raw string concatenation for commands executed by external tools.
- Require explicit safe-building utilities or wrappers for invoking psql from web-facing components.
- Runtime and log analysis:
- Monitor logs for unusual
\copyusage patterns, such asTO PROGRAMdirectives or unexpected filesystem paths originating from Import/Export operations. - Alert on anomalous processes spawned by the pgAdmin service user, especially shells or scripting interpreters.
- Automated SAST/IAST tools can be configured to detect OS command construction patterns and highlight injection risks early in the development process.

