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

Commit 4387b79

Browse files
authored
Don't set new room alias before potential 403 (#10930)
Fixes: #10929 Signed-off-by: Andrew Ferrazzutti <[email protected]>
1 parent da957a6 commit 4387b79

File tree

4 files changed

+113
-12
lines changed

4 files changed

+113
-12
lines changed

changelog.d/10930.bugfix

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Newly-created public rooms are now only assigned an alias if the room's creation has not been blocked by permission settings. Contributed by @AndrewFerr.

synapse/handlers/directory.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ async def create_association(
145145
if not self.config.roomdirectory.is_alias_creation_allowed(
146146
user_id, room_id, room_alias_str
147147
):
148-
# Lets just return a generic message, as there may be all sorts of
148+
# Let's just return a generic message, as there may be all sorts of
149149
# reasons why we said no. TODO: Allow configurable error messages
150150
# per alias creation rule?
151151
raise SynapseError(403, "Not allowed to create alias")
@@ -461,7 +461,7 @@ async def edit_published_room_list(
461461
if not self.config.roomdirectory.is_publishing_room_allowed(
462462
user_id, room_id, room_aliases
463463
):
464-
# Lets just return a generic message, as there may be all sorts of
464+
# Let's just return a generic message, as there may be all sorts of
465465
# reasons why we said no. TODO: Allow configurable error messages
466466
# per alias creation rule?
467467
raise SynapseError(403, "Not allowed to publish room")

synapse/handlers/room.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,15 @@ async def create_room(
773773
if not allowed_by_third_party_rules:
774774
raise SynapseError(403, "Room visibility value not allowed.")
775775

776+
if is_public:
777+
if not self.config.roomdirectory.is_publishing_room_allowed(
778+
user_id, room_id, room_alias
779+
):
780+
# Let's just return a generic message, as there may be all sorts of
781+
# reasons why we said no. TODO: Allow configurable error messages
782+
# per alias creation rule?
783+
raise SynapseError(403, "Not allowed to publish room")
784+
776785
directory_handler = self.hs.get_directory_handler()
777786
if room_alias:
778787
await directory_handler.create_association(
@@ -783,15 +792,6 @@ async def create_room(
783792
check_membership=False,
784793
)
785794

786-
if is_public:
787-
if not self.config.roomdirectory.is_publishing_room_allowed(
788-
user_id, room_id, room_alias
789-
):
790-
# Lets just return a generic message, as there may be all sorts of
791-
# reasons why we said no. TODO: Allow configurable error messages
792-
# per alias creation rule?
793-
raise SynapseError(403, "Not allowed to publish room")
794-
795795
preset_config = config.get(
796796
"preset",
797797
RoomCreationPreset.PRIVATE_CHAT

tests/handlers/test_directory.py

+101-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
from unittest.mock import Mock
1717

18-
import synapse
1918
import synapse.api.errors
19+
import synapse.rest.admin
2020
from synapse.api.constants import EventTypes
2121
from synapse.config.room_directory import RoomDirectoryConfig
2222
from synapse.rest.client import directory, login, room
@@ -432,6 +432,106 @@ def test_allowed(self):
432432
self.assertEquals(200, channel.code, channel.result)
433433

434434

435+
class TestCreatePublishedRoomACL(unittest.HomeserverTestCase):
436+
data = {"room_alias_name": "unofficial_test"}
437+
438+
servlets = [
439+
synapse.rest.admin.register_servlets_for_client_rest_resource,
440+
login.register_servlets,
441+
directory.register_servlets,
442+
room.register_servlets,
443+
]
444+
hijack_auth = False
445+
446+
def prepare(self, reactor, clock, hs):
447+
self.allowed_user_id = self.register_user("allowed", "pass")
448+
self.allowed_access_token = self.login("allowed", "pass")
449+
450+
self.denied_user_id = self.register_user("denied", "pass")
451+
self.denied_access_token = self.login("denied", "pass")
452+
453+
# This time we add custom room list publication rules
454+
config = {}
455+
config["alias_creation_rules"] = []
456+
config["room_list_publication_rules"] = [
457+
{"user_id": "*", "alias": "*", "action": "deny"},
458+
{"user_id": self.allowed_user_id, "alias": "*", "action": "allow"},
459+
]
460+
461+
rd_config = RoomDirectoryConfig()
462+
rd_config.read_config(config)
463+
464+
self.hs.config.roomdirectory.is_publishing_room_allowed = (
465+
rd_config.is_publishing_room_allowed
466+
)
467+
468+
return hs
469+
470+
def test_denied_without_publication_permission(self):
471+
"""
472+
Try to create a room, register an alias for it, and publish it,
473+
as a user without permission to publish rooms.
474+
(This is used as both a standalone test & as a helper function.)
475+
"""
476+
self.helper.create_room_as(
477+
self.denied_user_id,
478+
tok=self.denied_access_token,
479+
extra_content=self.data,
480+
is_public=True,
481+
expect_code=403,
482+
)
483+
484+
def test_allowed_when_creating_private_room(self):
485+
"""
486+
Try to create a room, register an alias for it, and NOT publish it,
487+
as a user without permission to publish rooms.
488+
(This is used as both a standalone test & as a helper function.)
489+
"""
490+
self.helper.create_room_as(
491+
self.denied_user_id,
492+
tok=self.denied_access_token,
493+
extra_content=self.data,
494+
is_public=False,
495+
expect_code=200,
496+
)
497+
498+
def test_allowed_with_publication_permission(self):
499+
"""
500+
Try to create a room, register an alias for it, and publish it,
501+
as a user WITH permission to publish rooms.
502+
(This is used as both a standalone test & as a helper function.)
503+
"""
504+
self.helper.create_room_as(
505+
self.allowed_user_id,
506+
tok=self.allowed_access_token,
507+
extra_content=self.data,
508+
is_public=False,
509+
expect_code=200,
510+
)
511+
512+
def test_can_create_as_private_room_after_rejection(self):
513+
"""
514+
After failing to publish a room with an alias as a user without publish permission,
515+
retry as the same user, but without publishing the room.
516+
517+
This should pass, but used to fail because the alias was registered by the first
518+
request, even though the room creation was denied.
519+
"""
520+
self.test_denied_without_publication_permission()
521+
self.test_allowed_when_creating_private_room()
522+
523+
def test_can_create_with_permission_after_rejection(self):
524+
"""
525+
After failing to publish a room with an alias as a user without publish permission,
526+
retry as someone with permission, using the same alias.
527+
528+
This also used to fail because of the alias having been registered by the first
529+
request, leaving it unavailable for any other user's new rooms.
530+
"""
531+
self.test_denied_without_publication_permission()
532+
self.test_allowed_with_publication_permission()
533+
534+
435535
class TestRoomListSearchDisabled(unittest.HomeserverTestCase):
436536
user_id = "@test:test"
437537

0 commit comments

Comments
 (0)