{"id":1475,"cve":"CVE-2025-64458","cve_url":"https:\/\/www.cvedetails.com\/cve\/CVE-2025-64458\/","cwe":"CWE-407","cwe_number":407,"cwe_url":"https:\/\/cwe.mitre.org\/data\/definitions\/407","cvss_score":7.5,"published":"2025-11-05","owasp":"A04 Insecure Design","odc":"Algorithm","cdc":"Incorrect Algorithm","category":"Resource Management","subcategory":"Resource Exhaustion","scope":"Remote","impact":"Denial of Service (DoS)","summary":"Denial-of-service in Django redirects on Windows via slow Unicode input.","permalink":"https:\/\/netpack.pt\/vaitp\/vulnerability\/CVE-2025-64458\/","description":"An issue was discovered in 5.1 before 5.1.14, 4.2 before 4.2.26, and 5.2 before 5.2.8. NFKC normalization in Python is slow on Windows. As a consequence, `django.http.HttpResponseRedirect`, `django.http.HttpResponsePermanentRedirect`, and the shortcut `django.shortcuts.redirect`  were subject to a potential  denial-of-service attack via certain inputs with a very large number of Unicode characters. Earlier, unsupported Django series (such as 5.0.x, 4.1.x, and 3.2.x) were not evaluated and may also be affected. Django would like to thank Seokchan Yoon for reporting this issue.","solution":"Upgrade to Django 5.2.8, 5.1.14, or 4.2.26.","affected_component":"Django","fixed_by_upgrading":true,"vulnerable_code":"# This code demonstrates the vulnerability described in CVE-2025-64458\n# by simulating the vulnerable components of an older Django version.\n# The vulnerability lies in a slow validation step within the redirect process\n# that can be exploited for a Denial-of-Service attack.\n\nimport sys\nimport time\nfrom urllib.parse import urlsplit\n\n# On Windows, the underlying Python implementation of unicodedata.normalize('NFKC', ...)\n# can have quadratic complexity for certain inputs, which is used by urlsplit for\n# Internationalized Domain Names (IDNA) processing. This script may not demonstrate\n# the severe slowdown on non-Windows platforms.\n\n# --- Simplified representation of vulnerable Django framework components ---\n\ndef iri_to_uri(iri):\n    \"\"\"A placeholder for Django's iri_to_uri function.\"\"\"\n    # The actual implementation is more complex, but this is sufficient for the demo.\n    return str(iri)\n\ndef url_has_allowed_scheme_and_netloc(url, allowed_schemes):\n    \"\"\"\n    Simplified representation of the Django utility function from vulnerable versions.\n    This function was called inside HttpResponseRedirect.__init__ and was the\n    source of the vulnerability.\n    \"\"\"\n    if not url:\n        return False\n    try:\n        # VULNERABLE STEP: urlsplit() is called on the user-provided URL.\n        # For a URL containing a hostname with a large number of specific\n        # Unicode characters (like U+0130), this call can be extremely slow\n        # on certain platforms (notably Windows) due to NFKC normalization.\n        url_info = urlsplit(url)\n        # The original function performed more checks here, but they are\n        # not relevant to demonstrating the performance issue.\n        return True\n    except ValueError:\n        # The input URL was malformed.\n        return False\n\nclass HttpResponse:\n    \"\"\"Simplified base class for an HTTP response.\"\"\"\n    def __init__(self, *args, **kwargs):\n        self.headers = {}\n        self.status_code = 200\n\n    def __setitem__(self, header, value):\n        self.headers[header] = value\n\nclass HttpResponseRedirectBase(HttpResponse):\n    \"\"\"\n    Simplified representation of the HttpResponseRedirectBase class as it\n    existed in vulnerable Django versions (e.g., 5.1 before 5.1.14).\n    \"\"\"\n    allowed_schemes = [\"http\", \"https\", \"ftp\"]\n\n    def __init__(self, redirect_to, *args, **kwargs):\n        super().__init__(*args, **kwargs)\n        self[\"Location\"] = iri_to_uri(redirect_to)\n\n        # THE VULNERABLE CALL:\n        # In vulnerable versions, the redirect URL was validated using this function.\n        # The fix for the CVE removed this call from the __init__ method.\n        if not url_has_allowed_scheme_and_netloc(redirect_to, self.allowed_schemes):\n            # In a real app, this would raise a DisallowedRedirect exception.\n            # We'll just print a message for this demonstration.\n            print(\"Warning: Disallowed redirect scheme or netloc.\", file=sys.stderr)\n\n\nclass HttpResponseRedirect(HttpResponseRedirectBase):\n    \"\"\"Simplified representation of the final redirect class.\"\"\"\n    status_code = 302\n\ndef redirect(to):\n    \"\"\"\n    Simplified representation of the django.shortcuts.redirect shortcut,\n    which is the common entry point for this vulnerability.\n    \"\"\"\n    return HttpResponseRedirect(to)\n\n\n# --- Demonstration of the Denial-of-Service attack ---\n\ndef vulnerable_view(request_url):\n    \"\"\"\n    Simulates a Django view that performs a redirect based on user input,\n    e.g., redirecting after login using a 'next' query parameter.\n    \"\"\"\n    print(f\"View received request to redirect to: {request_url[:70]}...\")\n    start_time = time.time()\n    # This call initiates the vulnerable chain: redirect() -> HttpResponseRedirect()\n    # -> HttpResponseRedirectBase.__init__() -> url_has_allowed_scheme_and_netloc() -> urlsplit()\n    response = redirect(request_url)\n    end_time = time.time()\n    duration = end_time - start_time\n    print(f\"Redirect response generated in {duration:.4f} seconds.\")\n    if duration > 2.0:\n        print(\"\\n[!] The request took an unusually long time to process.\")\n        print(\"[!] This demonstrates the Denial-of-Service vulnerability.\")\n    return response\n\nif __name__ == \"__main__\":\n    # The payload uses a character (U+0130) known to cause performance issues\n    # in NFKC normalization on some platforms.\n    malicious_character = \"\\u0130\"\n\n    # A large number of these characters are used to amplify the effect.\n    # On a vulnerable system (Python on Windows), even 20,000 can cause\n    # a significant delay (many seconds or minutes).\n    payload_size = 30000\n    host_payload = malicious_character * payload_size\n\n    # The attacker crafts a URL where the slow-to-process payload is in the\n    # hostname part, as this is what urlsplit will attempt to normalize.\n    attack_url = f\"http:\/\/{host_payload}.example.com\/\"\n\n    print(\"--- Demonstrating CVE-2025-64458 (pre-patch) ---\")\n    print(f\"Platform: {sys.platform}\")\n    print(f\"Using a payload of {payload_size} instances of character '\\\\u0130'.\")\n    print(\"-\" * 50)\n\n    vulnerable_view(attack_url)","patched_code":"import unicodedata\nfrom urllib.parse import quote, urlsplit, urlunsplit\n\n# This code is a hypothetical representation of the fix for CVE-2025-64458,\n# as the CVE number itself is not real. The logic demonstrates a plausible\n# fix based on the vulnerability's description.\n\n# The vulnerability is that NFKC normalization is slow on very long strings,\n# leading to a Denial-of-Service. The fix is to limit the input length\n# before performing the expensive normalization. A common URL length limit\n# is ~2048 characters.\nURL_MAX_LENGTH = 2048\n\n\ndef _patched_iri_to_uri(iri):\n    \"\"\"\n    Represents the fixed version of a utility function that converts an\n    IRI (Internationalized Resource Identifier) to a URI (Uniform\n    Resource Identifier).\n\n    The fix for CVE-2025-64458 involves preventing the expensive NFKC\n    normalization on excessively long strings by enforcing a length limit.\n    \"\"\"\n    if iri is None:\n        return iri\n\n    # --- THE FIX ---\n    # Check the length of the input before performing any costly operations.\n    # If the URL is too long, raise an error immediately instead of attempting\n    # to process it, which prevents the DoS attack.\n    if len(iri) > URL_MAX_LENGTH:\n        raise ValueError(\"The length of the URL exceeds the allowed limit.\")\n    # --- END OF FIX ---\n\n    # The original, potentially slow operation now only runs on inputs of a\n    # sanitized, reasonable length. This code simulates the logic found in\n    # Django's django.utils.encoding.iri_to_uri.\n    scheme, netloc, path, query, fragment = urlsplit(iri)\n\n    # Django's implementation is more complex, but the key is that NFKC\n    # normalization happens on parts of the URL.\n    if path:\n        # The expensive operation that causes the DoS on large inputs.\n        path = quote(unicodedata.normalize(\"NFKC\", path).encode(\"utf-8\"), safe=\"\/%\")\n    if query:\n        query = quote(unicodedata.normalize(\"NFKC\", query).encode(\"utf-8\"), safe=\"=&%?\")\n\n    # Reassemble the URI\n    return urlunsplit((scheme, netloc, path, query, fragment))\n\n\nclass HttpResponseBase:\n    \"\"\"A mock base class to simulate Django's HttpResponse.\"\"\"\n    def __init__(self, content_type=None, status=200, reason=None, charset=None):\n        self.headers = {}\n        self.status_code = status\n\n    def __setitem__(self, header, value):\n        self.headers[header] = value\n\n\nclass PatchedHttpResponseRedirect(HttpResponseBase):\n    \"\"\"\n    A mock of Django's HttpResponseRedirect demonstrating the patched behavior.\n    \"\"\"\n    status_code = 302\n\n    def __init__(self, redirect_to, *args, **kwargs):\n        super().__init__(*args, **kwargs)\n        # The 'redirect_to' URL is now sanitized by the patched function\n        # before being set as the 'Location' header.\n        self[\"Location\"] = _patched_iri_to_uri(redirect_to)\n\n\ndef patched_redirect(to, *args, **kwargs):\n    \"\"\"\n    A mock of Django's redirect shortcut using the patched redirect class.\n    \"\"\"\n    return PatchedHttpResponseRedirect(to, *args, **kwargs)\n\n\n# --- Demonstration of the fix ---\n\n# 1. A normal, valid URL works as expected.\nsafe_url = \"https:\/\/example.com\/path\/\u2713\"\ntry:\n    response = patched_redirect(safe_url)\n    # print(f\"Successfully processed safe URL. Location: {response.headers['Location']}\")\nexcept ValueError as e:\n    # print(f\"Error processing safe URL: {e}\")\n    pass\n\n\n# 2. An excessively long URL that would have caused a DoS.\n# Create a string with a large number of Unicode characters.\n# The '\u2460' character is one that requires NFKC normalization.\nmaliciously_long_url = \"https:\/\/example.com\/\" + \"\u2460\" * (URL_MAX_LENGTH + 1)\n\ntry:\n    # This call will now fail fast due to the length check in _patched_iri_to_uri.\n    response = patched_redirect(maliciously_long_url)\nexcept ValueError as e:\n    # The fix correctly raises a ValueError, preventing the DoS.\n    # print(f\"Correctly blocked malicious URL with error: {e}\")\n    pass","payload_needed":true,"payload":"```python\n'\\u1158' * 100000\n```","payload_status":"available","published_fix":[{"repository":"django\/django","commit_url":"https:\/\/github.com\/django\/django\/commit\/3790593781d26168e7306b5b2f8ea0309de16242","commit_subject":"[5.1.x] Fixed CVE-2025-64458 -- Mitigated potential DoS in HttpResponseRedirect\/HttpResponsePermanentRedirect on Windows.","units":[{"file":"django\/http\/response.py","function":null},{"file":"django\/http\/response.py","function":"HttpResponseRedirectBase.__init__"}]},{"repository":"django\/django","commit_url":"https:\/\/github.com\/django\/django\/commit\/4f5d904b63751dea9ffc3b0e046404a7fa5881ac","commit_subject":"[5.2.x] Fixed CVE-2025-64458 -- Mitigated potential DoS in HttpResponseRedirect\/HttpResponsePermanentRedirect on Windows.","units":[{"file":"django\/http\/response.py","function":null},{"file":"django\/http\/response.py","function":"HttpResponseRedirectBase.__init__"}]},{"repository":"django\/django","commit_url":"https:\/\/github.com\/django\/django\/commit\/770eea38d7a0e9ba9455140b5a9a9e33618226a7","commit_subject":"[4.2.x] Fixed CVE-2025-64458 -- Mitigated potential DoS in HttpResponseRedirect\/HttpResponsePermanentRedirect on Windows.","units":[{"file":"django\/utils\/html.py","function":null},{"file":"django\/utils\/http.py","function":null}]}],"references":null}