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

Commit

Permalink
Prevent the media store from writing outside of the configured directory
Browse files Browse the repository at this point in the history
Also tighten validation of server names by forbidding invalid characters
in IPv6 addresses and empty domain labels.
  • Loading branch information
Sean Quah committed Nov 19, 2021
1 parent 9f9d82a commit 91f2bd0
Show file tree
Hide file tree
Showing 5 changed files with 483 additions and 50 deletions.
18 changes: 17 additions & 1 deletion synapse/rest/media/v1/_base.py
Expand Up @@ -29,7 +29,7 @@
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__)

Expand All @@ -51,6 +51,19 @@


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
Expand All @@ -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:
Expand Down

0 comments on commit 91f2bd0

Please sign in to comment.