Severity:

HIGH RISK

(8.8)

OS Command Injection in pgAdmin 4 Import/Export

CVE-2026-7816

Modified

Vulnerability type

Category:

OS Command Injection

CWE:

CWE-78

Impact:

Remote Code Execution and Arbitrary File Write on the pgAdmin Server

Severity:

HIGH RISK

(8.8)

CVSS version:

3.1

Catch this CVE while code is being written

Detect vulnerable code directly in pull requests, so CVEs are fixed before merge.

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 \copy metacommand 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 \copy parenthesis and append TO PROGRAM 'cmd' or TO '/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 \copy template without robust parsing or escaping, allowing an attacker to:
  • Break out of the intended \copy (SELECT ...) TO STDOUT structure.
  • Introduce TO PROGRAM or 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, and log_verbosity parameters.
  • 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 \copy or 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 \copy usage patterns, such as TO PROGRAM directives 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.

Sources

SourceLink
github.comgithub.com/pgadmin-org/pgadmin4/issues/9899

Frequently asked

questions

Yes, but only by authenticated users. An attacker who can log in to pgAdmin and access the Import/Export query export feature can exploit the issue remotely, leading to code execution on the pgAdmin server.

The attacker needs a valid pgAdmin account with permission to perform Import/Export operations. They do not need direct shell or database superuser access, but their impact will be constrained by the OS-level privileges of the pgAdmin server process.

Replace string-based command building with structured, parser-backed construction. Enforce strict validation and allow-lists on all parameters that influence commands, and prevent untrusted input from controlling sensitive features such as PROGRAM targets or file paths used by external tools.

Adopt design principles where any interaction with external tools or shells:

  • Uses parameterized, structured APIs instead of raw string concatenation.
  • Enforces strong typing, allow-lists, and parser-based validation for all user-influenced fields.
  • Is covered by security code review and automated scanning rules specifically targeting command construction paths.
Find out what's already
exploitable in your codebase.