-
Notifications
You must be signed in to change notification settings - Fork 5.6k
[INFRA-774] fix(security): scope WorkSpaceMemberSerializer to block cross-workspace member reassignment via PATCH #9705
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
mguptahub
wants to merge
3
commits into
preview
Choose a base branch
from
infra-774/workspace-member-scope-to-caller
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.
+194
−3
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
105 changes: 105 additions & 0 deletions
105
apps/api/plane/tests/contract/app/test_workspace_member_cross_tenant_reassignment.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,105 @@ | ||
| # Copyright (c) 2023-present Plane Software, Inc. and contributors | ||
| # SPDX-License-Identifier: AGPL-3.0-only | ||
| # See the LICENSE file for details. | ||
|
|
||
| """Regression test for cross-workspace privilege escalation via | ||
| WorkSpaceMemberSerializer.partial_update. | ||
|
|
||
|
mguptahub marked this conversation as resolved.
Outdated
|
||
| Root cause: WorkSpaceMemberSerializer (and its read-only-in-practice siblings | ||
| WorkspaceMemberMeSerializer / WorkspaceMemberAdminSerializer) declared | ||
| fields = "__all__" with no read_only_fields, so DRF auto-generated a writable | ||
| `workspace` FK field. WorkSpaceMemberViewSet.partial_update passes raw | ||
| request.data straight into the serializer with no scrubbing, so a workspace | ||
| ADMIN could PATCH any other active member's WorkspaceMember row with a | ||
| `workspace` field pointing at a foreign workspace's UUID — moving that row | ||
| (and whatever role was also in the body) into the foreign workspace with no | ||
| invitation, no consent from its owner, and no audit trail. | ||
|
|
||
| Fixed by adding workspace/member (plus the usual created_by/updated_by/ | ||
| created_at/updated_at) to read_only_fields on all three serializers. | ||
| """ | ||
|
|
||
| import uuid | ||
|
|
||
| import pytest | ||
| from rest_framework import status | ||
| from rest_framework.test import APIClient | ||
|
|
||
| from plane.db.models import User, Workspace, WorkspaceMember | ||
|
|
||
| pytestmark = pytest.mark.contract | ||
|
|
||
|
|
||
| def _member_detail_url(slug: str, pk: uuid.UUID) -> str: | ||
| return f"/api/workspaces/{slug}/members/{pk}/" | ||
|
|
||
|
|
||
| def _make_user(email: str) -> User: | ||
| local_part = email.split("@")[0] | ||
| user = User.objects.create(email=email, username=local_part, first_name=local_part) | ||
| user.set_password("test-password") | ||
| user.save() | ||
| return user | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def foreign_workspace(db): | ||
| """Workspace B — the attacker's escalation target, unrelated to `workspace` (A).""" | ||
| owner = _make_user(f"foreign-owner-{uuid.uuid4().hex[:8]}@plane.so") | ||
| return Workspace.objects.create(name="Foreign Workspace", owner=owner, slug=f"foreign-ws-{uuid.uuid4().hex[:8]}") | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def victim_member(db, workspace): | ||
| """A second, active, non-bot member of workspace A — the row being attacked.""" | ||
| victim = _make_user(f"victim-{uuid.uuid4().hex[:8]}@plane.so") | ||
| return WorkspaceMember.objects.create(workspace=workspace, member=victim, role=15, is_active=True) | ||
|
|
||
|
|
||
| @pytest.mark.django_db | ||
| class TestWorkspaceMemberCrossTenantReassignment: | ||
| def test_admin_cannot_move_member_into_foreign_workspace( | ||
| self, workspace, foreign_workspace, victim_member, create_user | ||
| ): | ||
| """create_user is workspace A's admin (via the `workspace` fixture). They | ||
| must not be able to move victim_member's row into foreign_workspace by | ||
| PATCHing `workspace` in the body, even though they're a legitimate admin | ||
| of A and the request also carries an otherwise-valid `role`.""" | ||
| client = APIClient() | ||
| client.force_authenticate(user=create_user) | ||
|
|
||
| response = client.patch( | ||
| _member_detail_url(workspace.slug, victim_member.id), | ||
| {"workspace": str(foreign_workspace.id), "role": 20}, | ||
| format="json", | ||
| ) | ||
|
|
||
| assert response.status_code == status.HTTP_200_OK, f"got {response.status_code}: {response.data!r}" | ||
|
|
||
| victim_member.refresh_from_db() | ||
| assert victim_member.workspace_id == workspace.id, ( | ||
| f"workspace must stay A regardless of what the body requested — got moved to {victim_member.workspace_id!r}" | ||
| ) | ||
| foreign_row_exists = WorkspaceMember.objects.filter( | ||
| workspace=foreign_workspace, member_id=victim_member.member_id | ||
| ).exists() | ||
| assert not foreign_row_exists, "no row should have been created/moved in the foreign workspace" | ||
| # The legitimate part of the same request (role) must still apply — | ||
| # proves this isn't just silently rejecting the whole PATCH. | ||
| assert victim_member.role == 20 | ||
|
|
||
| def test_admin_can_still_change_role_without_workspace_in_body(self, workspace, victim_member, create_user): | ||
| """Positive control: the fix must not break the legitimate role-only PATCH.""" | ||
| client = APIClient() | ||
| client.force_authenticate(user=create_user) | ||
|
|
||
| response = client.patch( | ||
| _member_detail_url(workspace.slug, victim_member.id), | ||
| {"role": 5}, | ||
| format="json", | ||
| ) | ||
|
|
||
| assert response.status_code == status.HTTP_200_OK, f"got {response.status_code}: {response.data!r}" | ||
| victim_member.refresh_from_db() | ||
| assert victim_member.role == 5 | ||
| assert victim_member.workspace_id == workspace.id | ||
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.