From ae49b4d994656bc037513dcd064cb9ce5bb85649 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Fri, 12 Jun 2015 13:49:31 -0400 Subject: [1.7.x] Prevented newlines from being accepted in some validators. This is a security fix; disclosure to follow shortly. Thanks to Sjoerd Job Postmus for the report and draft patch. --- django/core/validators.py | 27 +++++++++++++++------------ docs/releases/1.4.21.txt | 26 ++++++++++++++++++++++++++ docs/releases/1.7.9.txt | 28 ++++++++++++++++++++++++++++ tests/validators/tests.py | 15 ++++++++++++++- 4 files changed, 83 insertions(+), 13 deletions(-) diff --git a/django/core/validators.py b/django/core/validators.py index 1e599ec765..462e3102e9 100644 --- a/django/core/validators.py +++ b/django/core/validators.py @@ -73,7 +73,7 @@ class URLValidator(RegexValidator): r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|' # ...or ipv4 r'\[?[A-F0-9]*:[A-F0-9:]+\]?)' # ...or ipv6 r'(?::\d+)?' # optional port - r'(?:/?|[/?]\S+)$', re.IGNORECASE) + r'(?:/?|[/?]\S+)\Z', re.IGNORECASE) message = _('Enter a valid URL.') schemes = ['http', 'https', 'ftp', 'ftps'] @@ -107,12 +107,15 @@ class URLValidator(RegexValidator): else: url = value +integer_validator = RegexValidator( + re.compile('^-?\d+\Z'), + message=_('Enter a valid integer.'), + code='invalid', +) + def validate_integer(value): - try: - int(value) - except (ValueError, TypeError): - raise ValidationError(_('Enter a valid integer.'), code='invalid') + return integer_validator(value) @deconstructible @@ -120,15 +123,15 @@ class EmailValidator(object): message = _('Enter a valid email address.') code = 'invalid' user_regex = re.compile( - r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*$" # dot-atom - r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-\011\013\014\016-\177])*"$)', # quoted-string + r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*\Z" # dot-atom + r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-\011\013\014\016-\177])*"\Z)', # quoted-string re.IGNORECASE) domain_regex = re.compile( - r'(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}|[A-Z0-9-]{2,}(?