-
Notifications
You must be signed in to change notification settings - Fork 5.6k
[WEB-8110] fix: sanitize page list order_by against an allowlist #9387
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
96b69d0
[WEB-8110] fix: sanitize page list order_by against an allowlist (GHS…
mguptahub 0c8e115
[WEB-8110] fix: fold sanitized order_by into a single order_by() call…
mguptahub 5bd11c5
Merge remote-tracking branch 'origin/preview' into web-8110/page-orde…
mguptahub b28d2a5
chore(security): drop advisory identifiers from code comments
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
93 changes: 93 additions & 0 deletions
93
apps/api/plane/tests/contract/app/test_page_order_by_allowlist_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,93 @@ | ||
| # Copyright (c) 2023-present Plane Software, Inc. and contributors | ||
| # SPDX-License-Identifier: AGPL-3.0-only | ||
| # See the LICENSE file for details. | ||
|
|
||
| """ | ||
| Regression tests for page order_by ORM injection (WEB-8110). | ||
|
|
||
| PageViewSet.get_queryset passed the raw order_by query param into .order_by(), | ||
| which resolves field names at call time — an unknown field raised FieldError | ||
| (500 DoS) and a relation path (e.g. owned_by__password) enabled ORM relational | ||
| traversal. The param is now sanitized against PAGE_ORDER_BY_ALLOWLIST. | ||
| """ | ||
|
|
||
| import pytest | ||
| from rest_framework import status | ||
|
|
||
| from plane.db.models import Page, Project, ProjectMember, ProjectPage | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def project_with_page(db, workspace, create_user): | ||
| project = Project.objects.create(name="P", identifier="PRD", workspace=workspace) | ||
| ProjectMember.objects.create( | ||
| workspace=workspace, project=project, member=create_user, role=20, is_active=True | ||
| ) | ||
| page = Page.objects.create(workspace=workspace, owned_by=create_user, access=Page.PUBLIC_ACCESS, name="pg") | ||
| ProjectPage.objects.create(workspace=workspace, project=project, page=page) | ||
| return project, page | ||
|
|
||
|
|
||
| def _pages_url(slug, project_id): | ||
| return f"/api/workspaces/{slug}/projects/{project_id}/pages/" | ||
|
|
||
|
|
||
| @pytest.mark.contract | ||
| class TestPageOrderByAllowlist: | ||
| @pytest.mark.django_db | ||
| @pytest.mark.parametrize( | ||
| "order_by", | ||
| [ | ||
| "password", # invalid field → FieldError (500) pre-fix | ||
| "bogus__field__x", # invalid relation path → FieldError (500) pre-fix | ||
| "owned_by__password", # valid relation path → ORM traversal pre-fix | ||
| ], | ||
| ) | ||
| def test_malicious_order_by_is_rejected(self, session_client, workspace, project_with_page, order_by): | ||
| project, _ = project_with_page | ||
|
|
||
| response = session_client.get(_pages_url(workspace.slug, project.id), {"order_by": order_by}) | ||
|
|
||
| # Sanitized to the safe default — no 500, no traversal. | ||
| assert response.status_code == status.HTTP_200_OK | ||
|
|
||
| @pytest.mark.django_db | ||
| @pytest.mark.parametrize("order_by", ["name", "-name", "created_at", "-created_at", "updated_at", "sort_order"]) | ||
| def test_allowlisted_order_by_is_accepted(self, session_client, workspace, project_with_page, order_by): | ||
| project, _ = project_with_page | ||
|
|
||
| response = session_client.get(_pages_url(workspace.slug, project.id), {"order_by": order_by}) | ||
|
|
||
| assert response.status_code == status.HTTP_200_OK | ||
|
|
||
| @pytest.mark.django_db | ||
| def test_no_order_by_param_defaults_ok(self, session_client, workspace, project_with_page): | ||
| project, _ = project_with_page | ||
|
|
||
| response = session_client.get(_pages_url(workspace.slug, project.id)) | ||
|
|
||
| assert response.status_code == status.HTTP_200_OK | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| @pytest.mark.django_db | ||
| def test_allowlisted_order_by_actually_orders_results(self, session_client, workspace, create_user): | ||
| """An allowlisted order_by must actually affect the result ordering — | ||
| guards against the param being silently overridden by a later | ||
| .order_by() call.""" | ||
| project = Project.objects.create(name="P2", identifier="ORD", workspace=workspace) | ||
| ProjectMember.objects.create( | ||
| workspace=workspace, project=project, member=create_user, role=20, is_active=True | ||
| ) | ||
| # Non-favorite public pages so the favourite-first primary sort is a | ||
| # no-op and the secondary (name) ordering is observable. | ||
| for name in ("Gamma", "Alpha", "Beta"): | ||
| page = Page.objects.create( | ||
| workspace=workspace, owned_by=create_user, access=Page.PUBLIC_ACCESS, name=name | ||
| ) | ||
| ProjectPage.objects.create(workspace=workspace, project=project, page=page) | ||
|
|
||
| asc = session_client.get(_pages_url(workspace.slug, project.id), {"order_by": "name"}) | ||
| desc = session_client.get(_pages_url(workspace.slug, project.id), {"order_by": "-name"}) | ||
|
|
||
| assert asc.status_code == status.HTTP_200_OK | ||
| assert [p["name"] for p in asc.json()] == ["Alpha", "Beta", "Gamma"] | ||
| assert [p["name"] for p in desc.json()] == ["Gamma", "Beta", "Alpha"] | ||
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
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.