Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions pkgs/cold-store-reaper/cold_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)",
Expand Down Expand Up @@ -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
Expand Down
52 changes: 52 additions & 0 deletions pkgs/cold-store-reaper/tests/test_cold_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ("<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">"
"<IsTruncated>true</IsTruncated>"
f"<NextContinuationToken>tok-{calls[chr(110)]}</NextContinuationToken>"
"<Contents><Key>k1</Key><Size>1</Size>"
"<LastModified>2026-10-01T00:00:00.000Z</LastModified></Contents>"
"</ListBucketResult>")
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)
Loading