Summary
Craft CMS versions prior to 5.9.9 and 4.17.4 contain a remote code execution flaw in the conditions system, where user-controlled strings are rendered through an unsandboxed Twig environment with escaping disabled.
Any authenticated Control Panel user, including low-privileged roles such as Authors or Editors, can send crafted condition rules through standard element listing endpoints to execute arbitrary code on the server.
Affected context
The vulnerability occurs in the Craft CMS control panel, specifically in the conditions system used for filtering and listing elements.
It affects deployments where:
- The
BaseElementSelectConditionRule::getElementIds()method is reachable via standard element listing endpoints, and - Condition rule data is accepted from authenticated users and processed server-side.
The issue is present even when hardened production settings are enabled, such as allowAdminChanges: false, devMode: false, and enableTwigSandbox: true, because the vulnerable code path uses an unsandboxed Twig rendering call.
Exploit preconditions
- Attack surface: HTTP(S) endpoints powering the Craft CMS Control Panel element listings.
- Required access: Any authenticated Control Panel account (e.g., Author, Editor), without administrator rights or special permissions.
- Trigger point: Submission or manipulation of element condition rules that flow into
BaseElementSelectConditionRule::getElementIds(), which then callsrenderObjectTemplate()with user-controlled input. - Why exploitation is possible: The application forwards attacker-controlled template strings into an unsandboxed Twig environment with escaping disabled, allowing arbitrary template expressions and server-side code constructs to be evaluated.
Why this happens
This vulnerability stems from treating user-controlled strings as trusted template code and rendering them in a powerful template engine without sandboxing.
The BaseElementSelectConditionRule::getElementIds() method passes condition rule values directly into renderObjectTemplate(), which invokes Twig with sandboxing and escaping disabled.
This matches CWE-94: Improper Control of Generation of Code, where dynamic code (or template expressions acting as code) is generated and executed based on untrusted input.
The core insecure pattern is:
- Mixing configuration/data input (condition rules) with executable template logic,
- Rendering that input in a context with access to sensitive functions, and
- Relying on general system hardening flags rather than strictly isolating or sanitizing what can be executed.
Potential impact
- Execution of arbitrary code on the web server under the Craft CMS process account
- Full compromise of the CMS instance and its data (content, configuration, secrets)
- Ability to deploy webshells or establish persistent backdoors
- Lateral movement to connected databases or internal services accessible from the CMS host
- Tampering with website content, templates, and user accounts
General mitigation guidance
Mitigation should focus on strictly separating user-controlled data from executable template code and constraining any dynamic rendering facility.
Recommended defensive patterns include:
- Avoid rendering user-supplied strings as templates; treat them as data only and use safe interpolation mechanisms rather than full template evaluation.
- Where templating is unavoidable, use a sandboxed template environment with a minimal, explicitly allowed set of functions, filters, and tags, and ensure that sandboxing cannot be bypassed on specific code paths.
- Enforce strong validation and normalization of condition rules and similar configuration inputs, limiting them to well-defined, non-executable structures (e.g., enums, whitelists, typed fields).
- Apply principle of least privilege to application-level roles: restrict access to powerful query or filtering features that could be abused as injection channels.
- Run the CMS under a least-privileged OS account with constrained filesystem and network access, so that impact of any template-based escape is reduced.
How teams detect issues like this
Detection can combine code-centric and behavior-centric approaches:
- Static analysis and code review:
- Search for calls to template rendering APIs (e.g.,
renderObjectTemplate, Twigrender) that consume user-supplied or configuration-derived strings. - Flag any rendering where sandboxing is disabled, escaping is turned off, or the environment has access to dangerous helpers or system APIs.
- Review condition-handling and query-building components, ensuring they never interpret arbitrary strings as executable templates or code.
- Policy and CI/CD enforcement:
- Add automated checks or linters that disallow unsandboxed templating for any input influenced by request parameters or user-editable fields.
- Require security-focused code review for any feature that introduces new dynamic rendering or expression evaluation capabilities.
- Runtime monitoring:
- Monitor logs for unusual or complex condition rule payloads and unexpected template errors.
- Enable application and web server logging to detect anomalous control panel activity from low-privileged roles, particularly around element listing and search endpoints.
Automated SAST/DAST or template-security-focused scanners can help identify code paths where template engines are invoked with excessive privileges or unsanitized input.

