π₯π₯π₯ Open-source Jira, Linear, Monday, and ClickUp alternative. Plane is a modern project management platform to manage tasks, sprints, docs, and triage.
TypeScript boardsbug-trackerdjangodockerganttissue-trackerjirajira-alternativekanbanlinearpostgresqlproduct-managementproject-managementproject-planningpythonreactredistypesciptvitework-management
fix(api): reject API key auth for deactivated user accounts (#9225)
sriram veeraghantafd16d033fccb
diff --git a/apps/api/plane/api/middleware/api_authentication.py b/apps/api/plane/api/middleware/api_authentication.pyindex abd8139857..ed03bbb8d4 100644--- a/apps/api/plane/api/middleware/api_authentication.py+++ b/apps/api/plane/api/middleware/api_authentication.py@@ -32,6 +32,7 @@ class APIKeyAuthentication(authentication.BaseAuthentication):Q(Q(expired_at__gt=timezone.now()) | Q(expired_at__isnull=True)),token=token,is_active=True,+ user__is_active=True,)except APIToken.DoesNotExist:raise AuthenticationFailed("Given API token is not valid")diff --git a/apps/api/plane/app/middleware/api_authentication.py b/apps/api/plane/app/middleware/api_authentication.pyindex abd8139857..ed03bbb8d4 100644--- a/apps/api/plane/app/middleware/api_authentication.py+++ b/apps/api/plane/app/middleware/api_authentication.py@@ -32,6 +32,7 @@ class APIKeyAuthentication(authentication.BaseAuthentication):Q(Q(expired_at__gt=timezone.now()) | Q(expired_at__isnull=True)),token=token,is_active=True,+ user__is_active=True,)except APIToken.DoesNotExist:raise AuthenticationFailed("Given API token is not valid")diff --git a/apps/api/plane/tests/contract/api/test_authentication.py b/apps/api/plane/tests/contract/api/test_authentication.pynew file mode 100644index 0000000000..d240567ccc--- /dev/null+++ b/apps/api/plane/tests/contract/api/test_authentication.py@@ -0,0 +1,44 @@+# Copyright (c) 2023-present Plane Software, Inc. and contributors+# SPDX-License-Identifier: AGPL-3.0-only+# See the LICENSE file for details.++"""+Contract tests for external API key authentication.++End-to-end proof that an API key cannot be used to access the API once the+owning user account has been deactivated.+"""++import pytest+from rest_framework import status+++class TestAPIKeyAuthenticationContract:+ """Test API key authentication behaviour against a live endpoint."""++ USERS_ME_URL = "/api/v1/users/me/"++ @pytest.mark.django_db+ def test_active_user_can_access_with_api_key(self, api_key_client):+ response = api_key_client.get(self.USERS_ME_URL)++ assert response.status_code == status.HTTP_200_OK++ @pytest.mark.django_db+ def test_deactivated_user_cannot_access_with_api_key(+ self, api_key_client, create_user+ ):+ # The account is disabled after the API key was generated.+ create_user.is_active = False+ create_user.save()++ response = api_key_client.get(self.USERS_ME_URL)++ # Access is denied once the account is deactivated. APIKeyAuthentication+ # does not set a WWW-Authenticate header, so DRF surfaces the+ # AuthenticationFailed as 403 Forbidden rather than 401.+ assert response.status_code in (+ status.HTTP_401_UNAUTHORIZED,+ status.HTTP_403_FORBIDDEN,+ )diff --git a/apps/api/plane/tests/unit/middleware/test_api_authentication.py b/apps/api/plane/tests/unit/middleware/test_api_authentication.pynew file mode 100644index 0000000000..5f67537c22--- /dev/null+++ b/apps/api/plane/tests/unit/middleware/test_api_authentication.py@@ -0,0 +1,46 @@+# Copyright (c) 2023-present Plane Software, Inc. and contributors+# SPDX-License-Identifier: AGPL-3.0-only+# See the LICENSE file for details.++"""+Unit tests for APIKeyAuthentication.++Covers the access-control guarantees of the external API key authentication:+- a valid token belonging to an active user authenticates successfully+- a valid token is rejected once the underlying user account is deactivated+ (prevents an authentication bypass via a disabled account that still holds+ a previously generated API key)+"""++import pytest+from rest_framework.exceptions import AuthenticationFailed++from plane.api.middleware.api_authentication import APIKeyAuthentication+from plane.db.models import APIToken+++class TestAPIKeyAuthentication:+ @pytest.mark.django_db+ def test_validate_api_token_authenticates_active_user(self, create_user):+ token = APIToken.objects.create(+ user=create_user, label="Active Token", token="active-user-token"+ )++ user, returned_token = APIKeyAuthentication().validate_api_token(token.token)++ assert user == create_user+ assert returned_token == token.token++ @pytest.mark.django_db+ def test_validate_api_token_rejects_deactivated_user(self, create_user):+ token = APIToken.objects.create(+ user=create_user, label="Stale Token", token="deactivated-user-token"+ )++ # Account is deactivated by an administrator after the token was issued.+ create_user.is_active = False+ create_user.save()++ with pytest.raises(AuthenticationFailed):+ APIKeyAuthentication().validate_api_token(token.token)