From 3f73a5a66e83c079ed4942298600cd38d39521e9 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Wed, 18 Nov 2020 14:08:43 -0500 Subject: [PATCH 01/12] Add basic SAML tests. --- changelog.d/8800.misc | 1 + tests/handlers/test_saml.py | 127 ++++++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 changelog.d/8800.misc create mode 100644 tests/handlers/test_saml.py diff --git a/changelog.d/8800.misc b/changelog.d/8800.misc new file mode 100644 index 000000000000..7824e367362b --- /dev/null +++ b/changelog.d/8800.misc @@ -0,0 +1 @@ +Add tests for SAML integration. diff --git a/tests/handlers/test_saml.py b/tests/handlers/test_saml.py new file mode 100644 index 000000000000..0667ba2e89d3 --- /dev/null +++ b/tests/handlers/test_saml.py @@ -0,0 +1,127 @@ +# Copyright 2020 The Matrix.org Foundation C.I.C. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from mock import Mock + +import attr + +from synapse.handlers.saml_handler import SamlHandler +from synapse.handlers.sso import MappingException + +from tests.unittest import HomeserverTestCase + +# These are a few constants that are used as config parameters in the tests. +BASE_URL = "https://synapse/" + + +@attr.s +class FakeAuthnResponse: + ava = attr.ib(type=dict) + + +class TestMappingProvider: + def __init__(self, config, module): + pass + + @staticmethod + def parse_config(config): + return + + @staticmethod + def get_saml_attributes(config): + return {"uid"}, {"displayName"} + + def get_remote_user_id(self, saml_response, client_redirect_url): + return saml_response.ava["uid"] + + def saml_response_to_user_attributes( + self, saml_response, failures, client_redirect_url + ): + return {"mxid_localpart": saml_response.ava["username"], "displayname": None} + + +class SamlHandlerTestCase(HomeserverTestCase): + def make_homeserver(self, reactor, clock): + + self.http_client = Mock(spec=["get_json"]) + self.http_client.user_agent = "Synapse Test" + + config = self.default_config() + config["public_baseurl"] = BASE_URL + saml_config = { + "sp_config": {"metadata": {}}, + # Disable grandfathering. + "grandfathered_mxid_source_attribute": None, + "user_mapping_provider": {"module": __name__ + ".TestMappingProvider"}, + } + config["saml2_config"] = saml_config + + hs = self.setup_test_homeserver( + http_client=self.http_client, + proxied_http_client=self.http_client, + config=config, + ) + + self.handler = SamlHandler(hs) + + return hs + + def test_map_saml_response_to_user(self): + """Ensure that mapping the SAML response returned from a provider to an MXID works properly.""" + saml_response = FakeAuthnResponse({"uid": "test_user", "username": "test_user"}) + # The redirect_url doesn't matter with the default user mapping provider. + redirect_url = "" + mxid = self.get_success( + self.handler._map_saml_response_to_user( + saml_response, redirect_url, "user-agent", "10.10.10.10" + ) + ) + self.assertEqual(mxid, "@test_user:test") + + # Some providers return an integer ID. + saml_response = FakeAuthnResponse({"uid": 1234, "username": "test_user_2"}) + mxid = self.get_success( + self.handler._map_saml_response_to_user( + saml_response, redirect_url, "user-agent", "10.10.10.10" + ) + ) + self.assertEqual(mxid, "@test_user_2:test") + + # Test if the mxid is already taken + store = self.hs.get_datastore() + self.get_success( + store.register_user(user_id="@test_user_3:test", password_hash=None) + ) + saml_response = FakeAuthnResponse({"uid": "test3", "username": "test_user_3"}) + e = self.get_failure( + self.handler._map_saml_response_to_user( + saml_response, redirect_url, "user-agent", "10.10.10.10" + ), + MappingException, + ) + self.assertEqual( + str(e.value), "Unable to generate a Matrix ID from the SAML response" + ) + + def test_map_saml_response_to_invalid_localpart(self): + """If the mapping provider generates an invalid localpart it should be rejected.""" + saml_response = FakeAuthnResponse({"uid": "test2", "username": "föö"}) + redirect_url = "" + e = self.get_failure( + self.handler._map_saml_response_to_user( + saml_response, redirect_url, "user-agent", "10.10.10.10" + ), + MappingException, + ) + self.assertEqual(str(e.value), "localpart is invalid: föö") From 3949b5890f12ec1eae8734012b2923025744d834 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Tue, 24 Nov 2020 08:43:48 -0500 Subject: [PATCH 02/12] Install xmlsec1 for pysaml2. --- .buildkite/scripts/test_old_deps.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.buildkite/scripts/test_old_deps.sh b/.buildkite/scripts/test_old_deps.sh index cdb77b556ca6..9905c4bc4f3c 100755 --- a/.buildkite/scripts/test_old_deps.sh +++ b/.buildkite/scripts/test_old_deps.sh @@ -6,7 +6,7 @@ set -ex apt-get update -apt-get install -y python3.5 python3.5-dev python3-pip libxml2-dev libxslt-dev zlib1g-dev tox +apt-get install -y python3.5 python3.5-dev python3-pip libxml2-dev libxslt-dev xmlsec1 zlib1g-dev tox export LANG="C.UTF-8" From bb06478cbe12ed085a884f1e9784ffe32ab13214 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Mon, 30 Nov 2020 08:08:13 -0500 Subject: [PATCH 03/12] Remove some bits of the tests that were copied from OIDC. --- tests/handlers/test_saml.py | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/tests/handlers/test_saml.py b/tests/handlers/test_saml.py index 0667ba2e89d3..325741ba6041 100644 --- a/tests/handlers/test_saml.py +++ b/tests/handlers/test_saml.py @@ -89,21 +89,12 @@ def test_map_saml_response_to_user(self): ) self.assertEqual(mxid, "@test_user:test") - # Some providers return an integer ID. - saml_response = FakeAuthnResponse({"uid": 1234, "username": "test_user_2"}) - mxid = self.get_success( - self.handler._map_saml_response_to_user( - saml_response, redirect_url, "user-agent", "10.10.10.10" - ) - ) - self.assertEqual(mxid, "@test_user_2:test") - # Test if the mxid is already taken store = self.hs.get_datastore() self.get_success( - store.register_user(user_id="@test_user_3:test", password_hash=None) + store.register_user(user_id="@test_user_2:test", password_hash=None) ) - saml_response = FakeAuthnResponse({"uid": "test3", "username": "test_user_3"}) + saml_response = FakeAuthnResponse({"uid": "test2", "username": "test_user_2"}) e = self.get_failure( self.handler._map_saml_response_to_user( saml_response, redirect_url, "user-agent", "10.10.10.10" @@ -116,7 +107,7 @@ def test_map_saml_response_to_user(self): def test_map_saml_response_to_invalid_localpart(self): """If the mapping provider generates an invalid localpart it should be rejected.""" - saml_response = FakeAuthnResponse({"uid": "test2", "username": "föö"}) + saml_response = FakeAuthnResponse({"uid": "test", "username": "föö"}) redirect_url = "" e = self.get_failure( self.handler._map_saml_response_to_user( From 003b25e9ffed2e3129e928555f91ea8688bbf436 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Mon, 30 Nov 2020 08:17:43 -0500 Subject: [PATCH 04/12] Add an additional test for generating in-use usernames. --- synapse/handlers/saml_handler.py | 2 +- tests/handlers/test_oidc.py | 2 +- tests/handlers/test_saml.py | 53 +++++++++++++++++++++++--------- 3 files changed, 41 insertions(+), 16 deletions(-) diff --git a/synapse/handlers/saml_handler.py b/synapse/handlers/saml_handler.py index 34db10ffe43e..7ffad7d8af22 100644 --- a/synapse/handlers/saml_handler.py +++ b/synapse/handlers/saml_handler.py @@ -265,7 +265,7 @@ async def saml_response_to_remapped_user_attributes( return UserAttributes( localpart=result.get("mxid_localpart"), display_name=result.get("displayname"), - emails=result.get("emails"), + emails=result.get("emails", []), ) with (await self._mapping_lock.queue(self._auth_provider_id)): diff --git a/tests/handlers/test_oidc.py b/tests/handlers/test_oidc.py index e880d32be687..c5a07c15c733 100644 --- a/tests/handlers/test_oidc.py +++ b/tests/handlers/test_oidc.py @@ -832,7 +832,7 @@ def test_map_userinfo_to_user_retries(self): # test_user is already taken, so test_user1 gets registered instead. self.assertEqual(mxid, "@test_user1:test") - # Register all of the potential users for a particular username. + # Register all of the potential usernames for a particular username. self.get_success( store.register_user(user_id="@tester:test", password_hash=None) ) diff --git a/tests/handlers/test_saml.py b/tests/handlers/test_saml.py index 325741ba6041..313bf2fc9e92 100644 --- a/tests/handlers/test_saml.py +++ b/tests/handlers/test_saml.py @@ -48,7 +48,8 @@ def get_remote_user_id(self, saml_response, client_redirect_url): def saml_response_to_user_attributes( self, saml_response, failures, client_redirect_url ): - return {"mxid_localpart": saml_response.ava["username"], "displayname": None} + localpart = saml_response.ava["username"] + (str(failures) if failures else "") + return {"mxid_localpart": localpart, "displayname": None} class SamlHandlerTestCase(HomeserverTestCase): @@ -75,6 +76,9 @@ def make_homeserver(self, reactor, clock): self.handler = SamlHandler(hs) + # Reduce the number of attempts when generating MXIDs. + self.handler._sso_handler._MAP_USERNAME_RETRIES = 3 + return hs def test_map_saml_response_to_user(self): @@ -89,30 +93,51 @@ def test_map_saml_response_to_user(self): ) self.assertEqual(mxid, "@test_user:test") - # Test if the mxid is already taken - store = self.hs.get_datastore() - self.get_success( - store.register_user(user_id="@test_user_2:test", password_hash=None) - ) - saml_response = FakeAuthnResponse({"uid": "test2", "username": "test_user_2"}) + def test_map_saml_response_to_invalid_localpart(self): + """If the mapping provider generates an invalid localpart it should be rejected.""" + saml_response = FakeAuthnResponse({"uid": "test", "username": "föö"}) + redirect_url = "" e = self.get_failure( self.handler._map_saml_response_to_user( saml_response, redirect_url, "user-agent", "10.10.10.10" ), MappingException, ) - self.assertEqual( - str(e.value), "Unable to generate a Matrix ID from the SAML response" - ) + self.assertEqual(str(e.value), "localpart is invalid: föö") - def test_map_saml_response_to_invalid_localpart(self): - """If the mapping provider generates an invalid localpart it should be rejected.""" - saml_response = FakeAuthnResponse({"uid": "test", "username": "föö"}) + def test_map_userinfo_to_user_retries(self): + """The mapping provider can retry generating an MXID if the MXID is already in use.""" + store = self.hs.get_datastore() + self.get_success( + store.register_user(user_id="@test_user:test", password_hash=None) + ) + saml_response = FakeAuthnResponse({"uid": "test", "username": "test_user"}) redirect_url = "" + mxid = self.get_success( + self.handler._map_saml_response_to_user( + saml_response, redirect_url, "user-agent", "10.10.10.10" + ) + ) + # test_user is already taken, so test_user1 gets registered instead. + self.assertEqual(mxid, "@test_user1:test") + + # Register all of the potential usernames for a particular username. + self.get_success( + store.register_user(user_id="@tester:test", password_hash=None) + ) + for i in range(1, 3): + self.get_success( + store.register_user(user_id="@tester%d:test" % i, password_hash=None) + ) + + # Now attempt to map to a username, this will fail since all potential usernames are taken. + saml_response = FakeAuthnResponse({"uid": "tester", "username": "tester"}) e = self.get_failure( self.handler._map_saml_response_to_user( saml_response, redirect_url, "user-agent", "10.10.10.10" ), MappingException, ) - self.assertEqual(str(e.value), "localpart is invalid: föö") + self.assertEqual( + str(e.value), "Unable to generate a Matrix ID from the SSO response" + ) From 91792568c4bce845779980505bc64121b89bfab5 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Tue, 1 Dec 2020 12:38:30 -0500 Subject: [PATCH 05/12] Fix a left-over OIDC term. --- tests/handlers/test_saml.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/handlers/test_saml.py b/tests/handlers/test_saml.py index 313bf2fc9e92..3873da9b5493 100644 --- a/tests/handlers/test_saml.py +++ b/tests/handlers/test_saml.py @@ -105,7 +105,7 @@ def test_map_saml_response_to_invalid_localpart(self): ) self.assertEqual(str(e.value), "localpart is invalid: föö") - def test_map_userinfo_to_user_retries(self): + def test_map_saml_response_to_user_retries(self): """The mapping provider can retry generating an MXID if the MXID is already in use.""" store = self.hs.get_datastore() self.get_success( From 1de016a9b508f5a230b416a17d95369cc0ce3b1d Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Tue, 1 Dec 2020 12:47:41 -0500 Subject: [PATCH 06/12] Update comments. --- tests/handlers/test_oidc.py | 2 +- tests/handlers/test_saml.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/handlers/test_oidc.py b/tests/handlers/test_oidc.py index c5a07c15c733..0ca95e9b0c9a 100644 --- a/tests/handlers/test_oidc.py +++ b/tests/handlers/test_oidc.py @@ -832,7 +832,7 @@ def test_map_userinfo_to_user_retries(self): # test_user is already taken, so test_user1 gets registered instead. self.assertEqual(mxid, "@test_user1:test") - # Register all of the potential usernames for a particular username. + # Register all of the potential mxids for a particular OIDC username. self.get_success( store.register_user(user_id="@tester:test", password_hash=None) ) diff --git a/tests/handlers/test_saml.py b/tests/handlers/test_saml.py index 3873da9b5493..5d60b4677fa9 100644 --- a/tests/handlers/test_saml.py +++ b/tests/handlers/test_saml.py @@ -121,7 +121,7 @@ def test_map_saml_response_to_user_retries(self): # test_user is already taken, so test_user1 gets registered instead. self.assertEqual(mxid, "@test_user1:test") - # Register all of the potential usernames for a particular username. + # Register all of the potential mxids for a particular SAML username. self.get_success( store.register_user(user_id="@tester:test", password_hash=None) ) From 6a581821d740afb24e856fe3f2648a7447aeb2a4 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Tue, 1 Dec 2020 12:48:16 -0500 Subject: [PATCH 07/12] Remove unused HTTP code. --- tests/handlers/test_saml.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/tests/handlers/test_saml.py b/tests/handlers/test_saml.py index 5d60b4677fa9..828b83e16a9b 100644 --- a/tests/handlers/test_saml.py +++ b/tests/handlers/test_saml.py @@ -12,8 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -from mock import Mock - import attr from synapse.handlers.saml_handler import SamlHandler @@ -54,10 +52,6 @@ def saml_response_to_user_attributes( class SamlHandlerTestCase(HomeserverTestCase): def make_homeserver(self, reactor, clock): - - self.http_client = Mock(spec=["get_json"]) - self.http_client.user_agent = "Synapse Test" - config = self.default_config() config["public_baseurl"] = BASE_URL saml_config = { From ef2e79f2d112b41fa9b58837ba7a949f7dda20b2 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Tue, 1 Dec 2020 12:51:05 -0500 Subject: [PATCH 08/12] Use singletons for handlers. --- tests/handlers/test_oidc.py | 9 +++++---- tests/handlers/test_saml.py | 8 +++----- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/tests/handlers/test_oidc.py b/tests/handlers/test_oidc.py index 0ca95e9b0c9a..003c49885f98 100644 --- a/tests/handlers/test_oidc.py +++ b/tests/handlers/test_oidc.py @@ -23,7 +23,7 @@ from twisted.python.failure import Failure from twisted.web._newclient import ResponseDone -from synapse.handlers.oidc_handler import OidcError, OidcHandler, OidcMappingProvider +from synapse.handlers.oidc_handler import OidcError, OidcMappingProvider from synapse.handlers.sso import MappingException from synapse.types import UserID @@ -155,13 +155,14 @@ def make_homeserver(self, reactor, clock): config=config, ) - self.handler = OidcHandler(hs) + self.handler = hs.get_oidc_handler() + sso_handler = hs.get_sso_handler() # Mock the render error method. self.render_error = Mock(return_value=None) - self.handler._sso_handler.render_error = self.render_error + sso_handler.render_error = self.render_error # Reduce the number of attempts when generating MXIDs. - self.handler._sso_handler._MAP_USERNAME_RETRIES = 3 + sso_handler._MAP_USERNAME_RETRIES = 3 return hs diff --git a/tests/handlers/test_saml.py b/tests/handlers/test_saml.py index 828b83e16a9b..603362b73e47 100644 --- a/tests/handlers/test_saml.py +++ b/tests/handlers/test_saml.py @@ -14,7 +14,6 @@ import attr -from synapse.handlers.saml_handler import SamlHandler from synapse.handlers.sso import MappingException from tests.unittest import HomeserverTestCase @@ -63,15 +62,14 @@ def make_homeserver(self, reactor, clock): config["saml2_config"] = saml_config hs = self.setup_test_homeserver( - http_client=self.http_client, - proxied_http_client=self.http_client, config=config, ) - self.handler = SamlHandler(hs) + self.handler = hs.get_saml_handler() # Reduce the number of attempts when generating MXIDs. - self.handler._sso_handler._MAP_USERNAME_RETRIES = 3 + sso_handler = hs.get_sso_handler() + sso_handler._MAP_USERNAME_RETRIES = 3 return hs From 1ab9ad13e273ff9fc83d2c3b65cef4822b91585d Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Tue, 1 Dec 2020 12:56:01 -0500 Subject: [PATCH 09/12] Use default_config method. --- tests/handlers/test_oidc.py | 18 ++++++++++-------- tests/handlers/test_saml.py | 11 ++++++----- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/tests/handlers/test_oidc.py b/tests/handlers/test_oidc.py index 003c49885f98..7135b0ed7259 100644 --- a/tests/handlers/test_oidc.py +++ b/tests/handlers/test_oidc.py @@ -127,13 +127,8 @@ async def get_json(url): class OidcHandlerTestCase(HomeserverTestCase): - def make_homeserver(self, reactor, clock): - - self.http_client = Mock(spec=["get_json"]) - self.http_client.get_json.side_effect = get_json - self.http_client.user_agent = "Synapse Test" - - config = self.default_config() + def default_config(self): + config = super().default_config() config["public_baseurl"] = BASE_URL oidc_config = { "enabled": True, @@ -149,10 +144,17 @@ def make_homeserver(self, reactor, clock): oidc_config.update(config.get("oidc_config", {})) config["oidc_config"] = oidc_config + return config + + def make_homeserver(self, reactor, clock): + + self.http_client = Mock(spec=["get_json"]) + self.http_client.get_json.side_effect = get_json + self.http_client.user_agent = "Synapse Test" + hs = self.setup_test_homeserver( http_client=self.http_client, proxied_http_client=self.http_client, - config=config, ) self.handler = hs.get_oidc_handler() diff --git a/tests/handlers/test_saml.py b/tests/handlers/test_saml.py index 603362b73e47..79fd47036f40 100644 --- a/tests/handlers/test_saml.py +++ b/tests/handlers/test_saml.py @@ -50,8 +50,8 @@ def saml_response_to_user_attributes( class SamlHandlerTestCase(HomeserverTestCase): - def make_homeserver(self, reactor, clock): - config = self.default_config() + def default_config(self): + config = super().default_config() config["public_baseurl"] = BASE_URL saml_config = { "sp_config": {"metadata": {}}, @@ -61,9 +61,10 @@ def make_homeserver(self, reactor, clock): } config["saml2_config"] = saml_config - hs = self.setup_test_homeserver( - config=config, - ) + return config + + def make_homeserver(self, reactor, clock): + hs = self.setup_test_homeserver() self.handler = hs.get_saml_handler() From 837bbf9e239f392c03cb814248948c38e8eec17d Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Tue, 1 Dec 2020 13:12:18 -0500 Subject: [PATCH 10/12] Update changelog. --- changelog.d/8800.misc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.d/8800.misc b/changelog.d/8800.misc index 7824e367362b..57cca8fee51c 100644 --- a/changelog.d/8800.misc +++ b/changelog.d/8800.misc @@ -1 +1 @@ -Add tests for SAML integration. +Add additional error checking for OpenID Connect and SAML mapping providers. From 6b1ac9a3964c6f454e6daf749b08d831d0114e8d Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Tue, 1 Dec 2020 13:29:32 -0500 Subject: [PATCH 11/12] Lint. --- tests/handlers/test_oidc.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/handlers/test_oidc.py b/tests/handlers/test_oidc.py index 7135b0ed7259..073cee2c3f24 100644 --- a/tests/handlers/test_oidc.py +++ b/tests/handlers/test_oidc.py @@ -153,8 +153,7 @@ def make_homeserver(self, reactor, clock): self.http_client.user_agent = "Synapse Test" hs = self.setup_test_homeserver( - http_client=self.http_client, - proxied_http_client=self.http_client, + http_client=self.http_client, proxied_http_client=self.http_client, ) self.handler = hs.get_oidc_handler() From 6b77f3e8655dd9920dd4f26ba329bd04779d09fc Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Tue, 1 Dec 2020 13:30:58 -0500 Subject: [PATCH 12/12] Remove an unused parameter. --- tests/handlers/test_oidc.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/handlers/test_oidc.py b/tests/handlers/test_oidc.py index 073cee2c3f24..c9807a7b73b8 100644 --- a/tests/handlers/test_oidc.py +++ b/tests/handlers/test_oidc.py @@ -152,9 +152,7 @@ def make_homeserver(self, reactor, clock): self.http_client.get_json.side_effect = get_json self.http_client.user_agent = "Synapse Test" - hs = self.setup_test_homeserver( - http_client=self.http_client, proxied_http_client=self.http_client, - ) + hs = self.setup_test_homeserver(proxied_http_client=self.http_client) self.handler = hs.get_oidc_handler() sso_handler = hs.get_sso_handler()