/

CVE-2026-31857

Severity:

HIGH RISK

(8.8)

Code Injection in Craft CMS Conditions System

CVE-2026-31857

Analyzed

Vulnerability type

Category:

Code Injection

CWE:

CWE-94

Impact:

Remote Code Execution by low-privileged authenticated users

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

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 calls renderObjectTemplate() 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, Twig render) 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.

Sources

SourceLink
github.comgithub.com/craftcms/cms/commit/8d4903647dcfd31b8d40ed027e27082013347a80
github.comgithub.com/craftcms/cms/security/advisories/GHSA-fp5j-j7j4-mcxc

Frequently asked

questions

Yes, it can be exploited remotely against exposed Craft CMS control panels, but the attacker must first obtain valid credentials for any Control Panel account (for example, an Author or Editor). Once authenticated, specially crafted condition rules can be sent through normal element listing interfaces.

The attacker only needs basic Control Panel access; administrator privileges or special permissions are not required. Any authenticated user who can interact with element listings using conditions can potentially trigger the vulnerable code path.

The safest pattern is to never execute or render user-controlled strings as code or templates. Use strict data models (enums, IDs, typed fields) for conditions, and if a template engine must be used, run it in a tightly sandboxed environment that cannot access dangerous functions or the underlying system.

Adopt a secure design policy that forbids evaluating user input as code, template, or expression, and enforce this through code review and automated checks. Treat any dynamic rendering or expression evaluation feature as high risk, require explicit threat modeling, and default to sandboxed, minimal-capability environments when templating is necessary.

Find out what's already
exploitable in your codebase.