python-validators/validators

validators.ip_address private kwarg does not work as intended

Closed this issue · 5 comments

Python: 3.12
Valdiators: 0.28.1

validators.ip_address does not properly handle the scenario when private=False

Result:

❯ python3.11
iPython 3.11.7 (main, Dec  4 2023, 18:10:11) [Clang 15.0.0 (clang-1500.1.0.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import validators
>>> a = "1.1.1.1"
>>> validators.ipv4(a)
True
>>> validators.ipv4(a, private=True)
ValidationError(func=ipv4, args={'value': '1.1.1.1', 'private': True})
>>> validators.ipv4(a, private=False)
ValidationError(func=ipv4, args={'value': '1.1.1.1', 'private': False})

Expected Result:

❯ python3.11
iPython 3.11.7 (main, Dec  4 2023, 18:10:11) [Clang 15.0.0 (clang-1500.1.0.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import validators
>>> a = "1.1.1.1"
>>> validators.ipv4(a)
True
>>> validators.ipv4(a, private=True)
ValidationError(func=ipv4, args={'value': '1.1.1.1', 'private': True})
>>> validators.ipv4(a, private=False)
True

I also noticed there are no unit tests that cover the above scenarios.

PR Open to address the above, including unit tests.

Hi, I do not understand. 1.1.1.1 does not fall into any of these categories.

"10.",  # private
"192.168.",  # private
"169.254.",  # link-local
"127.",  # localhost
"0.0.0.0",  # loopback

See:

Sure, some modifications are needed in the ranges, but ...

How is 1.1.1.1 a private address?

If I specify private=False we want validators to check to see if the IP address given is not private

If you read my original issue description under the "expected" outcome you will see that reflected there.

Expected Result:


❯ python3.11
iPython 3.11.7 (main, Dec  4 2023, 18:10:11) [Clang 15.0.0 (clang-1500.1.0.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import validators
>>> a = "1.1.1.1"
>>> validators.ipv4(a)
True
>>> validators.ipv4(a, private=True)
ValidationError(func=ipv4, args={'value': '1.1.1.1', 'private': True})
>>> validators.ipv4(a, private=False)
True

Specifically

>>> a = "1.1.1.1"
>>> validators.ipv4(a, private=False)
True

Right now there is only a way to check if an IP is private

Furthermore, the current functionality of private=False does nothing.

Ah, I see the problem now. Thanks!