fix(dev): jinja a sandbox breakout through malicious filenames #642
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
A bug in the Jinja compiler allows an attacker that controls both the content and filename of a template to execute arbitrary Python code, regardless of if Jinja's sandbox is used. To exploit the vulnerability, an attacker needs to control both the filename and the contents of a template. Whether that is the case depends on the type of application using Jinja. This vulnerability impacts users of applications which execute untrusted templates where the template author can also choose the template filename.
The root cause of this vulnerability lies in how Jinja's compiler handles template filenames during the compilation process. When an attacker can influence both the filename and the content of a template, they can craft malicious inputs that Jinja fails to sanitize properly. This failure leads to the execution of arbitrary code within the application's context.
Vulnerable Code
In this scenario, if an attacker can control the
template_name
and the contents of the template, they can introduce malicious code that Jinja will execute during rendering.Exploit Code and Proof of Concept
To demonstrate the exploitation of this vulnerability, consider the following proof of concept:
malicious_template.html
:Observe that the application executes the id command, demonstrating arbitrary code execution.
CVE-2024-56201 highlights the critical importance of validating both the sources and contents of templates in web applications. By understanding the mechanics of this vulnerability and implementing the recommended mitigations, developers can safeguard their applications against potential exploitation.
CWE-150