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
Add federation_domain_whitelist option #2820
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3f79378
make replication tests pass on OSX
ara4n 4b090cb
add federation_domain_whitelist
ara4n 313a489
incorporate PR feedback
ara4n b424c16
fix tests
ara4n c776c52
Back out unrelated changes
richvdh fa80b49
fix thinko
ara4n 3869981
remove unreachable except block
ara4n 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
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 |
---|---|---|
|
@@ -55,6 +55,17 @@ def read_config(self, config): | |
"block_non_admin_invites", False, | ||
) | ||
|
||
# FIXME: federation_domain_whitelist needs sytests | ||
self.federation_domain_whitelist = None | ||
federation_domain_whitelist = config.get( | ||
"federation_domain_whitelist", None | ||
) | ||
# turn the whitelist into a hash for speed of lookup | ||
if federation_domain_whitelist is not None: | ||
self.federation_domain_whitelist = {} | ||
for domain in federation_domain_whitelist: | ||
self.federation_domain_whitelist[domain] = True | ||
|
||
if self.public_baseurl is not None: | ||
if self.public_baseurl[-1] != '/': | ||
self.public_baseurl += '/' | ||
|
@@ -210,6 +221,17 @@ def default_config(self, server_name, **kwargs): | |
# (except those sent by local server admins). The default is False. | ||
# block_non_admin_invites: True | ||
|
||
# Restrict federation to the following whitelist of domains. | ||
# N.B. we recommend also firewalling your federation listener to limit | ||
# inbound federation traffic as early as possible, rather than relying | ||
# purely on this application-layer restriction. If not specified, the | ||
# default is to whitelist nothing. | ||
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. s/nothing/everything? |
||
# | ||
# federation_domain_whitelist: | ||
# - lon.example.com | ||
# - nyc.example.com | ||
# - syd.example.com | ||
|
||
# List of ports that Synapse should listen on, their purpose and their | ||
# configuration. | ||
listeners: | ||
|
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,8 +34,9 @@ | |
|
||
from synapse.http.matrixfederationclient import MatrixFederationHttpClient | ||
from synapse.util.stringutils import random_string | ||
from synapse.api.errors import SynapseError, HttpResponseException, \ | ||
NotFoundError | ||
from synapse.api.errors import ( | ||
SynapseError, HttpResponseException, NotFoundError, FederationDeniedError, | ||
) | ||
|
||
from synapse.util.async import Linearizer | ||
from synapse.util.stringutils import is_ascii | ||
|
@@ -77,6 +78,8 @@ def __init__(self, hs): | |
self.recently_accessed_remotes = set() | ||
self.recently_accessed_locals = set() | ||
|
||
self.federation_domain_whitelist = hs.config.federation_domain_whitelist | ||
|
||
# List of StorageProviders where we should search for media and | ||
# potentially upload to. | ||
storage_providers = [] | ||
|
@@ -222,6 +225,12 @@ def get_remote_media(self, request, server_name, media_id, name): | |
Deferred: Resolves once a response has successfully been written | ||
to request | ||
""" | ||
if ( | ||
self.federation_domain_whitelist is not None and | ||
server_name not in self.federation_domain_whitelist | ||
): | ||
raise FederationDeniedError(server_name) | ||
|
||
self.mark_recently_accessed(server_name, media_id) | ||
|
||
# We linearize here to ensure that we don't try and download remote | ||
|
@@ -256,6 +265,12 @@ def get_remote_media_info(self, server_name, media_id): | |
Returns: | ||
Deferred[dict]: The media_info of the file | ||
""" | ||
if ( | ||
self.federation_domain_whitelist is not None and | ||
server_name not in self.federation_domain_whitelist | ||
): | ||
raise FederationDeniedError(server_name) | ||
|
||
# We linearize here to ensure that we don't try and download remote | ||
# media multiple times concurrently | ||
key = (server_name, media_id) | ||
|
@@ -371,6 +386,9 @@ def _download_remote_file(self, server_name, media_id, file_id): | |
except NotRetryingDestination: | ||
logger.warn("Not retrying destination %r", server_name) | ||
raise SynapseError(502, "Failed to fetch remote media") | ||
except FederationDeniedError as e: | ||
logger.info(e) | ||
raise e | ||
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. this should probably be Although, looking at it, I don't think you'll ever get here, because |
||
except Exception: | ||
logger.exception("Failed to fetch remote media %s/%s", | ||
server_name, media_id) | ||
|
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
Oops, something went wrong.
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.
It feels very odd that you can whitelist everything by having an empty whitelist. I wonder if it would be better to make an empty whitelist actually mean that nothing is allowed, and have the default be
*
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.
good point; fixed.