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

Commit 190ab59

Browse files
authored
Use the proper error code when a canonical alias that does not exist is used. (#7109)
1 parent e341518 commit 190ab59

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

changelog.d/7109.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Return the proper error (M_BAD_ALIAS) when a non-existant canonical alias is provided.

synapse/handlers/message.py

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,38 @@ def handle_new_client_event(
851851
self.store.remove_push_actions_from_staging, event.event_id
852852
)
853853

854+
@defer.inlineCallbacks
855+
def _validate_canonical_alias(
856+
self, directory_handler, room_alias_str, expected_room_id
857+
):
858+
"""
859+
Ensure that the given room alias points to the expected room ID.
860+
861+
Args:
862+
directory_handler: The directory handler object.
863+
room_alias_str: The room alias to check.
864+
expected_room_id: The room ID that the alias should point to.
865+
"""
866+
room_alias = RoomAlias.from_string(room_alias_str)
867+
try:
868+
mapping = yield directory_handler.get_association(room_alias)
869+
except SynapseError as e:
870+
# Turn M_NOT_FOUND errors into M_BAD_ALIAS errors.
871+
if e.errcode == Codes.NOT_FOUND:
872+
raise SynapseError(
873+
400,
874+
"Room alias %s does not point to the room" % (room_alias_str,),
875+
Codes.BAD_ALIAS,
876+
)
877+
raise
878+
879+
if mapping["room_id"] != expected_room_id:
880+
raise SynapseError(
881+
400,
882+
"Room alias %s does not point to the room" % (room_alias_str,),
883+
Codes.BAD_ALIAS,
884+
)
885+
854886
@defer.inlineCallbacks
855887
def persist_and_notify_client_event(
856888
self, requester, event, context, ratelimit=True, extra_users=[]
@@ -905,15 +937,9 @@ def persist_and_notify_client_event(
905937
room_alias_str = event.content.get("alias", None)
906938
directory_handler = self.hs.get_handlers().directory_handler
907939
if room_alias_str and room_alias_str != original_alias:
908-
room_alias = RoomAlias.from_string(room_alias_str)
909-
mapping = yield directory_handler.get_association(room_alias)
910-
911-
if mapping["room_id"] != event.room_id:
912-
raise SynapseError(
913-
400,
914-
"Room alias %s does not point to the room" % (room_alias_str,),
915-
Codes.BAD_ALIAS,
916-
)
940+
yield self._validate_canonical_alias(
941+
directory_handler, room_alias_str, event.room_id
942+
)
917943

918944
# Check that alt_aliases is the proper form.
919945
alt_aliases = event.content.get("alt_aliases", [])
@@ -931,16 +957,9 @@ def persist_and_notify_client_event(
931957
new_alt_aliases = set(alt_aliases) - set(original_alt_aliases)
932958
if new_alt_aliases:
933959
for alias_str in new_alt_aliases:
934-
room_alias = RoomAlias.from_string(alias_str)
935-
mapping = yield directory_handler.get_association(room_alias)
936-
937-
if mapping["room_id"] != event.room_id:
938-
raise SynapseError(
939-
400,
940-
"Room alias %s does not point to the room"
941-
% (room_alias_str,),
942-
Codes.BAD_ALIAS,
943-
)
960+
yield self._validate_canonical_alias(
961+
directory_handler, alias_str, event.room_id
962+
)
944963

945964
federation_handler = self.hs.get_handlers().federation_handler
946965

0 commit comments

Comments
 (0)