Twig Path Traversal Vulnerability in Twig/Twig
Summary
GHSA-7cvr-xhm5-x998: Twig Path Traversal vulnerability in the filesystem loader. Affects twig/twig versions >= 1.0.0, < 1.12.3. Fixed in 1.12.3. How to fix ↓
Check if this vulnerability affects your application
Upload a manifest or SBOM and we'll show whether you're affected, plus every other risky package in the same scan.
No signup required · Results in about 60 seconds
Background
Twig is a popular templating engine for PHP that allows developers to create reusable templates for web applications. It is widely used in various PHP frameworks and applications. The Twig_Loader_Filesystem component is responsible for loading Twig templates from the filesystem. However, when non-trusted template names are used, a path traversal vulnerability can be exploited. This vulnerability allows an attacker to include files outside of the intended directory, potentially leading to unauthorized access or code execution.
Vulnerability Detail
The Twig path traversal vulnerability occurs when an application uses non-trusted template names in conjunction with the Twig_Loader_Filesystem component. By manipulating the template name, an attacker can navigate to parent directories and include files outside of the intended template directory. For example, by prepending the template name with "../", an attacker can include a file stored in a higher-level directory. It is important to note that only the "../" prefix is effective, and other variations will result in proper exceptions being thrown. This vulnerability can be exploited by an attacker with the ability to provide template names to the application.
How To Fix
Minimum fixed version
1.12.3
This is the first release that contains the fix.
composer require twig/twig:1.12.3
Tip — Not sure which version your app runs? Vulert can match this fix to it. Scan free →
Workaround
If upgrading Twig is not immediately feasible, a temporary workaround can be implemented by manually validating and sanitizing template names before passing them to the Twig_Loader_Filesystem component. This can be done by checking for any occurrences of the "../" prefix and rejecting template names that contain it. Here is an example of how this can be achieved in PHP:
$templateName = $_GET['template'];
if (strpos($templateName, '../') !== false) {
throw new Exception('Invalid template name');
}
// Load the template using Twig
$loader = new Twig_Loader_Filesystem('/path/to/templates');
$twig = new Twig_Environment($loader);
$template = $twig->load($templateName);
// Render the template
echo $template->render();
Conclusion
The Twig path traversal vulnerability in Twig/Twig can allow an attacker to include files outside of the intended template directory, potentially leading to unauthorized access or code execution. It is crucial to upgrade Twig to version 1.12.3 or later to mitigate this vulnerability. Additionally, developers should validate and sanitize template names to prevent the inclusion of malicious or unauthorized files. For further assistance in vulnerability remediation, please contact DevNack.com.
See every vulnerability recorded for twig/twig →
FAQ
Twig is a templating engine for PHP that allows developers to create reusable and modular templates for web applications. It provides a clean and intuitive syntax for separating presentation logic from application logic.
The Twig path traversal vulnerability occurs when an application uses non-trusted template names in conjunction with the Twig_Loader_Filesystem component. By manipulating the template name, an attacker can navigate to parent directories and include files outside of the intended template directory.
The Twig path traversal vulnerability can lead to unauthorized access to sensitive files or code execution. An attacker could include arbitrary files from the filesystem, potentially exposing sensitive information or executing malicious code within the application's context.
To fix the Twig path traversal vulnerability, it is recommended to upgrade Twig to version 1.12.3 or later. Additionally, developers should ensure that template names are properly validated and sanitized to prevent the inclusion of malicious or unauthorized files.
Yes, if upgrading Twig is not immediately feasible, a temporary workaround can be implemented by manually validating and sanitizing template names before passing them to the Twig_Loader_Filesystem component. This involves checking for the "../" prefix and rejecting template names that contain it.
References
Related vulnerabilities
Recent vulnerabilities recorded for twig/twig.