Skip to content

Commit 428656f

Browse files
authored
Keep 404 legacy links on the legacy host (#3018)
1 parent 18732c9 commit 428656f

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

apps/cms/tests.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ class Test404(TestCase):
8181
def test_legacy_path(self):
8282
self.assertEqual(legacy_path("/any/thing"), "http://legacy.python.org/any/thing")
8383

84+
def test_legacy_path_without_leading_slash(self):
85+
self.assertEqual(legacy_path("any/thing"), "http://legacy.python.org/any/thing")
86+
87+
def test_legacy_path_with_encoded_slash(self):
88+
self.assertEqual(legacy_path("/%2Fevil.test/x"), "http://legacy.python.org/%2Fevil.test/x")
89+
90+
def test_legacy_path_with_decoded_encoded_slash(self):
91+
self.assertEqual(legacy_path("//evil.test/x"), "http://legacy.python.org//evil.test/x")
92+
8493
def test_custom_404(self):
8594
"""Ensure custom 404 is set to 5 minutes"""
8695
response = self.client.get("/foo-bar/baz/9876")

apps/cms/views.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Views for the cms app, including custom error handlers."""
22

3-
from urllib.parse import urljoin
3+
from urllib.parse import quote
44

55
from django.shortcuts import render
66
from django.urls import reverse
@@ -11,7 +11,9 @@
1111

1212
def legacy_path(path):
1313
"""Build a path to the same path under the legacy.python.org domain."""
14-
return urljoin(LEGACY_PYTHON_DOMAIN, path)
14+
if not path.startswith("/"):
15+
path = f"/{path}"
16+
return f"{LEGACY_PYTHON_DOMAIN}{quote(path, safe='/%')}"
1517

1618

1719
def custom_404(request, exception, template_name="404.html"):

0 commit comments

Comments
 (0)