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

Commit

Permalink
Use federation ip blacklist for calls to unknown identity servers
Browse files Browse the repository at this point in the history
  • Loading branch information
anoadragon453 authored and richvdh committed Sep 23, 2019
1 parent 2c99c63 commit f5b805b
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 5 deletions.
1 change: 1 addition & 0 deletions changelog.d/6000.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Apply the federation blacklist to requests to identity servers.
3 changes: 3 additions & 0 deletions docs/sample_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ pid_file: DATADIR/homeserver.pid
# blacklist IP address CIDR ranges. If this option is not specified, or
# specified with an empty list, no ip range blacklist will be enforced.
#
# As of Synapse v1.4.0 this option also affects any outbound requests to identity
# servers provided by user input.
#
# (0.0.0.0 and :: are always blacklisted, whether or not they are explicitly
# listed here, since they correspond to unroutable addresses.)
#
Expand Down
3 changes: 3 additions & 0 deletions synapse/config/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,9 @@ def generate_config_section(
# blacklist IP address CIDR ranges. If this option is not specified, or
# specified with an empty list, no ip range blacklist will be enforced.
#
# As of Synapse v1.4.0 this option also affects any outbound requests to identity
# servers provided by user input.
#
# (0.0.0.0 and :: are always blacklisted, whether or not they are explicitly
# listed here, since they correspond to unroutable addresses.)
#
Expand Down
18 changes: 15 additions & 3 deletions synapse/handlers/identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
SynapseError,
)
from synapse.config.emailconfig import ThreepidBehaviour
from synapse.http.client import SimpleHttpClient
from synapse.util.stringutils import random_string

from ._base import BaseHandler
Expand All @@ -42,7 +43,12 @@ class IdentityHandler(BaseHandler):
def __init__(self, hs):
super(IdentityHandler, self).__init__(hs)

self.http_client = hs.get_simple_http_client()
self.http_client = SimpleHttpClient(hs)
# We create a blacklisting instance of SimpleHttpClient for contacting identity
# servers specified by clients
self.blacklisting_http_client = SimpleHttpClient(
hs, ip_blacklist=hs.config.federation_ip_range_blacklist
)
self.federation_http_client = hs.get_http_client()
self.hs = hs

Expand Down Expand Up @@ -143,7 +149,9 @@ def bind_threepid(
bind_url = "https://%s/_matrix/identity/api/v1/3pid/bind" % (id_server,)

try:
data = yield self.http_client.post_json_get_json(
# Use the blacklisting http client as this call is only to identity servers
# provided by a client
data = yield self.blacklisting_http_client.post_json_get_json(
bind_url, bind_data, headers=headers
)

Expand Down Expand Up @@ -246,7 +254,11 @@ def try_unbind_threepid_with_id_server(self, mxid, threepid, id_server):
headers = {b"Authorization": auth_headers}

try:
yield self.http_client.post_json_get_json(url, content, headers)
# Use the blacklisting http client as this call is only to identity servers
# provided by a client
yield self.blacklisting_http_client.post_json_get_json(
url, content, headers
)
changed = True
except HttpResponseException as e:
changed = False
Expand Down
7 changes: 6 additions & 1 deletion synapse/handlers/room_member.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from synapse.api.constants import EventTypes, Membership
from synapse.api.errors import AuthError, Codes, HttpResponseException, SynapseError
from synapse.handlers.identity import LookupAlgorithm, create_id_access_token_header
from synapse.http.client import SimpleHttpClient
from synapse.types import RoomID, UserID
from synapse.util.async_helpers import Linearizer
from synapse.util.distributor import user_joined_room, user_left_room
Expand Down Expand Up @@ -62,7 +63,11 @@ def __init__(self, hs):
self.auth = hs.get_auth()
self.state_handler = hs.get_state_handler()
self.config = hs.config
self.simple_http_client = hs.get_simple_http_client()
# We create a blacklisting instance of SimpleHttpClient for contacting identity
# servers specified by clients
self.simple_http_client = SimpleHttpClient(
hs, ip_blacklist=hs.config.federation_ip_range_blacklist
)

self.federation_handler = hs.get_handlers().federation_handler
self.directory_handler = hs.get_handlers().directory_handler
Expand Down
2 changes: 1 addition & 1 deletion synapse/http/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ def post_json_get_json(self, uri, post_json, headers=None):
Args:
uri (str):
post_json (object):
headers (dict[str, List[str]]|None): If not None, a map from
headers (dict[bytes, List[str]]|None): If not None, a map from
header name to a list of values for that header
Returns:
Expand Down

0 comments on commit f5b805b

Please sign in to comment.