diff --git a/proofline/client.py b/proofline/client.py index 1d8c1519..059f10a8 100644 --- a/proofline/client.py +++ b/proofline/client.py @@ -163,6 +163,14 @@ def _head_commit(value: str | None) -> None: raise ValueError("The head commit must be a full 40 or 64 character hexadecimal SHA.") +def _coordinate(value: str) -> str: + # Percent-encoding keeps "." and "..", which the URL then resolves as dot segments, so + # get_deployment_review(org, "..") would read list_reviews. The TypeScript SDK refuses them too. + if value in ("", ".", ".."): + raise ValueError("A path coordinate must be nonempty and not a traversal segment.") + return value + + def _next_cursor(page: ReviewPage, seen: set[str]) -> str | None: if not isinstance(page.truncated, bool): raise _transport.invalid_response() @@ -353,7 +361,7 @@ def list_pull_request_reviews( list_pull_request_reviews.sync_detailed, ReviewPage, organization_id, - repository, + _coordinate(repository), pull_request_number, **params, ) @@ -365,14 +373,17 @@ def get_pull_request_review( get_pull_request_review.sync_detailed, CustomerReviewDetail, organization_id, - repository, + _coordinate(repository), pull_request_number, run_number, ) def get_deployment_review(self, organization_id: UUID, public_key: str) -> CustomerReviewDetail: return self._call( - get_deployment_review.sync_detailed, CustomerReviewDetail, organization_id, public_key + get_deployment_review.sync_detailed, + CustomerReviewDetail, + organization_id, + _coordinate(public_key), ) def get_review(self, organization_id: UUID, review_id: UUID) -> CustomerReviewDetail: @@ -727,7 +738,7 @@ async def list_pull_request_reviews( list_pull_request_reviews.asyncio_detailed, ReviewPage, organization_id, - repository, + _coordinate(repository), pull_request_number, **params, ) @@ -739,7 +750,7 @@ async def get_pull_request_review( get_pull_request_review.asyncio_detailed, CustomerReviewDetail, organization_id, - repository, + _coordinate(repository), pull_request_number, run_number, ) @@ -751,7 +762,7 @@ async def get_deployment_review( get_deployment_review.asyncio_detailed, CustomerReviewDetail, organization_id, - public_key, + _coordinate(public_key), ) async def get_review(self, organization_id: UUID, review_id: UUID) -> CustomerReviewDetail: diff --git a/tests/test_http.py b/tests/test_http.py index 0af9d60f..49943921 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -14,7 +14,7 @@ import time import tomllib import traceback -from collections.abc import AsyncIterator +from collections.abc import AsyncIterator, Callable from contextlib import asynccontextmanager from datetime import UTC, datetime, timedelta from pathlib import Path @@ -1390,6 +1390,47 @@ async def test_head_commit_must_be_a_full_sha_before_any_request(mode: str) -> N assert len(server.requests) == 1 +@MODES +@pytest.mark.parametrize("value", ["", ".", ".."]) +@pytest.mark.parametrize( + ("name", "arguments"), + [ + ("list_pull_request_reviews", lambda value: (ORG, value, 7)), + ("get_pull_request_review", lambda value: (ORG, value, 7, 1)), + ("get_deployment_review", lambda value: (ORG, value)), + ], +) +async def test_traversal_path_coordinates_are_refused_before_any_request( + mode: str, name: str, arguments: Callable[[str], tuple[Any, ...]], value: str +) -> None: + # Percent-encoding leaves "." and ".." intact, so the URL would resolve them as dot segments + # and reach another endpoint, such as list_reviews for a deployment key of "..". + with Server(lambda request, index: Reply(body={})) as server: + async with session(mode, server.url) as client: + with pytest.raises(ValueError, match="traversal segment"): + await call(client, name, *arguments(value)) + assert server.requests == [] + + +@MODES +async def test_dotted_path_coordinates_stay_one_encoded_segment(mode: str) -> None: + detail = review_at("a" * 40) + + def reply(request: Request, index: int) -> Reply: + return Reply(body=page() if request.path.endswith("/7") else detail) + + with Server(reply) as server: + async with session(mode, server.url) as client: + await call(client, "list_pull_request_reviews", ORG, "owner/.github", 7) + await call(client, "get_pull_request_review", ORG, "owner/.github", 7, 1) + await call(client, "get_deployment_review", ORG, "...") + assert [request.path for request in server.requests] == [ + f"/api/v1/organizations/{ORG}/reviews/pr/owner%2F.github/7", + f"/api/v1/organizations/{ORG}/reviews/pr/owner%2F.github/7/runs/1", + f"/api/v1/organizations/{ORG}/reviews/deployments/...", + ] + + @MODES async def test_polling_backoff_and_timeout_keep_review_url_and_last_record( mode: str, monkeypatch: pytest.MonkeyPatch