Skip to content

Commit 22bd174

Browse files
apollo13felixxm
authored andcommitted
[3.1.x] Fixed #30530, CVE-2021-44420 -- Fixed potential bypass of an upstream access control based on URL paths.
Thanks Sjoerd Job Postmus and TengMA(@te3t123) for reports. Backport of d4dcd5b from main.
1 parent cfb780d commit 22bd174

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

django/urls/resolvers.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,11 @@ def __init__(self, regex, name=None, is_endpoint=False):
153153
self.converters = {}
154154

155155
def match(self, path):
156-
match = self.regex.search(path)
156+
match = (
157+
self.regex.fullmatch(path)
158+
if self._is_endpoint and self.regex.pattern.endswith('$')
159+
else self.regex.search(path)
160+
)
157161
if match:
158162
# If there are any named groups, use those as kwargs, ignoring
159163
# non-named groups. Otherwise, pass all non-named arguments as
@@ -240,7 +244,7 @@ def _route_to_regex(route, is_endpoint=False):
240244
converters[parameter] = converter
241245
parts.append('(?P<' + parameter + '>' + converter.regex + ')')
242246
if is_endpoint:
243-
parts.append('$')
247+
parts.append(r'\Z')
244248
return ''.join(parts), converters
245249

246250

docs/releases/2.2.25.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ Django 2.2.25 release notes
66

77
Django 2.2.25 fixes a security issue with severity "low" in 2.2.24.
88

9-
...
9+
CVE-2021-44420: Potential bypass of an upstream access control based on URL paths
10+
=================================================================================
11+
12+
HTTP requests for URLs with trailing newlines could bypass an upstream access
13+
control based on URL paths.

docs/releases/3.1.14.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ Django 3.1.14 release notes
66

77
Django 3.1.14 fixes a security issue with severity "low" in 3.1.13.
88

9-
...
9+
CVE-2021-44420: Potential bypass of an upstream access control based on URL paths
10+
=================================================================================
11+
12+
HTTP requests for URLs with trailing newlines could bypass an upstream access
13+
control based on URL paths.

tests/urlpatterns/tests.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,19 @@ def test_space_in_route(self):
147147
with self.assertRaisesMessage(ImproperlyConfigured, msg):
148148
path('space/<int: num>', empty_view)
149149

150+
def test_path_trailing_newlines(self):
151+
tests = [
152+
'/articles/2003/\n',
153+
'/articles/2010/\n',
154+
'/en/foo/\n',
155+
'/included_urls/extra/\n',
156+
'/regex/1/\n',
157+
'/users/1/\n',
158+
]
159+
for url in tests:
160+
with self.subTest(url=url), self.assertRaises(Resolver404):
161+
resolve(url)
162+
150163

151164
@override_settings(ROOT_URLCONF='urlpatterns.converter_urls')
152165
class ConverterTests(SimpleTestCase):

0 commit comments

Comments
 (0)