Twig Path Traversal Vulnerability in Twig/Twig

twig/twig Updated 02 Oct 2025

Summary

30/05/2024
MODERATE
Yes i
Moderate
5.3

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 ↓

Free scan

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.

Check my app free

No signup required · Results in about 60 seconds

Upload your dependency manifest or SBOM

Drag & drop your file here or

Java Javascript PHP Python Go Rust Ruby Dart C/C++ Elixir CycloneDX SPDX

Upload a supported dependency manifest or SBOM. No installation or source-code access required.

See supported files and SBOM formats
SBOM sbom.json · bom.json · bom.xml · *.spdx.json · *.cdx.json · *.cdx.xml
Java pom.xml · gradle.lockfile · buildscript-gradle.lockfile
Javascript package-lock.json · yarn.lock · pnpm-lock.yaml
PHP composer.lock
Python poetry.lock · requirements.txt · Pipfile.lock
Go go.mod
Rust Cargo.lock
Ruby Gemfile.lock
Dart pubspec.lock
C/C++ conan.lock
Elixir mix.lock
Enter your work email to receive your report link.

Verify your email to view the full report and start 30 days of continuous monitoring. No credit card required.

Sponsored Devnack Penetration testing for web, API and mobile Manual testing by engineers who build software, not an auditor's checklist. Trusted by teams at Swift and Google Talk to Devnack

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 →

Share this vulnerability

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.

Related vulnerabilities

Recent vulnerabilities recorded for twig/twig.

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 →

Subscribe to Vulert on Telegram
Daily digest of new CVEs, critical and high severity, affected packages and fixes — plus a weekly roundup.
Join the channel →
Help improve this record
Report an issue or send feedback