Summary
Craft CMS contains a code injection issue in the field layout preview endpoint where untrusted layout configuration is processed without sanitization.
An authenticated admin can inject Yii2 event handlers into the field layout configuration, leading to arbitrary PHP code execution and disclosure of sensitive environment data.
Affected context
The vulnerability appears in Craft CMS installations using versions between 5.5.0 and 5.9.13 where the FieldsController::actionRenderCardPreview() endpoint is accessible to authenticated admin users.
It triggers when the fieldLayoutConfig POST parameter is accepted and passed to Fields::createLayout() without first cleansing or normalizing the configuration.
Environments that rely on admin accounts for content or field layout management and expose this controller in the control panel are within the affected context.
Exploit preconditions
- Attack surface: HTTP(S) control panel endpoint handling field layout previews
- Required access: Authenticated Craft CMS admin account with permission to manage fields/layouts
- Trigger point: Submission of a crafted
fieldLayoutConfigPOST parameter toFieldsController::actionRenderCardPreview() - Why exploitation is possible: The layout configuration is treated as trusted and passed to
Fields::createLayout()without invokingComponent::cleanseConfig(), allowing injected event handler definitions (e.g.,on init) to be interpreted and executed by Yii2
Why this happens
The root cause is the direct use of untrusted configuration data as executable behavior within the Yii2 component system.
By passing fieldLayoutConfig straight into Fields::createLayout() without running it through Component::cleanseConfig(), the application allows attacker-controlled keys such as on init to register event handlers that point to arbitrary PHP callables or code paths.
This aligns with CWE-94: Improper Control of Generation of Code ('Code Injection'), where user-controlled data inadvertently shapes or injects executable logic rather than being treated as inert configuration.
Potential impact
- Execution of arbitrary PHP code within the Craft CMS application context
- Disclosure of sensitive environment variables, including database credentials and
CRAFT_SECURITY_KEY - Full compromise of application data and configuration
- Lateral movement within the hosting environment, depending on server permissions
- Tampering with content, configuration, or site behavior via injected logic
General mitigation guidance
Mitigation typically focuses on treating configuration data as untrusted and normalizing it before use in component systems.
Common defensive patterns include:
- Applying a cleansing or normalization step (such as
cleanseConfig-style routines) before passing user-supplied configuration into framework components - Restricting or disabling dynamic event handler definitions in data structures derived from HTTP requests
- Separating static, developer-controlled configuration from runtime, user-provided input to avoid blending data and behavior
- Enforcing least-privilege on admin roles and limiting who can modify complex layout or field configurations
- Implementing server-side allowlists for permissible configuration keys and structures when constructing layouts or components
How teams detect issues like this
High-level detection approaches can focus on both code and runtime behavior:
- Static analysis rules that flag paths where request data is passed into component or event systems without sanitization or cleansing
- Code review checklists emphasizing review of controller actions that transform request parameters into configuration arrays or component definitions
- Framework-level policies that prohibit dynamic event handler declarations from user-controlled input
- Logging and monitoring of unusual layout configuration submissions or unexpected event bindings, combined with anomaly detection for suspicious PHP call stacks
- Use of automated security review tools that recognize patterns such as user input flowing into framework event systems or dynamic code hooks

