This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Fix checking whether a room can be published on creation. #11392
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d5d47ab
Fix checking whether a room can be published on creation.
clokep ff36eb8
Newsfragment
clokep 7d0b096
Clean-up unit tests.
clokep 502e81e
Additional unit tests for creating aliases.
clokep 88e48a4
Fix current tests to actually publish room alias.
clokep 34b7da7
Additional test to skip match-all logic.
clokep 06d5a25
Lint
clokep 99a6cdb
Update comments.
clokep File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fix a bug introduced in v1.13.0 where creating and publishing a room could cause errors if `room_list_publication_rules` is configured. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,4 +1,5 @@ | ||||||
# Copyright 2018 New Vector Ltd | ||||||
# Copyright 2021 Matrix.org Foundation C.I.C. | ||||||
# | ||||||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||||||
# you may not use this file except in compliance with the License. | ||||||
|
@@ -12,6 +13,9 @@ | |||||
# See the License for the specific language governing permissions and | ||||||
# limitations under the License. | ||||||
|
||||||
from typing import List | ||||||
|
||||||
from synapse.types import JsonDict | ||||||
from synapse.util import glob_to_regex | ||||||
|
||||||
from ._base import Config, ConfigError | ||||||
|
@@ -20,7 +24,7 @@ | |||||
class RoomDirectoryConfig(Config): | ||||||
section = "roomdirectory" | ||||||
|
||||||
def read_config(self, config, **kwargs): | ||||||
def read_config(self, config, **kwargs) -> None: | ||||||
self.enable_room_list_search = config.get("enable_room_list_search", True) | ||||||
|
||||||
alias_creation_rules = config.get("alias_creation_rules") | ||||||
|
@@ -47,7 +51,7 @@ def read_config(self, config, **kwargs): | |||||
_RoomDirectoryRule("room_list_publication_rules", {"action": "allow"}) | ||||||
] | ||||||
|
||||||
def generate_config_section(self, config_dir_path, server_name, **kwargs): | ||||||
def generate_config_section(self, config_dir_path, server_name, **kwargs) -> str: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
not sure about the |
||||||
return """ | ||||||
# Uncomment to disable searching the public room list. When disabled | ||||||
# blocks searching local and remote room lists for local and remote | ||||||
|
@@ -113,33 +117,35 @@ def generate_config_section(self, config_dir_path, server_name, **kwargs): | |||||
# action: allow | ||||||
""" | ||||||
|
||||||
def is_alias_creation_allowed(self, user_id, room_id, alias): | ||||||
def is_alias_creation_allowed(self, user_id: str, room_id: str, alias: str) -> bool: | ||||||
"""Checks if the given user is allowed to create the given alias | ||||||
|
||||||
Args: | ||||||
user_id (str) | ||||||
room_id (str) | ||||||
alias (str) | ||||||
user_id: The user to check. | ||||||
room_id: The room ID for the alias. | ||||||
alias: The alias being created. | ||||||
|
||||||
Returns: | ||||||
boolean: True if user is allowed to create the alias | ||||||
True if user is allowed to create the alias | ||||||
""" | ||||||
for rule in self._alias_creation_rules: | ||||||
if rule.matches(user_id, room_id, [alias]): | ||||||
return rule.action == "allow" | ||||||
|
||||||
return False | ||||||
|
||||||
def is_publishing_room_allowed(self, user_id, room_id, aliases): | ||||||
def is_publishing_room_allowed( | ||||||
self, user_id: str, room_id: str, aliases: List[str] | ||||||
) -> bool: | ||||||
"""Checks if the given user is allowed to publish the room | ||||||
|
||||||
Args: | ||||||
user_id (str) | ||||||
room_id (str) | ||||||
aliases (list[str]): any local aliases associated with the room | ||||||
user_id: The user ID publishing the room. | ||||||
room_id: The room being published. | ||||||
aliases: any local aliases associated with the room | ||||||
|
||||||
Returns: | ||||||
boolean: True if user can publish room | ||||||
True if user can publish room | ||||||
""" | ||||||
for rule in self._room_list_publication_rules: | ||||||
if rule.matches(user_id, room_id, aliases): | ||||||
|
@@ -153,11 +159,11 @@ class _RoomDirectoryRule: | |||||
creating an alias or publishing a room. | ||||||
""" | ||||||
|
||||||
def __init__(self, option_name, rule): | ||||||
def __init__(self, option_name: str, rule: JsonDict): | ||||||
""" | ||||||
Args: | ||||||
option_name (str): Name of the config option this rule belongs to | ||||||
rule (dict): The rule as specified in the config | ||||||
option_name: Name of the config option this rule belongs to | ||||||
rule: The rule as specified in the config | ||||||
""" | ||||||
|
||||||
action = rule["action"] | ||||||
|
@@ -181,18 +187,18 @@ def __init__(self, option_name, rule): | |||||
except Exception as e: | ||||||
raise ConfigError("Failed to parse glob into regex") from e | ||||||
|
||||||
def matches(self, user_id, room_id, aliases): | ||||||
def matches(self, user_id: str, room_id: str, aliases: List[str]) -> bool: | ||||||
"""Tests if this rule matches the given user_id, room_id and aliases. | ||||||
|
||||||
Args: | ||||||
user_id (str) | ||||||
room_id (str) | ||||||
aliases (list[str]): The associated aliases to the room. Will be a | ||||||
single element for testing alias creation, and can be empty for | ||||||
testing room publishing. | ||||||
user_id: The user ID to check. | ||||||
room_id: The room ID to check. | ||||||
aliases: The associated aliases to the room. Will be a single element | ||||||
for testing alias creation, and can be empty for testing room | ||||||
publishing. | ||||||
|
||||||
Returns: | ||||||
boolean | ||||||
True if the rule matches. | ||||||
""" | ||||||
|
||||||
# Note: The regexes are anchored at both ends | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
config
?kwargs
maybe?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a separate branch which does all of the
read_config
andgenerate_config_section
methods at once. Hope that's OK!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aye that's reet :)