Summary
SQL injection in pgAdmin 4 affects multiple dialog templates that embed user-supplied description fields and identifiers directly into SQL literals without proper escaping.
An authenticated user with permission to create or alter specific PostgreSQL objects can craft descriptions or object names that break out of single-quoted literals and inject arbitrary SQL, executed under their existing database role.
Affected context
The vulnerability occurs in pgAdmin 4's server-side Jinja templates that generate SQL statements for creating or altering database objects and for querying statistics.
It specifically affects dialogs for Domains (and their constraints), Foreign Tables, Languages, Event Triggers, and a Views OID-lookup query where COMMENT ON ... IS '<description>' is rendered using unescaped data.description.
Related issues also appear in pgstattuple/pgstatindex stats templates where relation names are interpolated into SQL calls as single-quoted literals with incomplete escaping.
Risk materializes when a logged-in pgAdmin user has permission to create/alter these objects or to create objects with crafted names in schemas where they hold CREATE privileges.
Exploit preconditions
- Attack surface: pgAdmin 4 web interface and its SQL-generating dialog templates.
- Required access: Authenticated pgAdmin access bound to a PostgreSQL role with:
- CREATE/ALTER rights on affected objects (domains, foreign tables, languages, event triggers), or
- CREATE privileges on schemas used with pgstattuple/pgstatindex stats views.
- Trigger point: Submitting a description or object name containing an apostrophe or crafted payload through a dialog field, which is then rendered into SQL as a single-quoted literal without proper escaping.
- Why exploitation is possible: The templates wrap
{{ data.description }}(and certain identifiers) directly in single quotes instead of passing through a literal-escaping routine, causing user-supplied text to terminate the string literal and append arbitrary SQL that runs with the user's existing database privileges.
Why this happens
The underlying bug is improper handling of untrusted input in dynamically generated SQL, mapping to CWE-89: SQL Injection and CWE-116: Improper Encoding or Escaping of Output.
Jinja templates concatenate user-controlled fields into SQL string literals using patterns like '{{ data.description }}' without applying a correct literal-escaping filter, allowing embedded quote characters to break out of the intended literal.
Additionally, the qtLiteral helper previously failed open by returning unescaped values when its conn argument was missing or when adaptation failed, masking plumbing issues and allowing unescaped data to reach SQL generation sites.
In the stats templates, identifiers are embedded directly in function calls as single-quoted strings with only partial escaping, recreating the same injection pattern for specially named tables or indexes.
Potential impact
- Injected SQL can run with all privileges of the authenticated PostgreSQL role associated with the pgAdmin session.
- Users with powerful database roles (for example, those allowed to use
COPY ... TO/FROM PROGRAM) can potentially chain SQL injection into OS command execution on the PostgreSQL host. - Application-layer controls that restrict or gate direct use of the pgAdmin Query Tool can be bypassed, since dangerous SQL is smuggled through seemingly benign dialog operations.
- Database integrity, availability, or confidentiality can be affected to the extent permitted by the compromised database role (e.g., data modification, deletion, or extraction).
General mitigation guidance
Effective mitigation focuses on consistently treating all user-controlled fields as untrusted when constructing SQL and ensuring they are never concatenated directly into query text.
Common defensive patterns include:
- Using parameterized queries or driver-level literal adaptation for all values, letting the database driver handle quoting and escaping.
- Applying dedicated escaping utilities (such as a
qtLiteral-style helper) that both escape and wrap values, and failing fast when the helper cannot be safely invoked. - Avoiding manual single-quote wrapping of template interpolations; instead, delegate quoting to safe filters or helper routines and remove any code paths that insert raw template data into SQL.
- Refactoring templates and helper utilities to address relations and identifiers via stable handles (such as OIDs or regclass casts) instead of embedding raw names when possible.
- Designing the application so higher-privileged roles are rarely used via UI-driven features, and preferring least-privilege roles for routine operations.
How teams detect issues like this
Detection typically combines template review, automated scanning, and behavioral testing.
Helpful strategies include:
- Static analysis or custom linters that flag any SQL template fragments containing
'<template-variable>'patterns or other direct concatenation of template variables into SQL literals. - Code review checklists that require all dynamic SQL to pass through parameterization or approved escaping helpers, and that treat "escape-on-failure" behavior in helpers as a defect.
- Automated tests that render templates with payloads containing quotes or control characters and assert that the resulting SQL preserves balanced literals and driver-escaped output.
- CI policies and tooling that inspect
*.sqland Jinja templates for known-dangerous idioms, enforcing an allowlist of safe patterns and failing builds when violations are detected.

