From 22394bd3a18a7d9a8957a0b431f8ae4e5ca03a8c Mon Sep 17 00:00:00 2001 From: Hasan Ramezani Date: Tue, 20 Aug 2019 11:53:10 +0200 Subject: [PATCH] Fixed #29667 -- Prohibited whitespaces in path() URLs. --- django/urls/resolvers.py | 3 +++ tests/urlpatterns/tests.py | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/django/urls/resolvers.py b/django/urls/resolvers.py index 2154a46320..2ff8b2c775 100644 --- a/django/urls/resolvers.py +++ b/django/urls/resolvers.py @@ -8,6 +8,7 @@ attributes of the resolved URL match. import functools import inspect import re +import string from importlib import import_module from urllib.parse import quote @@ -206,6 +207,8 @@ def _route_to_regex(route, is_endpoint=False): For example, 'foo/' returns '^foo\\/(?P[0-9]+)' and {'pk': }. """ + if not set(route).isdisjoint(string.whitespace): + raise ImproperlyConfigured("URL route '%s' cannot contain whitespace." % route) original_route = route parts = ['^'] converters = {} diff --git a/tests/urlpatterns/tests.py b/tests/urlpatterns/tests.py index 3c41104506..92c4e6399e 100644 --- a/tests/urlpatterns/tests.py +++ b/tests/urlpatterns/tests.py @@ -130,6 +130,11 @@ class SimplifiedURLTests(SimpleTestCase): with self.assertRaisesMessage(ImproperlyConfigured, msg): path('foo//', empty_view) + def test_space_in_route(self): + msg = "URL route 'space/' cannot contain whitespace." + with self.assertRaisesMessage(ImproperlyConfigured, msg): + path('space/', empty_view) + @override_settings(ROOT_URLCONF='urlpatterns.converter_urls') class ConverterTests(SimpleTestCase):