/

CVE-2026-42031

Severity:

CRITICAL RISK

(9.8)

SQL Injection in CKAN datastore_search_sql Endpoint

CVE-2026-42031

Analyzed

Vulnerability type

Category:

SQL Injection

CWE:

CWE-89

Impact:

Data Exposure and Database Compromise

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

A SQL injection vulnerability in CKAN's datastore_search_sql functionality allows crafted queries to be executed directly against the underlying PostgreSQL database.

When exploited, this issue can expose private datasets and PostgreSQL system information, especially in deployments where untrusted users can submit or influence SQL-based search inputs.

Affected context

The vulnerability occurs in CKAN instances that expose or internally rely on the datastore_search_sql action for querying the DataStore.

It is most relevant where:

  • Users (or integrations) can provide SQL or SQL-like filters to search data
  • The application passes these inputs into PostgreSQL with insufficient sanitization or parameterization
  • Access controls and database roles permit reading private resources or system catalogs

The issue manifests in multi-tenant data portals, open data hubs, or internal CKAN deployments where untrusted or semi-trusted users interact with the DataStore search APIs.

Exploit preconditions

  • Attack surface: Web/API interface exposing datastore_search_sql or equivalent custom actions.
  • Required access: Ability to invoke the vulnerable endpoint or influence its SQL query parameters (e.g., authenticated user, API key holder, or integrated service).
  • Trigger point: Submission of specially crafted SQL fragments within request parameters that are concatenated into a query.
  • Why exploitation is possible: User-controlled input is incorporated into SQL statements without strict parameterization, validation, or proper escaping, enabling alteration of the intended query structure.

Why this happens

This issue is rooted in CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection').

The vulnerable pattern arises when the datastore_search_sql functionality accepts SQL or SQL-like input and incorporates it into database queries as raw text rather than as bound parameters.

Because the application logic does not fully constrain or validate the structure of the SQL being executed, attackers can inject additional clauses or statements that override filters, bypass permission checks, or query sensitive system tables.

At an abstract level, the bug class is: "trusting user-provided query fragments to control database commands," instead of treating them as untrusted data bound to a fixed, pre-defined query shape.

Potential impact

  • Unauthorized reading of private or restricted datasets stored in CKAN.
  • Enumeration of PostgreSQL system catalogs and metadata (e.g., table structures, user information visible to the database role).
  • Discovery of application schema details that facilitate further attacks.
  • Potential escalation to broader database compromise if combined with weak database roles or misconfigurations.
  • Bypassing logical access controls implemented in the application layer around DataStore queries.

General mitigation guidance

Mitigation centers on enforcing strict separation between data and SQL logic and reducing trust in user-controlled query structure.

Recommended defensive patterns include:

  • Avoiding direct execution of user-supplied SQL; instead, expose higher-level query APIs that map inputs onto a fixed set of parameterized queries.
  • Applying rigorous server-side validation and whitelisting for any query-like parameters, limiting them to allowed columns, operators, and clauses.
  • Using parameterized queries or query builders that treat all user input as data, never as part of the SQL syntax.
  • Implementing robust authorization checks within the query construction layer to ensure that private datasets and system catalogs cannot be accessed regardless of query content.
  • Configuring database roles with least-privilege access, so that application accounts cannot read unnecessary system tables or unrelated schemas even if a query is manipulated.

How teams detect issues like this

High-level detection approaches include:

  • Static analysis: Scan code paths related to datastore_search_sql and similar endpoints for patterns where request parameters are concatenated into SQL strings or passed to low-level execution APIs without parameterization.
  • Code review: Pay special attention to any feature that accepts "SQL" or "custom query" input; verify that only a constrained subset of query operations is allowed and that raw SQL execution is avoided.
  • Logging and monitoring: Enable detailed logging of SQL queries run by the application (with appropriate privacy safeguards) and monitor for anomalous patterns such as unexpected UNION, JOIN, or INFORMATION_SCHEMA references.
  • Policy enforcement: Use application security gateways or database firewalls that can enforce rules on SQL statements, blocking queries that target restricted tables or contain suspicious constructs.
  • Automated tooling: Incorporate SAST/DAST tools into CI/CD pipelines to flag unsanitized SQL construction and to probe exposed endpoints for injection-like behavior.

Sources

SourceLink
github.comgithub.com/ckan/ckan/security/advisories/GHSA-h7j7-3rx6-xvcg

Frequently asked

questions

Exploitability depends on who can reach the datastore_search_sql functionality. If exposed to untrusted or semi-trusted users—such as public API consumers or portal users—an attacker may craft queries that access data beyond their intended scope. In closed, tightly controlled environments, the risk is reduced but still present if internal users or integrations are compromised.

Typically, an attacker needs the ability to call the vulnerable endpoint or influence its query parameters, which may require an API key or application account. However, in some deployments, anonymous or broadly shared access to the datastore_search_sql endpoint could make exploitation possible without individual authentication.

The safest approach is to remove or drastically limit raw SQL execution based on user input. Instead, expose only high-level query operations that are implemented using parameterized queries or ORM constructs. All user-provided values should be treated purely as parameters, never as SQL syntax, and authorization checks should be enforced at the service layer before queries are constructed.

Prevention strategies include adopting a "no raw SQL from user input" policy, mandating parameterized queries throughout the codebase, and introducing secure query abstraction layers. Combine this with security-focused code reviews for any feature that manipulates queries, automated static analysis rules for SQL injection patterns, and strict least-privilege configurations for database accounts to limit the blast radius of any future issues.

Find out what's already
exploitable in your codebase.