CVE-2024-56363
Jinja2 template injection allows arbitrary code execution via unsanitized user input.
- CVSS 7.8
- CWE-97
- Input Validation and Sanitization
- Remote
APTRS (Automated Penetration Testing Reporting System) is a Python and Django-based automated reporting tool designed for penetration testers and security organizations. In 1.0, there is a vulnerability in the web application's handling of user-supplied input that is incorporated into a Jinja2 template. Specifically, when user input is improperly sanitized or validated, an attacker can inject Jinja2 syntax into the template, causing the server to execute arbitrary code. For example, an attacker might be able to inject expressions like {{ config }}, {{ self.class.mro[1].subclasses() }}, or more dangerous payloads that trigger execution of arbitrary Python code. The vulnerability can be reproduced by submitting crafted input to all the template fields handled by ckeditor, that are passed directly to a Jinja2 template. If the input is rendered without sufficient sanitization, it results in the execution of malicious Jinja2 code on the server.
- CWE
- CWE-97
- CVSS base score
- 7.8
- Published
- 2024-12-23
- OWASP
- A03 Injection
- Orthogonal defect classification
- Interface
- Code defect classification
- Incorrect Functionality
- Category
- Input Validation and Sanitization
- Subcategory
- Insecure Parsing or Deserialization
- Accessibility scope
- Remote
- Impact
- Arbitrary Code Execution
- Affected component
- APTRS
Solution
Upgrade to APTRS 1.1 or later.
Vulnerable code sample
from flask import Flask, render_template, request
from jinja2 import Environment, FileSystemLoader
app = Flask(__name__)
template_env = Environment(loader=FileSystemLoader('.'))
@app.route('/', methods=['GET', 'POST'])
def index():
# VULNERABLE: This code is susceptible to command injection
if request.method == 'POST':
user_input = request.form.get('content', '')
template = template_env.from_string(f"<div>{user_input}</div>")
rendered_html = template.render()
return f'<h1>Rendered Content:</h1> {rendered_html}'
return '''
<form method="post">
<textarea name="content"></textarea>
<button type="submit">Render</button>
</form>
'''
if __name__ == '__main__':
app.run()Patched code sample
from flask import Flask, render_template, request
from jinja2 import Environment, FileSystemLoader
app = Flask(__name__)
template_env = Environment(loader=FileSystemLoader('.'))
@app.route('/', methods=['GET', 'POST'])
def index():
# SECURE: This version prevents command injection
if request.method == 'POST':
user_input = request.form.get('content', '')
template = template_env.from_string(f"<div>{{{{ {user_input} }}}}</div>")
rendered_html = template.render()
return f'<h1>Rendered Content:</h1> {rendered_html}'
return '''
<form method="post">
<textarea name="content"></textarea>
<button type="submit">Render</button>
</form>
'''
if __name__ == '__main__':
app.run()Payload
{{ self.class.mro[1].subclasses()[122].__init__.__globals__['os'].popen('whoami').read() }}
Cite this entry
@misc{vaitp:cve202456363,
title = {{Jinja2 template injection allows arbitrary code execution via unsanitized user input.
}},
author = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
year = {2024},
note = {VAITP Python Vulnerability Dataset, entry CVE-2024-56363},
howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2024-56363/}}
}
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 ::
