-
Notifications
You must be signed in to change notification settings - Fork 5.2k
fix(api): validate assignees/labels in IssueSerializer instead of silently dropping invalid ids #9526
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
houssaineamzil
wants to merge
1
commit into
makeplane:preview
Choose a base branch
from
houssaineamzil:fix/issue-9517-assignee-label-validation
base: preview
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+220
−9
Open
fix(api): validate assignees/labels in IssueSerializer instead of silently dropping invalid ids #9526
Changes from all commits
Commits
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
136 changes: 136 additions & 0 deletions
136
apps/api/plane/tests/contract/api/test_issue_assignee_label_validation.py
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,136 @@ | ||
| # Copyright (c) 2023-present Plane Software, Inc. and contributors | ||
| # SPDX-License-Identifier: AGPL-3.0-only | ||
| # See the LICENSE file for details. | ||
|
|
||
| import pytest | ||
| from rest_framework import status | ||
|
|
||
| from plane.celery import app as celery_app | ||
| from plane.db.models import Issue, Label, Project, ProjectMember, State, User | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def celery_eager(): | ||
| """ | ||
| Run Celery tasks synchronously in-process instead of publishing to a | ||
| broker. There's no RabbitMQ/broker in this local sandbox, and these | ||
| tests only care about the HTTP response contract, not async delivery. | ||
| """ | ||
| original = celery_app.conf.task_always_eager | ||
| celery_app.conf.task_always_eager = True | ||
| celery_app.conf.task_eager_propagates = False | ||
| yield | ||
| celery_app.conf.task_always_eager = original | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def project(db, workspace, create_user): | ||
| """Create a test project with the user as an admin member and a default state.""" | ||
| project = Project.objects.create( | ||
| name="Test Project", identifier="TP", workspace=workspace, created_by=create_user | ||
| ) | ||
| ProjectMember.objects.create(project=project, member=create_user, role=20, is_active=True) | ||
| State.objects.create( | ||
| name="Backlog", | ||
| color="#000000", | ||
| group="backlog", | ||
| default=True, | ||
| project=project, | ||
| workspace=workspace, | ||
| created_by=create_user, | ||
| ) | ||
| return project | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def create_issue(db, project, workspace, create_user): | ||
| return Issue.objects.create(name="Existing Issue", project=project, workspace=workspace, created_by=create_user) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def outsider_user(db): | ||
| """A user who exists in the workspace/system but is NOT a member of `project`.""" | ||
| user = User.objects.create(email="outsider@plane.so", username="outsider-user") | ||
| user.set_password("outsider-password") | ||
| user.save() | ||
| return user | ||
|
|
||
|
|
||
| @pytest.mark.contract | ||
| class TestIssueAssigneeLabelValidationContract: | ||
| """ | ||
| Contract: creating/updating a work item through the external REST API | ||
| (``/api/v1/...``) must reject assignee/label ids that don't belong to the | ||
| project with a 400, instead of silently dropping them and returning | ||
| 200/201. See makeplane/plane#9517. | ||
| """ | ||
|
|
||
| def get_list_url(self, workspace_slug, project_id): | ||
| return f"/api/v1/workspaces/{workspace_slug}/projects/{project_id}/issues/" | ||
|
|
||
| def get_detail_url(self, workspace_slug, project_id, issue_id): | ||
| return f"/api/v1/workspaces/{workspace_slug}/projects/{project_id}/issues/{issue_id}/" | ||
|
|
||
| @pytest.mark.django_db | ||
| def test_create_with_non_member_assignee_is_rejected(self, api_key_client, workspace, project, outsider_user): | ||
| url = self.get_list_url(workspace.slug, project.id) | ||
|
|
||
| response = api_key_client.post( | ||
| url, {"name": "New Issue", "assignees": [str(outsider_user.id)]}, format="json" | ||
| ) | ||
|
|
||
| assert response.status_code == status.HTTP_400_BAD_REQUEST | ||
| assert not Issue.objects.filter(name="New Issue").exists() | ||
|
|
||
| @pytest.mark.django_db | ||
| def test_create_with_foreign_label_is_rejected(self, api_key_client, workspace, project): | ||
| other_project = Project.objects.create(name="Other", identifier="OTH", workspace=workspace) | ||
| foreign_label = Label.objects.create(name="Foreign", project=other_project) | ||
|
|
||
| url = self.get_list_url(workspace.slug, project.id) | ||
| response = api_key_client.post( | ||
| url, {"name": "New Issue", "labels": [str(foreign_label.id)]}, format="json" | ||
| ) | ||
|
|
||
| assert response.status_code == status.HTTP_400_BAD_REQUEST | ||
| assert not Issue.objects.filter(name="New Issue").exists() | ||
|
|
||
| @pytest.mark.django_db | ||
| def test_update_with_non_member_assignee_is_rejected_and_leaves_assignees_unchanged( | ||
| self, api_key_client, workspace, project, create_issue, outsider_user | ||
| ): | ||
| url = self.get_detail_url(workspace.slug, project.id, create_issue.id) | ||
|
|
||
| response = api_key_client.patch(url, {"assignees": [str(outsider_user.id)]}, format="json") | ||
|
|
||
| assert response.status_code == status.HTTP_400_BAD_REQUEST | ||
| assert list(create_issue.assignees.all()) == [] | ||
|
|
||
| @pytest.mark.django_db | ||
| def test_create_with_mix_of_valid_and_invalid_assignee_is_rejected_entirely( | ||
| self, api_key_client, workspace, project, create_user, outsider_user | ||
| ): | ||
| """A partially-valid list must reject the whole request, not silently keep only the valid id.""" | ||
| url = self.get_list_url(workspace.slug, project.id) | ||
|
|
||
| response = api_key_client.post( | ||
| url, | ||
| {"name": "New Issue", "assignees": [str(create_user.id), str(outsider_user.id)]}, | ||
| format="json", | ||
| ) | ||
|
|
||
| assert response.status_code == status.HTTP_400_BAD_REQUEST | ||
| assert not Issue.objects.filter(name="New Issue").exists() | ||
|
|
||
| @pytest.mark.django_db | ||
| def test_create_with_valid_assignee_still_works(self, api_key_client, workspace, project, create_user): | ||
| """Regression guard: a genuinely valid project member must still be assignable.""" | ||
| url = self.get_list_url(workspace.slug, project.id) | ||
|
|
||
| response = api_key_client.post( | ||
| url, {"name": "New Issue", "assignees": [str(create_user.id)]}, format="json" | ||
| ) | ||
|
|
||
| assert response.status_code == status.HTTP_201_CREATED | ||
| issue = Issue.objects.get(name="New Issue") | ||
| assert list(issue.assignees.values_list("id", flat=True)) == [create_user.id] | ||
61 changes: 61 additions & 0 deletions
61
apps/api/plane/tests/unit/serializers/test_issue_serializer_api.py
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,61 @@ | ||
| # Copyright (c) 2023-present Plane Software, Inc. and contributors | ||
| # SPDX-License-Identifier: AGPL-3.0-only | ||
| # See the LICENSE file for details. | ||
|
|
||
| import pytest | ||
|
|
||
| from plane.api.serializers.issue import IssueSerializer | ||
| from plane.db.models import Project, ProjectMember, Label, User | ||
|
|
||
|
|
||
| @pytest.mark.unit | ||
| class TestIssueSerializerAssigneeAndLabelValidation: | ||
| """Test that IssueSerializer rejects invalid assignees/labels instead of silently dropping them""" | ||
|
|
||
| @pytest.mark.django_db | ||
| def test_rejects_assignee_who_is_not_an_active_project_member(self, db, workspace, create_user): | ||
| """An assignee id that isn't an active project member (role >= 15) must raise a validation error""" | ||
| project = Project.objects.create(name="Test Project", identifier="TEST", workspace=workspace) | ||
|
|
||
| outsider = User.objects.create( | ||
| email="outsider@example.com", first_name="Out", last_name="Sider", username="outsider" | ||
| ) | ||
| # Not a member of the project at all | ||
|
|
||
| serializer = IssueSerializer( | ||
| data={"name": "Test Issue", "assignees": [str(outsider.id)]}, | ||
| context={"project_id": project.id, "workspace_id": workspace.id}, | ||
| ) | ||
|
|
||
| assert not serializer.is_valid() | ||
| assert "assignees" in str(serializer.errors).lower() or "assignee" in str(serializer.errors).lower() | ||
|
|
||
| @pytest.mark.django_db | ||
| def test_rejects_label_that_does_not_belong_to_project(self, db, workspace, create_user): | ||
| """A label id that belongs to a different project must raise a validation error""" | ||
| project = Project.objects.create(name="Test Project", identifier="TEST", workspace=workspace) | ||
| other_project = Project.objects.create(name="Other Project", identifier="OTHER", workspace=workspace) | ||
|
|
||
| foreign_label = Label.objects.create(name="Foreign Label", project=other_project) | ||
|
|
||
| serializer = IssueSerializer( | ||
| data={"name": "Test Issue", "labels": [str(foreign_label.id)]}, | ||
| context={"project_id": project.id, "workspace_id": workspace.id}, | ||
| ) | ||
|
|
||
| assert not serializer.is_valid() | ||
| assert "labels" in str(serializer.errors).lower() or "label" in str(serializer.errors).lower() | ||
|
|
||
| @pytest.mark.django_db | ||
| def test_accepts_assignee_who_is_an_active_project_member(self, db, workspace, create_user): | ||
| """A valid active project member id should still be accepted""" | ||
| project = Project.objects.create(name="Test Project", identifier="TEST", workspace=workspace) | ||
| ProjectMember.objects.create(project=project, member=create_user, role=15, is_active=True) | ||
|
|
||
| serializer = IssueSerializer( | ||
| data={"name": "Test Issue", "assignees": [str(create_user.id)]}, | ||
| context={"project_id": project.id, "workspace_id": workspace.id}, | ||
| ) | ||
|
|
||
| assert serializer.is_valid(), serializer.errors | ||
| assert list(serializer.validated_data["assignees"]) == [create_user.id] |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Restore
task_eager_propagatesduring fixture teardown.The fixture changes
task_eager_propagateson Line 21 but does not restore it. Later tests can observeFalseinstead of their prior configuration.Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents