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

fix broken avatar checks when server_name contains a port #13927

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/13927.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a bug which prevented setting an avatar on homeservers which have an explicit port in their `server_name` and have `max_avatar_size` and/or `allowed_avatar_mimetypes` configuration. Contributed by @ashfame.
6 changes: 5 additions & 1 deletion synapse/handlers/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,11 @@ async def check_avatar_size_and_mime_type(self, mxc: str) -> bool:
if not self.max_avatar_size and not self.allowed_avatar_mimetypes:
return True

server_name, _, media_id = parse_and_validate_mxc_uri(mxc)
host, port, media_id = parse_and_validate_mxc_uri(mxc)
if port is not None:
server_name = host + ":" + str(port)
richvdh marked this conversation as resolved.
Show resolved Hide resolved
else:
server_name = host
richvdh marked this conversation as resolved.
Show resolved Hide resolved

if server_name == self.server_name:
media_info = await self.store.get_local_media(media_id)
Expand Down
49 changes: 49 additions & 0 deletions tests/handlers/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from typing import Any, Awaitable, Callable, Dict
from unittest.mock import Mock

from parameterized import parameterized

from twisted.test.proto_helpers import MemoryReactor

import synapse.types
Expand Down Expand Up @@ -327,6 +329,53 @@ def test_avatar_constraint_mime_type(self) -> None:
)
self.assertFalse(res)

@unittest.override_config(
{"server_name": "test:8888", "allowed_avatar_mimetypes": ["image/png"]}
)
def test_avatar_constraint_on_local_server_with_port(self):
"""Test that avatar metadata is correctly fetched when the media is on a local
server and the server has an explicit port.

richvdh marked this conversation as resolved.
Show resolved Hide resolved
(This was previously a bug)
"""
local_server_name = self.hs.config.server.server_name
media_id = "local"
local_mxc = f"mxc://{local_server_name}/{media_id}"

# mock up the existence of the avatar file
self._setup_local_files({media_id: {"mimetype": "image/png"}})

# and now check that check_avatar_size_and_mime_type is happy
self.assertTrue(
self.get_success(self.handler.check_avatar_size_and_mime_type(local_mxc))
)

@parameterized.expand([("remote",), ("remote:1234",)])
@unittest.override_config({"allowed_avatar_mimetypes": ["image/png"]})
def test_check_avatar_on_remote_server(self, remote_server_name: str) -> None:
"""Test that avatar metadata is correctly fetched from a remote server"""
media_id = "remote"
remote_mxc = f"mxc://{remote_server_name}/{media_id}"

# if the media is remote, check_avatar_size_and_mime_type just checks the
# media cache, so we don't need to instantiate a real remote server. It is
# sufficient to poke an entry into the db.
self.get_success(
richvdh marked this conversation as resolved.
Show resolved Hide resolved
self.hs.get_datastores().main.store_cached_remote_media(
media_id=media_id,
media_type="image/png",
media_length=50,
origin=remote_server_name,
time_now_ms=self.clock.time_msec(),
upload_name=None,
filesystem_id="xyz",
)
)

self.assertTrue(
self.get_success(self.handler.check_avatar_size_and_mime_type(remote_mxc))
)

def _setup_local_files(self, names_and_props: Dict[str, Dict[str, Any]]):
"""Stores metadata about files in the database.

Expand Down