CVE-2024-28116: Getgrav/grav Server-Side Template Injection (SSTI) With Vulnerability
HIGHCVE-2024-28116 is a HIGH (CVSS 8.8) server-side template injection (ssti) with vulnerability (CWE-94: Improper Control of Generation of Code ('Code Injection')) affecting Getgrav/grav libraries in the Packagist ecosystem. The issue involves server-side template injection (ssti) with grav cms security sandbox bypass. Remediation is available by upgrading affected packages to version 1.7.45.
CVE-2024-28116 at a Glance
Security Impact
CVSS: 8.8
The security impact metrics below are parsed directly from the CVSS v3.1 vector string (CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H).
How to Fix CVE-2024-28116
To remediate this vulnerability, upgrade all affected Getgrav/grav dependencies to a patched release. Both direct and transitive dependencies should be checked, as frameworks or security libraries may pull in vulnerable versions transitively.
Upgrade getgrav/grav to 1.7.45
Run the following command in your project directory to verify that all resolved dependencies use the patched release:
inspect dependencies for getgrav/grav
How to Check if You Are Affected
To determine whether your application is affected by CVE-2024-28116, inspect your project dependencies for Getgrav/grav:
inspect dependencies for getgrav/grav
Analyze the terminal output to locate any version of Getgrav/grav in your dependency tree. If any resolved version matches the vulnerable ranges listed in the table below (for example, versions earlier than 1.7.45), your application is affected.
Note: Verify declared build manifests and deployed runtime container environments.
Technical Details
Vulnerability Mechanism & Technical Analysis
This vulnerability is classified under CWE-94: Improper Control of Generation of Code ('Code Injection'), where the product constructs all or part of a code segment using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the syntax or behavior of the intended code segment. In Getgrav/grav, software state validation or input boundaries are circumvented during processing. Review the official source advisory statement below for specific affected components, classes, and reproduction details.
Source Advisory Statement
CVE-2024-28116 Affected Getgrav/grav Versions
1 component(s)| Product / Package | Ecosystem | Affected Versions | Fixed Version |
|---|---|---|---|
|
getgrav/grav
|
Packagist |
< 1.7.45
|
1.7.45
|
References
Frequently Asked Questions
What is CVE-2024-28116?
Which Getgrav/grav versions are affected?
What version fixes CVE-2024-28116?
Is getgrav/grav affected?
What is the CVSS score of CVE-2024-28116?
What CWE is associated with CVE-2024-28116?
How can I check Packagist dependencies for CVE-2024-28116?
Machine-readable OSV JSON
OSV v1.5 Schema
{
"schema_version" : "1.4.0",
"id" : "GHSA-c9gp-64c4-2rrh",
"modified" : "2024-03-22T16:30:26Z",
"published" : "2024-03-22T16:30:26Z",
"aliases" : [ "CVE-2024-28116" ],
"summary" : "Server-Side Template Injection (SSTI) with Grav CMS security sandbox bypass",
"details" : "### Summary\nGrav CMS is vulnerable to a Server-Side Template Injection (SSTI), which allows any authenticated user (editor permissions are sufficient) to execute arbitrary code on the remote server bypassing the existing security sandbox.\n\n### Details\nThe Grav CMS implements a custom sandbox to protect the powerful Twig methods \"registerUndefinedFunctionCallback()\" and \"registerUndefinedFilterCallback()\", in order to avoid SSTI attacks by denying the calling of dangerous PHP functions into the Twig template directives (such as: \"exec()\", \"passthru()\", \"system()\", etc.). \nThe current defenses are based on a blacklist of prohibited functions (PHP, Twig), checked through the \"isDangerousFunction()\" method called in the file \"system/src/Grav/Common/Twig.php\":\n\n```php\n...\n$this->twig = new TwigEnvironment($loader_chain, $params);\n\n$this->twig->registerUndefinedFunctionCallback(function (string $name) use ($config) {\n $allowed = $config->get('system.twig.safe_functions');\n if (is_array($allowed) && in_array($name, $allowed, true) && function_exists($name)) {\n return new TwigFunction($name, $name);\n }\n if ($config->get('system.twig.undefined_functions')) {\n if (function_exists($name)) {\n if (!Utils::isDangerousFunction($name)) {\n user_error(\"PHP function {$name}() was used as Twig function. This is deprecated in Grav 1.7. Please add it to system configuration: `system.twig.safe_functions`\", E_USER_DEPRECATED);\n\n return new TwigFunction($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 function. If you really want to use it, please add it to system configuration: `system.twig.safe_functions`\"));\n }\n\n return new TwigFunction($name, static function () {});\n }\n\n return false;\n});\n\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 return new TwigFilter($name, $name);\n }\n...\n```\nIn the code above it can be seen that the calls of the \"isDangerousFunction()\" are not performed when the method/filter in the \"$name\" variable has been considered safe. A function can be defined safe only by an administrator user, by adding it into the configuration properties \"system.twig.safe_functions\" and/or \"system.twig.safe_filters\" (a sort of whitelists that by default are empty) of the configuration file \"system/config/system.yaml\".\n\nIt is to note that within the \"system/src/Grav/Common/Twig.php\" file a Twig class is defined (with its constructor, methods and attributes) and in particular the Twig object (and environment) is instantiated on it:\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 /**\n * Constructor\n *\n * @param Grav $grav\n */\n public function __construct(Grav $grav)\n {\n $this->grav = $grav;\n $this->twig_paths = [];\n }\n\n /**\n * Twig initialization that sets the twig loader chain, then the environment, then extensions\n * and also the base set of twig vars\n *\n * @return $this\n */\n public function init()\n {\n if (null === $this->twig) {\n /** @var Config $config */\n $config = $this->grav['config'];\n...\n```\nSince the security sandbox does not protect the Twig object it is possible to interact with it (e.g. call its methods, read/write its attributes) through opportunely crafted Twig template directives injected on a web page. \nThen an authenticated editor user could be able to add arbitrary functions into the Twig attributes \"system.twig.safe_functions\" and \"system.twig.safe_filters\" in order to circumvent the Grav CMS sandbox.\n\n\n### PoC\nAn authenticated user with the permissions to edit a page (having Twig processing enabled) on the Grav CMS admin console, could create/edit a web page containing a malicious template directive to execute arbitrary OS commands on the remote web server.\nFor instance, in order to abuse the vulnerability and execute the prohibited \"system('id')\" code, bypassing the sandbox, the editor could generate a web page containing the following template directives:\n```\n{% set arr = {'1':'system', '2':'foo'} %}\n{{ var_dump(grav.twig.twig_vars['config'].set('system.twig.safe_functions', arr)) }}\n{{ system('id') }}\n```\nOnce saved the malicious page could be accessed by unauthenticated users to execute the \"system('id')\" code on the remote server hosting the vulnerable Grav CMS.\n\n\n### Impact\nIt is possible to execute remote code on the underlying server and compromise it.\n\n\n### Tested version\nGrav CMS v1.7.43\n\n\n### Reported by\nMaurizio Siddu",
"severity" : [ {
"type" : "CVSS_V3",
"score" : "CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H"
} ],
"affected" : [ {
"package" : {
"ecosystem" : "Packagist",
"name" : "getgrav/grav"
},
"ranges" : [ {
"type" : "ECOSYSTEM",
"events" : [ {
"introduced" : "0"
}, {
"fixed" : "1.7.45"
} ]
} ]
} ],
"references" : [ {
"type" : "WEB",
"url" : "https://github.com/getgrav/grav/security/advisories/GHSA-c9gp-64c4-2rrh"
}, {
"type" : "ADVISORY",
"url" : "https://nvd.nist.gov/vuln/detail/CVE-2024-28116"
}, {
"type" : "WEB",
"url" : "https://github.com/getgrav/grav/commit/4149c81339274130742831422de2685f298f3a6e"
}, {
"type" : "PACKAGE",
"url" : "https://github.com/getgrav/grav"
} ],
"database_specific" : {
"cwe_ids" : [ "CWE-1336", "CWE-94" ],
"severity" : "HIGH",
"github_reviewed" : true,
"github_reviewed_at" : "2024-03-22T16:30:26Z",
"nvd_published_at" : "2024-03-21T22:15:11Z"
}
}