diff options
| author | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2021-05-24 09:55:14 +0200 |
|---|---|---|
| committer | Carlton Gibson <carlton.gibson@noumenal.es> | 2021-06-02 10:44:39 +0200 |
| commit | 9f75e2e562fa0c0482f3dde6fc7399a9070b4a3d (patch) | |
| tree | 24b1f55302cfd60a9c321ef1c00d70d42c0acd88 /django/core/validators.py | |
| parent | dfaba12cda060b8b292ae1d271b44bf810b1c5b9 (diff) | |
[3.2.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/core/validators.py')
| -rw-r--r-- | django/core/validators.py | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/django/core/validators.py b/django/core/validators.py index f9abec602c..731ccf2d46 100644 --- a/django/core/validators.py +++ b/django/core/validators.py @@ -66,7 +66,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 @@ -276,6 +276,19 @@ def validate_ipv4_address(value): ipaddress.IPv4Address(value) except ValueError: raise ValidationError(_('Enter a valid IPv4 address.'), code='invalid', params={'value': value}) + 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', + params={'value': value}, + ) def validate_ipv6_address(value): |
