@@ -110,6 +110,10 @@ def on_create_room(
110110 If yes, make sure the event is correct. Otherwise, append an event with the
111111 default rule to the initial state.
112112
113+ Checks if a m.rooms.power_levels event is being set during room creation.
114+ If yes, make sure the event is allowed. Otherwise, set power_level_content_override
115+ in the config dict to our modified version of the default room power levels.
116+
113117 Args:
114118 requester: The user who is making the createRoom request.
115119 config: The createRoom config dict provided by the user.
@@ -187,6 +191,10 @@ def on_create_room(
187191 if not allowed :
188192 raise SynapseError (400 , "Invalid power levels content override" )
189193
194+ use_default_power_levels = True
195+ if config .get ("power_level_content_override" ):
196+ use_default_power_levels = False
197+
190198 # Second loop for events we need to know the current rule to process.
191199 for event in config .get ("initial_state" , []):
192200 if event ["type" ] == EventTypes .PowerLevels :
@@ -196,8 +204,44 @@ def on_create_room(
196204 if not allowed :
197205 raise SynapseError (400 , "Invalid power levels content" )
198206
207+ use_default_power_levels = False
208+
209+ # If power levels were not overridden by the user, override with DINUM's preferred
210+ # defaults instead
211+ if use_default_power_levels :
212+ config ["power_level_content_override" ] = self ._get_default_power_levels (
213+ requester .user .to_string ()
214+ )
215+
199216 return True
200217
218+ # If power levels are not overridden by the user during room creation, the following
219+ # rules are used instead. Changes from Synapse's default power levels are noted.
220+ #
221+ # The same power levels are currently applied regardless of room preset.
222+ @staticmethod
223+ def _get_default_power_levels (user_id : str ) -> Dict :
224+ return {
225+ "users" : {user_id : 100 },
226+ "users_default" : 0 ,
227+ "events" : {
228+ EventTypes .Name : 50 ,
229+ EventTypes .PowerLevels : 100 ,
230+ EventTypes .RoomHistoryVisibility : 100 ,
231+ EventTypes .CanonicalAlias : 50 ,
232+ EventTypes .RoomAvatar : 50 ,
233+ EventTypes .Tombstone : 100 ,
234+ EventTypes .ServerACL : 100 ,
235+ EventTypes .RoomEncryption : 100 ,
236+ },
237+ "events_default" : 0 ,
238+ "state_default" : 100 , # Admins should be the only ones to perform other tasks
239+ "ban" : 50 ,
240+ "kick" : 50 ,
241+ "redact" : 50 ,
242+ "invite" : 50 , # All rooms should require mod to invite, even private
243+ }
244+
201245 @defer .inlineCallbacks
202246 def check_threepid_can_be_invited (
203247 self , medium : str , address : str , state_events : StateMap [EventBase ],
@@ -272,7 +316,9 @@ def check_event_allowed(
272316 rule = self ._get_rule_from_state (state_events )
273317
274318 if event .type == EventTypes .PowerLevels :
275- return self ._is_power_level_content_allowed (event .content , rule )
319+ return self ._is_power_level_content_allowed (
320+ event .content , rule , on_room_creation = False
321+ )
276322
277323 if event .type == EventTypes .Member or event .type == EventTypes .ThirdPartyInvite :
278324 return self ._on_membership_or_invite (event , rule , state_events )
@@ -468,7 +514,9 @@ def _on_membership_or_invite_direct(
468514
469515 return True
470516
471- def _is_power_level_content_allowed (self , content : Dict , access_rule : str ) -> bool :
517+ def _is_power_level_content_allowed (
518+ self , content : Dict , access_rule : str , on_room_creation : bool = True
519+ ) -> bool :
472520 """Check if a given power levels event is permitted under the given access rule.
473521
474522 It shouldn't be allowed if it either changes the default PL to a non-0 value or
@@ -478,10 +526,26 @@ def _is_power_level_content_allowed(self, content: Dict, access_rule: str) -> bo
478526 Args:
479527 content: The content of the m.room.power_levels event to check.
480528 access_rule: The access rule in place in this room.
529+ on_room_creation: True if this call is happening during a room's
530+ creation, False otherwise.
481531
482532 Returns:
483533 Whether the content of the power levels event is valid.
484534 """
535+ # Only enforce these rules during room creation
536+ #
537+ # We want to allow admins to modify or fix the power levels in a room if they
538+ # have a special circumstance, but still want to encourage a certain pattern during
539+ # room creation.
540+ if on_room_creation :
541+ # If invite requirements are <PL50
542+ if content .get ("invite" , 50 ) < 50 :
543+ return False
544+
545+ # If "other" state requirements are <PL100
546+ if content .get ("state_default" , 100 ) < 100 :
547+ return False
548+
485549 # Check if we need to apply the restrictions with the current rule.
486550 if access_rule not in RULES_WITH_RESTRICTED_POWER_LEVELS :
487551 return True
0 commit comments