Summary
A code injection vulnerability in Craft CMS allows authenticated control-panel users to bypass cleansing for string-typed field-layout elements.
By supplying crafted JSON strings, an attacker may cause Yii2 behavior attachments and event handlers to be instantiated, potentially leading to arbitrary code execution when the application processes the layout.
Affected context
The vulnerability occurs in Craft CMS control-panel functionality that processes field-layout elements supplied as JSON strings. It becomes risky when an authenticated user can submit or modify field layouts and the application accepts string-typed elements without fully applying cleansing and validation before object creation.
Exploit preconditions
- Attack surface: Craft CMS control-panel field-layout management functionality
- Required access: Authenticated access with permission to submit or modify affected field-layout data
- Trigger point: Processing of crafted field-layout elements through the application's object-creation path
- Why exploitation is possible: String-form layout elements can bypass cleansing validation and reach dynamic Yii2 behavior or event-handler instantiation
Why this happens
The underlying issue is insufficient validation and normalization of string-typed field-layout elements before they are passed to dynamic object-construction logic.
When attacker-controlled input can influence behavior attachments, event handlers, or other class-oriented data, a call such as Craft::createObject() may instantiate unintended objects and execute associated behavior. This maps to CWE-94: Improper Control of Generation of Code, because untrusted input influences executable application behavior rather than remaining limited to inert configuration data.
Potential impact
- An attacker could execute arbitrary code in the Craft CMS application context
- The application and its hosted content could be compromised
- Sensitive configuration data or credentials may be exposed
- The attacker could alter content, create persistence, or pivot to connected services
- Availability could be affected through destructive or resource-intensive actions
General mitigation guidance
Mitigation generally focuses on ensuring that field-layout input remains data-only and cannot control executable object construction.
Common defensive patterns include:
- Applying strict allowlist-based schemas to field-layout elements
- Canonicalizing and validating JSON values before any object creation or event registration
- Rejecting unexpected class names, behavior definitions, and handler specifications
- Avoiding dynamic object instantiation from user-controlled values
- Separating control-panel permissions so only trusted administrators can modify layouts
- Running the application with least-privilege filesystem, database, and service access
- Treating authenticated control-panel input as untrusted and enforcing server-side validation
How teams detect issues like this
Organizations can look for this vulnerability class through:
- Static analysis rules identifying dynamic object-construction calls that consume request or JSON-derived values
- Code review checks for cleansing that occurs after parsing, normalization, or object instantiation
- Tests using unexpected string-typed layout elements and verifying strict rejection
- Monitoring for unusual field-layout changes, behavior definitions, or event-handler values
- Policy enforcement requiring allowlisted schemas and prohibiting user-controlled class resolution
- Neutral automated code-review or software-composition tools that flag unsafe dynamic instantiation patterns

