Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IPv6 regex is also broken #28

Closed
hirusha-adi opened this issue Oct 24, 2024 · 1 comment
Closed

IPv6 regex is also broken #28

hirusha-adi opened this issue Oct 24, 2024 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@hirusha-adi
Copy link
Owner

import re
import unittest

def is_ipaddr(input_str: str) -> bool:
    """
    Check if input string is a valid IP address (both IPv4 and IPv6).
    Based on:
        IPv4: https://stackoverflow.com/questions/5284147/validating-ipv4-addresses-with-regexp
        IPv6: https://stackoverflow.com/questions/53497/regular-expression-that-matches-valid-ipv6-addresses

    Args:
        input_str (str): The string to check.

    Returns:
        bool: 
            True if the string is a valid IP address, 
            False if it is not.
    """

    ipv4 = r'^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$'
    ipv6 = r'(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))'
    return (re.match(ipv4, input_str) or re.match(ipv6, input_str)) is not None

class TestIsIPAddr(unittest.TestCase):

    def test_ipv6(self):
        self.assertTrue(is_ipaddr('::1'))
        self.assertTrue(is_ipaddr('2001:0db8:85a3:0000:0000:8a2e:0370:7334'))
        self.assertTrue(is_ipaddr('::'))
        self.assertFalse(is_ipaddr(':'))
        self.assertFalse(is_ipaddr('::1:'))

if __name__ == '__main__':
    unittest.main()

Result:

.F
======================================================================
FAIL: test_ipv6 (__main__.TestIsIPAddr.test_ipv6)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/mnt/B84CC2894CC241BC/development/hirusha-adi/Near/tmp.py", line 41, in test_ipv6
    self.assertFalse(is_ipaddr('::1:'))
AssertionError: True is not false

----------------------------------------------------------------------
Ran 2 tests in 0.002s

FAILED (failures=1)

Consider using socket or ipaddress modules in python

@hirusha-adi hirusha-adi added the bug Something isn't working label Oct 24, 2024
@hirusha-adi hirusha-adi self-assigned this Oct 24, 2024
hirusha-adi added a commit that referenced this issue Oct 24, 2024
@hirusha-adi
Copy link
Owner Author

Fixed in commit: 7bc0f89

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant