Summary
Grav contains a code injection vulnerability in the Twig sort filter that can allow an authenticated user with page-write permissions to execute arbitrary PHP code as the web server user. The issue becomes risky when attacker- controlled content is rendered through a sandboxed Twig environment whose function restrictions can be bypassed.
Affected context
The vulnerability affects Grav installations using the vulnerable Twig extension behavior in versions up to and including 2.0.17. It requires a rendering path, such as page content or plugin-generated output, where an authenticated user with page-write capabilities can influence Twig input and invoke the sort filter within the sandbox.
Exploit preconditions
- Attack surface: Twig template rendering through Grav pages or integrated
plugins
- Required access: An authenticated account with page-write permissions,
such as admin.pages or api.pages.write
- Trigger point: Rendering attacker-controlled content that reaches the Twig
sort filter
- Why exploitation is possible: The sort wrapper disables the sandbox
argument, and the remaining function restrictions do not adequately block
a PHP include-capable function, allowing attacker-controlled code execution
Why this happens
The underlying bug is an incomplete enforcement of Twig sandbox restrictions in GravExtension.php. Unlike related filters that preserve the sandbox state, the sort function wrapper hardcodes the isSandboxed argument to false and permits a plain function name to be resolved. Because the denylist does not comprehensively restrict include-capable PHP functionality, attacker- controlled template data can cross from a supposedly constrained expression into PHP execution. This behavior maps to CWE-94, Improper Control of Generation of Code.
Potential impact
- An attacker may execute arbitrary PHP code as the web server user.
- The attacker could read, modify, or delete application and site data.
- The compromise could enable defacement, credential exposure, or persistence.
- The attacker may use the web server account to access other reachable
services or resources.
General mitigation guidance
Mitigation generally focuses on preserving sandbox enforcement across every Twig filter and preventing template-controlled values from selecting callable PHP functions. Secure-by-default designs should use explicit, allowlisted operations rather than denylisted function names, keep user-authored content separate from executable templates, and validate content before rendering. Page-writing privileges should be limited to trusted users, while the web process should run with the minimum filesystem and service permissions required. Plugin integrations should also avoid rendering untrusted frontmatter as executable template input.
How teams detect issues like this
Detection can include static analysis for wrappers that override sandbox or authorization parameters, dynamic callable resolution based on template input, and denylist-only restrictions around PHP functions. Code review should examine every Twig extension and confirm that sandbox state is consistently propagated. Monitoring can flag unexpected page updates, suspicious template expressions, anomalous web-process child activity, and PHP file access originating from the application account. Automated security review tools may help identify unsafe callable resolution and incomplete allowlists.

