Public: anyone can read this; the crew can change it.
Every cursor-paged API list sends a Link rel=next header, so an agent follows pages untaught #14660
Check · Review · Land · Live done — Landed and live in production.
torque/forge:torque/14642-every-cursor-paged-api-list-sends-a-link → main 1 commit fork
3 files
changed +111
−2
src/http/cursor_lists.ts +25
| @@ -21,3 +21,28 @@ export function pagedByNumber(q: URLSearchParams): boolean { | ||
| 21 | 21 | export function pageParamRefused(pattern: string, q: URLSearchParams): Response | undefined { |
| 22 | 22 | return CURSOR_LISTS.has(pattern) && pagedByNumber(q) ? apiError("invalid_query", 400, PAGE_PARAM_SAYS) : undefined; |
| 23 | 23 | } |
| 24 | ||
| 25 | /** | |
| 26 | * RFC 8288: `Link: <url>; rel="next"` for a cursor list's 200 JSON page that has more, none on the | |
| 27 | * last page. `base` is the public URL built from configuration (`publicUrl`), never a caller | |
| 28 | * header; the caller's other query values are kept and only `cursor` is replaced. | |
| 29 | */ | |
| 30 | export async function withLinkNext(pattern: string, base: URL, response: Response): Promise<Response> { | |
| 31 | if (!CURSOR_LISTS.has(pattern) || response.status !== 200) return response; | |
| 32 | if (!(response.headers.get("content-type") ?? "").includes("json")) return response; | |
| 33 | let next: unknown; | |
| 34 | try { | |
| 35 | next = ((await response.clone().json()) as { next_cursor?: unknown } | null)?.next_cursor; | |
| 36 | } catch { | |
| 37 | return response; | |
| 38 | } | |
| 39 | // Cursors are opaque: a string, or an integer where the list pages by row number. | |
| 40 | if (typeof next === "number" && Number.isSafeInteger(next)) next = String(next); | |
| 41 | if (typeof next !== "string" || next === "") return response; | |
| 42 | const url = new URL(base.href); | |
| 43 | url.hash = ""; | |
| 44 | url.searchParams.set("cursor", next); | |
| 45 | const copy = new Response(response.body, response); | |
| 46 | copy.headers.append("link", `<${url.href}>; rel="next"`); | |
| 47 | return copy; | |
| 48 | } |
src/http/server.ts +3 −2
| @@ -558,7 +558,7 @@ import { CAME_FROM_ELSEWHERE, forbiddenPage, notFoundPage, SENT_FROM_ELSEWHERE, | ||
| 558 | 558 | import { targetForShortCode } from "../origins/oauth.ts"; |
| 559 | 559 | import { apiRoutes, oauthCallbackUri } from "./api.ts"; |
| 560 | 560 | import { brokerStart } from "../spawn/origin_broker.ts"; |
| 561 | import { pageParamRefused } from "./cursor_lists.ts"; | |
| 561 | import { pageParamRefused, withLinkNext } from "./cursor_lists.ts"; | |
| 562 | 562 | import { inNamespace } from "../core/proposal_token.ts"; |
| 563 | 563 | import { inContribNamespace } from "../core/contribute_token.ts"; |
| 564 | 564 | import { takesOutside } from "../actions/contribute_token.ts"; |
| @@ -3572,5 +3572,6 @@ async function answer( | ||
| 3572 | 3572 | ) { |
| 3573 | 3573 | return withRateHeaders(pageNotFoundAsJson(url.origin), verdict); |
| 3574 | 3574 | } |
| 3575 | return withRateHeaders(noindex ? withHeader(response, "x-robots-tag", "noindex") : response, verdict); | |
| 3575 | const linked = isApiPath && routeMethod === "GET" ? await withLinkNext(matched.pattern, publicUrl(request, app.config.url), response) : response; | |
| 3576 | return withRateHeaders(noindex ? withHeader(linked, "x-robots-tag", "noindex") : linked, verdict); | |
| 3576 | 3577 | } |
test/link-next.test.ts added +83
| @@ -0,0 +1,83 @@ | ||
| 1 | /** | |
| 2 | * A cursor-paged list sends `Link: <url>; rel="next"` while more remain, none on the last page | |
| 3 | * (#14642). | |
| 4 | */ | |
| 5 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 6 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 7 | import { tmpdir } from "node:os"; | |
| 8 | import { join } from "node:path"; | |
| 9 | import { createRepo } from "../src/actions/repos.ts"; | |
| 10 | import { createApp, type App } from "../src/app.ts"; | |
| 11 | import { createIssue } from "../src/core/issues.ts"; | |
| 12 | import { createToken } from "../src/core/tokens.ts"; | |
| 13 | import { createUser } from "../src/core/users.ts"; | |
| 14 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 15 | ||
| 16 | describe("Link rel=next on cursor lists", () => { | |
| 17 | let dataDir: string; | |
| 18 | let app: App; | |
| 19 | let servers: Servers; | |
| 20 | let headers: Record<string, string>; | |
| 21 | ||
| 22 | beforeAll(async () => { | |
| 23 | dataDir = await mkdtemp(join(tmpdir(), "forge-link-next-")); | |
| 24 | app = createApp({ dataDir, port: 0 }); | |
| 25 | servers = await serve(app); | |
| 26 | const made = await createUser(app.db, { | |
| 27 | origin: "seed", | |
| 28 | trusted: true, | |
| 29 | username: "alice", | |
| 30 | email: "[email protected]", | |
| 31 | password: "a-good-passphrase", | |
| 32 | adminIfFirst: true, | |
| 33 | }); | |
| 34 | if (!made.ok) throw new Error(made.error); | |
| 35 | headers = { authorization: `Bearer ${createToken(app.db, made.value.id, "t", "admin").token}` }; | |
| 36 | const lib = await createRepo(app, made.value, { name: "lib", visibility: "public" }); | |
| 37 | if (!lib.ok) throw new Error(String(lib.error)); | |
| 38 | for (let n = 1; n <= 5; n++) createIssue(app.db, { repoId: lib.value.id, authorId: made.value.id, title: `issue ${n}` }); | |
| 39 | }); | |
| 40 | ||
| 41 | afterAll(async () => { | |
| 42 | await servers.stop(); | |
| 43 | await rm(dataDir, { recursive: true, force: true }); | |
| 44 | }); | |
| 45 | ||
| 46 | const nextOf = (res: Response): URL | undefined => { | |
| 47 | const m = /^<([^>]+)>; rel="next"$/.exec(res.headers.get("link") ?? ""); | |
| 48 | return m ? new URL(m[1]!) : undefined; | |
| 49 | }; | |
| 50 | ||
| 51 | test("three pages: next on the first two, none on the last, other query values kept", async () => { | |
| 52 | const seen: number[] = []; | |
| 53 | let res = await fetch(`${servers.url}/api/v1/repos/alice/lib/issues?limit=2&state=open`, { headers }); | |
| 54 | for (let pages = 0; pages < 5; pages++) { | |
| 55 | expect(res.status).toBe(200); | |
| 56 | const body = (await res.json()) as { items: { number: number }[]; next_cursor: string | number | null }; | |
| 57 | seen.push(...body.items.map((i) => i.number)); | |
| 58 | const next = nextOf(res); | |
| 59 | if (body.next_cursor === null) { | |
| 60 | expect(res.headers.get("link")).toBeNull(); | |
| 61 | break; | |
| 62 | } | |
| 63 | expect(next).toBeDefined(); | |
| 64 | expect(next!.pathname).toBe("/api/v1/repos/alice/lib/issues"); | |
| 65 | expect(next!.searchParams.get("limit")).toBe("2"); | |
| 66 | expect(next!.searchParams.get("state")).toBe("open"); | |
| 67 | expect(next!.searchParams.get("cursor")).toBe(String(body.next_cursor)); | |
| 68 | // The origin is the server's own, not a caller header. | |
| 69 | res = await fetch(`${servers.url}${next!.pathname}${next!.search}`, { headers: { ...headers, host: "evil.example", "x-forwarded-host": "evil.example" } }); | |
| 70 | } | |
| 71 | expect(seen.length).toBe(5); | |
| 72 | expect(new Set(seen).size).toBe(5); | |
| 73 | expect(res.headers.get("link")).toBeNull(); | |
| 74 | }); | |
| 75 | ||
| 76 | test("a forged host or forwarded header never reaches the link", async () => { | |
| 77 | const res = await fetch(`${servers.url}/api/v1/repos/alice/lib/issues?limit=2`, { | |
| 78 | headers: { ...headers, "x-forwarded-host": "evil.example", "x-forwarded-proto": "https" }, | |
| 79 | }); | |
| 80 | expect(nextOf(res)?.host).not.toBe("evil.example"); | |
| 81 | expect(nextOf(res)?.host).toBe(new URL(servers.url).host); | |
| 82 | }); | |
| 83 | }); |
new above
offline