Summary
Craft Commerce's inventory levels table endpoint builds an ORDER BY clause using user-controlled sorting parameters without validation, enabling SQL injection.
An authenticated attacker with access to the Commerce Inventory section can manipulate sort[0][direction] and sort[0][sortField] to run arbitrary SQL, which may lead to full database compromise.
Affected context
The vulnerability occurs in Craft Commerce installations where the inventory levels table data endpoint accepts sorting controls from authenticated users.
Specifically, the sort[0][direction] and sort[0][sortField] query parameters are concatenated into an addOrderBy() clause without robust validation.
This risk is present when backend code directly incorporates user-supplied sorting fields or directions into SQL fragments, especially ORDER BY clauses, rather than mapping them through a fixed, trusted list.
Exploit preconditions
- Attack surface: HTTP(S) requests to the Craft Commerce backend, specifically the inventory levels table data endpoint.
- Required access: Authenticated access to the Commerce Inventory section (e.g., staff or admin-level role with inventory viewing permissions).
- Trigger point: Submission of crafted sort[0][direction] and/or sort[0][sortField] parameters that are incorporated into an addOrderBy() clause.
- Why exploitation is possible: The application concatenates unvalidated user input directly into SQL used for ordering results, allowing injection of attacker-controlled SQL fragments that the database will parse and execute.
Why this happens
This issue stems from constructing SQL queries by concatenating user-controlled input into the ORDER BY clause without using parameterization or a strict mapping.
The sort[0][direction] and sort[0][sortField] parameters are treated as trusted fragments and passed to addOrderBy(), effectively embedding raw input into the query.
This maps to CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection'), where application code fails to neutralize or constrain external input before using it in SQL statements.
Potential impact
- An attacker could execute arbitrary SQL queries within the application's database context.
- The database contents may be read, including sensitive customer and order information where present.
- Data can potentially be modified, deleted, or corrupted, impacting inventory, orders, and configuration.
- Application behavior may be altered by changing configuration or session-related data stored in the database.
- In some environments, database-level compromise could be leveraged to gain further access to underlying infrastructure.
General mitigation guidance
Effective mitigation focuses on treating all query-building inputs as untrusted and constraining them before use:
- Use parameterized queries or query builders that separate data values from SQL structure, even for ORDER BY and other dynamic clauses.
- Restrict sortable fields to a predefined, server-side whitelist and map incoming sort keys to these known column names rather than using them directly.
- Normalize and strictly validate sort direction values (e.g., only allowing 'ASC' or 'DESC' chosen from a fixed enumeration).
- Encapsulate query construction in well-reviewed data access layers, avoiding ad-hoc string concatenation in controller or view code.
- Apply the principle of least privilege to the database account so that, if injection occurs, the available damage is reduced.
How teams detect issues like this
Organizations can detect and monitor for this class of vulnerability using several approaches:
- Static analysis: Configure SAST rules to flag direct string concatenation into SQL helpers (e.g., addOrderBy, where, orderByRaw) when any operand originates from HTTP parameters or request data.
- Code review: During manual review, pay special attention to query construction around search, filtering, and sorting endpoints, verifying use of whitelists and parameterization.
- Security testing: Incorporate automated SQL injection testing in QA and CI pipelines, focusing on non-obvious vectors like ORDER BY, GROUP BY, and LIMIT/OFFSET parameters.
- Runtime monitoring: Use database query logging or application monitoring tools to detect anomalous or malformed SQL, such as unexpected UNION, subqueries, or multiple statements originating from UI-driven endpoints.

