CVEReports
CVEReports

Automated vulnerability intelligence platform. Comprehensive reports for high-severity CVEs generated by AI.

Product

  • Home
  • Sitemap
  • RSS Feed

Company

  • About
  • Contact
  • Privacy Policy
  • Terms of Service

© 2026 CVEReports. All rights reserved.

Made with love by Amit Schendel & Alon Barad



CVE-2026-31857
8.1

CVE-2026-31857: Authenticated Remote Code Execution in Craft CMS via Server-Side Template Injection

Alon Barad
Alon Barad
Software Engineer

Mar 12, 2026·4 min read·5 visits

PoC Available

Executive Summary (TL;DR)

Authenticated users can achieve Remote Code Execution in Craft CMS by injecting malicious Twig payloads into relational condition rules, bypassing production security restrictions.

Craft CMS versions 4.x and 5.x are vulnerable to a high-severity Server-Side Template Injection (SSTI) flaw. Authenticated attackers with minimal Control Panel permissions can execute arbitrary PHP code. The vulnerability exists in the processing of relational condition rules within the element index and search functionalities.

Vulnerability Overview

Craft CMS utilizes a conditions system to manage relational condition rules for element filtering and searching within the Control Panel. This system processes dynamic strings through the Twig templating engine to resolve element IDs. A Server-Side Template Injection (SSTI) vulnerability occurs when user-supplied input is processed by this system without adequate sanitization or sandboxing.

The flaw allows authenticated users, including those with restricted roles such as Authors or Editors, to supply arbitrary Twig templates. These templates are evaluated by the backend server. Because the templates are processed in an unsandboxed environment, an attacker can invoke native PHP functions.

The vulnerability is tracked as CVE-2026-31857 and carries a CVSS 4.0 score of 8.1. It represents a significant privilege escalation vector, allowing low-privileged users to achieve full Remote Code Execution (RCE) on the underlying host infrastructure.

Root Cause Analysis

The vulnerability originates in the BaseElementSelectConditionRule class. This class handles relational filters, such as filtering entries that are related to specific IDs. The methods getElementId() and getElementIds() process user-supplied element ID strings to resolve relational queries.

These methods pass the element ID strings directly to Craft::$app->getView()->renderObjectTemplate(). This utility function evaluates dynamic strings containing Twig syntax. Prior to the patch, this function operated without a Twig sandbox and with auto-escaping disabled.

The absence of a sandbox grants the template full access to the Twig environment. This includes access to the global craft object and powerful template filters. Consequently, any executable code embedded within the Twig syntax is processed and executed by the PHP interpreter.

Code Analysis

The patch remediates the vulnerability by enforcing sandboxed execution for object templates. The insecure renderObjectTemplate method call was replaced with renderSandboxedObjectTemplate.

// Vulnerable Code (src/base/conditions/BaseElementSelectConditionRule.php)
- return Craft::$app->getView()->renderObjectTemplate($elementId, $referenceElement);
 
// Patched Code
+ return Craft::$app->getView()->renderSandboxedObjectTemplate($elementId, $referenceElement);

The vendor also implemented supplementary defense-in-depth measures to prevent secondary injection vectors. A new helper method, ElementHelper::cleanseQueryCriteria(), was introduced to sanitize user-submitted criteria arrays.

// src/helpers/ElementHelper.php
public static function cleanseQueryCriteria(array $criteria): array {
    unset(
        $criteria['where'], $criteria['orderBy'], $criteria['indexBy'],
        $criteria['select'], $criteria['selectOption'], $criteria['from'],
        $criteria['groupBy'], $criteria['join'], $criteria['having'],
        $criteria['union'], $criteria['withQueries'], $criteria['params']
    );
    return $criteria;
}

This helper strips dangerous SQL-related keys before the criteria array is passed to the ElementQuery builder. This mitigates related SQL injection vulnerabilities tracked under GHSA-g7j6-fmwx-7vp8.

Exploitation Methodology

Exploitation requires the attacker to hold an authenticated session with access to the Control Panel. The attacker targets endpoints responsible for element indexes or saved searches, such as /index.php?p=admin/actions/element-indexes/save-index.

The attacker submits an HTTP POST request containing a maliciously crafted $criteria array. This array defines a relational condition rule where the "Element ID" template contains the Twig payload. When the backend processes this rule, the payload is executed.

Valid payloads leverage Twig filters or global objects to invoke PHP functions. For example, the payload {{ ["id"]|map("phpinfo") }} utilizes the map filter to execute the phpinfo function. Alternatively, {{craft.app.view.evaluateDynamicContent('system("whoami")')}} accesses the App instance directly to execute system commands.

Impact Assessment

Successful exploitation results in arbitrary PHP code execution within the context of the web server process. The attacker gains the ability to read sensitive files, modify the database, or establish persistent access to the host operating system.

The vulnerability circumvents standard Craft CMS production hardening configurations. Specifically, the exploit functions even when allowAdminChanges and devMode are disabled. The global enableTwigSandbox configuration does not prevent this attack because the vulnerable rendering function explicitly bypassed the sandbox.

This significantly elevates the risk profile for environments that grant Author or Editor permissions to untrusted or external users. A compromised low-privileged account can be immediately leveraged to completely compromise the application and the underlying infrastructure.

Remediation and Mitigation

Administrators must upgrade Craft CMS deployments to version 5.9.9 or 4.17.4 to address this vulnerability. These versions enforce sandboxing within the relational condition rule processing and introduce necessary query cleansing.

If immediate patching is not feasible, organizations should restrict Control Panel access to highly trusted administrators. Auditing existing user permissions is recommended to minimize the attack surface.

Security teams can deploy Web Application Firewall (WAF) rules to detect and block common Twig SSTI patterns. Rules should inspect POST requests directed at /index.php?p=admin/actions/element-indexes/* for strings such as {{, |map, |filter, and evaluateDynamicContent.

Fix Analysis (2)

Technical Appendix

CVSS Score
8.1/ 10
CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:H/VI:H/VA:H/SC:N/SI:N/SA:N/E:U

Affected Systems

Craft CMS 4Craft CMS 5

Affected Versions Detail

Product
Affected Versions
Fixed Version
Craft CMS 5
craftcms
>= 5.0.0-RC1, < 5.9.95.9.9
Craft CMS 4
craftcms
>= 4.0.0-beta.1, < 4.17.44.17.4
AttributeDetail
CWE IDCWE-94
CVSS 4.0 Score8.1
Attack VectorNetwork
Authentication RequiredYes
Exploit StatusProof of Concept
KEV ListedNo

MITRE ATT&CK Mapping

T1059Command and Scripting Interpreter
Execution
T1203Exploitation for Client Execution
Execution
CWE-94
Improper Control of Generation of Code ('Code Injection')

The product constructs all or part of a code segment using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the intended syntax or behavior of the generated code.

Vulnerability Timeline

Initial commits addressing create() Twig function restrictions
2026-02-05
Release of Craft CMS 5.9.9 and 4.17.4 with the vulnerability fix
2026-02-11
Official publication of CVE-2026-31857
2026-03-11

References & Sources

  • [1]NVD Record: CVE-2026-31857
  • [2]GitHub Advisory: GHSA-fp5j-j7j4-mcxc
  • [3]Fix Commit (5.x)
  • [4]Fix Commit (4.x)
  • [5]Craft CMS Changelog

Attack Flow Diagram

Press enter or space to select a node. You can then use the arrow keys to move the node around. Press delete to remove it and escape to cancel.
Press enter or space to select an edge. You can then press delete to remove it or escape to cancel.