diff options
author | Maximilian Hils <git@maximilianhils.com> | 2017-02-22 13:17:53 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-22 13:17:53 +0100 |
commit | ba76dbc672aeaa4454b82002f386bf22c06a039e (patch) | |
tree | 68f5e198dc10495fc4a4d2662643fb3d2c55d7b5 | |
parent | 391f28f78cf9f4ee2f82c69c7f5ce370b69b77cd (diff) | |
parent | aa6b0f299e7cebb70a7e5b1f8b63d7d45683e31f (diff) | |
download | mitmproxy-ba76dbc672aeaa4454b82002f386bf22c06a039e.tar.gz mitmproxy-ba76dbc672aeaa4454b82002f386bf22c06a039e.tar.bz2 mitmproxy-ba76dbc672aeaa4454b82002f386bf22c06a039e.zip |
Merge pull request #2048 from ujjwal96/ipv6-addresses
Absolute IPv6 addresses supported
-rw-r--r-- | mitmproxy/net/check.py | 14 | ||||
-rw-r--r-- | test/mitmproxy/net/test_check.py | 1 |
2 files changed, 13 insertions, 2 deletions
diff --git a/mitmproxy/net/check.py b/mitmproxy/net/check.py index f793d397..d30c1df6 100644 --- a/mitmproxy/net/check.py +++ b/mitmproxy/net/check.py @@ -1,3 +1,4 @@ +import ipaddress import re # Allow underscore in host name @@ -6,17 +7,26 @@ _label_valid = re.compile(b"(?!-)[A-Z\d\-_]{1,63}(?<!-)$", re.IGNORECASE) def is_valid_host(host: bytes) -> bool: """ - Checks if a hostname is valid. + Checks if the passed bytes are a valid DNS hostname or an IPv4/IPv6 address. """ try: host.decode("idna") except ValueError: return False + # RFC1035: 255 bytes or less. if len(host) > 255: return False if host and host[-1:] == b".": host = host[:-1] - return all(_label_valid.match(x) for x in host.split(b".")) + # DNS hostname + if all(_label_valid.match(x) for x in host.split(b".")): + return True + # IPv4/IPv6 address + try: + ipaddress.ip_address(host.decode('idna')) + return True + except ValueError: + return False def is_valid_port(port): diff --git a/test/mitmproxy/net/test_check.py b/test/mitmproxy/net/test_check.py index 9dbc02e0..0ffd6b2e 100644 --- a/test/mitmproxy/net/test_check.py +++ b/test/mitmproxy/net/test_check.py @@ -11,3 +11,4 @@ def test_is_valid_host(): assert check.is_valid_host(b"one.two.") # Allow underscore assert check.is_valid_host(b"one_two") + assert check.is_valid_host(b"::1") |