CVE-2024-28116: Getgrav/grav Server-Side Template Injection (SSTI) With Vulnerability

HIGH
Advisory Identifier: GHSA-c9gp-64c4-2rrh
CVSS 8.8
CWE-94 CWE-1336
Published
Advisory Summary

CVE-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

CVE Identifier CVE-2024-28116
GHSA Identifier GHSA-c9gp-64c4-2rrh
Severity HIGH
CVSS v3.1 Base Score 8.8
CWE Classification CWE-94: Improper Control of Generation of Code ('Code Injection')
Affected Product Getgrav/grav
Package Ecosystem Packagist
Published Date
Last Updated

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).

Attack Vector Network (AV:N)
Attack Complexity Low (AC:L)
Privileges Required Low (PR:L)
User Interaction None (UI:N)
Scope Unchanged (S:U)
Confidentiality High (C:H)
Integrity High (I:H)
Availability High (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.

getgrav/grav fixed: 1.7.45
Upgrade getgrav/grav to 1.7.45
Verify Direct and Transitive Dependencies

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

### Summary Grav 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. ### Details The 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.). The 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": ```php ... $this->twig = new TwigEnvironment($loader_chain, $params); $this->twig->registerUndefinedFunctionCallback(function (string $name) use ($config) { $allowed = $config->get('system.twig.safe_functions'); if (is_array($allowed) && in_array($name, $allowed, true) && function_exists($name)) { return new TwigFunction($name, $name); } if ($config->get('system.twig.undefined_functions')) { if (function_exists($name)) { if (!Utils::isDangerousFunction($name)) { 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); return new TwigFunction($name, $name); } /** @var Debugger $debugger */ $debugger = $this->grav['debugger']; $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`")); } return new TwigFunction($name, static function () {}); } return false; }); $this->twig->registerUndefinedFilterCallback(function (string $name) use ($config) { $allowed = $config->get('system.twig.safe_filters'); if (is_array($allowed) && in_array($name, $allowed, true) && function_exists($name)) { return new TwigFilter($name, $name); } if ($config->get('system.twig.undefined_filters')) { if (function_exists($name)) { if (!Utils::isDangerousFunction($name)) { 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); return new TwigFilter($name, $name); } ... ``` In 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". It 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: ```php /** * Class Twig * @package Grav\Common\Twig */ class Twig { /** @var Environment */ public $twig; /** @var array */ public $twig_vars = []; /** @var array */ public $twig_paths; /** @var string */ public $template; ... /** * Constructor * * @param Grav $grav */ public function __construct(Grav $grav) { $this->grav = $grav; $this->twig_paths = []; } /** * Twig initialization that sets the twig loader chain, then the environment, then extensions * and also the base set of twig vars * * @return $this */ public function init() { if (null === $this->twig) { /** @var Config $config */ $config = $this->grav['config']; ... ``` Since 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. Then 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. ### PoC An 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. For 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: ``` {% set arr = {'1':'system', '2':'foo'} %} {{ var_dump(grav.twig.twig_vars['config'].set('system.twig.safe_functions', arr)) }} {{ system('id') }} ``` Once 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. ### Impact It is possible to execute remote code on the underlying server and compromise it. ### Tested version Grav CMS v1.7.43 ### Reported by Maurizio Siddu

CVE-2024-28116 Affected Getgrav/grav Versions

1 component(s)
Fixed Versions:
1.7.45
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?
CVE-2024-28116 is a security vulnerability in Getgrav/grav involving Server-Side Template Injection (SSTI) with Grav CMS security sandbox bypass under CWE-94: Improper Control of Generation of Code ('Code Injection').
Which Getgrav/grav versions are affected?
Affected versions include releases prior to 1.7.45 across affected packages: getgrav/grav.
What version fixes CVE-2024-28116?
Upgrading to 1.7.45 (or the latest patched release for your respective branch) resolves the vulnerability.
Is getgrav/grav affected?
Yes, getgrav/grav is affected in releases prior to 1.7.45. Upgrade your dependency to 1.7.45 or later.
What is the CVSS score of CVE-2024-28116?
CVE-2024-28116 has a CVSS v3.1 base score of 8.8 (HIGH) with vector string CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H.
What CWE is associated with CVE-2024-28116?
CVE-2024-28116 is classified under CWE-94: Improper Control of Generation of Code ('Code Injection') (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.).
How can I check Packagist dependencies for CVE-2024-28116?
Run the dependency tree command: 'inspect dependencies for getgrav/grav'. This command lists direct and transitive dependencies in your build configuration so you can verify if a vulnerable version is resolved.
Machine-readable OSV JSON OSV v1.5 Schema
Normalized schema output
{
  "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"
  }
}