CVE-2023-34448: Getgrav/grav Grav Server-side Template Injection (SSTI) Vulnerability
HIGHCVE-2023-34448 is a HIGH (CVSS 7.2) grav server-side template injection (ssti) vulnerability (CWE-20: Improper Input Validation) affecting Getgrav/grav libraries in the Packagist ecosystem. The issue involves grav server-side template injection (ssti) via twig default filters. Remediation is available by upgrading affected packages to version 1.7.42.
CVE-2023-34448 at a Glance
Security Impact
CVSS: 7.2
The security impact metrics below are parsed directly from the CVSS v3.1 vector string (CVSS:3.1/AV:N/AC:L/PR:H/UI:N/S:U/C:H/I:H/A:H).
How to Fix CVE-2023-34448
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.42
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-2023-34448, 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.42), 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-20: Improper Input Validation, where the product receives input or data, but it does not validate or incorrectly validates that the input has the properties that are required to process the data safely and correctly. 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-2023-34448 Affected Getgrav/grav Versions
1 component(s)| Product / Package | Ecosystem | Affected Versions | Fixed Version |
|---|---|---|---|
|
getgrav/grav
|
Packagist |
< 1.7.42
|
1.7.42
|
References
Frequently Asked Questions
What is CVE-2023-34448?
Which Getgrav/grav versions are affected?
What version fixes CVE-2023-34448?
Is getgrav/grav affected?
What is the CVSS score of CVE-2023-34448?
What CWE is associated with CVE-2023-34448?
How can I check Packagist dependencies for CVE-2023-34448?
Machine-readable OSV JSON
OSV v1.5 Schema
{
"schema_version" : "1.4.0",
"id" : "GHSA-whr7-m3f8-mpm8",
"modified" : "2023-06-16T19:37:08Z",
"published" : "2023-06-16T19:37:08Z",
"aliases" : [ "CVE-2023-34448" ],
"summary" : "Grav Server-side Template Injection (SSTI) via Twig Default Filters",
"details" : "Hi,\n\nactually we have sent the bug report to [security@getgrav.org](mailto:security@getgrav.org) on 27th March 2023 and on 10th April 2023.\n\n# Grav Server-side Template Injection (SSTI) via Twig Default Filters\n\n## Summary: \n| **Product** | Grav CMS |\n| ----------------------- | --------------------------------------------- |\n| **Vendor** | Grav |\n| **Severity** | High - Users with login access to Grav Admin panel and page creation/update permissions are able to obtain remote code/command execution |\n| **Affected Versions** | <= [v1.7.40](https://github.com/getgrav/grav/tree/1.7.40) (Commit [685d762](https://github.com/getgrav/grav/commit/685d76231a057416651ed192a6a2e83720800e61)) (Latest version as of writing) |\n| **Tested Versions** | v1.7.40 |\n| **Internal Identifier** | STAR-2023-0008 |\n| **CVE Identifier** | TBD |\n| **CWE(s)** | CWE-184: Incomplete List of Disallowed Inputs, CWE-1336: Improper Neutralization of Special Elements Used in a Template Engine |\n\n## CVSS3.1 Scoring System: \n**Base Score:** 7.2 (High) \n**Vector String:** `CVSS:3.1/AV:N/AC:L/PR:H/UI:N/S:U/C:H/I:H/A:H` \n| **Metric** | **Value** |\n| ---------------------------- | --------- |\n| **Attack Vector (AV)** | Network |\n| **Attack Complexity (AC)** | Low |\n| **Privileges Required (PR)** | High |\n| **User Interaction (UI)** | None |\n| **Scope (S)** | Unchanged |\n| **Confidentiality \\(C)** | High |\n| **Integrity (I)** | High |\n| **Availability (A)** | High |\n\n## Product Overview: \nGrav is a PHP-based flat-file content management system (CMS) designed to provide a fast and simple way to build websites. It supports rendering of web pages written in Markdown and Twig expressions, and provides an administration panel to manage the entire website via an optional Admin plugin.\n\n## Vulnerability Summary: \nThe patch for [CVE-2022-2073](https://huntr.dev/bounties/3ef640e6-9e25-4ecb-8ec1-64311d63fe66/), a server-side template injection vulnerability in Grav leveraging the default `filter()` function, did not block other built-in functions exposed by Twig's Core Extension that could be used to invoke arbitrary unsafe functions, thereby allowing for remote code execution.\n\n## Vulnerability Details: \nTwig comes with an extension known as the [Core Extension](https://github.com/twigphp/Twig/blob/v1.44.7/src/Extension/CoreExtension.php) that is enabled by default when initialising a new [Twig environment](https://github.com/twigphp/Twig/blob/v1.44.7/src/Environment.php#L148). Twig's Core Extension provides multiple built-in filters, such as the `filter()` function, which can be used in Twig templates. \n\n[CVE-2022-2073](https://huntr.dev/bounties/3ef640e6-9e25-4ecb-8ec1-64311d63fe66/) leverages the default `filter()` filter function in Twig to invoke arbitrary unsafe functions. This was patched by overriding the default `filter()` filter function in commit [9d6a2d](https://www.github.com/getgrav/grav/commit/9d6a2dba09fd4e56f5cdfb9a399caea355bfeb83) of Grav v1.7.34 to perform validation checks on the arguments passed to `filter()`:\n~~~diff php\n...\nclass GravExtension extends AbstractExtension implements GlobalsInterface\n{\n ...\n public function getFilters(): array\n {\n return [\n ...\n // Security fix\n+ new TwigFilter('filter', [$this, 'filterFilter'], ['needs_environment' => true]),\n ];\n }\n \n ...\n\n+ /**\n+ * @param Environment $env\n+ * @param array $array\n+ * @param callable|string $arrow\n+ * @return array|CallbackFilterIterator\n+ * @throws RuntimeError\n+ */\n+ function filterFilter(Environment $env, $array, $arrow)\n+ {\n+ if (is_string($arrow) && Utils::isDangerousFunction($arrow)) {\n+ throw new RuntimeError('Twig |filter(\"' . $arrow . '\") is not allowed.');\n+ }\n+\n+ return \\twig_array_filter($env, $array, $arrow);\n+ }\n}\n~~~\n\nHowever, looking at the source code of [/src/Extension/CoreExtension.php](https://github.com/twigphp/Twig/blob/v1.44.7/src/Extension/CoreExtension.php) of Twig, alternative default Twig filters could also be used invoke arbitrary functions:\n~~~php\n...\nclass CoreExtension extends AbstractExtension\n{\n ...\n public function getFilters(): array\n {\n return [\n ...\n // array helpers\n ...\n new TwigFilter('filter', 'twig_array_filter', ['needs_environment' => true]), // unsafe\n new TwigFilter('map', 'twig_array_map', ['needs_environment' => true]), // unsafe\n new TwigFilter('reduce', 'twig_array_reduce', ['needs_environment' => true]), // unsafe\n ];\n }\n~~~\n\nThe three filter functions above respectively call `array_filter()`, `array_map()` and `array_reduce()`. Since only `filter()` is being overriden by Grav to ensure that the callable passed to `filter()` does not result in the invocation of an unsafe function, the other two functions (i.e. `map()` and `reduce()`) could be used by an authenticated attacker that is able to inject and render malicious templates to gain remote code execution.\n\n## Exploit Conditions: \nThis vulnerability can be exploited if the attacker has access to:\n1. an administrator account, or\n2. a non-administrator, user account that are granted the following permissions:\n - login access to Grav admin panel, and\n - page creation or update rights\n\n## Reproduction Steps: \n1. Log in to Grav Admin using an administrator account.\n2. Navigate to `Accounts > Add`, and ensure that the following permissions are assigned when creating a new low-privileged user:\n * Login to Admin - Allowed\n * Page Update - Allowed\n2. Log out of Grav Admin, and log back in using the account created in step 2.\n3. Navigate to `http://<grav_installation>/admin/pages/home`.\n4. Click the `Advanced` tab and select the checkbox beside `Twig` to ensure that Twig processing is enabled for the modified webpage.\n5. Under the `Content` tab, insert the following payload within the editor:\n ~~~twig\n {{ ['id'] | map('system') }}\n {{ ['id'] | reduce('system') }}\n ~~~\n4. Click the Preview button. Observe that the output of the `id` shell command is returned in the preview.\n\n## Suggested Mitigations: \nOverride the built-in Twig `map()` and `reduce()` filter functions in `system/src/Grav/Common/Twig/Extension/GravExtension.php` to validate the argument passed to the filter in `$arrow`.\n\nFor example:\n~~~diff\n...\nclass GravExtension extends AbstractExtension implements GlobalsInterface\n{\n ...\n public function getFilters(): array\n {\n return [\n ...\n // Security fix\n new TwigFilter('filter', [$this, 'filterFilter'], ['needs_environment' => true]),\n+ new TwigFilter('map', [$this, 'mapFilter'], ['needs_environment' => true]),\n+ new TwigFilter('reduce', [$this, 'reduceFilter'], ['needs_environment' => true]),\n ];\n }\n\n ...\n+ /**\n+ * @param Environment $env\n+ * @param array $array\n+ * @param callable|string $arrow\n+ * @return array|CallbackFilterIterator\n+ * @throws RuntimeError\n+ */\n+ function mapFilter(Environment $env, $array, $arrow)\n+ {\n+ if (!$arrow instanceof Closure && !is_string($arrow) || Utils::isDangerousFunction($arrow)) {\n+ throw new RuntimeError('Twig |map(\"' . $arrow . '\") is not allowed.');\n+ }\n+\n+ return \\twig_array_map($env, $array, $arrow);\n+ }\n+ \n+ /**\n+ * @param Environment $env\n+ * @param array $array\n+ * @param callable|string $arrow\n+ * @return array|CallbackFilterIterator\n+ * @throws RuntimeError\n+ */\n+ function reduceFilter(Environment $env, $array, $arrow)\n+ {\n+ if (!$arrow instanceof Closure && !is_string($arrow) || Utils::isDangerousFunction($arrow)) {\n+ throw new RuntimeError('Twig |reduce(\"' . $arrow . '\") is not allowed.');\n+ }\n+\n+ return \\twig_array_reduce($env, $array, $arrow);\n+ }\n}\n~~~\n\n## Detection Guidance: \nThe following strategies may be used to detect potential exploitation attempts.\n1. Searching within Markdown pages using the following shell command: \n `grep -Priz -e '\\|\\s*(map|reduce)\\s*\\(' /path/to/webroot/user/pages/`\n2. Searching within Doctrine cache data using the following shell command: \n `grep -Priz -e '\\|\\s*(map|reduce)\\s*\\(' --include '*.doctrinecache.data' /path/to/webroot/cache/`\n3. Searching within Twig cache using the following shell command: \n `grep -Priz -e 'twig_array_(map|reduce)' /path/to/webroot/cache/twig/`\n4. Searching within compiled Twig template files using the following shell command: \n `grep -Priz -e '\\|\\s*(map|reduce)\\s*\\(' /path/to/webroot/cache/compiled/files/`\n\nNote that it is not possible to detect indicators of compromise reliably using the Grav log file (located at `/path/to/webroot/logs/grav.log` by default), as successful exploitation attempts do not generate any additional logs. However, it is worthwhile to examine any PHP errors or warnings logged to determine the existence of any failed exploitation attempts.\n\n## Credits: \nNgo Wei Lin ([@Creastery](https://twitter.com/Creastery)) & Wang Hengyue ([@w_hy_04](https://twitter.com/w_hy_04)) of STAR Labs SG Pte. Ltd. ([@starlabs_sg](https://twitter.com/starlabs_sg))\n\n## Vulnerability Disclosure: \nThis vulnerability report is subject to a 120 day disclosure deadline as per [STAR Labs SG Pte. Ltd.'s Vulnerability Disclosure Policy](https://starlabs.sg/advisories/STAR%20Labs%20SG%20Pte.%20Ltd.%20Vulnerability%20Disclosure%20Policy.pdf). After 120 days have elapsed, the vulnerability report will be published to the public by [STAR Labs SG Pte. Ltd.](https://starlabs.sg/) (STAR Labs). \n\nThe scheduled disclosure date is _**25th July, 2023**_. Disclosure at an earlier date is also possible if agreed upon by all parties. \n\nKindly note that STAR Labs reserved and assigned the following CVE identifiers to the respective vulnerabilities presented in this report: \n1. **CVE-2023-30596**\n Server-side Template Injection (SSTI) in getgrav/grav <= v1.7.40 allows Grav Admin users with page creation or update rights to bypass the dangerous functions denylist check in `GravExtension.filterFilter()` and to achieve remote code execution via Twig's default filters `map()` and `reduce()`. This is a bypass of CVE-2022-2073.\n",
"severity" : [ {
"type" : "CVSS_V3",
"score" : "CVSS:3.1/AV:N/AC:L/PR:H/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.42"
} ]
} ]
} ],
"references" : [ {
"type" : "WEB",
"url" : "https://github.com/getgrav/grav/security/advisories/GHSA-whr7-m3f8-mpm8"
}, {
"type" : "ADVISORY",
"url" : "https://nvd.nist.gov/vuln/detail/CVE-2023-34448"
}, {
"type" : "WEB",
"url" : "https://github.com/getgrav/grav/commit/244758d4383034fe4cd292d41e477177870b65ec"
}, {
"type" : "WEB",
"url" : "https://github.com/getgrav/grav/commit/71bbed12f950de8335006d7f91112263d8504f1b"
}, {
"type" : "WEB",
"url" : "https://github.com/getgrav/grav/commit/8c2c1cb72611a399f13423fc6d0e1d998c03e5c8"
}, {
"type" : "WEB",
"url" : "https://github.com/getgrav/grav/commit/9d01140a63c77075ef09b26ef57cf186138151a5"
}, {
"type" : "PACKAGE",
"url" : "https://github.com/getgrav/grav"
}, {
"type" : "WEB",
"url" : "https://github.com/twigphp/Twig/blob/v1.44.7/src/Environment.php#L148"
}, {
"type" : "WEB",
"url" : "https://huntr.dev/bounties/3ef640e6-9e25-4ecb-8ec1-64311d63fe66"
}, {
"type" : "WEB",
"url" : "https://www.github.com/getgrav/grav/commit/9d6a2dba09fd4e56f5cdfb9a399caea355bfeb83"
} ],
"database_specific" : {
"cwe_ids" : [ "CWE-1336", "CWE-20" ],
"severity" : "HIGH",
"github_reviewed" : true,
"github_reviewed_at" : "2023-06-16T19:37:08Z",
"nvd_published_at" : "2023-06-14T23:15:11Z"
}
}