CVE-2024-56201
Jinja template injection allows arbitrary code execution via controlled filename and content.
- CVSS 5.4
- CWE-150
- Design Defects
- Remote
Jinja is an extensible templating engine. Prior to 3.1.5, 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. This vulnerability is fixed in 3.1.5.
- CWE
- CWE-150
- CVSS base score
- 5.4
- Published
- 2024-12-23
- OWASP
- A03 Injection
- Orthogonal defect classification
- Algorithm
- Code defect classification
- Incorrect Algorithm
- Category
- Design Defects
- Subcategory
- Insecure Parsing or Deserialization
- Accessibility scope
- Remote
- Impact
- Arbitrary Code Execution
- Affected component
- Jinja
- Fixed by upgrading
- Yes
Solution
Upgrade to Jinja 3.1.5 or later.
Vulnerable code sample
from jinja2 import Environment, FileSystemLoader
def render_user_template(user_filename: str, user_content: str) -> str:
# VULNERABLE: untrusted filename used directly with FileSystemLoader
env = Environment(loader=FileSystemLoader("/tmp/templates"))
# The attacker controls both filename and content, enabling arbitrary code exec
tmpl = env.from_string(user_content, name=user_filename)
return tmpl.render()Patched code sample
from jinja2 import Environment, FileSystemLoader
import os
def render_user_template(user_filename: str, user_content: str) -> str:
# FIX: sanitize filename to prevent execution via crafted names
safe_name = os.path.basename(user_filename)
env = Environment(loader=FileSystemLoader("/tmp/templates"))
tmpl = env.from_string(user_content, name=safe_name)
return tmpl.render()Published fix
Public sources link this CVE to the changes below. The code samples above were generated to illustrate the defect; these links lead to the code the project actually changed.
- fix f-string syntax error in code generation (pallets/jinja)
src/jinja2/compiler.py · CodeGenerator.visit_FromImport
Payload
{% set x = namespace() %}
{% set x.filename = "__import__('os').system('touch /tmp/pwned')" %}
{{ include(x.filename) }}
Cite this entry
@misc{vaitp:cve202456201,
title = {{Jinja template injection allows arbitrary code execution via controlled filename and content.}},
author = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
year = {2024},
note = {VAITP Python Vulnerability Dataset, entry CVE-2024-56201},
howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2024-56201/}}
}
Introducing the "VAITP dataset": a specialized repository of Python vulnerabilities and patches, meticulously compiled for the use of the security research community. As Python's prominence grows, understanding and addressing potential security vulnerabilities become crucial. Crafted by and for the cybersecurity community, this dataset offers a valuable resource for researchers, analysts, and developers to analyze and mitigate the security risks associated with Python. Through the comprehensive exploration of vulnerabilities and corresponding patches, the VAITP dataset fosters a safer and more resilient Python ecosystem, encouraging collaborative advancements in programming security.
The supreme art of war is to subdue the enemy without fighting.
Sun Tzu – “The Art of War”
:: Shaping the future through research and ingenuity ::
