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 11 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
43 changes: 43 additions & 0 deletions tests/handlers/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,49 @@ def test_avatar_constraint_mime_type(self) -> None:
)
self.assertFalse(res)

@unittest.override_config({"allowed_avatar_mimetypes": ["image/png"]})
def test_avatar_fetching_metadata_right_source(self) -> None:
"""Tests that local and remote files are correctly fetched for metadata
when checking for avatar size and mime type."""
# This test only concerns itself with correctly figuring out the host and port
# based on mxc url so that metadata can be fetched from the right place i.e.
# get_local_media() for local image & get_cached_remote_media() for remote image
richvdh marked this conversation as resolved.
Show resolved Hide resolved
remote_server = "test:8080"
richvdh marked this conversation as resolved.
Show resolved Hide resolved
store = self.hs.get_datastores().main

richvdh marked this conversation as resolved.
Show resolved Hide resolved
self.get_success(
store.store_local_media(
media_id="local",
media_type="image/png",
media_length=50,
time_now_ms=self.clock.time_msec(),
upload_name=None,
user_id=UserID.from_string("@whatever:test"),
)
)
res = self.get_success(
self.handler.check_avatar_size_and_mime_type("mxc://test/local")
)
self.assertTrue(res)
richvdh marked this conversation as resolved.
Show resolved Hide resolved

self.get_success(
richvdh marked this conversation as resolved.
Show resolved Hide resolved
store.store_cached_remote_media(
media_id="remote",
media_type="image/png",
media_length=50,
origin=remote_server,
time_now_ms=self.clock.time_msec(),
upload_name=None,
filesystem_id="remote",
)
)
ashfame marked this conversation as resolved.
Show resolved Hide resolved
res = self.get_success(
self.handler.check_avatar_size_and_mime_type(
"mxc://" + remote_server + "/remote"
)
)
ashfame marked this conversation as resolved.
Show resolved Hide resolved
self.assertTrue(res)

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

Expand Down