From fd945021876f8cdde4fdc016a0fe8aaf11520ab1 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 02:22:53 +0000 Subject: [PATCH] fleet-paperwork: bound the two GitHub App page loops AppClient.installations() and assert_repo_in_scope() followed pages with while True and no bound. Both now stop after GITHUB_LIST_MAX_PAGES (100 pages of 100) and fail naming the listing, per the pagination contract (#6622 rule 2). Normal termination (a short page, or reaching total_count) is unchanged. Tests cover both terminations and both bound breaches. Fixes #9150 Co-Authored-By: Claude Opus 5.5 --- infra/fleet-paperwork/fleet_paperwork.py | 25 +++++-- .../tests/test_fleet_paperwork.py | 72 +++++++++++++++++++ 2 files changed, 91 insertions(+), 6 deletions(-) diff --git a/infra/fleet-paperwork/fleet_paperwork.py b/infra/fleet-paperwork/fleet_paperwork.py index 183b0b8182..a70eda654c 100755 --- a/infra/fleet-paperwork/fleet_paperwork.py +++ b/infra/fleet-paperwork/fleet_paperwork.py @@ -67,6 +67,11 @@ DEFAULT_SOPS = "~/secrets/pm-stack.sops.yaml" DEFAULT_LEDGER = "~/.local/state/fleet-paperwork/ledger.tsv" APP_SLUG_HINT = "developing.today (App ID read from the sops file at runtime)" +# Pagination contract (#6622 rule 2): every GitHub page loop carries a +# page bound whose breach is an error naming the listing. 100 pages of +# 100 is far past any installation or repository count this App has, +# so crossing it means the listing is not terminating. +GITHUB_LIST_MAX_PAGES = 100 def die(msg: str) -> "SystemExit": @@ -260,8 +265,7 @@ def installations(self) -> list[dict]: # empty page, mirroring assert_repo_in_scope below; the # endpoint returns a top-level array with no total_count. collected: list[dict] = [] - page = 1 - while True: + for page in range(1, GITHUB_LIST_MAX_PAGES + 1): _, data = http( "GET", f"/app/installations?per_page=100&page={page}", @@ -277,7 +281,11 @@ def installations(self) -> list[dict]: collected.extend(data) if len(data) < 100: break - page += 1 + else: + die( + "/app/installations listing exceeded the page bound " + f"({GITHUB_LIST_MAX_PAGES} pages of 100) without a short page (#9150)" + ) self._installations = collected return self._installations @@ -309,8 +317,8 @@ def token_for(self, owner: str) -> str: def assert_repo_in_scope(self, repo: str) -> None: owner = repo.split("/")[0] token = self.token_for(owner) - page, seen = 1, [] - while True: + seen: list[str] = [] + for page in range(1, GITHUB_LIST_MAX_PAGES + 1): _, data = http( "GET", f"/installation/repositories?per_page=100&page={page}", token ) @@ -335,7 +343,12 @@ def assert_repo_in_scope(self, repo: str) -> None: ) if len(seen) >= total or not repos: break - page += 1 + else: + die( + f"/installation/repositories listing for {owner} exceeded the page " + f"bound ({GITHUB_LIST_MAX_PAGES} pages of 100) before reaching its " + "total_count (#9150)" + ) if repo not in seen: die( f"repo {repo} is not in the app installation scope for {owner} " diff --git a/infra/fleet-paperwork/tests/test_fleet_paperwork.py b/infra/fleet-paperwork/tests/test_fleet_paperwork.py index a25e31f012..cea17d9c2d 100644 --- a/infra/fleet-paperwork/tests/test_fleet_paperwork.py +++ b/infra/fleet-paperwork/tests/test_fleet_paperwork.py @@ -515,6 +515,78 @@ def fake_http(method, path, token, payload=None, **kw): self.assertEqual(hit["number"], 5) +class TestPageBounds(unittest.TestCase): + """#9150 (#6622 rule 2): both App page loops stop at a page bound and + fail naming the listing instead of following pages forever.""" + + def client(self): + c = fp.AppClient.__new__(fp.AppClient) + c.jwt = "jwt" + c._tokens = {} + c._installations = None + return c + + def test_installations_stops_at_short_page(self): + def fake_http(method, path, token, body=None, expect=dict): + page = int(path.rsplit("page=", 1)[1]) + return 200, [ + {"id": page * 1000 + i} for i in range(100 if page == 1 else 3) + ] + + with unittest.mock.patch.object(fp, "http", fake_http): + self.assertEqual(len(self.client().installations()), 103) + + def test_installations_never_short_fails_at_bound(self): + calls = [] + + def fake_http(method, path, token, body=None, expect=dict): + calls.append(path) + return 200, [{"id": i} for i in range(100)] + + with ( + unittest.mock.patch.object(fp, "http", fake_http), + unittest.mock.patch("sys.stderr"), + ): + with self.assertRaises(SystemExit): + self.client().installations() + self.assertEqual(len(calls), fp.GITHUB_LIST_MAX_PAGES) + + def test_repositories_never_reaching_total_fails_at_bound(self): + calls = [] + + def fake_http(method, path, token, body=None, expect=dict): + calls.append(path) + page = int(path.rsplit("page=", 1)[1]) + return 200, { + "total_count": 10**9, + "repositories": [{"full_name": f"o/r{page}-{i}"} for i in range(100)], + } + + c = self.client() + c.token_for = lambda owner: "tok" + with ( + unittest.mock.patch.object(fp, "http", fake_http), + unittest.mock.patch("sys.stderr"), + ): + with self.assertRaises(SystemExit): + c.assert_repo_in_scope("o/missing") + self.assertEqual(len(calls), fp.GITHUB_LIST_MAX_PAGES) + + def test_repositories_found_on_second_page(self): + def fake_http(method, path, token, body=None, expect=dict): + page = int(path.rsplit("page=", 1)[1]) + names = [f"o/r{i}" for i in range(100)] if page == 1 else ["o/target"] + return 200, { + "total_count": 101, + "repositories": [{"full_name": n} for n in names], + } + + c = self.client() + c.token_for = lambda owner: "tok" + with unittest.mock.patch.object(fp, "http", fake_http): + self.assertIsNone(c.assert_repo_in_scope("o/target")) + + if __name__ == "__main__": unittest.main()