@@ -295,8 +295,8 @@ def _get_dns_txt_options(hostname):
295295 return None
296296 except Exception as exc :
297297 raise ConfigurationError (str (exc ))
298- return '&' . join ([ maybe_decode ( val )
299- for res in results for val in res . strings ] )
298+ return (
299+ b'&' . join ([ b'' . join ( res . strings ) for res in results ])). decode ( 'utf-8' )
300300
301301
302302def parse_uri (uri , default_port = DEFAULT_PORT , validate = True , warn = False ):
@@ -386,12 +386,28 @@ def parse_uri(uri, default_port=DEFAULT_PORT, validate=True, warn=False):
386386 raise InvalidURI (
387387 "%s URIs must include one, "
388388 "and only one, hostname" % (SRV_SCHEME ,))
389- hostname , port = nodes [0 ]
389+ fqdn , port = nodes [0 ]
390390 if port is not None :
391391 raise InvalidURI (
392392 "%s URIs must not include a port number" % (SRV_SCHEME ,))
393- nodes = _get_dns_srv_hosts (hostname )
394- dns_options = _get_dns_txt_options (hostname )
393+ nodes = _get_dns_srv_hosts (fqdn )
394+
395+ try :
396+ plist = fqdn .split ("." )[1 :]
397+ except Exception :
398+ raise ConfigurationError ("Invalid URI host" )
399+ slen = len (plist )
400+ if slen < 2 :
401+ raise ConfigurationError ("Invalid URI host" )
402+ for node in nodes :
403+ try :
404+ nlist = node [0 ].split ("." )[1 :][- slen :]
405+ except Exception :
406+ raise ConfigurationError ("Invalid SRV host" )
407+ if plist != nlist :
408+ raise ConfigurationError ("Invalid SRV host" )
409+
410+ dns_options = _get_dns_txt_options (fqdn )
395411 if dns_options :
396412 options = split_options (dns_options , validate , warn )
397413 else :
0 commit comments