This repository was 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
Drop support for calling /_matrix/client/v3/rooms/{roomId}/invite without an id_access_token
#13241
Merged
Merged
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
0f6487e
Drop v1 lookup
Vetchu ef1c794
Add changelog
Vetchu e9c789a
Address code review
Vetchu e354ec6
Repair tests PoC.
Vetchu eb5ba09
Update changelog.d/13241.removal
Vetchu 72313f6
merge develop
Vetchu 779e78c
Add tests, address concerns
Vetchu 5668dcd
minify test
Vetchu 6e908e4
missing comma
Vetchu 242fa48
Merge branch 'develop' into misc/drop-v1-lookup
Vetchu 6698279
Address comments
Vetchu 48d88f0
Merge branch 'misc/drop-v1-lookup' of github.com:Vetchu/synapse into …
Vetchu 6b1413b
Merge branch 'develop' into misc/drop-v1-lookup
Vetchu dacd33d
Merge branch 'matrix-org:develop' into misc/drop-v1-lookup
Vetchu 9a04234
Update tests/rest/client/test_rooms.py
Vetchu e115f60
addressed the comments
Vetchu c535ea8
Merge branch 'develop' into misc/drop-v1-lookup
Vetchu f2a74b2
add missing check
Vetchu bb84fe4
Merge branch 'develop' into misc/drop-v1-lookup
Vetchu 8ab4b35
Merge branch 'develop' into misc/drop-v1-lookup
Vetchu f9600bb
fixed test
Vetchu 0073118
catch missing param for 3pid
Vetchu ebcdbba
synapseerror
Vetchu b97b27f
validate earlier
Vetchu 23be3b9
merge develop
Vetchu d1b6ae8
Merge remote-tracking branch 'origin' into misc/drop-v1-lookup
Vetchu 21d9be1
Next iteration
Vetchu caeee4d
allow exception to go up
Vetchu 3792e8c
Merge branch 'develop' into misc/drop-v1-lookup
Vetchu 3dfd8fb
beautify code
Vetchu 5b237aa
hopefully addresses the comments
Vetchu 27951c5
Update synapse/handlers/room.py
richvdh c6aa582
Merge branch 'develop' into misc/drop-v1-lookup
richvdh 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 @@ | ||
| Drop support for calling `/_matrix/client/v3/rooms/{roomId}/invite` without an `id_access_token`, which was not permitted by the spec. Contributed by @Vetchu. |
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 |
|---|---|---|
|
|
@@ -538,11 +538,7 @@ async def proxy_msisdn_submit_token( | |
| raise SynapseError(400, "Error contacting the identity server") | ||
|
|
||
| async def lookup_3pid( | ||
| self, | ||
| id_server: str, | ||
| medium: str, | ||
| address: str, | ||
| id_access_token: Optional[str] = None, | ||
| self, id_server: str, medium: str, address: str, id_access_token: str | ||
| ) -> Optional[str]: | ||
| """Looks up a 3pid in the passed identity server. | ||
|
|
||
|
|
@@ -557,60 +553,15 @@ async def lookup_3pid( | |
| Returns: | ||
| the matrix ID of the 3pid, or None if it is not recognized. | ||
| """ | ||
| if id_access_token is not None: | ||
| try: | ||
| results = await self._lookup_3pid_v2( | ||
| id_server, id_access_token, medium, address | ||
| ) | ||
| return results | ||
|
|
||
| except Exception as e: | ||
| # Catch HttpResponseExcept for a non-200 response code | ||
| # Check if this identity server does not know about v2 lookups | ||
| if isinstance(e, HttpResponseException) and e.code == 404: | ||
| # This is an old identity server that does not yet support v2 lookups | ||
| logger.warning( | ||
| "Attempted v2 lookup on v1 identity server %s. Falling " | ||
| "back to v1", | ||
| id_server, | ||
| ) | ||
| else: | ||
| logger.warning("Error when looking up hashing details: %s", e) | ||
| return None | ||
|
|
||
| return await self._lookup_3pid_v1(id_server, medium, address) | ||
|
|
||
| async def _lookup_3pid_v1( | ||
| self, id_server: str, medium: str, address: str | ||
| ) -> Optional[str]: | ||
| """Looks up a 3pid in the passed identity server using v1 lookup. | ||
|
|
||
| Args: | ||
| id_server: The server name (including port, if required) | ||
| of the identity server to use. | ||
| medium: The type of the third party identifier (e.g. "email"). | ||
| address: The third party identifier (e.g. "[email protected]"). | ||
|
|
||
| Returns: | ||
| the matrix ID of the 3pid, or None if it is not recognized. | ||
| """ | ||
| try: | ||
| data = await self.blacklisting_http_client.get_json( | ||
| "%s%s/_matrix/identity/api/v1/lookup" % (id_server_scheme, id_server), | ||
| {"medium": medium, "address": address}, | ||
| results = await self._lookup_3pid_v2( | ||
| id_server, id_access_token, medium, address | ||
| ) | ||
|
|
||
| if "mxid" in data: | ||
| # note: we used to verify the identity server's signature here, but no longer | ||
| # require or validate it. See the following for context: | ||
| # https://github.com/matrix-org/synapse/issues/5253#issuecomment-666246950 | ||
| return data["mxid"] | ||
| except RequestTimedOutError: | ||
| raise SynapseError(500, "Timed out contacting identity server") | ||
| except OSError as e: | ||
| logger.warning("Error from v1 identity server lookup: %s" % (e,)) | ||
|
|
||
| return None | ||
| return results | ||
| except Exception as e: | ||
| logger.warning("Error when looking up hashing details: %s", e) | ||
| return None | ||
|
|
||
| async def _lookup_3pid_v2( | ||
| self, id_server: str, id_access_token: str, medium: str, address: str | ||
|
|
@@ -739,7 +690,7 @@ async def ask_id_server_for_third_party_invite( | |
| room_type: Optional[str], | ||
| inviter_display_name: str, | ||
| inviter_avatar_url: str, | ||
| id_access_token: Optional[str] = None, | ||
| id_access_token: str, | ||
| ) -> Tuple[str, List[Dict[str, str]], Dict[str, str], str]: | ||
| """ | ||
| Asks an identity server for a third party invite. | ||
|
|
@@ -760,7 +711,7 @@ async def ask_id_server_for_third_party_invite( | |
| inviter_display_name: The current display name of the | ||
| inviter. | ||
| inviter_avatar_url: The URL of the inviter's avatar. | ||
| id_access_token (str|None): The access token to authenticate to the identity | ||
| id_access_token (str): The access token to authenticate to the identity | ||
| server with | ||
|
|
||
| Returns: | ||
|
|
@@ -792,71 +743,24 @@ async def ask_id_server_for_third_party_invite( | |
| invite_config["org.matrix.web_client_location"] = self._web_client_location | ||
|
|
||
| # Add the identity service access token to the JSON body and use the v2 | ||
| # Identity Service endpoints if id_access_token is present | ||
| # Identity Service endpoints | ||
| data = None | ||
| base_url = "%s%s/_matrix/identity" % (id_server_scheme, id_server) | ||
|
|
||
| if id_access_token: | ||
| key_validity_url = "%s%s/_matrix/identity/v2/pubkey/isvalid" % ( | ||
| id_server_scheme, | ||
| id_server, | ||
| ) | ||
| key_validity_url = "%s%s/_matrix/identity/v2/pubkey/isvalid" % ( | ||
| id_server_scheme, | ||
| id_server, | ||
| ) | ||
|
|
||
| # Attempt a v2 lookup | ||
| url = base_url + "/v2/store-invite" | ||
| try: | ||
| data = await self.blacklisting_http_client.post_json_get_json( | ||
| url, | ||
| invite_config, | ||
| {"Authorization": create_id_access_token_header(id_access_token)}, | ||
| ) | ||
| except RequestTimedOutError: | ||
| raise SynapseError(500, "Timed out contacting identity server") | ||
| except HttpResponseException as e: | ||
| if e.code != 404: | ||
| logger.info("Failed to POST %s with JSON: %s", url, e) | ||
| raise e | ||
|
|
||
| if data is None: | ||
| key_validity_url = "%s%s/_matrix/identity/api/v1/pubkey/isvalid" % ( | ||
| id_server_scheme, | ||
| id_server, | ||
| url = "%s%s/_matrix/identity/v2/store-invite" % (id_server_scheme, id_server) | ||
| try: | ||
| data = await self.blacklisting_http_client.post_json_get_json( | ||
| url, | ||
| invite_config, | ||
| {"Authorization": create_id_access_token_header(id_access_token)}, | ||
| ) | ||
| url = base_url + "/api/v1/store-invite" | ||
|
|
||
| try: | ||
| data = await self.blacklisting_http_client.post_json_get_json( | ||
| url, invite_config | ||
| ) | ||
| except RequestTimedOutError: | ||
| raise SynapseError(500, "Timed out contacting identity server") | ||
| except HttpResponseException as e: | ||
| logger.warning( | ||
| "Error trying to call /store-invite on %s%s: %s", | ||
| id_server_scheme, | ||
| id_server, | ||
| e, | ||
| ) | ||
|
|
||
| if data is None: | ||
| # Some identity servers may only support application/x-www-form-urlencoded | ||
| # types. This is especially true with old instances of Sydent, see | ||
| # https://github.com/matrix-org/sydent/pull/170 | ||
| try: | ||
| data = await self.blacklisting_http_client.post_urlencoded_get_json( | ||
| url, invite_config | ||
| ) | ||
| except HttpResponseException as e: | ||
| logger.warning( | ||
| "Error calling /store-invite on %s%s with fallback " | ||
| "encoding: %s", | ||
| id_server_scheme, | ||
| id_server, | ||
| e, | ||
| ) | ||
| raise e | ||
|
|
||
| # TODO: Check for success | ||
| except RequestTimedOutError: | ||
| raise SynapseError(500, "Timed out contacting identity server") | ||
|
|
||
| token = data["token"] | ||
| public_keys = data.get("public_keys", []) | ||
| if "public_key" in data: | ||
|
|
||
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
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 |
|---|---|---|
|
|
@@ -25,15 +25,13 @@ | |
|
|
||
|
|
||
| class IdentityTestCase(unittest.HomeserverTestCase): | ||
|
|
||
| servlets = [ | ||
| synapse.rest.admin.register_servlets_for_client_rest_resource, | ||
| room.register_servlets, | ||
| login.register_servlets, | ||
| ] | ||
|
|
||
| def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer: | ||
|
|
||
| config = self.default_config() | ||
| config["enable_3pid_lookup"] = False | ||
| self.hs = self.setup_test_homeserver(config=config) | ||
|
|
@@ -54,6 +52,7 @@ def test_3pid_lookup_disabled(self) -> None: | |
| "id_server": "testis", | ||
| "medium": "email", | ||
| "address": "[email protected]", | ||
| "id_access_token": tok, | ||
| } | ||
| request_url = ("/rooms/%s/invite" % (room_id)).encode("ascii") | ||
| channel = self.make_request( | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -3461,3 +3461,21 @@ def test_threepid_invite_spamcheck(self) -> None: | |
|
|
||
| # Also check that it stopped before calling _make_and_store_3pid_invite. | ||
| make_invite_mock.assert_called_once() | ||
|
|
||
| def test_400_missing_param_without_id_access_token(self) -> None: | ||
| """ | ||
| Test that a 3pid invite request returns 400 M_MISSING_PARAM | ||
| if we do not include id_access_token. | ||
| """ | ||
| channel = self.make_request( | ||
| method="POST", | ||
| path="/rooms/" + self.room_id + "/invite", | ||
| content={ | ||
| "id_server": "example.com", | ||
| "medium": "email", | ||
| "address": "[email protected]", | ||
| }, | ||
| access_token=self.tok, | ||
| ) | ||
| self.assertEqual(channel.code, 400) | ||
richvdh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| self.assertEqual(channel.json_body["errcode"], "M_MISSING_PARAM") | ||
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 |
|---|---|---|
|
|
@@ -97,7 +97,12 @@ def test_invite_3pid(self) -> None: | |
| channel = self.make_request( | ||
| "POST", | ||
| "/rooms/%s/invite" % (room_id,), | ||
| {"id_server": "test", "medium": "email", "address": "[email protected]"}, | ||
| { | ||
| "id_server": "test", | ||
| "medium": "email", | ||
| "address": "[email protected]", | ||
| "id_access_token": "anytoken", | ||
| }, | ||
| access_token=self.banned_access_token, | ||
| ) | ||
| self.assertEqual(200, channel.code, channel.result) | ||
|
|
||
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.