From 1039d09ed618afadf5d24a741d85ec34be29edd7 Mon Sep 17 00:00:00 2001 From: "Marcus R. Matos" Date: Fri, 3 Apr 2020 19:56:54 -0500 Subject: #3885 handle hyphens in domain name, enhance validation checks --- mitmproxy/net/check.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) (limited to 'mitmproxy') diff --git a/mitmproxy/net/check.py b/mitmproxy/net/check.py index a19ad6fe..90600195 100644 --- a/mitmproxy/net/check.py +++ b/mitmproxy/net/check.py @@ -1,8 +1,22 @@ import ipaddress import re -# Allow underscore in host name -_label_valid = re.compile(br"(?!-)[A-Z\d\-_]{1,63}(? bool: @@ -16,10 +30,14 @@ def is_valid_host(host: bytes) -> bool: # RFC1035: 255 bytes or less. if len(host) > 255: return False + # Trim trailing period if host and host[-1:] == b".": host = host[:-1] - # DNS hostname - if all(_label_valid.match(x) for x in host.split(b".")): + # DNS label + if b"." in host and _label_valid.match(host): + return True + # hostname + if b"." not in host and _host_valid.match(host): return True # IPv4/IPv6 address try: -- cgit v1.2.3 From 2722f4fd764657b1f059c66aa75a879acd56abca Mon Sep 17 00:00:00 2001 From: "Marcus R. Matos" Date: Fri, 3 Apr 2020 20:15:50 -0500 Subject: #3885 handle hyphens in domain name, enhance validation checks, linter updates --- mitmproxy/net/check.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'mitmproxy') diff --git a/mitmproxy/net/check.py b/mitmproxy/net/check.py index 90600195..32e733af 100644 --- a/mitmproxy/net/check.py +++ b/mitmproxy/net/check.py @@ -15,7 +15,9 @@ we'll go with the least restrictive rules while still providing a sanity check. """ # label regex: in total between 4 and 255 chars, tld 2 to 63 chars, each label 1 to 63 chars -_label_valid = re.compile(br"^(?=.{4,255}$)([A-Z0-9_-]([A-Z0-9_-]{0,61}[A-Z0-9_-])?\.){1,126}[A-Z0-9][A-Z0-9-]{0,61}[A-Z0-9]$", re.IGNORECASE) +_label_valid = re.compile( + br"^(?=.{4,255}$)([A-Z0-9_-]([A-Z0-9_-]{0,61}[A-Z0-9_-])?\.)" + br"{1,126}[A-Z0-9][A-Z0-9-]{0,61}[A-Z0-9]$", re.IGNORECASE) _host_valid = re.compile(br"[A-Z0-9\-_]{1,63}$", re.IGNORECASE) -- cgit v1.2.3 From 901c0f6ede67b419e263eb1876f8720c791ed07f Mon Sep 17 00:00:00 2001 From: "Marcus R. Matos" Date: Sun, 5 Apr 2020 15:50:28 -0500 Subject: #3885 implement simpler regex for host validation --- mitmproxy/net/check.py | 29 +++++------------------------ 1 file changed, 5 insertions(+), 24 deletions(-) (limited to 'mitmproxy') diff --git a/mitmproxy/net/check.py b/mitmproxy/net/check.py index 32e733af..ffb5e163 100644 --- a/mitmproxy/net/check.py +++ b/mitmproxy/net/check.py @@ -1,24 +1,9 @@ import ipaddress import re -""" -The rules for host names are different from DNS Names (aka "Label"). -DNS Names allow for hyphens and underscores (RFC-2872). -Hostnames DO allow for hyphens, but not underscores. (RFC-952, RFC-1123) -The main issue is the existence of DNS labels that are actually -capable of being resolved to a valid IP, even if the label -isn't a valid hostname (e.g. api-.example.com, @.example.com) - -Since the value we're checking could be an IP, a host name, a DNS label, or a FQDN, -and there are cases where DNS or Hostnames are misconfigured despite RFC -we'll go with the least restrictive rules while still providing a sanity check. -""" - -# label regex: in total between 4 and 255 chars, tld 2 to 63 chars, each label 1 to 63 chars -_label_valid = re.compile( - br"^(?=.{4,255}$)([A-Z0-9_-]([A-Z0-9_-]{0,61}[A-Z0-9_-])?\.)" - br"{1,126}[A-Z0-9][A-Z0-9-]{0,61}[A-Z0-9]$", re.IGNORECASE) -_host_valid = re.compile(br"[A-Z0-9\-_]{1,63}$", re.IGNORECASE) +# Allow underscore in host name +# Note: This could be a DNS label, a hostname, a FQDN, or an IP +_label_valid = re.compile(br"[A-Z\d\-_]{1,63}$", re.IGNORECASE) def is_valid_host(host: bytes) -> bool: @@ -32,14 +17,10 @@ def is_valid_host(host: bytes) -> bool: # RFC1035: 255 bytes or less. if len(host) > 255: return False - # Trim trailing period if host and host[-1:] == b".": host = host[:-1] - # DNS label - if b"." in host and _label_valid.match(host): - return True - # hostname - if b"." not in host and _host_valid.match(host): + # DNS hostname + if all(_label_valid.match(x) for x in host.split(b".")): return True # IPv4/IPv6 address try: -- cgit v1.2.3