Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,7 @@ answer newbie questions, and generally made Django that much better:
Karderio <karderio@gmail.com>
Karen Tracey <kmtracey@gmail.com>
Karol Sikora <elektrrrus@gmail.com>
Kasey Steinhauer <kstein257@gmail.com>
Kasun Herath <kasunh01@gmail.com>
Katherine “Kati” Michel <kthrnmichel@gmail.com>
Kathryn Killebrew <kathryn.killebrew@gmail.com>
Expand Down
5 changes: 2 additions & 3 deletions django/http/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,12 +642,11 @@ def __init__(
):
super().__init__(*args, **kwargs)
self["Location"] = iri_to_uri(redirect_to)
redirect_to_str = str(redirect_to)
if max_length is not None and len(redirect_to_str) > max_length:
if max_length is not None and len(self["Location"]) > max_length:
raise DisallowedRedirect(
f"Unsafe redirect exceeding {max_length} characters"
)
parsed = urlsplit(redirect_to_str)
parsed = urlsplit(str(redirect_to))
if preserve_request:
self.status_code = self.status_code_preserve_request
if parsed.scheme and parsed.scheme not in self.allowed_schemes:
Expand Down
3 changes: 2 additions & 1 deletion tests/admin_scripts/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2450,7 +2450,8 @@ def test_invalid_choice_db_option(self):
if PY314:
expected_error = (
r"Error: argument --database: invalid choice: 'deflaut', "
r"maybe you meant 'default'\? \(choose from default, other\)"
r"maybe you meant 'default'\? "
r"\(choose from '?default'?, '?other'?\)"
)
else:
expected_error = (
Expand Down
13 changes: 13 additions & 0 deletions tests/httpwrappers/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
parse_cookie,
)
from django.test import SimpleTestCase
from django.utils.encoding import iri_to_uri
from django.utils.functional import lazystr
from django.utils.http import MAX_URL_REDIRECT_LENGTH

Expand Down Expand Up @@ -498,6 +499,18 @@ def test_redirect_url_max_length(self):
response = response_class(long_url)
self.assertEqual(response.url, long_url)

def test_redirect_url_max_length_checks_encoded_location(self):
long_url = "/" + "é" * (MAX_URL_REDIRECT_LENGTH - 1)
self.assertLessEqual(len(long_url), MAX_URL_REDIRECT_LENGTH)
self.assertGreater(len(iri_to_uri(long_url)), MAX_URL_REDIRECT_LENGTH)
for response_class in (HttpResponseRedirect, HttpResponsePermanentRedirect):
msg = f"Unsafe redirect exceeding {MAX_URL_REDIRECT_LENGTH} characters"
with (
self.subTest(response_class=response_class),
self.assertRaisesMessage(DisallowedRedirect, msg),
):
response_class(long_url)

def test_redirect_url_max_length_override_via_param(self):
base_url = "https://example.com/"
for (max_length, length), response_class in itertools.product(
Expand Down
Loading