Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e3e992e

Browse files
committed
[1.1.X] SECURITY ALERT: Corrected regular expressions for URL and email fields.
Certain email addresses/URLs could trigger a catastrophic backtracking situation, causing 100% CPU and server overload. If deliberately triggered, this could be the basis of a denial-of-service attack. This security vulnerability was disclosed in public, so we're skipping our normal security release process to get the fix out as soon as possible. This is a security related update. A full announcement will follow. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@11604 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 17173ac commit e3e992e

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

django/forms/fields.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ def clean(self, value):
421421
email_re = re.compile(
422422
r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*" # dot-atom
423423
r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-011\013\014\016-\177])*"' # quoted-string
424-
r')@(?:[A-Z0-9]+(?:-*[A-Z0-9]+)*\.)+[A-Z]{2,6}$', re.IGNORECASE) # domain
424+
r')@(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?$', re.IGNORECASE) # domain
425425

426426
class EmailField(RegexField):
427427
default_error_messages = {
@@ -532,7 +532,7 @@ def clean(self, data, initial=None):
532532

533533
url_re = re.compile(
534534
r'^https?://' # http:// or https://
535-
r'(?:(?:[A-Z0-9]+(?:-*[A-Z0-9]+)*\.)+[A-Z]{2,6}|' #domain...
535+
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?|' #domain...
536536
r'localhost|' #localhost...
537537
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
538538
r'(?::\d+)?' # optional port

tests/regressiontests/forms/fields.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,13 @@
767767
>>> f.clean('[email protected]')
768768
769769
770+
# Check for runaway regex security problem. This will take for-freeking-ever
771+
# if the security fix isn't in place.
772+
>>> f.clean('[email protected]')
773+
Traceback (most recent call last):
774+
...
775+
ValidationError: [u'Enter a valid e-mail address.']
776+
770777
>>> f = EmailField(required=False)
771778
>>> f.clean('')
772779
u''
@@ -972,6 +979,32 @@
972979
Traceback (most recent call last):
973980
...
974981
ValidationError: [u'Enter a valid URL.']
982+
>>> f.clean('.')
983+
Traceback (most recent call last):
984+
...
985+
ValidationError: [u'Enter a valid URL.']
986+
>>> f.clean('com.')
987+
Traceback (most recent call last):
988+
...
989+
ValidationError: [u'Enter a valid URL.']
990+
>>> f.clean('http://example.com.')
991+
u'http://example.com./'
992+
>>> f.clean('example.com.')
993+
u'http://example.com./'
994+
995+
# hangs "forever" if catastrophic backtracking in ticket:#11198 not fixed
996+
>>> f.clean('http://%s' % ("X"*200,))
997+
Traceback (most recent call last):
998+
...
999+
ValidationError: [u'Enter a valid URL.']
1000+
1001+
# a second test, to make sure the problem is really addressed, even on
1002+
# domains that don't fail the domain label length check in the regex
1003+
>>> f.clean('http://%s' % ("X"*60,))
1004+
Traceback (most recent call last):
1005+
...
1006+
ValidationError: [u'Enter a valid URL.']
1007+
9751008
>>> f.clean('http://.com')
9761009
Traceback (most recent call last):
9771010
...

0 commit comments

Comments
 (0)