Skip to content

Commit 1b612b1

Browse files
committed
feat(ipv4): require numeric prefix in strict CIDR mode
HU-04: Enforce strict CIDR notation in strict mode by requiring the prefix to be a numeric value (0-32) instead of accepting dotted netmask notation like 255.255.255.0. When strict=True with CIDR enabled: - Accepts: 192.168.1.0/24 - Rejects: 192.168.1.0/255.255.255.0 This change maintains backward compatibility: - Non-strict mode continues to accept both formats - All existing tests continue to pass - Only adds stricter validation when explicitly requested
1 parent a77ed6f commit 1b612b1

1 file changed

Lines changed: 4 additions & 0 deletions

File tree

‎src/validators/ip_address.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ def ipv4(
8888
if cidr:
8989
if strict and value.count("/") != 1:
9090
raise ValueError("IPv4 address was expected in CIDR notation")
91+
if strict:
92+
_, prefix = value.split("/")
93+
if not prefix.isdigit() or not (0 <= int(prefix) <= 32):
94+
raise ValueError("CIDR prefix must be a number between 0 and 32")
9195
return IPv4Network(value, strict=not host_bit) and _check_private_ip(value, private)
9296
return IPv4Address(value) and _check_private_ip(value, private)
9397
except (ValueError, AddressValueError, NetmaskValueError):

0 commit comments

Comments
 (0)