Repository navigation
[WEB-8372] fix(security): restrict StateViewSet.partial_update to project admins #9473
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
73675d7
[WEB-8372] fix(security): restrict StateViewSet.partial_update to pro…
mguptahub 110bd54
[WEB-8372] fix: invalidate states cache on partial_update + strengthe…
mguptahub 3321fd3
chore(security): drop advisory identifiers from code comments
mguptahub fe446c3
Merge remote-tracking branch 'origin/preview' into web-8372/state-par…
mguptahub 1a72b7d
Merge branch 'preview' into web-8372/state-partial-update-admin-only
mguptahub 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
98 changes: 98 additions & 0 deletions
98
apps/api/plane/tests/contract/app/test_state_partial_update_admin_scope_app.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,98 @@ | ||
| # 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 StateViewSet.partial_update authorization. | ||
|
|
||
| Editing a workflow state is project | ||
| configuration — its sibling writes (create, destroy, mark_as_default) are all | ||
| ``@allow_permission([ROLE.ADMIN])``. ``partial_update`` was the outlier at | ||
| ``[ADMIN, MEMBER, GUEST]``, so any project Guest could rewrite any state | ||
| (name/color/group/description/sequence/order) and set it as the project default, | ||
| functionally bypassing the admin-only ``mark_as_default``. | ||
|
|
||
| The fix restricts ``partial_update`` to ADMIN. | ||
| """ | ||
|
|
||
| from uuid import uuid4 | ||
|
|
||
| import pytest | ||
| from rest_framework import status | ||
| from rest_framework.test import APIClient | ||
|
|
||
| from plane.db.models import Project, ProjectMember, State, User, WorkspaceMember | ||
|
|
||
|
|
||
| def _state_url(slug, project_id, pk): | ||
| return f"/api/workspaces/{slug}/projects/{project_id}/states/{pk}/" | ||
|
|
||
|
|
||
| def _member_of(workspace, project, *, role): | ||
| unique = uuid4().hex[:8] | ||
| user = User.objects.create(email=f"state-{role}-{unique}@plane.so", username=f"state_{role}_{unique}") | ||
| user.set_password("test-password") | ||
| user.save() | ||
| WorkspaceMember.objects.create(workspace=workspace, member=user, role=role, is_active=True) | ||
| ProjectMember.objects.create(project=project, member=user, workspace=workspace, role=role, is_active=True) | ||
| client = APIClient() | ||
| client.force_authenticate(user=user) | ||
| return client | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def project(db, workspace, create_user): | ||
| project = Project.objects.create( | ||
| name="State Project", identifier="ST", workspace=workspace, created_by=create_user | ||
| ) | ||
| ProjectMember.objects.create(project=project, member=create_user, workspace=workspace, role=20, is_active=True) | ||
| return project | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def state(db, workspace, project): | ||
| return State.objects.create( | ||
| name="Backlog", color="#60646C", group="backlog", default=False, project=project, workspace=workspace | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.contract | ||
| @pytest.mark.django_db | ||
| class TestStatePartialUpdateAdminScope: | ||
| """Editing a workflow state must be ADMIN-only (matches create/destroy/default).""" | ||
|
|
||
| def test_guest_cannot_patch_state(self, workspace, project, state): | ||
| guest_client = _member_of(workspace, project, role=5) | ||
| response = guest_client.patch( | ||
| _state_url(workspace.slug, project.id, state.id), {"name": "Guest Renamed", "default": True}, format="json" | ||
| ) | ||
| assert response.status_code == status.HTTP_403_FORBIDDEN, ( | ||
| f"Got {response.status_code}: {getattr(response, 'data', None)!r}" | ||
| ) | ||
| state.refresh_from_db() | ||
| assert state.name == "Backlog" | ||
| assert state.default is False | ||
|
|
||
| def test_member_cannot_patch_state(self, workspace, project, state): | ||
| member_client = _member_of(workspace, project, role=15) | ||
| response = member_client.patch( | ||
| _state_url(workspace.slug, project.id, state.id), | ||
| {"name": "Member Renamed", "default": True}, | ||
| format="json", | ||
| ) | ||
| assert response.status_code == status.HTTP_403_FORBIDDEN, ( | ||
| f"Got {response.status_code}: {getattr(response, 'data', None)!r}" | ||
| ) | ||
| state.refresh_from_db() | ||
| assert state.name == "Backlog" | ||
| assert state.default is False | ||
|
|
||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| def test_admin_can_patch_state(self, session_client, workspace, project, state): | ||
| """Positive control: a project admin may still edit a state.""" | ||
| response = session_client.patch( | ||
| _state_url(workspace.slug, project.id, state.id), {"name": "Admin Renamed"}, format="json" | ||
| ) | ||
| assert response.status_code == status.HTTP_200_OK, ( | ||
| f"Got {response.status_code}: {getattr(response, 'data', None)!r}" | ||
| ) | ||
| state.refresh_from_db() | ||
| assert state.name == "Admin Renamed" | ||
Oops, something went wrong.
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.