티스토리 수익 글 보기

티스토리 수익 글 보기

[1.8.x] Fixed catastrophic backtracking in URLValidator. · django/django@8f9a4d3 · GitHub
Skip to content

Commit 8f9a4d3

Browse files
shaibtimgraham
authored andcommitted
[1.8.x] Fixed catastrophic backtracking in URLValidator.
Thanks João Silva for reporting the problem and Tim Graham for finding the problematic RE and for review. This is a security fix; disclosure to follow shortly.
1 parent 574dd5e commit 8f9a4d3

File tree

5 files changed

+14
1
lines changed

5 files changed

+14
1
lines changed

django/core/validators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class URLValidator(RegexValidator):
7373

7474
# Host patterns
7575
hostname_re = r'[a-z' + ul + r'0-9](?:[a-z' + ul + r'0-9-]*[a-z' + ul + r'0-9])?'
76-
domain_re = r'(?:\.[a-z' + ul + r'0-9]+(?:[a-z' + ul + r'0-9-]*[a-z' + ul + r'0-9]+)*)*'
76+
domain_re = r'(?:\.(?!-)[a-z' + ul + r'0-9-]*(?<!-))*'
7777
tld_re = r'\.(?:[a-z' + ul + r']{2,}|xn--[a-z0-9]+)\.?'
7878
host_re = '(' + hostname_re + domain_re + tld_re + '|localhost)'
7979

docs/releases/1.8.3.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ The undocumented, internally unused ``validate_integer()`` function is now
6060
stricter as it validates using a regular expression instead of simply casting
6161
the value using ``int()`` and checking if an exception was raised.
6262

63+
Denial-of-service possibility in URL validation
64+
===============================================
65+
66+
:class:`~django.core.validators.URLValidator` included a regular expression
67+
that was extremely slow to evaluate against certain invalid inputs. This regular
68+
expression has been simplified and optimized.
69+
6370
Bugfixes
6471
========
6572

tests/validators/invalid_urls.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ http://foo.bar/foo(bar)baz quux
3535
http://-error-.invalid/
3636
http://-a.b.co
3737
http://a.b-.co
38+
http://a.-b.co
39+
http://a.b-.c.co
3840
http:/
3941
http://
4042
http://

tests/validators/tests.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@
172172
# Trailing newlines not accepted
173173
(URLValidator(), 'http://www.djangoproject.com/\n', ValidationError),
174174
(URLValidator(), 'http://[::ffff:192.9.5.5]\n', ValidationError),
175+
# Trailing junk does not take forever to reject
176+
(URLValidator(), 'http://www.asdasdasdasdsadfm.com.br ', ValidationError),
177+
(URLValidator(), 'http://www.asdasdasdasdsadfm.com.br z', ValidationError),
175178

176179
(BaseValidator(True), True, None),
177180
(BaseValidator(True), False, ValidationError),

tests/validators/valid_urls.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ http://www.example.com/
77
http://www.example.com:8000/test
88
http://valid-with-hyphens.com/
99
http://subdomain.example.com/
10+
http://a.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
1011
http://200.8.9.10/
1112
http://200.8.9.10:8000/test
1213
http://su--b.valid-----hyphens.com/

0 commit comments

Comments
 (0)