Summary
In pgAdmin 4 server mode, two SQL Editor endpoints lacked authentication checks while ultimately reaching a pickle deserialization sink on session-backed data.
If an attacker can also forge a server-side session file using the Flask SECRET_KEY and write access to the sessions directory, this gap can be used to turn a partial compromise into unauthenticated remote code execution in the pgAdmin process.
Affected context
The issue occurs in pgAdmin 4 when running in server mode, specifically in the SQL Editor blueprint endpoints:
DELETE /sqleditor/close/<trans_id>POST /sqleditor/initialize/sqleditor/update_connection/<sgid>/<sid>/<did>
These routes were missing the @pga_login_required decorator, unlike other state-mutating SQL Editor routes.
The vulnerability is only meaningful in deployments where:
- pgAdmin is exposed over the network in server mode, and
- server-side session files are used and stored on disk, and
- other weaknesses (misconfiguration, prior compromise, or leakage) give an attacker knowledge of the Flask
SECRET_KEYand write access to thesessions/directory.
In DESKTOP mode, the global re-authentication hook prevents unauthenticated access, so this defect does not materialize there.
Exploit preconditions
- Attack surface: HTTP access to pgAdmin 4 in server mode, reaching the SQL Editor endpoints:
/sqleditor/close/<trans_id>/sqleditor/initialize/sqleditor/update_connection/<sgid>/<sid>/<did>- Required access:
- Network access to the pgAdmin HTTP interface, and
- Separate, pre-existing capabilities to:
- Obtain or guess pgAdmin's Flask
SECRET_KEY - Write crafted session files into the pgAdmin
sessions/directory on the host - Trigger point:
- An unauthenticated HTTP request to one of the affected endpoints that references a
trans_idfor which the attacker has already planted a malicioussession['gridData'][<trans_id>]['command_obj']pickle payload. - During request handling, the code eventually calls
pickle.loadson this untrustedcommand_obj. - Why exploitation is possible:
- The missing
@pga_login_requireddecorator means the normal authentication and MFA checks do not run before the transaction ID is dereferenced and the deserialization path is reached. - Because the code trusts server-side session files as if they were implicitly safe, a forged session file containing a hostile pickle can lead to arbitrary code execution when loaded.
- Thus, the missing authentication wrapper converts what would otherwise be a post-compromise local abuse path into an unauthenticated remote trigger for deserialization of attacker-controlled data.
Why this happens
The root cause is the intersection of two vulnerability classes:
- CWE-306: Missing Authentication for Critical Function – The two state-mutating SQL Editor endpoints were the only routes in their module not decorated with
@pga_login_required. This breaks the module's security convention and exposes functionality that should be gated by authentication and MFA checks. - CWE-502: Deserialization of Untrusted Data – Both endpoints ultimately reach a
pickle.loadscall onsession['gridData'][<trans_id>]['command_obj'], implicitly trusting that the server-side session contents are benign.
In abstract terms, the pattern is:
- A privileged control path (closing or updating an SQL editor transaction) is reachable over the network.
- The control path dereferences indirectly controlled identifiers (
trans_id) to look up serialized objects thought to be trustworthy. - Deserialization uses a powerful, code-executing format (Python pickle) without constraining the provenance or integrity of the serialized data via authentication or additional authorization checks.
The missing authentication wrapper removes a key precondition (authenticated session) that the developers assumed, allowing a separate environmental weakness (forged session files) to be chained into arbitrary code execution.
Potential impact
- Unauthenticated remote code execution in the pgAdmin 4 server process when an attacker already has
SECRET_KEYknowledge and session directory write access. - Execution of arbitrary Python code under the operating system account that runs pgAdmin, potentially leading to full host compromise.
- Manipulation or exfiltration of databases managed via pgAdmin, including issuing arbitrary SQL against configured servers.
- Lateral movement within the environment using the compromised host as a pivot.
- Persistence mechanisms planted via malicious code executed through the pickle payload.
General mitigation guidance
Effective mitigation focuses on both restoring proper authentication boundaries and hardening deserialization patterns:
- Ensure that all stateful or sensitive HTTP endpoints in the SQL Editor and similar modules are consistently wrapped in the appropriate authentication decorators (e.g.,
@pga_login_required) or equivalent access control mechanisms. - Treat server-side session contents as untrusted unless they are cryptographically protected against tampering and tightly scoped; avoid using general-purpose code-executing serialization formats (like pickle) for data that may ever be attacker-controlled.
- Prefer safer serialization formats (e.g., JSON) for session payloads and avoid storing executable objects in sessions; store references or identifiers and resolve them server-side using trusted lookups.
- Apply defense-in-depth authorization checks around operations that load or execute serialized state, ensuring that only authenticated and appropriately authorized users can reach those code paths.
- Isolate the pgAdmin runtime with least privilege at the OS and container level so that, even if deserialization is abused, the process has minimal filesystem and network capabilities.
- Manage and protect the Flask
SECRET_KEYas a sensitive credential: restrict access, avoid hardcoding, and use secret management systems so it cannot be trivially leaked or reused across environments.
How teams detect issues like this
Detection and assurance efforts can focus on both code-level patterns and deployment behavior:
- Static analysis and code review:
- Flag HTTP route handlers in Flask or similar frameworks that mutate state but lack authentication decorators or middleware (e.g., missing
@login_requiredor project-specific equivalents). - Search for uses of
pickle.loads,pickle.load, or analogous APIs in web application code and verify that inputs are tightly controlled and never derived from attacker-influenced sources, including session storage. - Enforce coding standards requiring explicit authentication and authorization annotations for all new routes, with automated linting or CI checks to prevent regressions.
- Runtime and log-based detection:
- Monitor HTTP access logs for unauthenticated requests to endpoints that normally should require authentication, especially those not covered by common authentication middleware.
- Track unusual or high-volume access patterns to SQL Editor endpoints, particularly from unexpected IPs or without preceding login events.
- Configuration and environment review:
- Periodically audit secrets management practices to ensure
SECRET_KEYand session storage paths are not exposed or writable by untrusted principals. - Use security scanning tools that understand common Python web frameworks to identify routes without authentication guards and dangerous deserialization sinks.

