Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Making parse_server_name more consistent (#14007)
Browse files Browse the repository at this point in the history
Fixes #12122
  • Loading branch information
Abdullah0sama authored Oct 11, 2022
1 parent 17c031b commit a9934d4
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog.d/14007.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make `parse_server_name` consistent in handling invalid server names.
4 changes: 2 additions & 2 deletions synapse/util/stringutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def parse_server_name(server_name: str) -> Tuple[str, Optional[int]]:
ValueError if the server name could not be parsed.
"""
try:
if server_name[-1] == "]":
if server_name and server_name[-1] == "]":
# ipv6 literal, hopefully
return server_name, None

Expand Down Expand Up @@ -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.

# look for ipv6 literals
if host[0] == "[":
if host and host[0] == "[":
if host[-1] != "]":
raise ValueError("Mismatched [...] in server name '%s'" % (server_name,))

Expand Down
3 changes: 3 additions & 0 deletions tests/http/test_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ def test_parse_server_name(self):
"[0abc:1def::1234]": ("[0abc:1def::1234]", None),
"1.2.3.4:1": ("1.2.3.4", 1),
"[0abc:1def::1234]:8080": ("[0abc:1def::1234]", 8080),
":80": ("", 80),
"": ("", None),
}

for i, o in test_data.items():
Expand All @@ -42,6 +44,7 @@ def test_validate_bad_server_names(self):
"newline.com\n",
".empty-label.com",
"1234:5678:80", # too many colons
":80",
]
for i in test_data:
try:
Expand Down

0 comments on commit a9934d4

Please sign in to comment.