diff --git a/pm-stack/projector/src/github.ts b/pm-stack/projector/src/github.ts index 2ce17f93b9..fc3db3dfb0 100644 --- a/pm-stack/projector/src/github.ts +++ b/pm-stack/projector/src/github.ts @@ -211,6 +211,16 @@ export function nextPageFromLink(linkHeader: string | null): string | null { return null; } +/// Volume bound for listResource (#6622 pagination contract, rule 2; +/// #9010): every other page loop in the pm-stack carries a named bound. +/// Sized generously above the largest legitimate reconcile listing +/// across the four repos at per_page=100 (200 pages = 20,000 rows). +/// Breach is an error naming the listing (rule 3: truncation is never +/// silent) — a stream that keeps minting next links (misbehaving +/// proxy, GitHub pagination defect, redirect loop) fails closed +/// instead of growing memory without limit. +export const MAX_LIST_PAGES = 200; + export async function listResource( auth: GitHubAuth, initialPath: string, @@ -219,7 +229,9 @@ export async function listResource( ): Promise { const out: T[] = []; let path: string | null = initialPath; + let pages = 0; while (path) { + pages += 1; // nextPageFromLink returns the Link header's absolute URL — use it // as-is. Prefixing it again produced api.github.com/https://... 404s, // which discarded every multi-page stream (written=0 everywhere). @@ -249,6 +261,11 @@ export async function listResource( // of fetching every page of history on every pass. if (opts.stopAfterPage?.(page as T[])) break; path = nextPageFromLink(res.headers.get("link")); + if (path && pages >= MAX_LIST_PAGES) { + throw new Error( + "github_api_pagination_bound: " + initialPath + " exceeded " + MAX_LIST_PAGES + " pages", + ); + } } return out; } diff --git a/pm-stack/projector/test/github.test.ts b/pm-stack/projector/test/github.test.ts index 80d278a4e9..07a3627a84 100644 --- a/pm-stack/projector/test/github.test.ts +++ b/pm-stack/projector/test/github.test.ts @@ -1,6 +1,6 @@ import { describe, test, expect } from "bun:test"; import { generateKeyPairSync } from "node:crypto"; -import { appJwt, apiTokenFor, listResource } from "../src/github"; +import { appJwt, apiTokenFor, listResource, MAX_LIST_PAGES } from "../src/github"; function testKeyB64(type: "pkcs8" | "pkcs1"): string { const { privateKey } = generateKeyPairSync("rsa", { modulusLength: 2048 }); @@ -100,6 +100,49 @@ describe("listResource pagination", () => { }); }); + +describe("listResource pagination bound (#9010)", () => { + test("a stream that keeps minting next links errors at the bound, naming the listing", async () => { + let calls = 0; + const fetchFn = async (url: string) => { + calls += 1; + const next = url + (url.includes("page=") ? "" : "?page=2"); + return new Response(JSON.stringify([{ n: calls }]), { + status: 200, + headers: { link: "<" + next + ">; rel=\"next\"" }, + }); + }; + const result = await listResource<{ n: number }>( + { kind: "token", token: "t" }, + "repos/o/r/issues?per_page=100", + fetchFn as unknown as typeof fetch, + ).catch((error: unknown) => error); + expect(result).toBeInstanceOf(Error); + expect((result as Error).message).toContain("github_api_pagination_bound"); + expect((result as Error).message).toContain("repos/o/r/issues?per_page=100"); + expect(calls).toBe(MAX_LIST_PAGES); + }); + + test("a legitimate read of exactly the bound completes when the last page has no next link", async () => { + let calls = 0; + const fetchFn = async () => { + calls += 1; + const isLast = calls >= MAX_LIST_PAGES; + return new Response(JSON.stringify([{ n: calls }]), { + status: 200, + headers: isLast ? {} : { link: "; rel=\"next\"" }, + }); + }; + const rows = await listResource<{ n: number }>( + { kind: "token", token: "t" }, + "repos/o/r/issues?per_page=100", + fetchFn as unknown as typeof fetch, + ); + expect(rows.length).toBe(MAX_LIST_PAGES); + expect(calls).toBe(MAX_LIST_PAGES); + }); +}); + describe("apiTokenFor installation resolution", () => { test("discovers the installation per repo when no static id is configured", async () => { const env = { GITHUB_APP_ID: "123", GITHUB_APP_PRIVATE_KEY: testKeyB64("pkcs8") };