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

Commit a588b7b

Browse files
committed
Raise errors according to the spec.
1 parent 05e35ce commit a588b7b

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

synapse/api/errors.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ class Codes:
7575
INVALID_SIGNATURE = "M_INVALID_SIGNATURE"
7676
USER_DEACTIVATED = "M_USER_DEACTIVATED"
7777
BAD_ALIAS = "M_BAD_ALIAS"
78+
# For restricted join rules.
79+
UNABLE_AUTHORISE_JOIN = "M_UNABLE_TO_AUTHORISE_JOIN"
80+
CANNOT_ALLOW = "M_CANNOT_ALLOW"
7881

7982

8083
class CodeMessageException(RuntimeError):

synapse/handlers/event_auth.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
Membership,
2121
RestrictedJoinRuleTypes,
2222
)
23-
from synapse.api.errors import AuthError, SynapseError
23+
from synapse.api.errors import AuthError, Codes, SynapseError
2424
from synapse.api.room_versions import KNOWN_ROOM_VERSIONS, RoomVersion
2525
from synapse.events import EventBase
2626
from synapse.events.builder import EventBuilder
27-
from synapse.types import StateMap
27+
from synapse.types import StateMap, get_domain_from_id
2828
from synapse.util.metrics import Measure
2929

3030
if TYPE_CHECKING:
@@ -39,6 +39,7 @@ class EventAuthHandler:
3939
def __init__(self, hs: "HomeServer"):
4040
self._clock = hs.get_clock()
4141
self._store = hs.get_datastore()
42+
self._server_name = hs.hostname
4243

4344
async def check_from_context(
4445
self, room_version: str, event, context, do_sig_check=True
@@ -133,7 +134,9 @@ async def get_user_which_could_invite(
133134
return chosen_user
134135

135136
# No user was found.
136-
raise SynapseError(400, "Unable to find a user which could issue an invite")
137+
raise SynapseError(
138+
400, "Unable to find a user which could issue an invite", Codes.CANNOT_ALLOW
139+
)
137140

138141
async def check_host_in_room(self, room_id: str, host: str) -> bool:
139142
with Measure(self._clock, "check_host_in_room"):
@@ -179,6 +182,18 @@ async def check_restricted_join_rules(
179182
# in any of them.
180183
allowed_rooms = await self.get_rooms_that_allow_join(state_ids)
181184
if not await self.is_user_in_rooms(allowed_rooms, user_id):
185+
186+
# If this is a remote request, the user might be in an allowed room
187+
# that we do not know about.
188+
if get_domain_from_id(user_id) != self._server_name:
189+
for room_id in allowed_rooms:
190+
if not await self._store.is_host_joined(room_id, self._server_name):
191+
raise SynapseError(
192+
400,
193+
f"Unable to check if {user_id} is in allowed rooms.",
194+
Codes.UNABLE_AUTHORISE_JOIN,
195+
)
196+
182197
raise AuthError(
183198
403,
184199
"You do not belong to any of the required rooms to join this room.",

0 commit comments

Comments
 (0)