mirror of
https://git.anonymousland.org/anonymousland/synapse.git
synced 2025-05-03 01:44:48 -04:00
Reject invalid server names (#3480)
Make sure that server_names used in auth headers are sane, and reject them with a sensible error code, before they disappear off into the depths of the system.
This commit is contained in:
parent
f741630847
commit
508196e08a
5 changed files with 122 additions and 25 deletions
|
@ -38,6 +38,36 @@ _Server = collections.namedtuple(
|
|||
)
|
||||
|
||||
|
||||
def parse_server_name(server_name):
|
||||
"""Split a server name into host/port parts.
|
||||
|
||||
Does some basic sanity checking of the
|
||||
|
||||
Args:
|
||||
server_name (str): server name to parse
|
||||
|
||||
Returns:
|
||||
Tuple[str, int|None]: host/port parts.
|
||||
|
||||
Raises:
|
||||
ValueError if the server name could not be parsed.
|
||||
"""
|
||||
try:
|
||||
if server_name[-1] == ']':
|
||||
# ipv6 literal, hopefully
|
||||
if server_name[0] != '[':
|
||||
raise Exception()
|
||||
|
||||
return server_name, None
|
||||
|
||||
domain_port = server_name.rsplit(":", 1)
|
||||
domain = domain_port[0]
|
||||
port = int(domain_port[1]) if domain_port[1:] else None
|
||||
return domain, port
|
||||
except Exception:
|
||||
raise ValueError("Invalid server name '%s'" % server_name)
|
||||
|
||||
|
||||
def matrix_federation_endpoint(reactor, destination, ssl_context_factory=None,
|
||||
timeout=None):
|
||||
"""Construct an endpoint for the given matrix destination.
|
||||
|
@ -50,9 +80,7 @@ def matrix_federation_endpoint(reactor, destination, ssl_context_factory=None,
|
|||
timeout (int): connection timeout in seconds
|
||||
"""
|
||||
|
||||
domain_port = destination.split(":")
|
||||
domain = domain_port[0]
|
||||
port = int(domain_port[1]) if domain_port[1:] else None
|
||||
domain, port = parse_server_name(destination)
|
||||
|
||||
endpoint_kw_args = {}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue