From 18058f7cd8d75a83d14cb9bd7d92888699a41ac7 Mon Sep 17 00:00:00 2001 From: Apoorva Rohilla <61989678+apoorvarohilla@users.noreply.github.com> Date: Mon, 23 Nov 2026 17:00:04 +0530 Subject: [PATCH 1/2] Build the export path from a validated organization UUID export_reviews is the only REST call whose path is formatted by hand, and it interpolated organization_id without encoding. A non-UUID value could add path segments, leave the organization through dot segments, or replace the dataset through "?": export_reviews(f"{org}/exports/spend?x=", "reviews") requested the spend export, and because both datasets are row version 1 the page decoded as reviews without an error. Convert organization_id to a UUID before building the path and raise ValueError otherwise, before any request, as the method already does for an unknown dataset or format. iter_export_pages inherits the check. Equivalent UUID spellings still reach the canonical path. Co-Authored-By: Claude Opus 5.5 --- proofline/client.py | 20 +++++++++++++-- tests/test_http.py | 59 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 2 deletions(-) diff --git a/proofline/client.py b/proofline/client.py index 1d8c1519..fd8c0503 100644 --- a/proofline/client.py +++ b/proofline/client.py @@ -189,6 +189,20 @@ def _pagination_limit(resource: str = "Review") -> ProoflineError: ) +def _export_path(organization_id: UUID, dataset: ExportDataset) -> str: + # This path is built by hand rather than by a generated operation, so the + # organization must become one canonical UUID segment. Any other text could + # add segments, leave the organization through dot segments, or replace the + # dataset through "?" or "#", and the page would still decode as `dataset`. + try: + organization = ( + organization_id if isinstance(organization_id, UUID) else UUID(organization_id) + ) + except (TypeError, ValueError, AttributeError): + raise ValueError("Export organization_id must be a UUID.") from None + return f"/api/v1/organizations/{organization}/exports/{dataset}" + + def _export_page_limit(max_pages: int) -> None: if isinstance(max_pages, bool) or not isinstance(max_pages, int) or not 1 <= max_pages <= 1_000: raise ValueError("Export pagination requires max_pages 1..1000.") @@ -290,6 +304,7 @@ def export_reviews( raise ValueError("Unknown export dataset.") if format not in ("csv", "ndjson"): raise ValueError("Export format must be csv or ndjson.") + path = _export_path(organization_id, dataset) params = {"from": from_time, "to": to_time, "format": format} if cursor is not None: params["cursor"] = cursor @@ -297,7 +312,7 @@ def export_reviews( params["group_by"] = group_by try: response = self._http.get( - f"/api/v1/organizations/{organization_id}/exports/{dataset}", + path, params=params, headers={"Accept": "text/csv" if format == "csv" else "application/x-ndjson"}, ) @@ -660,6 +675,7 @@ async def export_reviews( raise ValueError("Unknown export dataset.") if format not in ("csv", "ndjson"): raise ValueError("Export format must be csv or ndjson.") + path = _export_path(organization_id, dataset) params = {"from": from_time, "to": to_time, "format": format} if cursor is not None: params["cursor"] = cursor @@ -667,7 +683,7 @@ async def export_reviews( params["group_by"] = group_by try: response = await self._http.get( - f"/api/v1/organizations/{organization_id}/exports/{dataset}", + path, params=params, headers={"Accept": "text/csv" if format == "csv" else "application/x-ndjson"}, ) diff --git a/tests/test_http.py b/tests/test_http.py index 0af9d60f..cf107a80 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -1992,6 +1992,65 @@ async def test_export_iterator_validates_bound_before_requests(mode: str, bound: assert not server.requests +EXPORT_WINDOW: dict[str, Any] = {"from_time": "2026-10-01T00:00:00Z", "to_time": "2026-10-25T00:00:00Z"} + + +# Each value would otherwise change the request target: dot segments leave the +# organization, a slash adds path segments, and "?" or "#" replace the dataset. +@MODES +@pytest.mark.parametrize( + "organization_id", + [ + "..", + ".", + "", + f"{ORG}/../../reviews", + f"{ORG}/exports/spend?x=", + f"{ORG}#", + "not-a-uuid", + ORG.int, + None, + ], +) +async def test_export_refuses_an_organization_id_that_is_not_a_uuid( + mode: str, organization_id: Any +) -> None: + with Server(lambda request, index: Reply(status=404, body={"detail": "no"})) as server: + async with session(mode, server.url) as client: + with pytest.raises(ValueError, match="organization_id"): + await call(client, "export_reviews", organization_id, "reviews", **EXPORT_WINDOW) + iterator = client.iter_export_pages(organization_id, "reviews", **EXPORT_WINDOW) + with pytest.raises(ValueError, match="organization_id"): + await next_export(iterator) + assert not server.requests + + +@MODES +@pytest.mark.parametrize( + "organization_id", [ORG, str(ORG), str(ORG).upper(), f"{{{ORG}}}", ORG.hex] +) +async def test_export_sends_the_canonical_organization_path( + mode: str, organization_id: Any +) -> None: + def respond(request: Request, index: int) -> Reply: + return Reply( + body=b'{"dataset_version":1}\n', + headers={ + "Content-Type": "application/x-ndjson", + "X-Proofline-Export-Version": "1", + "X-Proofline-Export-Settled-Until": "2026-10-25T00:00:00Z", + }, + ) + + with Server(respond) as server: + async with session(mode, server.url) as client: + page = await call(client, "export_reviews", organization_id, "reviews", **EXPORT_WINDOW) + assert page.rows == [{"dataset_version": 1}] + assert urlsplit(server.requests[0].path).path == ( + f"/api/v1/organizations/{ORG}/exports/reviews" + ) + + @MODES @pytest.mark.parametrize( ("cursors", "resume", "yielded"), From 29c65fb32f63ab68606ff869fb2a454e86d4a154 Mon Sep 17 00:00:00 2001 From: Apoorva Rohilla <61989678+apoorvarohilla@users.noreply.github.com> Date: Mon, 23 Nov 2026 17:42:41 +0530 Subject: [PATCH 2/2] Test organization spellings that actually differ Review feedback: ORG has no hex letters, so str(ORG).upper() equaled str(ORG) and the upper-case case repeated the string case. Use a UUID with hex letters, check that every spelling is a distinct input, and expect its canonical lower-case path. Also wrap EXPORT_WINDOW to the 100-column limit. Co-Authored-By: Claude Opus 5.5 --- tests/test_http.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/tests/test_http.py b/tests/test_http.py index cf107a80..3f0a348c 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -1992,7 +1992,10 @@ async def test_export_iterator_validates_bound_before_requests(mode: str, bound: assert not server.requests -EXPORT_WINDOW: dict[str, Any] = {"from_time": "2026-10-01T00:00:00Z", "to_time": "2026-10-25T00:00:00Z"} +EXPORT_WINDOW: dict[str, Any] = { + "from_time": "2026-10-01T00:00:00Z", + "to_time": "2026-10-25T00:00:00Z", +} # Each value would otherwise change the request target: dot segments leave the @@ -2025,10 +2028,23 @@ async def test_export_refuses_an_organization_id_that_is_not_a_uuid( assert not server.requests +# ORG has no hex letters, so its upper-case spelling would equal its string. +LETTERED_ORG = UUID("3fa85f64-5717-4562-b3fc-2c963f66afa6") +ORG_SPELLINGS = [ + LETTERED_ORG, + str(LETTERED_ORG), + str(LETTERED_ORG).upper(), + f"{{{LETTERED_ORG}}}", + LETTERED_ORG.hex.upper(), +] + + +def test_organization_spellings_are_distinct() -> None: + assert len({repr(spelling) for spelling in ORG_SPELLINGS}) == len(ORG_SPELLINGS) + + @MODES -@pytest.mark.parametrize( - "organization_id", [ORG, str(ORG), str(ORG).upper(), f"{{{ORG}}}", ORG.hex] -) +@pytest.mark.parametrize("organization_id", ORG_SPELLINGS) async def test_export_sends_the_canonical_organization_path( mode: str, organization_id: Any ) -> None: @@ -2047,7 +2063,7 @@ def respond(request: Request, index: int) -> Reply: page = await call(client, "export_reviews", organization_id, "reviews", **EXPORT_WINDOW) assert page.rows == [{"dataset_version": 1}] assert urlsplit(server.requests[0].path).path == ( - f"/api/v1/organizations/{ORG}/exports/reviews" + f"/api/v1/organizations/{LETTERED_ORG}/exports/reviews" )