Making parse_server_name more consistent (#14007)

Fixes #12122
pull/14140/head
Abdullah Osama 2022-10-11 14:42:11 +02:00 committed by GitHub
parent 17c031b251
commit a9934d48c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 6 additions and 2 deletions

1
changelog.d/14007.misc Normal file
View File

@ -0,0 +1 @@
Make `parse_server_name` consistent in handling invalid server names.

View File

@ -86,7 +86,7 @@ def parse_server_name(server_name: str) -> Tuple[str, Optional[int]]:
ValueError if the server name could not be parsed. ValueError if the server name could not be parsed.
""" """
try: try:
if server_name[-1] == "]": if server_name and server_name[-1] == "]":
# ipv6 literal, hopefully # ipv6 literal, hopefully
return server_name, None return server_name, None
@ -123,7 +123,7 @@ def parse_and_validate_server_name(server_name: str) -> Tuple[str, Optional[int]
# that nobody is sneaking IP literals in that look like hostnames, etc. # that nobody is sneaking IP literals in that look like hostnames, etc.
# look for ipv6 literals # look for ipv6 literals
if host[0] == "[": if host and host[0] == "[":
if host[-1] != "]": if host[-1] != "]":
raise ValueError("Mismatched [...] in server name '%s'" % (server_name,)) raise ValueError("Mismatched [...] in server name '%s'" % (server_name,))

View File

@ -25,6 +25,8 @@ class ServerNameTestCase(unittest.TestCase):
"[0abc:1def::1234]": ("[0abc:1def::1234]", None), "[0abc:1def::1234]": ("[0abc:1def::1234]", None),
"1.2.3.4:1": ("1.2.3.4", 1), "1.2.3.4:1": ("1.2.3.4", 1),
"[0abc:1def::1234]:8080": ("[0abc:1def::1234]", 8080), "[0abc:1def::1234]:8080": ("[0abc:1def::1234]", 8080),
":80": ("", 80),
"": ("", None),
} }
for i, o in test_data.items(): for i, o in test_data.items():
@ -42,6 +44,7 @@ class ServerNameTestCase(unittest.TestCase):
"newline.com\n", "newline.com\n",
".empty-label.com", ".empty-label.com",
"1234:5678:80", # too many colons "1234:5678:80", # too many colons
":80",
] ]
for i in test_data: for i in test_data:
try: try: