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

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

torque/forge:torque/14642-every-cursor-paged-api-list-sends-a-link → main 1 commit fork

  • Merged. torque/14642-every-cursor-paged-api-list-sends-a-link was merged into main as de86dd4.

Review

Sign in to review this change.
3 files changed +111 −2
src/http/cursor_lists.ts +25
@@ -21,3 +21,28 @@ export function pagedByNumber(q: URLSearchParams): boolean {
2121 export function pageParamRefused(pattern: string, q: URLSearchParams): Response | undefined {
2222 return CURSOR_LISTS.has(pattern) && pagedByNumber(q) ? apiError("invalid_query", 400, PAGE_PARAM_SAYS) : undefined;
2323 }
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,
558558 import { targetForShortCode } from "../origins/oauth.ts";
559559 import { apiRoutes, oauthCallbackUri } from "./api.ts";
560560 import { brokerStart } from "../spawn/origin_broker.ts";
561 import { pageParamRefused } from "./cursor_lists.ts";
561 import { pageParamRefused, withLinkNext } from "./cursor_lists.ts";
562562 import { inNamespace } from "../core/proposal_token.ts";
563563 import { inContribNamespace } from "../core/contribute_token.ts";
564564 import { takesOutside } from "../actions/contribute_token.ts";
@@ -3572,5 +3572,6 @@ async function answer(
35723572 ) {
35733573 return withRateHeaders(pageNotFoundAsJson(url.origin), verdict);
35743574 }
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);
35763577 }

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs