diff options
author | Sean Quah <seanq@element.io> | 2021-11-23 13:06:56 +0000 |
---|---|---|
committer | Sean Quah <seanq@element.io> | 2021-11-23 13:06:56 +0000 |
commit | 454c3d7694aaec4a225bdb2cedfb9432e7e9a597 (patch) | |
tree | 8f3ba48a82102eea84112bc464830a6b0616a2ff /synapse/rest/media/v1/_base.py | |
parent | Refactor the code to inject bundled relations during serialization. (#11408) (diff) | |
parent | Merge tag 'v1.47.1' (diff) | |
download | synapse-454c3d7694aaec4a225bdb2cedfb9432e7e9a597.tar.xz |
Merge branch 'master' into develop
Diffstat (limited to 'synapse/rest/media/v1/_base.py')
-rw-r--r-- | synapse/rest/media/v1/_base.py | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/synapse/rest/media/v1/_base.py b/synapse/rest/media/v1/_base.py index 014fa893d6..9b40fd8a6c 100644 --- a/synapse/rest/media/v1/_base.py +++ b/synapse/rest/media/v1/_base.py @@ -29,7 +29,7 @@ from synapse.api.errors import Codes, SynapseError, cs_error from synapse.http.server import finish_request, respond_with_json from synapse.http.site import SynapseRequest from synapse.logging.context import make_deferred_yieldable -from synapse.util.stringutils import is_ascii +from synapse.util.stringutils import is_ascii, parse_and_validate_server_name logger = logging.getLogger(__name__) @@ -51,6 +51,19 @@ TEXT_CONTENT_TYPES = [ def parse_media_id(request: Request) -> Tuple[str, str, Optional[str]]: + """Parses the server name, media ID and optional file name from the request URI + + Also performs some rough validation on the server name. + + Args: + request: The `Request`. + + Returns: + A tuple containing the parsed server name, media ID and optional file name. + + Raises: + SynapseError(404): if parsing or validation fail for any reason + """ try: # The type on postpath seems incorrect in Twisted 21.2.0. postpath: List[bytes] = request.postpath # type: ignore @@ -62,6 +75,9 @@ def parse_media_id(request: Request) -> Tuple[str, str, Optional[str]]: server_name = server_name_bytes.decode("utf-8") media_id = media_id_bytes.decode("utf8") + # Validate the server name, raising if invalid + parse_and_validate_server_name(server_name) + file_name = None if len(postpath) > 2: try: |