-
Notifications
You must be signed in to change notification settings - Fork 412
An federation whitelist query endpoint extension #16848
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
1d03cfa
An federation whitelist query endpoint extension
erikjohnston 8653451
Newsfile
erikjohnston d4c1270
Add tests
erikjohnston b79f8e4
Merge branch 'develop' into erikj/federation_whitelist
devonh 264746c
Merge branch 'develop' into erikj/federation_whitelist
devonh da1b7b9
Update docs/usage/configuration/config_documentation.md
devonh 2ec7a55
Update docs/usage/configuration/config_documentation.md
devonh d9aa8a9
Update docs/usage/configuration/config_documentation.md
devonh 1829e4a
Update tests/rest/synapse/client/test_federation_whitelist.py
devonh a70d14f
Add fed whitelist test that filters duplicates
devonh 77bd7b2
Update fed whitelist docstring for clarity
devonh 75a3ec2
Move config option under federation
devonh 320fb3e
Version federation whitelist endpoint
devonh e3794ad
Remove reference to extension feature
devonh a75e5b3
Merge branch 'develop' into erikj/federation_whitelist
devonh 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 hidden or 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 @@ | ||
| Add a feature that allows clients to query the configured federation whitelist. Disabled by default. |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,66 @@ | ||
| # | ||
| # This file is licensed under the Affero General Public License (AGPL) version 3. | ||
| # | ||
| # Copyright (C) 2024 New Vector, Ltd | ||
| # | ||
| # This program is free software: you can redistribute it and/or modify | ||
| # it under the terms of the GNU Affero General Public License as | ||
| # published by the Free Software Foundation, either version 3 of the | ||
| # License, or (at your option) any later version. | ||
| # | ||
| # See the GNU Affero General Public License for more details: | ||
| # <https://www.gnu.org/licenses/agpl-3.0.html>. | ||
| # | ||
|
|
||
| import logging | ||
| from typing import TYPE_CHECKING, Tuple | ||
|
|
||
| from synapse.http.server import DirectServeJsonResource | ||
| from synapse.http.site import SynapseRequest | ||
| from synapse.types import JsonDict | ||
|
|
||
| if TYPE_CHECKING: | ||
| from synapse.server import HomeServer | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class FederationWhitelistResource(DirectServeJsonResource): | ||
| """Custom endpoint (disabled by default) to fetch the federation whitelist | ||
| config. | ||
|
|
||
| Only enabled if `federation_whitelist_endpoint_enabled` feature is enabled. | ||
|
|
||
| Response format: | ||
|
|
||
| { | ||
| "whitelist_enabled": true, // Whether the federation whitelist is being enforced | ||
| "whitelist": [ // Which server names are allowed by the whitelist | ||
| "example.com" | ||
| ] | ||
| } | ||
| """ | ||
|
|
||
| PATH = "/_synapse/client/v1/config/federation_whitelist" | ||
|
|
||
| def __init__(self, hs: "HomeServer"): | ||
| super().__init__() | ||
|
|
||
| self._federation_whitelist = hs.config.federation.federation_domain_whitelist | ||
|
|
||
| self._auth = hs.get_auth() | ||
|
|
||
| async def _async_render_GET(self, request: SynapseRequest) -> Tuple[int, JsonDict]: | ||
| await self._auth.get_user_by_req(request) | ||
|
|
||
| whitelist = [] | ||
| if self._federation_whitelist: | ||
| # federation_whitelist is actually a dict, not a list | ||
| whitelist = list(self._federation_whitelist) | ||
|
|
||
| return_dict: JsonDict = { | ||
| "whitelist_enabled": self._federation_whitelist is not None, | ||
| "whitelist": whitelist, | ||
| } | ||
|
|
||
| return 200, return_dict |
This file contains hidden or 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,12 @@ | ||
| # | ||
| # This file is licensed under the Affero General Public License (AGPL) version 3. | ||
| # | ||
| # Copyright (C) 2024 New Vector, Ltd | ||
| # | ||
| # This program is free software: you can redistribute it and/or modify | ||
| # it under the terms of the GNU Affero General Public License as | ||
| # published by the Free Software Foundation, either version 3 of the | ||
| # License, or (at your option) any later version. | ||
| # | ||
| # See the GNU Affero General Public License for more details: | ||
| # <https://www.gnu.org/licenses/agpl-3.0.html>. |
This file contains hidden or 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,12 @@ | ||
| # | ||
| # This file is licensed under the Affero General Public License (AGPL) version 3. | ||
| # | ||
| # Copyright (C) 2024 New Vector, Ltd | ||
| # | ||
| # This program is free software: you can redistribute it and/or modify | ||
| # it under the terms of the GNU Affero General Public License as | ||
| # published by the Free Software Foundation, either version 3 of the | ||
| # License, or (at your option) any later version. | ||
| # | ||
| # See the GNU Affero General Public License for more details: | ||
| # <https://www.gnu.org/licenses/agpl-3.0.html>. |
This file contains hidden or 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,119 @@ | ||
| # | ||
| # This file is licensed under the Affero General Public License (AGPL) version 3. | ||
| # | ||
| # Copyright (C) 2024 New Vector, Ltd | ||
| # | ||
| # This program is free software: you can redistribute it and/or modify | ||
| # it under the terms of the GNU Affero General Public License as | ||
| # published by the Free Software Foundation, either version 3 of the | ||
| # License, or (at your option) any later version. | ||
| # | ||
| # See the GNU Affero General Public License for more details: | ||
| # <https://www.gnu.org/licenses/agpl-3.0.html>. | ||
|
|
||
| from typing import Dict | ||
|
|
||
| from twisted.web.resource import Resource | ||
|
|
||
| from synapse.rest import admin | ||
| from synapse.rest.client import login | ||
| from synapse.rest.synapse.client import build_synapse_client_resource_tree | ||
|
|
||
| from tests import unittest | ||
|
|
||
|
|
||
| class FederationWhitelistTests(unittest.HomeserverTestCase): | ||
| servlets = [ | ||
| admin.register_servlets_for_client_rest_resource, | ||
| login.register_servlets, | ||
| ] | ||
|
|
||
| def create_resource_dict(self) -> Dict[str, Resource]: | ||
| base = super().create_resource_dict() | ||
| base.update(build_synapse_client_resource_tree(self.hs)) | ||
| return base | ||
|
|
||
| def test_default(self) -> None: | ||
| "If the config option is not enabled, the endpoint should 404" | ||
| channel = self.make_request( | ||
| "GET", "/_synapse/client/v1/config/federation_whitelist", shorthand=False | ||
| ) | ||
|
|
||
| self.assertEqual(channel.code, 404) | ||
|
|
||
| @unittest.override_config({"federation_whitelist_endpoint_enabled": True}) | ||
| def test_no_auth(self) -> None: | ||
| "Endpoint requires auth when enabled" | ||
|
|
||
| channel = self.make_request( | ||
| "GET", "/_synapse/client/v1/config/federation_whitelist", shorthand=False | ||
| ) | ||
|
|
||
| self.assertEqual(channel.code, 401) | ||
|
|
||
| @unittest.override_config({"federation_whitelist_endpoint_enabled": True}) | ||
| def test_no_whitelist(self) -> None: | ||
| "Test when there is no whitelist configured" | ||
|
|
||
| self.register_user("user", "password") | ||
| tok = self.login("user", "password") | ||
|
|
||
| channel = self.make_request( | ||
| "GET", | ||
| "/_synapse/client/v1/config/federation_whitelist", | ||
| shorthand=False, | ||
| access_token=tok, | ||
| ) | ||
|
|
||
| self.assertEqual(channel.code, 200) | ||
| self.assertEqual( | ||
| channel.json_body, {"whitelist_enabled": False, "whitelist": []} | ||
| ) | ||
|
|
||
| @unittest.override_config( | ||
| { | ||
| "federation_whitelist_endpoint_enabled": True, | ||
| "federation_domain_whitelist": ["example.com"], | ||
| } | ||
| ) | ||
| def test_whitelist(self) -> None: | ||
| "Test when there is a whitelist configured" | ||
|
|
||
| self.register_user("user", "password") | ||
| tok = self.login("user", "password") | ||
|
|
||
| channel = self.make_request( | ||
| "GET", | ||
| "/_synapse/client/v1/config/federation_whitelist", | ||
| shorthand=False, | ||
| access_token=tok, | ||
| ) | ||
|
|
||
| self.assertEqual(channel.code, 200) | ||
| self.assertEqual( | ||
| channel.json_body, {"whitelist_enabled": True, "whitelist": ["example.com"]} | ||
| ) | ||
|
|
||
| @unittest.override_config( | ||
| { | ||
| "federation_whitelist_endpoint_enabled": True, | ||
| "federation_domain_whitelist": ["example.com", "example.com"], | ||
| } | ||
| ) | ||
| def test_whitelist_no_duplicates(self) -> None: | ||
| "Test when there is a whitelist configured with duplicates, no duplicates are returned" | ||
|
|
||
| self.register_user("user", "password") | ||
| tok = self.login("user", "password") | ||
|
|
||
| channel = self.make_request( | ||
| "GET", | ||
| "/_synapse/client/v1/config/federation_whitelist", | ||
| shorthand=False, | ||
| access_token=tok, | ||
| ) | ||
|
|
||
| self.assertEqual(channel.code, 200) | ||
| self.assertEqual( | ||
| channel.json_body, {"whitelist_enabled": True, "whitelist": ["example.com"]} | ||
| ) | ||
devonh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.