summaryrefslogtreecommitdiff
path: root/django/core/validators.py
diff options
context:
space:
mode:
Diffstat (limited to 'django/core/validators.py')
-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 d32b54f68c..2da0688e28 100644
--- a/django/core/validators.py
+++ b/django/core/validators.py
@@ -75,7 +75,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
@@ -256,6 +256,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):