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

Commit d549099

Browse files
validate room alias before interacting with the room directory (#13106)
1 parent f33356e commit d549099

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

changelog.d/13106.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a long-standing bug where room directory requests would cause an internal server error if given a malformed room alias.

synapse/rest/client/directory.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ def __init__(self, hs: "HomeServer"):
4646
self.auth = hs.get_auth()
4747

4848
async def on_GET(self, request: Request, room_alias: str) -> Tuple[int, JsonDict]:
49+
if not RoomAlias.is_valid(room_alias):
50+
raise SynapseError(400, "Room alias invalid", errcode=Codes.INVALID_PARAM)
4951
room_alias_obj = RoomAlias.from_string(room_alias)
5052

5153
res = await self.directory_handler.get_association(room_alias_obj)
@@ -55,6 +57,8 @@ async def on_GET(self, request: Request, room_alias: str) -> Tuple[int, JsonDict
5557
async def on_PUT(
5658
self, request: SynapseRequest, room_alias: str
5759
) -> Tuple[int, JsonDict]:
60+
if not RoomAlias.is_valid(room_alias):
61+
raise SynapseError(400, "Room alias invalid", errcode=Codes.INVALID_PARAM)
5862
room_alias_obj = RoomAlias.from_string(room_alias)
5963

6064
content = parse_json_object_from_request(request)
@@ -89,6 +93,8 @@ async def on_PUT(
8993
async def on_DELETE(
9094
self, request: SynapseRequest, room_alias: str
9195
) -> Tuple[int, JsonDict]:
96+
if not RoomAlias.is_valid(room_alias):
97+
raise SynapseError(400, "Room alias invalid", errcode=Codes.INVALID_PARAM)
9298
room_alias_obj = RoomAlias.from_string(room_alias)
9399
requester = await self.auth.get_user_by_req(request)
94100

tests/rest/client/test_directory.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,19 @@ def set_alias_via_directory(
215215
self.assertEqual(channel.code, expected_code, channel.result)
216216
return alias
217217

218+
def test_invalid_alias(self) -> None:
219+
alias = "#potato"
220+
channel = self.make_request(
221+
"GET",
222+
f"/_matrix/client/r0/directory/room/{alias}",
223+
access_token=self.user_tok,
224+
)
225+
self.assertEqual(channel.code, HTTPStatus.BAD_REQUEST, channel.result)
226+
self.assertIn("error", channel.json_body, channel.json_body)
227+
self.assertEqual(
228+
channel.json_body["errcode"], "M_INVALID_PARAM", channel.json_body
229+
)
230+
218231
def random_alias(self, length: int) -> str:
219232
return RoomAlias(random_string(length), self.hs.hostname).to_string()
220233

0 commit comments

Comments
 (0)