/

CVE-2026-31858

Severity:

HIGH RISK

(8.8)

SQL Injection in Craft CMS ElementSearchController Search

CVE-2026-31858

Analyzed

Vulnerability type

Category:

SQL Injection

CWE:

CWE-89

Impact:

Database Compromise and Data Exfiltration

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

A SQL injection vulnerability in Craft CMS's ElementSearchController::actionSearch() endpoint allows authenticated control panel users to inject arbitrary SQL through search criteria parameters.

When user-controlled criteria such as criteria[where] or criteria[orderBy] are passed directly into query construction without proper neutralization, an attacker can perform boolean-based blind SQL injection to enumerate and extract database contents.

Affected context

This vulnerability occurs in Craft CMS instances where control panel access is granted to non-admin users and the ElementSearchController::actionSearch() endpoint is reachable.

It specifically affects search requests that pass complex criteria structures (e.g., criteria[where], criteria[orderBy], or similar query properties) into the element search mechanism.

The issue manifests when these parameters are not sanitized or overridden before being used to build database queries, allowing attacker-supplied fragments to influence the final SQL.

Exploit preconditions

  • Attack surface: HTTP requests to the Craft CMS control panel, targeting the ElementSearchController search endpoint.
  • Required access: Any authenticated control panel account; administrative privileges are not required.
  • Trigger point: Submission of crafted search criteria parameters (criteria[where], criteria[orderBy], or related properties) that are incorporated into SQL queries.
  • Why exploitation is possible: The controller does not apply the unset/neutralization logic that was added to a related controller, so user-controlled parameters are propagated directly into query builders, enabling attacker-controlled SQL fragments to modify query structure and logic.

Why this happens

The underlying bug is a classic SQL injection (CWE-89) arising from improper neutralization of special elements used in an SQL command.

The ElementSearchController::actionSearch() endpoint accepts nested criteria parameters intended to influence search behavior, but it fails to discard or strictly validate sensitive query-building properties such as where and orderBy.

As a result, the application effectively concatenates or maps untrusted input into SQL query components without using a safe abstraction or parameterization layer to constrain the values to data-only content.

This insecure pattern mirrors a previously fixed issue in another controller, but the defensive unset logic was not applied here, leaving a parallel injection path open.

Potential impact

  • Full read access to database tables via boolean-based blind SQL injection.
  • Extraction of sensitive user data, including credentials (hashed passwords), tokens, and personal information.
  • Discovery of application internals such as configuration values, feature flags, and schema details.
  • Potential pivoting to other weaknesses if sensitive secrets (e.g., API keys, connection strings) are stored in the database.
  • Integrity impact, such as modification or deletion of content, where the database permissions and injection vector allow it.

General mitigation guidance

Effective mitigation focuses on constraining how user-controlled criteria influence database queries and ensuring that untrusted input cannot alter SQL structure:

  • Treat all incoming criteria fields as untrusted and strictly whitelist which fields may affect query options (e.g., limit allowed sort fields and directions).
  • Remove or ignore dangerous query-building properties (where, orderBy, raw expressions) from request data before using them in query construction.
  • Use parameterized queries and ORM/query-builder APIs that separate data values from SQL syntax and avoid passing raw fragments.
  • Normalize search behavior behind higher-level APIs that accept only safe, validated options instead of arbitrary query conditions.
  • Apply the same secure-by-default patterns consistently across all controllers and endpoints that share similar query construction logic.

How teams detect issues like this

Detection efforts should concentrate on identifying untrusted parameters that flow into query construction and on monitoring for anomalous query behavior:

  • Use static application security testing (SAST) to flag direct use of request parameters in where, orderBy, or raw SQL expressions, especially in controller actions and search endpoints.
  • During code review, look for patterns where request criteria are mapped directly to query-builder methods without whitelisting or transformation.
  • Enforce development policies requiring that only predefined, enumerated values may control ordering, filtering, and other SQL clauses.
  • Monitor database logs and application logs for unusual patterns, such as highly complex predicates, excessive OR chains, or repetitive, timing-focused queries that may indicate blind injection attempts.
  • Integrate automated review tools into CI pipelines to catch reintroductions of unchecked query criteria in new or refactored controllers.

Sources

SourceLink
github.comgithub.com/craftcms/cms/commit/e1a3dd669ae31491b86ad996e88a1d30d33d9a42
github.comgithub.com/craftcms/cms/security/advisories/GHSA-g7j6-fmwx-7vp8

Frequently asked

questions

Yes, it is exploitable over HTTP, but only by users who can authenticate to the Craft CMS control panel. It is not available to completely unauthenticated visitors by default.

Any non-admin control panel account with access to the affected search functionality is sufficient. The attacker does not need administrative privileges; basic authenticated access to the search endpoint is enough.

The safest pattern is to treat all request-supplied criteria as untrusted, strip or override dangerous query properties (where, orderBy, and raw expressions), and restrict queries to use only predefined, parameterized conditions and whitelisted sort options.

Use a consistent query abstraction that never accepts raw SQL fragments from user input, enforce strict whitelisting for filter and sort parameters, and apply centralized validation/normalization of criteria across all controllers. Complement this with regular security reviews and automated checks that flag direct mapping of request parameters into query constructors.

Find out what's already
exploitable in your codebase.