The vulnerability is a SQL injection in the MongoLite Aggregation Optimizer in Cockpit CMS, present in versions prior to 2.13.5. The root cause is the failure to sanitize user-provided field names used in aggregation queries. An attacker with a low-privileged API key can send a malicious request to the /api/content/aggregate/{model} endpoint with a crafted field name, leading to arbitrary SQL execution.
The patch, identified in commit b6a0b45c5e8fe16f3027b889583cc3a9127ab4b0, addresses this by introducing proper escaping for field names. The analysis of this patch shows that several functions within lib/MongoLite/Aggregation/Optimizer.php were constructing SQL queries using raw user input. The primary vulnerable function is toJsonExtractRaw, which directly incorporated a field name into a json_extract SQL function. Other functions like applyGroup, applySort, applyCount, toJsonExtractNumeric, and toSQL were also vulnerable as they used unescaped user input to build parts of the SQL query. During an exploit, these functions would likely be present in the runtime execution profile.