Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 39e2297

Browse files
committed
Fixed CVE-2025-27556 -- Mitigated potential DoS in url_has_allowed_host_and_scheme() on Windows.
Thank you sw0rd1ight for the report.
1 parent 00c68f0 commit 39e2297

File tree

6 files changed

+44
-4
lines changed

6 files changed

+44
-4
lines changed

django/core/validators.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from django.core.exceptions import ValidationError
88
from django.utils.deconstruct import deconstructible
9+
from django.utils.http import MAX_URL_LENGTH
910
from django.utils.ipv6 import is_valid_ipv6_address
1011
from django.utils.regex_helper import _lazy_re_compile
1112
from django.utils.translation import gettext_lazy as _
@@ -152,7 +153,7 @@ class URLValidator(RegexValidator):
152153
message = _("Enter a valid URL.")
153154
schemes = ["http", "https", "ftp", "ftps"]
154155
unsafe_chars = frozenset("\t\r\n")
155-
max_length = 2048
156+
max_length = MAX_URL_LENGTH
156157

157158
def __init__(self, schemes=None, **kwargs):
158159
super().__init__(**kwargs)

django/utils/html.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from django.core.validators import EmailValidator
1414
from django.utils.deprecation import RemovedInDjango70Warning
1515
from django.utils.functional import Promise, cached_property, keep_lazy, keep_lazy_text
16-
from django.utils.http import RFC3986_GENDELIMS, RFC3986_SUBDELIMS
16+
from django.utils.http import MAX_URL_LENGTH, RFC3986_GENDELIMS, RFC3986_SUBDELIMS
1717
from django.utils.regex_helper import _lazy_re_compile
1818
from django.utils.safestring import SafeData, SafeString, mark_safe
1919
from django.utils.text import normalize_newlines
@@ -41,7 +41,6 @@
4141
)
4242
)
4343

44-
MAX_URL_LENGTH = 2048
4544
MAX_STRIP_TAGS_DEPTH = 50
4645

4746

django/utils/http.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
RFC3986_GENDELIMS = ":/?#[]@"
4141
RFC3986_SUBDELIMS = "!$&'()*+,;="
42+
MAX_URL_LENGTH = 2048
4243

4344

4445
def urlencode(query, doseq=False):
@@ -274,7 +275,10 @@ def url_has_allowed_host_and_scheme(url, allowed_hosts, require_https=False):
274275
def _url_has_allowed_host_and_scheme(url, allowed_hosts, require_https=False):
275276
# Chrome considers any URL with more than two slashes to be absolute, but
276277
# urlsplit is not so flexible. Treat any url with three slashes as unsafe.
277-
if url.startswith("///"):
278+
if url.startswith("///") or len(url) > MAX_URL_LENGTH:
279+
# urlsplit does not perform validation of inputs. Unicode normalization
280+
# is very slow on Windows and can be a DoS attack vector.
281+
# https://docs.python.org/3/library/urllib.parse.html#url-parsing-security
278282
return False
279283
try:
280284
url_info = urlsplit(url)

docs/releases/5.0.14.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,13 @@ Django 5.0.14 release notes
55
*April 2, 2025*
66

77
Django 5.0.14 fixes a security issue with severity "moderate" in 5.0.13.
8+
9+
CVE-2025-27556: Potential denial-of-service vulnerability in ``LoginView``, ``LogoutView``, and ``set_language()`` on Windows
10+
=============================================================================================================================
11+
12+
Python's :func:`NFKC normalization <python:unicodedata.normalize>` is slow on
13+
Windows. As a consequence, :class:`~django.contrib.auth.views.LoginView`,
14+
:class:`~django.contrib.auth.views.LogoutView`, and
15+
:func:`~django.views.i18n.set_language` were subject to a potential
16+
denial-of-service attack via certain inputs with a very large number of Unicode
17+
characters.

docs/releases/5.1.8.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ Django 5.1.8 release notes
77
Django 5.1.8 fixes a security issue with severity "moderate" and several bugs
88
in 5.1.7.
99

10+
CVE-2025-27556: Potential denial-of-service vulnerability in ``LoginView``, ``LogoutView``, and ``set_language()`` on Windows
11+
=============================================================================================================================
12+
13+
Python's :func:`NFKC normalization <python:unicodedata.normalize>` is slow on
14+
Windows. As a consequence, :class:`~django.contrib.auth.views.LoginView`,
15+
:class:`~django.contrib.auth.views.LogoutView`, and
16+
:func:`~django.views.i18n.set_language` were subject to a potential
17+
denial-of-service attack via certain inputs with a very large number of Unicode
18+
characters.
19+
1020
Bugfixes
1121
========
1222

tests/utils_tests/test_http.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from django.utils.datastructures import MultiValueDict
88
from django.utils.http import (
99
MAX_HEADER_LENGTH,
10+
MAX_URL_LENGTH,
1011
base36_to_int,
1112
content_disposition_header,
1213
escape_leading_slashes,
@@ -274,6 +275,21 @@ def test_secure_param_non_https_urls(self):
274275
False,
275276
)
276277

278+
def test_max_url_length(self):
279+
allowed_host = "example.com"
280+
max_extra_characters = "é" * (MAX_URL_LENGTH - len(allowed_host) - 1)
281+
max_length_boundary_url = f"{allowed_host}/{max_extra_characters}"
282+
cases = [
283+
(max_length_boundary_url, True),
284+
(max_length_boundary_url + "ú", False),
285+
]
286+
for url, expected in cases:
287+
with self.subTest(url=url):
288+
self.assertIs(
289+
url_has_allowed_host_and_scheme(url, allowed_hosts={allowed_host}),
290+
expected,
291+
)
292+
277293

278294
class URLSafeBase64Tests(unittest.TestCase):
279295
def test_roundtrip(self):

0 commit comments

Comments
 (0)