python-validators / validators

Python Data Validation for Humans™.
MIT License
958 stars 152 forks source link

validators.ip_address private kwarg does not work as intended #373

Closed grleblanc closed 4 months ago

grleblanc commented 4 months ago

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.

grleblanc commented 4 months ago

PR Open to address the above, including unit tests.

yozachar commented 4 months ago

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?

grleblanc commented 4 months ago

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

grleblanc commented 4 months ago

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

yozachar commented 4 months ago

Ah, I see the problem now. Thanks!