Skip to main content
CVE Vulnerability Database
Vulnerability Database/CVE-2024-22195

CVE-2024-22195: Palletsprojects Jinja XSS Vulnerability

CVE-2024-22195 is an XSS flaw in Palletsprojects Jinja that allows attackers to inject arbitrary HTML attributes via the xmlattr filter, bypassing auto escaping. This article covers technical details, impact, and mitigation.

Updated:

CVE-2024-22195 Overview

CVE-2024-22195 is a Cross-Site Scripting (XSS) vulnerability affecting Jinja, the popular extensible templating engine for Python. The vulnerability exists in the xmlattr filter, which can be abused to inject arbitrary HTML attribute keys and values into rendered HTML templates. This injection bypasses Jinja's auto-escaping mechanism, potentially enabling attackers to execute malicious scripts in the context of a user's browser session.

Critical Impact

Attackers can inject arbitrary HTML attributes into rendered templates, bypassing auto-escaping and leading to Cross-Site Scripting attacks. This may also allow bypass of blacklist-based attribute validation checks.

Affected Products

  • Palletsprojects Jinja (versions prior to 3.1.3)

Discovery Timeline

  • 2024-01-11 - CVE CVE-2024-22195 published to NVD
  • 2025-11-03 - Last updated in NVD database

Technical Details for CVE-2024-22195

Vulnerability Analysis

This vulnerability stems from improper handling of user-controlled input within the Jinja xmlattr filter. Jinja uses special placeholders in templates that allow developers to write code similar to Python syntax. While Jinja implements auto-escaping to prevent XSS attacks, the xmlattr filter provides a bypass mechanism.

The xmlattr filter is designed to convert a dictionary of attributes into a space-separated list of key="value" pairs for HTML/XML attributes. However, when attacker-controlled data flows into this filter, it becomes possible to inject arbitrary HTML attribute keys and values that the auto-escaping mechanism does not properly sanitize.

This attack vector is particularly dangerous because it can bypass not only Jinja's built-in protections but also application-level attribute validation checks that rely on blacklist-based filtering approaches.

Root Cause

The root cause of this vulnerability lies in the xmlattr filter's insufficient validation and sanitization of attribute names and values. The filter processes dictionary data and outputs HTML attributes without adequately ensuring that the attribute names and values cannot be used to inject malicious content. When combined with attacker-controlled input, this allows the injection of event handler attributes (such as onclick, onerror, etc.) or other dangerous HTML attributes that can execute JavaScript code.

Attack Vector

The attack is network-based and requires user interaction. An attacker would need to craft malicious input that gets processed through a Jinja template using the xmlattr filter. When a victim visits a page that renders this template with the attacker's payload, the injected HTML attributes become part of the DOM, potentially triggering script execution.

The attacker could exploit this by:

  1. Identifying an application that uses Jinja templates with the xmlattr filter on user-controlled data
  2. Crafting a payload containing malicious HTML attributes (e.g., event handlers)
  3. Submitting the payload through the application's input mechanisms
  4. When the template renders for any user viewing that content, the injected attributes execute the malicious script

The vulnerability mechanism allows injection of arbitrary HTML attributes through the xmlattr filter. When user-controlled data containing specially crafted dictionary keys or values is passed to this filter, it can result in the injection of event handler attributes or other malicious HTML constructs. For detailed technical analysis, see the GitHub Security Advisory.

Detection Methods for CVE-2024-22195

Indicators of Compromise

  • Unusual HTML attributes appearing in rendered pages, particularly event handlers not expected by the application
  • Web application firewall logs showing attempts to inject HTML attribute patterns through input fields
  • Client-side errors or unexpected JavaScript execution reported by browser security monitoring
  • Suspicious dictionary structures in application logs containing attribute-like key-value pairs

Detection Strategies

  • Review Jinja templates for usage of the xmlattr filter with user-controlled input
  • Implement Content Security Policy (CSP) headers to detect and block inline script execution
  • Deploy web application firewall rules to identify HTML attribute injection patterns
  • Audit application code for data flows from user input to Jinja template rendering with xmlattr

Monitoring Recommendations

  • Enable verbose logging for template rendering operations in production environments
  • Monitor CSP violation reports for indicators of XSS exploitation attempts
  • Implement real-time alerting for unusual patterns in rendered HTML output
  • Track dependency versions and receive alerts when vulnerable Jinja versions are detected in your environment

How to Mitigate CVE-2024-22195

Immediate Actions Required

  • Upgrade Jinja to version 3.1.3 or later immediately
  • Audit all Jinja templates for usage of the xmlattr filter with user-controlled input
  • Implement strict Content Security Policy headers as a defense-in-depth measure
  • Review and strengthen input validation for any data that flows into template rendering

Patch Information

Palletsprojects has released Jinja version 3.1.3 which addresses this vulnerability. The fix ensures proper sanitization of attribute names and values when using the xmlattr filter. Users should upgrade to this version or later as soon as possible.

For more information, see the GitHub Release Note for version 3.1.3 and the GitHub Security Advisory.

Linux distribution users should check for updates:

Workarounds

  • Avoid using the xmlattr filter with user-controlled data until the patch can be applied
  • Implement allowlist-based validation for any attributes that must be dynamically rendered
  • Apply strict input sanitization before data reaches template rendering contexts
  • Use alternative rendering approaches that do not rely on the vulnerable xmlattr filter
bash
# Upgrade Jinja to patched version
pip install --upgrade Jinja2>=3.1.3

# Verify installed version
pip show Jinja2 | grep Version

Disclaimer: This content was generated using AI. While we strive for accuracy, please verify critical information with official sources.

Default Legacy - Prefooter | Experience the World’s Most Advanced Cybersecurity Platform

Experience the Most Advanced Cybersecurity Platform

See how the world’s most intelligent, autonomous cybersecurity platform can protect your organization today and into the future.