2020 Membership,
2121 RestrictedJoinRuleTypes,
2222)
23- from synapse.api.errors import AuthError, SynapseError
23+ from synapse.api.errors import AuthError, Codes, SynapseError
2424from synapse.api.room_versions import KNOWN_ROOM_VERSIONS, RoomVersion
2525from synapse.events import EventBase
2626from synapse.events.builder import EventBuilder
27- from synapse.types import StateMap
27+ from synapse.types import StateMap, get_domain_from_id
2828from synapse.util.metrics import Measure
2929
3030if 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