/

CVE-2021-47748

Severity:

CRITICAL RISK

(9.8)

OS Command Injection in Hasura GraphQL run_sql Endpoint

CVE-2021-47748

Analyzed

Vulnerability type

Category:

OS Command Injection

CWE:

CWE-78

Impact:

Remote Code Execution

Severity:

CRITICAL RISK

(9.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

Hasura GraphQL 1.3.3 contains an OS command injection flaw where crafted GraphQL queries can abuse the run_sql endpoint to execute arbitrary shell commands on the underlying host.

By leveraging PostgreSQL's COPY FROM PROGRAM functionality through manipulated SQL, an attacker can turn database-level access into full remote code execution under the database server's privileges.

Affected context

This vulnerability arises in Hasura deployments exposing the run_sql endpoint to clients that can send arbitrary or semi-arbitrary SQL via GraphQL.

It primarily affects environments where:

  • Hasura is configured with the run_sql operation enabled and reachable over the network
  • The connected PostgreSQL instance supports COPY FROM PROGRAM (commonly when running on Unix-like systems with this capability enabled)
  • Authorization rules allow the attacker-controlled role to issue SQL statements that invoke COPY FROM PROGRAM or equivalent OS-interacting features

In these contexts, the application effectively bridges HTTP/GraphQL inputs to underlying system command execution via the database engine.

Exploit preconditions

  • Attack surface: Network-exposed Hasura GraphQL API, specifically the run_sql endpoint.
  • Required access: Ability to send authenticated (or misconfigured anonymous) GraphQL requests that can invoke run_sql with attacker-controlled SQL.
  • Trigger point: Submission of malicious SQL via GraphQL that uses PostgreSQL's COPY FROM PROGRAM feature (or similar) to execute arbitrary OS commands.
  • Why exploitation is possible: The API provides a powerful SQL execution primitive without sufficiently constraining which SQL features can be used, allowing database-level functionality that can execute system commands to be invoked through a web-accessible endpoint.

Why this happens

The core issue is an OS command injection pathway exposed indirectly through SQL.

Hasura's run_sql endpoint allows clients to submit arbitrary SQL statements, which are then executed by PostgreSQL.

PostgreSQL, in turn, offers features such as COPY FROM PROGRAM that can run shell commands on the host where the database server is running.

Because the application:

  • Treats incoming SQL as trusted logic,
  • Fails to enforce strict whitelisting or sandboxing of SQL capabilities,
  • And exposes this behavior to external callers via GraphQL,

it manifests as CWE-78: Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection'), albeit via the database engine as an intermediary.

The insecure pattern is exposing a generic "execute SQL" interface to untrusted users when the underlying SQL execution environment can invoke operating system commands.

Potential impact

  • Remote execution of arbitrary OS commands with the privileges of the PostgreSQL server process or host.
  • Exfiltration of sensitive data such as configuration files, environment variables, credentials, and secrets stored on disk.
  • Installation of backdoors or malware on the database host, enabling persistent compromise.
  • Lateral movement inside the infrastructure, including pivoting from the database host to application servers or other internal services.
  • Destruction or tampering of database contents leading to data loss, integrity violations, and potential regulatory non-compliance.

General mitigation guidance

Effective mitigation focuses on reducing both the power of exposed SQL interfaces and the ability of the database to run OS commands:

  • Restrict or disable powerful SQL endpoints: Limit or disable run_sql for untrusted roles; expose only narrowly scoped, parameterized operations instead of generic SQL execution.
  • Constrain database OS features: Disable COPY FROM PROGRAM and similar OS-level capabilities where possible, or confine them to trusted, internal-only use cases.
  • Enforce least-privilege database roles: Run the application using database roles that lack permissions to invoke OS-interacting functions and administrative commands.
  • Apply strict authorization around administrative operations: Separate operational/admin-only SQL endpoints from public or semi-public GraphQL APIs, and guard them with strong authentication and network segmentation.
  • Adopt secure-by-default query patterns: Prefer parameterized queries and stored procedures with fixed logic over free-form SQL execution surfaces exposed to clients.
  • Harden infrastructure around the database host: Use system-level sandboxing, constrained execution environments, and minimal privileges for the database service account to lower the blast radius if a command is executed.

How teams detect issues like this

Detection generally combines application-level, database-level, and code-level signals:

  • Static and configuration analysis:
  • Identify code paths or configuration flags that expose general-purpose SQL execution endpoints like run_sql.
  • Flag usages where external roles can trigger arbitrary SQL without strict whitelisting of allowed statements.
  • Code review focus areas:
  • Review API handlers and GraphQL resolvers that forward user input directly into SQL execution functions.
  • Look for endpoints that are documented as "admin" or "debug" APIs but are reachable in production environments.
  • Database monitoring and logging:
  • Enable detailed database logs and alert on usage of commands such as COPY FROM PROGRAM or other functions known to spawn OS processes.
  • Monitor for unusual SQL patterns coming from application roles, especially those that include references to file paths, shell commands, or external programs.
  • Runtime and SIEM correlation:
  • Correlate suspicious SQL statements with OS-level process creation, unexpected network connections from the database host, or filesystem changes.
  • Automated security testing:
  • Use security scanners and test harnesses to probe for command execution via SQL-capable endpoints, under controlled conditions.

Sources

SourceLink
github.comgithub.com/hasura/graphql-engine
exploit-db.comexploit-db.com/exploits/49802
vulncheck.comvulncheck.com/advisories/hasura-graphql-remote-code-execution

Frequently asked

questions

Yes, if the Hasura GraphQL endpoint (including run_sql) is exposed over the network and accessible to an attacker, they can potentially exploit it remotely without needing local access to the host running PostgreSQL.

The attacker must be able to send GraphQL requests that invoke run_sql with controlled SQL.

In some deployments this may require authentication with a role that has admin-like privileges; in misconfigured environments, weaker roles (or even anonymous access) may be sufficient if authorization is not properly enforced.

The most robust pattern is to eliminate generic SQL execution from the exposed API surface.

Replace run_sql-style endpoints for untrusted clients with narrowly scoped operations built on parameterized queries or stored procedures, combined with strict role-based access control and minimized database privileges.

Avoid exposing raw SQL or other powerful execution primitives directly to clients, especially when the underlying platform can run OS commands.

Design APIs around specific business operations rather than generic execution, apply least privilege to database roles, disable or restrict OS-level database features where possible, and regularly review admin/debug interfaces to ensure they are not reachable from untrusted networks or roles.

Find out what's already
exploitable in your codebase.