Skip to content

Why 127.0.0.1 valid and how to validate only cidr notation #290

@mertcangokgoz

Description

@mertcangokgoz

I wrote a cidr validation code like below

def ip_address_cidr_validation(value: str) -> bool:
    """Validate IP address in CIDR notation."""
    return bool(
        validators.ipv4(value, cidr=True, strict=True)
        or validators.ipv6(value, cidr=True, strict=True)
    )

while there was no problem in older versions, I saw that the following 3 values are "True" in the latest version of the library.

shouldn't the following three ip's be false? it's not "cidr"

2001:0db8:85a3:0000:0000:8a2e:0370:7334 -> False
127.0.0.1 -> False
1.1.1.1/24 -> False
192.0.2.0/255.255.255.0 -> False

My test case

@pytest.mark.parametrize(
    ("cidr", "status"),
    [
        ("1.1.1.1/32", True),
        ("178.251.45.0/24", True),
        ("2001:0db8:85a3:0000:0000:8a2e:0370:7334/128", True),
        ("192.0.2.0/255.255.255.0", False),
        ("192.168.0.0/32", True),
        ("1.1.1.1/24", True),
        ("127.0.0.1", False),
        ("127.0.0.2.4", False),
        ("2001:0db8:85a3:0000:0000:8a2e:0370:7334", False),
        ("192.168.0. 1", False),
        ("192.168.0.1 /32", False),
    ],
)
def test_ipaddress_cidr_validation_valid(cidr: str, status: bool) -> None:
    """Ensure the function works correctly when CIDR is entered correctly."""
    cidr_validator = ip_address_cidr_validation(cidr)
    assert cidr_validator is status

How can this happen? Am I applying this validator incorrectly?

Metadata

Metadata

Assignees

Labels

bugIssue: Works not as designed

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions