From 38611c55f215d6b5d91f0e5322b444a4af7fdbe4 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 9 Sep 2022 20:58:09 -0400 Subject: [PATCH 1/2] Bump jsonschema from 4.15.0 to 4.16.0 (#303) Bumps [jsonschema](https://github.com/python-jsonschema/jsonschema) from 4.15.0 to 4.16.0. - [Release notes](https://github.com/python-jsonschema/jsonschema/releases) - [Changelog](https://github.com/python-jsonschema/jsonschema/blob/main/CHANGELOG.rst) - [Commits](https://github.com/python-jsonschema/jsonschema/compare/v4.15.0...v4.16.0) --- updated-dependencies: - dependency-name: jsonschema dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- Pipfile | 2 +- Pipfile.lock | 42 +++++++++++++++++++++--------------------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/Pipfile b/Pipfile index 1ccc261..9e9f3a8 100644 --- a/Pipfile +++ b/Pipfile @@ -8,7 +8,7 @@ asgiref = "==3.5.2" attrs = "==22.1.0" django-mailer = "==2.2" importlib-resources = "==5.9.0" -jsonschema = "==4.15.0" +jsonschema = "==4.16.0" ldap3 = "==2.9.1" lockfile = "==0.12.2" mysqlclient = "==2.1.1" diff --git a/Pipfile.lock b/Pipfile.lock index a2e4a1f..11705f8 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "2b4b101dcfda400e31de68837f332d6f353dc8bc92d0163736f8971f2ca47be4" + "sha256": "c0022dd79f93ca64cf26e8bd539953ea94c2d49917c0cc29f166589036632c2f" }, "pipfile-spec": 6, "requires": { @@ -37,23 +37,23 @@ "sha256:58d5c3d29f5a36ffeb94f02f0d786cd53014cf9b3b3951d42e0080d8a9498d30", "sha256:ad9aa55b65ef2808eb405f46cf74df7fcb7044d5cbc26487f96eb2ef2e436693" ], - "markers": "python_full_version >= '3.6.0'", + "markers": "python_version >= '3.6'", "version": "==4.11.1" }, "certifi": { "hashes": [ - "sha256:84c85a9078b11105f04f3036a9482ae10e4621616db313fe045dd24743a0820d", - "sha256:fe86415d55e84719d75f8b69414f6438ac3547d2078ab91b67e779ef69378412" + "sha256:43dadad18a7f168740e66944e4fa82c6611848ff9056ad910f8f7a3e46ab89e0", + "sha256:cffdcd380919da6137f76633531a5817e3a9f268575c128249fb637e4f9e73fb" ], - "markers": "python_full_version >= '3.6.0'", - "version": "==2022.6.15" + "markers": "python_version >= '3.6'", + "version": "==2022.6.15.1" }, "charset-normalizer": { "hashes": [ "sha256:5a3d016c7c547f69d6f81fb0db9449ce888b418b5b9952cc5e6e66843e9dd845", "sha256:83e9a75d1911279afd89352c68b45348559d1fc0506b054b346651b5e7fee29f" ], - "markers": "python_full_version >= '3.6.0'", + "markers": "python_version >= '3.6'", "version": "==2.1.1" }, "django": { @@ -98,11 +98,11 @@ }, "jsonschema": { "hashes": [ - "sha256:21f4979391bdceb044e502fd8e79e738c0cdfbdc8773f9a49b5769461e82fe1e", - "sha256:2df0fab225abb3b41967bb3a46fd37dc74b1536b5296d0b1c2078cd072adf0f7" + "sha256:165059f076eff6971bae5b742fc029a7b4ef3f9bcf04c14e4776a7605de14b23", + "sha256:9e74b8f9738d6a946d70705dc692b74b5429cd0960d58e79ffecfc43b2221eb9" ], "index": "pypi", - "version": "==4.15.0" + "version": "==4.16.0" }, "ldap3": { "hashes": [ @@ -146,11 +146,11 @@ }, "oauthlib": { "hashes": [ - "sha256:23a8208d75b902797ea29fd31fa80a15ed9dc2c6c16fe73f5d346f83f6fa27a2", - "sha256:6db33440354787f9b7f3a6dbd4febf5d0f93758354060e802f6c06cb493022fe" + "sha256:1565237372795bf6ee3e5aba5e2a85bd5a65d0e2aa5c628b9a97b7d7a0da3721", + "sha256:88e912ca1ad915e1dcc1c06fc9259d19de8deacd6fd17cc2df266decc2e49066" ], - "markers": "python_full_version >= '3.6.0'", - "version": "==3.2.0" + "markers": "python_version >= '3.6'", + "version": "==3.2.1" }, "pyasn1": { "hashes": [ @@ -259,7 +259,7 @@ "sha256:3b2503d3c7084a42b1ebd08116e5f81aadfaea95863628c80a3b774a11b7c759", "sha256:fc53893b3da2c33de295667a0e19f078c14bf86544af307354de5fcf12a3f30d" ], - "markers": "python_full_version >= '3.6.0'", + "markers": "python_version >= '3.6'", "version": "==2.3.2.post1" }, "sqlparse": { @@ -289,7 +289,7 @@ "sha256:89885494684c929d9191c57aa27502afc87a579be5cdd3225c77c463ea043745", "sha256:ee5842fa3a795f023514ac2d801c4a81d1743bbe642e3940143326b3a00addd7" ], - "markers": "python_full_version >= '3.6.0'", + "markers": "python_version >= '3.6'", "version": "==4.2" }, "urllib3": { @@ -327,18 +327,18 @@ }, "certifi": { "hashes": [ - "sha256:84c85a9078b11105f04f3036a9482ae10e4621616db313fe045dd24743a0820d", - "sha256:fe86415d55e84719d75f8b69414f6438ac3547d2078ab91b67e779ef69378412" + "sha256:43dadad18a7f168740e66944e4fa82c6611848ff9056ad910f8f7a3e46ab89e0", + "sha256:cffdcd380919da6137f76633531a5817e3a9f268575c128249fb637e4f9e73fb" ], - "markers": "python_full_version >= '3.6.0'", - "version": "==2022.6.15" + "markers": "python_version >= '3.6'", + "version": "==2022.6.15.1" }, "charset-normalizer": { "hashes": [ "sha256:5a3d016c7c547f69d6f81fb0db9449ce888b418b5b9952cc5e6e66843e9dd845", "sha256:83e9a75d1911279afd89352c68b45348559d1fc0506b054b346651b5e7fee29f" ], - "markers": "python_full_version >= '3.6.0'", + "markers": "python_version >= '3.6'", "version": "==2.1.1" }, "click": { -- knotmirror 0.0.1 From 732bfbf97baf35432cebe49df2758a1c0c49efed Mon Sep 17 00:00:00 2001 From: Ben C Date: Mon, 12 Sep 2022 08:47:05 -0400 Subject: [PATCH 2/2] Added validation for users not in LDAP_BASE_CONTEXT --- Users/ldap_auth.py | 10 ++++++++-- Users/ldap_errors.py | 9 +++++++++ Users/ldap_mock.py | 20 +++++++++++++++++++- tests/test_auth.py | 18 +++++++++++++++++- 4 files changed, 53 insertions(+), 4 deletions(-) diff --git a/Users/ldap_auth.py b/Users/ldap_auth.py index 2d2d3d6..ea6a078 100644 --- a/Users/ldap_auth.py +++ b/Users/ldap_auth.py @@ -12,7 +12,7 @@ from django.db.models import Q from ldap3 import Connection, Server, ALL, ObjectDef, Reader, NTLM, Entry from ldap3.core.exceptions import LDAPSocketOpenError -from .ldap_errors import LDAPConnectionError, LDAPInvalidCredentials, LDAPAuthException +from .ldap_errors import LDAPConnectionError, LDAPInvalidCredentials, LDAPNotInContextException, LDAPAuthException from .models import User @@ -180,7 +180,10 @@ class LDAPAuthentication(BaseBackend): reader = self.get_all_users(conn) results = reader.match("msDS-PrincipalName", username) - return results[0] + if len(results) == 0: + raise LDAPNotInContextException() + else: + return results[0] def authenticate(self, request, username: str = None, password: str = None) -> Optional[User]: """ @@ -203,6 +206,9 @@ class LDAPAuthentication(BaseBackend): return self.create_from_ldap(ldap_user, guid) except LDAPInvalidCredentials: return None + except LDAPNotInContextException: + messages.add_message(request, messages.ERROR, "This user is not allowed to login to CodeReview, please contact your administrator.") + return None except LDAPConnectionError: if settings.DEBUG is False: messages.add_message(request, messages.ERROR, diff --git a/Users/ldap_errors.py b/Users/ldap_errors.py index 9b4a748..112b3cb 100644 --- a/Users/ldap_errors.py +++ b/Users/ldap_errors.py @@ -23,3 +23,12 @@ class LDAPConnectionError(LDAPAuthException): """ This exception is used when we can't contact ActiveDirectory """ + pass + + +class LDAPNotInContextException(LDAPAuthException): + """ + This exception is used when we can't find the user in the LDAP_BASE_CONTEXT, this could mean that the user is not in our class/campus + """ + pass + diff --git a/Users/ldap_mock.py b/Users/ldap_mock.py index 6726009..d63e4b8 100644 --- a/Users/ldap_mock.py +++ b/Users/ldap_mock.py @@ -28,6 +28,7 @@ class LDAPMockAuthentication(LDAPAuthentication): 'tests/ldap_test_server/test_schema.json') users = {} + raw_users = {} @classmethod def construct_dn(cls, username: str) -> str: @@ -56,6 +57,19 @@ class LDAPMockAuthentication(LDAPAuthentication): user = cls.users.get(username, {'ou': None}) return user.get('ou') + @classmethod + def create_user_manual(cls, conn: Connection, dn: str, user: dict) -> None: + """ + This function creates a user manually without automatic setup of the dn and other values + + :param dn: Distinguished name of the user + :type dn: str + :param user: The dictionary to use when creating the user entry + :type user: dict + """ + + conn.strategy.add_entry(dn, user) + @classmethod def create_fake_user(cls, conn: Connection, username: str, email: str, password: str, first: str, last: str, ou: str = None) -> None: """ @@ -99,6 +113,9 @@ class LDAPMockAuthentication(LDAPAuthentication): for username in cls.users.keys(): user = cls.users.get(username) cls.create_fake_user(conn, username, f"{username}@example.com", user['password'], user['first'], user['last'], user['ou']) + for dn in cls.raw_users.keys(): + user = cls.raw_users.get(dn) + cls.create_user_manual(conn, dn, user) @classmethod def get_connection(cls, username: str, password: str) -> Connection: @@ -116,7 +133,8 @@ class LDAPMockAuthentication(LDAPAuthentication): cls.setup_server() username = username.split('\\')[1] - conn = Connection(cls.server, cls.construct_dn(username), password, + dn = cls.construct_dn(username) if username not in [k.split(',')[0].split('=')[1] for k in cls.raw_users.keys()] else f"cn={username},ou=EvilPeople,dc=example,dc=com" + conn = Connection(cls.server, dn, password, client_strategy=MOCK_SYNC) cls.create_fake_users(conn) return cls.bind_connection(conn) diff --git a/tests/test_auth.py b/tests/test_auth.py index a09c295..166d525 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -18,7 +18,7 @@ class LogoutTest(SimpleBaseCase): @override_settings(AUTHENTICATION_BACKENDS=['Users.ldap_mock.LDAPMockAuthentication'], LDAP_DOMAIN="example", - LDAP_BASE_CONTEXT="ou=ITP Users,dc=itp,dc=example", LDAP_URL="0.0.0.0", LDAP_ADMIN_NAME="example\\admin") + LDAP_BASE_CONTEXT="ou=ITP Users,dc=itp,dc=example,dc=com", LDAP_URL="0.0.0.0", LDAP_ADMIN_NAME="example\\admin") class LDAPAuthTest(SimpleBaseCase): url = reverse('login') @@ -50,6 +50,18 @@ class LDAPAuthTest(SimpleBaseCase): 'ou': "AM" } } + LDAPMockAuthentication.raw_users = { + 'cn=bob,ou=EvilPeople,dc=example,dc=com': { + 'objectGUID': str(uuid4()), + 'objectClass': 'user', + 'mail': 'b.evilguy@evilcorp.bad', + 'distinguishedName': 'cn=bob,ou=EvilPeople,dc=example,dc=com', + 'msDs-principalName': "example\\bob", + 'userPassword': "imsoevil123", + 'givenName': "Bobert", + 'sn': 'EvilGuy' + } + } def test_admin_login(self) -> None: self.client.post(self.url, {'username': "admin", "password": "admin_password123"}) @@ -95,6 +107,10 @@ class LDAPAuthTest(SimpleBaseCase): response = self.client.post(self.url, {'username': "invalid_user", 'password': "invalid_password"}) self.assertEqual(len(response.context.get('form').non_field_errors()), 1) + def test_not_in_context(self) -> None: + response = self.client.post(self.url, {'username': "bob", 'password': "imsoevil123"}) + self.assertMessage(response, "This user is not allowed to login to CodeReview, please contact your administrator.") + @override_settings(AUTHENTICATION_BACKENDS=['Users.ldap_auth.LDAPAuthentication']) def test_cant_connect(self) -> None: response = self.client.post(self.url, {'username': "admin", 'password': "admin_password123"}) -- knotmirror 0.0.1