1515# limitations under the License.
1616
1717"""Utilities for interacting with Identity Servers"""
18-
1918import logging
2019import urllib .parse
2120from typing import Awaitable , Callable , Dict , List , Optional , Tuple
3433from synapse .types import JsonDict , Requester
3534from synapse .util import json_decoder
3635from synapse .util .hash import sha256_and_url_safe_base64
37- from synapse .util .stringutils import assert_valid_client_secret , random_string
36+ from synapse .util .stringutils import (
37+ assert_valid_client_secret ,
38+ random_string ,
39+ valid_id_server_location ,
40+ )
3841
3942from ._base import BaseHandler
4043
@@ -172,6 +175,11 @@ async def bind_threepid(
172175 server with, if necessary. Required if use_v2 is true
173176 use_v2: Whether to use v2 Identity Service API endpoints. Defaults to True
174177
178+ Raises:
179+ SynapseError: On any of the following conditions
180+ - the supplied id_server is not a valid identity server name
181+ - we failed to contact the supplied identity server
182+
175183 Returns:
176184 The response from the identity server
177185 """
@@ -181,6 +189,12 @@ async def bind_threepid(
181189 if id_access_token is None :
182190 use_v2 = False
183191
192+ if not valid_id_server_location (id_server ):
193+ raise SynapseError (
194+ 400 ,
195+ "id_server must be a valid hostname with optional port and path components" ,
196+ )
197+
184198 # Decide which API endpoint URLs to use
185199 headers = {}
186200 bind_data = {"sid" : sid , "client_secret" : client_secret , "mxid" : mxid }
@@ -269,12 +283,21 @@ async def try_unbind_threepid_with_id_server(
269283 id_server: Identity server to unbind from
270284
271285 Raises:
272- SynapseError: If we failed to contact the identity server
286+ SynapseError: On any of the following conditions
287+ - the supplied id_server is not a valid identity server name
288+ - we failed to contact the supplied identity server
273289
274290 Returns:
275291 True on success, otherwise False if the identity
276292 server doesn't support unbinding
277293 """
294+
295+ if not valid_id_server_location (id_server ):
296+ raise SynapseError (
297+ 400 ,
298+ "id_server must be a valid hostname with optional port and path components" ,
299+ )
300+
278301 url = "https://%s/_matrix/identity/api/v1/3pid/unbind" % (id_server ,)
279302 url_bytes = "/_matrix/identity/api/v1/3pid/unbind" .encode ("ascii" )
280303
0 commit comments