Summary
Craft CMS contains a SQL injection vulnerability in the element-indexes/get-elements endpoint where the criteria[orderBy] parameter from the JSON body is used directly in an ORDER BY clause without proper sanitization.
An attacker with Control Panel access can inject arbitrary SQL into the query's ordering logic by manipulating this parameter, potentially leading to data exposure, logic tampering, or broader compromise of the application's database.
Affected context
The vulnerability exists in Craft CMS installations that expose the element-indexes/get-elements endpoint and accept JSON request bodies containing a criteria[orderBy] parameter.
It is relevant when:
- Users have authenticated Control Panel access and can trigger element index queries
- The backend constructs SQL queries dynamically from request parameters
- The
criteria[orderBy]field is interpolated into the SQLORDER BYclause without using safe parameterization or whitelisting
Exploit preconditions
- Attack surface: HTTP(S) API endpoint
element-indexes/get-elementsin Craft CMS. - Required access: Valid Control Panel account with sufficient permission to access element indexes.
- Input vector: JSON body parameter
criteria[orderBy], especially whenviewState[order]is omitted or set to the same payload. - Trigger point: Backend processing of the request constructs an SQL query and directly inserts the
orderByvalue into theORDER BYclause. - Why exploitation is possible: The application trusts the
orderByvalue as safe SQL syntax and concatenates it into the query string instead of enforcing strict whitelisting of sortable columns or using structured query building mechanisms.
Why this happens
This issue stems from dynamically building SQL queries using user-controlled data in the ORDER BY clause without proper validation or use of safe query construction patterns.
Instead of restricting criteria[orderBy] to a predefined set of allowed column names and directions, or mapping it through a safe abstraction, the application incorporates it directly into the SQL string.
This pattern falls under CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection'), where special characters and SQL fragments from user input are not neutralized, allowing the attacker to alter the structure and semantics of the query.
Potential impact
- Extraction of sensitive data by manipulating queries to reveal unintended records or metadata
- Tampering with application logic that depends on query ordering or conditional clauses
- Discovery of database schema details (table/column names) through crafted payloads
- Potential chaining with other weaknesses (e.g., misconfigured database permissions) to modify or delete data
- Increased risk of privilege escalation if database contents (such as credential hashes or API keys) are exposed
General mitigation guidance
Effective mitigation focuses on eliminating the ability for user input to shape raw SQL syntax:
- Restrict
orderByinput to a strict whitelist of known, safe column identifiers and directions, mapping user-friendly values to internal constants. - Use query builder abstractions or ORM mechanisms that represent ordering as structured data rather than as raw SQL fragments.
- Avoid concatenating
ORDER BYexpressions directly; instead, validate that requested fields correspond to allowed model attributes and that sort directions are limited to known values (e.g., ASC, DESC). - Apply a defense-in-depth approach by minimizing database privileges for the application account and avoiding granting unnecessary write or administrative permissions.
- Centralize query construction patterns so that parameter handling, validation, and sanitization are consistent across endpoints.
How teams detect issues like this
Detection typically involves both preventative and reactive approaches:
- Static analysis rules that flag dynamic SQL construction, especially where request parameters influence
ORDER BY,GROUP BY, or other structural clauses. - Secure code review checklists that call out any direct use of user-provided values in query fragments, with special attention to sorting or filtering criteria.
- Logging and monitoring of database queries for anomalous patterns, such as unexpected keywords or complex expressions in
ORDER BYclauses originating from application traffic. - Integration of automated security testing (e.g., SAST/DAST tools) into CI/CD pipelines to surface SQL injection patterns in routes that accept JSON bodies and construct queries dynamically.

