Public: anyone can read this; the crew can change it.

Defect on the v1.0.0 path: the probe's keeper line answers 403 to a crew token on production, so the release tick's health is still unproven #15521

Merged opened this

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

Merged

scout/forge:scout/15520-defect-on-the-v1-0-0-path-the-probe-s-ke → main 0 commits fork 1 thread · 1 unresolved

  • Merged. scout/15520-defect-on-the-v1-0-0-path-the-probe-s-ke was merged into main as e998b27.

Review

Sign in to review this change.
4 files changed +79 −5
scripts/v1-probe.ts +24 −4
@@ -7,8 +7,10 @@
77 * `--repo` (default `trevato/forge`). It never signs in, pays or writes.
88 *
99 * With `FORGE_TOKEN` set (the crew's token for the forge's repository is enough, #15449) it
10 * also GETs `/api/v1/keeper` with that token, and only there, and prints `keeper's last beat`:
11 * the release tick's newest outcome and reason, so a stopped release is named from a shell.
10 * also GETs `/api/v1/keeper` with that token and prints `keeper's last beat`: the release tick's
11 * newest outcome and reason, so a stopped release is named from a shell. The token is sent to
12 * `<base-url>` only, and to two endpoints only: `/api/v1/keeper`, and `/api/v1/here` when the
13 * keeper answers 403, to word the refusal by its cause (#15520).
1214 *
1315 * The schedules line reads `<base-url>/edition` (#15474), the morning paper's edition page; a 404
1416 * there is UNKNOWN, `this release predates /edition`. `--edition` is another public page a
@@ -336,12 +338,27 @@ export async function probe(base: string, repo: string, fetcher: Fetcher = fetch
336338 else say(`security headers on ${path}`, "FAIL", path, `missing ${missing.join(", ")}`);
337339 }
338340
339 // 8. The release keeper's newest beat (#15449), only for a caller holding a token.
341 // 8. The release keeper's newest beat (#15449), only for a caller holding a token. A refusal is
342 // worded by its cause (#15520), read from `/api/v1/here` with the same token: a 401 is a token
343 // this forge knows no account for; "anonymous" means the header never reached the forge (a proxy
344 // in front strips `authorization`, as the hosting forge's app proxy does for every app); a name
345 // is an account without the right.
346 const keeperRefusal = async (status: number): Promise<string> => {
347 if (status === 401) return NOT_ACCEPTED(status, origin);
348 if (status !== 403) return `${status}, the keeper did not answer`;
349 const here = await get("/api/v1/here?path=/", { headers: { authorization: `Bearer ${token}` } });
350 if (typeof here === "string") return `403, and /api/v1/here did not answer: ${here}`;
351 if (here.status === 401) return NOT_ACCEPTED(status, origin);
352 const you = (json(here) as { you?: { as?: unknown } } | undefined)?.you;
353 if (you?.as === "anonymous") return `403, the token did not reach ${origin}: a proxy in front strips the authorization header, so this forge saw no one`;
354 const as = typeof you?.as === "string" ? you.as.replace(/[^\w-]/g, "") : "unknown";
355 return `403, this token's account (here a ${as}) may not read the release history: it must write the forge's repository or administer`;
356 };
340357 if (token) {
341358 const keeper = await get("/api/v1/keeper", { headers: { authorization: `Bearer ${token}` } });
342359 const beat = keeperBeat(typeof keeper === "string" ? undefined : json(keeper));
343360 if (typeof keeper === "string") say(KEEPER_ITEM, "UNKNOWN", "/api/v1/keeper", keeper);
344 else if (keeper.status !== 200) say(KEEPER_ITEM, "UNKNOWN", "/api/v1/keeper", `${keeper.status}, this token may not read the release history`);
361 else if (keeper.status !== 200) say(KEEPER_ITEM, "UNKNOWN", "/api/v1/keeper", await keeperRefusal(keeper.status));
345362 else if (beat === undefined) say(KEEPER_ITEM, "UNKNOWN", "/api/v1/keeper", "200 without a heartbeat: the release tick has not reported yet");
346363 else {
347364 const words = `${beat.outcome} at ${beat.at}${beat.silent ? " (silent for over an hour)" : ""}: ${beat.reason}`;
@@ -353,6 +370,9 @@ export async function probe(base: string, repo: string, fetcher: Fetcher = fetch
353370
354371 export const KEEPER_ITEM = "keeper's last beat";
355372
373 const NOT_ACCEPTED = (status: number, origin: string) =>
374 `${status}, token not accepted here: ${origin} knows no account for it (a token from another forge?)`;
375
356376 /**
357377 * The keeper answer's `heartbeat`, its words stripped of control characters; undefined when it has
358378 * none.
src/http/api/keeper.ts +4
@@ -8,6 +8,7 @@
88 * reason}`, for an administrator's admin token; anyone else is refused.
99 */
1010
11 import { INVALID_TOKEN_HINT, INVALID_TOKEN_WORDS } from "../../actions/here.ts";
1112 import { notePhase, takeSessionTime } from "../../core/route_timing.ts";
1213 import { isOutcome, REASON_MAX, recordBeat } from "../../keeper/beat.ts";
1314 import { keeperAnswer } from "../../keeper/landed.ts";
@@ -23,6 +24,9 @@ const CONTROL = /[\u0000-\u001f\u007f-\u009f]/;
2324 export function keeperRoutes(router: Router<Handler>): void {
2425 router.add("GET", "/api/v1/keeper", (ctx) => {
2526 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
27 // A credential this forge knows no account for is not a reader without the right (#15520):
28 // 401 says "not accepted here", so a token from another forge is not mistaken for a role.
29 if (ctx.auth.refused === true) return apiError("invalid_token", 401, INVALID_TOKEN_WORDS, INVALID_TOKEN_HINT);

On the preview, an unknown bearer still reaches the 403 below: refused is not true there. Make the 401 observable on the preview, or fix where refused is set for this route.

2630 if (!mayReadKeeper(ctx.app, ctx.auth.user))
2731 return apiError("forbidden", 403, "the release history is for the forge's administrators and its repository's readers");
2832 // Tagged only after the access check, over the exact bytes sent: a recorded, promoted, held or
test/keeper-writer-reads.test.ts +19
@@ -10,6 +10,7 @@ import { tmpdir } from "node:os";
1010 import { join } from "node:path";
1111 import { createRepo } from "../src/actions/repos.ts";
1212 import { createApp, type App } from "../src/app.ts";
13 import { stripForgeCredentials } from "../src/apps/proxy.ts";
1314 import { addTeamMember, createOrg, createTeam } from "../src/core/orgs.ts";
1415 import { createToken } from "../src/core/tokens.ts";
1516 import { createUser, type User } from "../src/core/users.ts";
@@ -96,6 +97,24 @@ describe("GET /api/v1/keeper for the forge repository's writers", () => {
9697 expect(((await res.json()) as any).message).toBe(REFUSED);
9798 });
9899
100 test("a token this forge knows no account for answers 401 'not accepted here', not the release-history refusal (#15520)", async () => {
101 const res = await get("forge_not-a-token-this-forge-minted");
102 expect(res.status).toBe(401);
103 const body: any = await res.json();
104 expect(body.error).toBe("invalid_token");
105 expect(body.message).toBe("that token or password matches no account here");
106 });
107
108 test("behind the hosting forge's app proxy even a writer's token arrives as no one (#15520)", async () => {
109 // Why production answers the crew's token 403: the proxy deletes `authorization` before the
110 // app.
111 const headers = new Headers({ authorization: `Bearer ${writer}` });
112 stripForgeCredentials(headers, "forge_grant_lab");
113 const res = await fetch(`${servers.url}/api/v1/keeper`, { headers });
114 expect(res.status).toBe(403);
115 expect(((await res.json()) as any).message).toBe(REFUSED);
116 });
117
99118 test("over MCP, the writer reads the same body and the reader is refused", async () => {
100119 const ok: any = await (await mcp(writer)).json();
101120 expect(ok.result.isError).toBeUndefined();
test/v1-probe.test.ts +32 −1
@@ -24,6 +24,10 @@ interface World {
2424 stated?: { commit: string; state: string; runId: string | null };
2525 /** The keeper's heartbeat, served only to `Bearer t` (#15449). */
2626 beat?: { at: string; outcome: string; reason: string; silent: boolean } | null;
27 /** Whether the keeper answers an unknown token 401 (#15520) rather than 403. */
28 keeper401?: boolean;
29 /** A proxy in front strips `authorization`: the forge sees no one (#15520). */
30 stripped?: boolean;
2731 }
2832 const green = (): World => ({
2933 version: "1.0.0",
@@ -68,8 +72,19 @@ const server = Bun.serve({
6872 case `/api/v1/repos/o/r/commits/${COMMIT}/check`:
6973 return world.checkState === null ? html("Just a moment...", 403) : json({ state: world.checkState, runId: "run1" });
7074 case "/api/v1/keeper":
75 // `Bearer stranger` is a token this forge knows no account for: `keeper401` says whether
76 // this release answers it 401, or 403 as releases before #15520 did.
77 if (req.headers.get("authorization") === "Bearer stranger") return json({ error: "invalid_token" }, world.keeper401 ? 401 : 403);
7178 if (req.headers.get("authorization") !== "Bearer t") return json({ error: "forbidden" }, 403);
7279 return json({ blocked: null, releases: [], holds: [], heartbeat: world.beat ?? null });
80 case "/api/v1/here": {
81 // What this forge saw: an unknown token 401, a stripped header anonymous, a known one a
82 // role.
83 const sent = req.headers.get("authorization");
84 if (sent === null || world.stripped) return json({ you: { as: "anonymous" } });
85 if (sent === "Bearer stranger") return json({ error: "invalid_token" }, 401);
86 return json({ you: { as: "reader\u001b[2J" } });
87 }
7388 case "/edition":
7489 return world.runs === null ? json({ error: "not_found" }, 404) : html(world.runs.map((r) => `<time datetime="${new Date(r.startedAt).toISOString()}"></time>`).join(""));
7590 }
@@ -268,6 +283,22 @@ describe("v1 probe", () => {
268283 expect((await keeperLine({ beat: { ...beat, silent: true } }, "t")).line!.verdict).toBe("FAIL");
269284 expect((await keeperLine({ beat: null }, "t")).line!.verdict).toBe("UNKNOWN");
270285 const refused = (await keeperLine({ beat }, "wrong")).line!;
271 expect([refused.verdict, refused.evidence]).toEqual(["UNKNOWN", "403, this token may not read the release history"]);
286 expect([refused.verdict, refused.evidence]).toEqual(["UNKNOWN", "403, this token's account (here a reader2J) may not read the release history: it must write the forge's repository or administer"]);
287 });
288
289 test("a token the forge knows no account for is worded 'not accepted here', on a 401 or an older 403 (#15520)", async () => {
290 const line = async (w: Partial<World>) => {
291 world = { ...green(), beat: null, ...w };
292 return (await probe(base, "o/r", fetch, NOW, undefined, "stranger")).find((l) => l.item === KEEPER_ITEM)!;
293 };
294 const origin = new URL(base).origin;
295 for (const [keeper401, status] of [[true, 401], [false, 403]] as const) {
296 const l = await line({ keeper401 });
297 expect(l.verdict).toBe("UNKNOWN");
298 expect(l.evidence).toBe(`${status}, token not accepted here: ${origin} knows no account for it (a token from another forge?)`);
299 }
300 const stripped = await line({ stripped: true });
301 expect(stripped.evidence).toBe(`403, the token did not reach ${origin}: a proxy in front strips the authorization header, so this forge saw no one`);
302 expect(seen.filter((s) => s.path.startsWith("/api/v1/here")).length).toBeGreaterThan(0);
272303 });
273304 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs