From 2d74bd12eeef623fc3778427dc85ece69ff7de66 Mon Sep 17 00:00:00 2001 From: "developing-today[bot]" <5454422+developing-today[bot]@users.noreply.github.com> Date: Fri, 6 Nov 2026 21:10:56 -0600 Subject: [PATCH] =?UTF-8?q?fix(cold-store-reaper):=20bound=20Drive=20and?= =?UTF-8?q?=20S3=20listing=20loops=20=E2=80=94=20a=20fresh-token=20page=20?= =?UTF-8?q?stream=20fails=20loudly=20instead=20of=20looping=20forever=20(#?= =?UTF-8?q?6602)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit list_backups and s3_list_objects already refused stuck/repeated continuation tokens, but a stream of fresh tokens paged forever (both loops probed still running at a 300-page sentinel). Add LIST_MAX_PAGES (10_000) and LIST_MAX_RESULTS (1_000_000) — orders of magnitude above any legitimate backup folder/bucket — checked per page in both loops, raising ColdStoreError naming the listing. Three regression tests drive fresh-token streams into both bounds. --- pkgs/cold-store-reaper/cold_store.py | 25 +++++++++ .../tests/test_cold_store.py | 52 +++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/pkgs/cold-store-reaper/cold_store.py b/pkgs/cold-store-reaper/cold_store.py index c79926c690..6d61cca363 100755 --- a/pkgs/cold-store-reaper/cold_store.py +++ b/pkgs/cold-store-reaper/cold_store.py @@ -68,6 +68,15 @@ # Batch size is therefore computed per table from its column count against # a 90-parameter budget, keeping headroom under the hard limit. D1_MAX_PARAMS_PER_QUERY = 100 + +# Listing bounds (#6602): the Drive and S3 listing loops already refuse +# stuck/repeated continuation tokens, but a stream of FRESH tokens pages +# forever. Legitimate backup folders/buckets are orders of magnitude +# below these caps (Drive pages hold 1000 entries, S3 pages up to 1000 +# keys), so crossing one means the endpoint is not terminating the +# listing — fail loudly instead of looping. +LIST_MAX_PAGES = 10_000 +LIST_MAX_RESULTS = 1_000_000 D1_SYNC_PARAM_BUDGET = 90 @@ -1220,7 +1229,15 @@ def list_backups(self, db_name: str | None = None) -> list[dict]: q = f"'{folder_id}' in parents and trashed = false" page_token = None seen_tokens = set() + pages = 0 while True: + pages += 1 + if pages > LIST_MAX_PAGES or len(out) > LIST_MAX_RESULTS: + raise ColdStoreError( + f"files.list for backups of db {db} exceeded the listing " + f"bound ({LIST_MAX_PAGES} pages / {LIST_MAX_RESULTS} results) " + "without the listing terminating (#6602)" + ) params = { "q": q, "fields": "nextPageToken,files(id,name,size,createdTime)", @@ -1813,8 +1830,16 @@ def s3_list_objects(self, bucket: str) -> list[dict]: forever, appending duplicates. Raise instead, naming the bucket, so the caller sees a stuck endpoint, not a hang.""" out: list[dict] = [] + pages = 0 token = None while True: + pages += 1 + if pages > LIST_MAX_PAGES or len(out) > LIST_MAX_RESULTS: + raise ColdStoreError( + f"list of {bucket} exceeded the listing bound " + f"({LIST_MAX_PAGES} pages / {LIST_MAX_RESULTS} results) " + "without the listing terminating (#6602)" + ) query = {"list-type": "2"} if token: query["continuation-token"] = token diff --git a/pkgs/cold-store-reaper/tests/test_cold_store.py b/pkgs/cold-store-reaper/tests/test_cold_store.py index 2ad00b4760..9f2fa98a82 100644 --- a/pkgs/cold-store-reaper/tests/test_cold_store.py +++ b/pkgs/cold-store-reaper/tests/test_cold_store.py @@ -3288,3 +3288,55 @@ def advancing(n, h, d): if __name__ == "__main__": unittest.main(verbosity=2) +import json +from unittest import mock + + +class TestListingBounds6602Drive(ColdStoreTestBase): + """#6602: a fresh-token page stream must hit the listing bound.""" + + def _fresh_token_fake(self, calls): + def fake(method, url, headers=None, **kw): + calls["n"] += 1 + body = {"files": [{"id": "f1", "name": "b", "size": 1, + "createdTime": "2026-01-01T00:00:00Z"}], + "nextPageToken": "tok-%d" % calls["n"]} + return 200, {}, json.dumps(body).encode() + return fake + + def test_drive_fresh_token_stream_hits_page_bound(self): + calls = {"n": 0} + with mock.patch.object(cold_store, "http_request", self._fresh_token_fake(calls)), mock.patch.object(cold_store, "LIST_MAX_PAGES", 5): + with mock.patch.object(self.store, "_folder_id", lambda name: "folder1"), mock.patch.object(self.store, "_auth_headers", lambda: {}): + with self.assertRaises(cold_store.ColdStoreError) as ctx: + self.store.list_backups("supabase") + self.assertIn("supabase", str(ctx.exception)) + self.assertEqual(calls["n"], 5) + + def test_drive_result_bound_trips(self): + calls = {"n": 0} + with mock.patch.object(cold_store, "http_request", self._fresh_token_fake(calls)), mock.patch.object(cold_store, "LIST_MAX_RESULTS", 0): + with mock.patch.object(self.store, "_folder_id", lambda name: "folder1"), mock.patch.object(self.store, "_auth_headers", lambda: {}): + with self.assertRaises(cold_store.ColdStoreError): + self.store.list_backups("supabase") + + +class TestListingBounds6602S3(S3TestBase): + def test_s3_fresh_token_stream_hits_page_bound(self): + calls = {"n": 0} + + def fake(method, bucket, query=None, **kw): + calls["n"] += 1 + body = ("" + "true" + f"tok-{calls[chr(110)]}" + "k11" + "2026-10-01T00:00:00.000Z" + "") + return 200, {}, body.encode() + + with mock.patch.object(self.store, "_s3_request", fake), mock.patch.object(cold_store, "LIST_MAX_PAGES", 5): + with self.assertRaises(cold_store.ColdStoreError) as ctx: + self.store.s3_list_objects("supabase-backups") + self.assertIn("supabase-backups", str(ctx.exception)) + self.assertEqual(calls["n"], 5)