diff --git a/tests/main_test.py b/tests/main_test.py index 347711c3..db63f04c 100644 --- a/tests/main_test.py +++ b/tests/main_test.py @@ -136,16 +136,22 @@ def test_ip(): @pytest.mark.skipif(not inet_pton, reason="inet_pton unavailable") def test_looks_like_ip_with_inet_pton(): assert looks_like_ip("1.1.1.1", inet_pton) is True + assert looks_like_ip("a.1.1.1", inet_pton) is False + assert looks_like_ip("1.1.1.1\n", inet_pton) is False assert looks_like_ip("256.256.256.256", inet_pton) is False def test_looks_like_ip_without_inet_pton(): assert looks_like_ip("1.1.1.1", None) is True + assert looks_like_ip("a.1.1.1", None) is False + assert looks_like_ip("1.1.1.1\n", None) is False assert looks_like_ip("256.256.256.256", None) is False def test_similar_to_ip(): assert_extract("1\xe9", ("", "", "1\xe9", "")) + assert_extract("1.1.1.1\ncom", ("", "1.1.1", "1\ncom", "")) + assert_extract("1.1.1.1\rcom", ("", "1.1.1", "1\rcom", "")) def test_punycode(): @@ -172,7 +178,7 @@ def test_punycode(): "com", ), ) - # This subdomain generates UnicodeError 'incomplete punicode string' + # This subdomain generates UnicodeError 'incomplete punycode string' assert_extract( "xn--tub-1m9d15sfkkhsifsbqygyujjrw60.google.com", ( @@ -199,6 +205,10 @@ def test_invalid_puny_with_puny(): ) +def test_invalid_puny_with_nonpuny(): + assert_extract("xn--ß‌꫶ᢥ.com", ("xn--ß‌꫶ᢥ.com", "", "xn--ß‌꫶ᢥ", "com")) + + def test_puny_with_non_puny(): assert_extract( "http://xn--zckzap6140b352by.blog.so-net.教育.hk", diff --git a/tldextract/remote.py b/tldextract/remote.py index ea11b467..3bbf4b0b 100644 --- a/tldextract/remote.py +++ b/tldextract/remote.py @@ -65,4 +65,4 @@ def looks_like_ip( return True except OSError: return False - return IP_RE.match(maybe_ip) is not None + return IP_RE.fullmatch(maybe_ip) is not None diff --git a/tldextract/tldextract.py b/tldextract/tldextract.py index 6dafcb17..90901f3d 100644 --- a/tldextract/tldextract.py +++ b/tldextract/tldextract.py @@ -63,7 +63,7 @@ import idna from .cache import DiskCache, get_cache_dir -from .remote import IP_RE, lenient_netloc, looks_like_ip +from .remote import lenient_netloc, looks_like_ip from .suffix_list import get_suffix_lists LOG = logging.getLogger("tldextract") @@ -126,7 +126,11 @@ def ipv4(self) -> str: >>> extract('http://256.1.1.1').ipv4 '' """ - if not (self.suffix or self.subdomain) and IP_RE.match(self.domain): + if ( + self.domain + and not (self.suffix or self.subdomain) + and looks_like_ip(self.domain) + ): return self.domain return ""