summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorMariusz Felisiak <felisiak.mariusz@gmail.com>2021-05-24 09:55:14 +0200
committerCarlton Gibson <carlton.gibson@noumenal.es>2021-06-02 10:38:07 +0200
commit203d4ab9ebcd72fc4d6eb7398e66ed9e474e118e (patch)
tree90cadfe142a850b3f35c2505cdc3a9055ccff96e /django
parent20c67a0693c4ede2b09af02574823485e82e4c8f (diff)
[3.1.x] Fixed CVE-2021-33571 -- Prevented leading zeros in IPv4 addresses.
validate_ipv4_address() was affected only on Python < 3.9.5, see [1]. URLValidator() uses a regular expressions and it was affected on all Python versions. [1] https://bugs.python.org/issue36384
Diffstat (limited to 'django')
-rw-r--r--django/core/validators.py14
1 files changed, 13 insertions, 1 deletions
diff --git a/django/core/validators.py b/django/core/validators.py
index 900a2b558e..1d0e6a9470 100644
--- a/django/core/validators.py
+++ b/django/core/validators.py
@@ -64,7 +64,7 @@ class URLValidator(RegexValidator):
ul = '\u00a1-\uffff' # Unicode letters range (must not be a raw string).
# IP patterns
- ipv4_re = r'(?:25[0-5]|2[0-4]\d|[0-1]?\d?\d)(?:\.(?:25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}'
+ ipv4_re = r'(?:0|25[0-5]|2[0-4]\d|1\d?\d?|[1-9]\d?)(?:\.(?:0|25[0-5]|2[0-4]\d|1\d?\d?|[1-9]\d?)){3}'
ipv6_re = r'\[[0-9a-f:.]+\]' # (simple regex, validated later)
# Host patterns
@@ -247,6 +247,18 @@ def validate_ipv4_address(value):
ipaddress.IPv4Address(value)
except ValueError:
raise ValidationError(_('Enter a valid IPv4 address.'), code='invalid')
+ else:
+ # Leading zeros are forbidden to avoid ambiguity with the octal
+ # notation. This restriction is included in Python 3.9.5+.
+ # TODO: Remove when dropping support for PY39.
+ if any(
+ octet != '0' and octet[0] == '0'
+ for octet in value.split('.')
+ ):
+ raise ValidationError(
+ _('Enter a valid IPv4 address.'),
+ code='invalid',
+ )
def validate_ipv6_address(value):