Summary
Craft CMS uses a blocklist-based filter to prevent dangerous PHP functions from being invoked through Twig non-Closure arrow functions, but several risky functions are not covered.
With sufficient backend permissions or misconfiguration, an attacker can leverage these unblocked functions to execute arbitrary code or access sensitive resources via template execution.
Affected context
This vulnerability affects Craft CMS installations where Twig templates can be influenced by users who:
- Have administrative or near-administrative capabilities, or
- Have access to the System Messages utility, or
- Operate in environments where
allowAdminChangesis enabled in production.
It manifests specifically in contexts where Twig non-Closure arrow functions can end up invoking PHP functions, and where the blocklist is relied upon as the primary safeguard against dangerous function calls. The issue is more impactful in production environments where templates are processed with high-privilege application context.
Exploit preconditions
- Attack surface: Server-side template rendering within Craft CMS (Twig engine).
- Required access:
- A compromised or malicious admin account, or
- An account with access to the System Messages utility, or
- A production environment with
allowAdminChangesenabled and reachable by the attacker. - Trigger point: Rendering of Twig templates that can execute non-Closure arrow functions which internally call PHP functions.
- Why exploitation is possible: The protection mechanism relies on a PHP function blocklist that is incomplete. Several dangerous functions remain callable, enabling attackers with sufficient template influence to chain them into payloads that can perform code execution, file operations, outbound network calls, or further template-based injection.
Why this happens
The core issue is a reliance on an incomplete blocklist to constrain what PHP functions can be invoked from Twig non-Closure arrow functions.
This design aligns with CWE-94: Improper Control of Generation of Code, because user-influenced template content can effectively trigger execution of dynamically constructed code paths via PHP functions that should have been considered unsafe.
Supporting issues include:
- CWE-184: Incomplete Blacklist, where not all high-risk functions are enumerated or consistently blocked.
- CWE-1336: Improper Neutralization of Special Elements Used in a Template Engine, since the template execution environment is not fully constrained, allowing template constructs to act as a bridge from templating logic into lower-level PHP functionality.
Abstractly, the insecure pattern is: "Restrict powerful execution primitives with a hand-maintained denylist inside a templating context that processes attacker-influenced data," rather than enforcing a strict allowlist or isolating template execution from sensitive APIs.
Potential impact
- Remote code execution (RCE) in the context of the web server process.
- Arbitrary file reads from the server's filesystem, potentially including configuration files and secrets.
- Server-side request forgery (SSRF), allowing outbound requests to internal services or cloud metadata endpoints.
- Server-side template injection (SSTI) chains that escalate from template control to full code execution.
- Unauthorized access to sensitive application data or credentials.
- Lateral movement within the hosting environment if the compromised process has broader network or file privileges.
General mitigation guidance
Effective mitigation focuses on reducing the power of the template execution environment and shrinking the attack surface of callable functions, rather than attempting to maintain ever-growing blocklists.
Recommended defensive patterns include:
- Use allowlists instead of blocklists: Define a minimal, explicit set of PHP functions that templates are allowed to call, and deny all others by default.
- Harden template execution contexts: Separate untrusted or semi-trusted template content into a restricted sandbox that does not expose powerful APIs, filesystem access, or network functions.
- Limit administrative capabilities in production:
- Disable
allowAdminChangesin production where possible. - Restrict access to powerful utilities such as System Messages to a minimal set of accounts.
- Apply least-privilege principles: Run the web server and PHP-FPM processes with the least privileges required (limited filesystem access, constrained network egress, and minimal OS permissions) to reduce blast radius if code execution occurs.
- Reduce template mutability: Avoid giving non-operations staff the ability to directly modify high-privilege templates or system messages that are executed with full application context.
- Centralize security-sensitive functionality: Encapsulate file access, network calls, and command execution behind vetted service layers, and never expose such primitives directly to template logic.
How teams detect issues like this
Detection should combine code-centric and configuration-centric approaches:
- Static analysis and code review:
- Search template-related code for use of Twig non-Closure arrow functions that can invoke PHP functions.
- Flag any template-exposed APIs that map directly to filesystem operations, network calls, or dynamic code execution.
- Look for denylist/blocklist implementations controlling function access and verify whether the pattern is inverted to an allowlist.
- Policy-based checks:
- Enforce organizational policies that disallow powerful PHP functions from being accessible in templating environments by default.
- Guardrails in CI to reject changes that introduce new template-callable functions without security review.
- Runtime monitoring:
- Log and monitor template rendering paths used by privileged panels (like System Messages) and watch for unusual patterns or spikes in error logs related to templates.
- Track use of sensitive PHP functions and correlate invocation contexts to template execution where feasible.
- Automated tooling:
- Employ SAST tools or custom rulesets that understand Twig/Craft CMS integration to surface templating contexts where user-influenced content is combined with powerful PHP primitives.
- Periodically run security tests that attempt benign template payloads to validate that sandbox and function restrictions are enforced.

