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

Report key: an admin makes, rotates and revokes it through the JSON API, in the settings page's words #18289

opened this

Merged 5h 26m ago, live in production

torque/forge:torque/18286-report-key-an-admin-makes-rotates-and-re → main 0 commits fork 1 thread · 1 unresolved

  • Merged. torque/18286-report-key-an-admin-makes-rotates-and-re was merged into main as ff66ea0.

Review

Sign in to review this change.
9 files changed +80 −9
docs/features/crash-reports.md +1 −1
@@ -6,7 +6,7 @@ row: Crash reports
66
77 A crash an app reports becomes an ordinary `bug` issue on the app's own repository. An app, or its tester, sends it with `POST /api/v1/repos/<owner>/<name>/crash-reports` (`/api/v1/apps/...` is the same handler, kept as an alias for builds already using it; use `repos`), no token for a public repository (a tester cannot always open an issue, so the forge files it for them). The repository need not be a hosted app: a native app's repository takes reports too. A private repository answers a signed-in person who can write to it, or a build that sends its report key as `X-Report-Key`; everyone else gets the same 404 as for a repository that does not exist, so the door never says which private ones there are.
88
9 - **The report key.** The repository's admin makes it in the "Report key" card of the repository's settings page: "Make report key" shows it once, "Rotate report key" replaces it at once, "Revoke report key" removes it. In the terminal it is the same screen as "Crash groups of a repository": `n` makes or rotates, `x` revokes, after a confirm, as the page asks. The page's forms are `src/web/pages/report_key.ts`, the terminal's keys `src/tui/screens/crash_groups.ts`, both through `src/actions/report_key.ts`. A script does the same with `POST` (make or rotate, answers 201 `{key}` once) and `DELETE` (revoke) on `/api/v1/repos/<owner>/<name>/report-key`, an `rw` token of the admin; anyone else gets 404. Only its hash is kept. It files crash reports for that repository and does nothing else.
9 - **The report key.** The repository's admin makes it in the "Report key" card of the repository's settings page: "Make report key" shows it once, "Rotate report key" replaces it at once, "Revoke report key" removes it. In the terminal it is the same screen as "Crash groups of a repository": `n` makes or rotates, `x` revokes, after a confirm, as the page asks. The page's forms are `src/web/pages/report_key.ts`, the terminal's keys `src/tui/screens/crash_groups.ts`, both through `src/actions/report_key.ts`. A script does the same with `POST` (make or rotate, answers 201 `{key}` once) and `DELETE` (revoke) on `/api/v1/repos/<owner>/<name>/report-key`, an `rw` token of the admin, and reads when it was made with `GET` (the key line, never the key, an `ro` token will do); anyone else gets 404. Only its hash is kept. It files crash reports for that repository and does nothing else.
1010
1111 - **The body.** The Apple app's own: `{"v":1,"kind":"crash","app":{"version":"0.1","build":"50"},"os":"iOS 26.2","device":"iPhone","at":"2026-12-04T00:00:00Z","previousRun":{"launchedAt":"2026-12-03T23:58:00Z","breadcrumbs":["12:00:01 tab repos"]},"findings":[{"kind":"crash","signal":"5","exception":"6","termination":"Namespace SIGNAL","version":"0.1","frames":["Forge +1445064","SwiftUI +7"]}]}`. Each finding (up to 20; `crash`, `hang`, `cpu` or `disk`, 1 to 12 frames) files or counts one group, with the previous run's breadcrumbs; with no finding the previous run is one group with no frames. `os` and `device` model go in the issue, nothing that names a person has a field. The first shape, `{"v":1,"build":"50","kind":"crash","frames":[...],"breadcrumbs":[...]}`, still works. Up to 200 breadcrumbs, each line cut at 200 characters. The app's other spellings work too: `app` as text (up to 64 characters) or `{version, build}`, `previousRun` as `true` or an object, `breadcrumbs` on a finding, and no build at all when it sends findings (such a report is counted but never reopens a closed group, and a build is never invented for it); `at` is checked and dropped, the forge stamping its own time; two findings of one group count once. A top-level `frames` and `findings` together file all of them. Any other field is a 422 in words; over 128 KB is a 413. The answer is 202 `{issue, count, opened, reopened, issues}`. The anonymous `/api/v1/telemetry/apple` door also takes a strict crash report into the owner's telemetry view, apart from these issues and with no key (#18006).
1212 - **One issue per group.** Reports with the same kind and top three frames are one group. The first files the issue (title from the top frame and the build, body with the frames, breadcrumbs, count and builds); a later one raises the count on the issue's "Seen" line (nothing else in the body is rewritten) and adds a comment for the first 20 sightings.
src/contract/openapi/routes/deleteApiV1ReposOwnerNameReportKey.ts +1 −1
@@ -6,5 +6,5 @@ export const route: Route = {
66 summary: "Revoke a repository's report key (rw scope, its admin); 404 when there is none",
77 auth: true,
88 role: "member",
9 refuses: [401, 404],
9 refuses: [404],
1010 };
src/contract/openapi/routes/getApiV1ReposOwnerNameReportKey.ts added +13
@@ -0,0 +1,13 @@
1 import type { Route } from "../shared.ts";
2
3 export const route: Route = {
4 method: "get",
5 path: "/api/v1/repos/{owner}/{name}/report-key",
6 summary: "Read the report key's state: the key line, never the key (repository admin)",
7 detail: "Answers `{made, line}`: when the key was made (or null) and the settings page's words about it, never the key. The repository's admin only; anyone else gets 404.",
8 auth: true,
9 role: "member",

The route answers only the repository's admin, so role "member" is wrong; use the admin role the sibling POST/DELETE entries carry.

10 status: 200,
11 refusals: { not_found: "no repository by that name, or you are not its admin (404)" },
12 refuses: [404],
13 };
src/contract/openapi/routes/index.ts +1
@@ -243,6 +243,7 @@ export { route as getApiV1ReposOwnerNamePullsNumberThreads } from "./getApiV1Rep
243243 export { route as getApiV1ReposOwnerNameQueue } from "./getApiV1ReposOwnerNameQueue.ts";
244244 export { route as getApiV1ReposOwnerNameQueueGates } from "./getApiV1ReposOwnerNameQueueGates.ts";
245245 export { route as getApiV1ReposOwnerNameReleases } from "./getApiV1ReposOwnerNameReleases.ts";
246 export { route as getApiV1ReposOwnerNameReportKey } from "./getApiV1ReposOwnerNameReportKey.ts";
246247 export { route as getApiV1ReposOwnerNameReports } from "./getApiV1ReposOwnerNameReports.ts";
247248 export { route as getApiV1ReposOwnerNameReportsIdStatus } from "./getApiV1ReposOwnerNameReportsIdStatus.ts";
248249 export { route as getApiV1ReposOwnerNameRoadmap } from "./getApiV1ReposOwnerNameRoadmap.ts";
src/contract/openapi/routes/postApiV1ReposOwnerNameReportKey.ts +1 −1
@@ -9,5 +9,5 @@ export const route: Route = {
99 role: "member",
1010 status: 201,
1111 refusals: { not_found: "no such repository, or you do not administer it (404)" },
12 refuses: [401, 404],
12 refuses: [404],
1313 };
src/http/api/crash_reports.ts +24 −6
@@ -14,11 +14,11 @@
1414
1515 import type { App } from "../../app.ts";
1616 import { reportCrashes } from "../../actions/crash_reports.ts";
17 import { makeKey, revokeKey } from "../../actions/report_key.ts";
17 import { keyLine, makeKey, revokeKey } from "../../actions/report_key.ts";
1818 import { parseReports, REPORT_ERRORS } from "../../core/crash_groups.ts";
1919 import { RateLimiter } from "../../core/ratelimit.ts";
20 import { reportKeyMatches } from "../../core/report_key.ts";
21 import { canWrite, getRepo } from "../../core/repos.ts";
20 import { reportKeyMadeAt, reportKeyMatches } from "../../core/report_key.ts";
21 import { canAdmin, canWrite, getRepo } from "../../core/repos.ts";
2222 import type { Handler } from "../context.ts";
2323 import { BODY_CAPS, RATE_CAPS, readBounded } from "../limits.ts";
2424 import { clientBucket } from "../peer.ts";
@@ -31,10 +31,24 @@ const limits = new WeakMap<App, { client: RateLimiter; forge: RateLimiter }>();
3131 export function crashReportRoutes(router: Router<Handler>): void {
3232 router.add("POST", "/api/v1/repos/:owner/:name/crash-reports", crashReport);
3333 router.add("POST", "/api/v1/apps/:owner/:name/crash-reports", crashReport);
34 router.add("GET", "/api/v1/repos/:owner/:name/report-key", reportKeyRead);
3435 router.add("POST", "/api/v1/repos/:owner/:name/report-key", reportKeyWrite(true));
3536 router.add("DELETE", "/api/v1/repos/:owner/:name/report-key", reportKeyWrite(false));
3637 }
3738
39 /**
40 * The key's state (#18286): the settings page's key line and when it was made, never the key.
41 * Admins only, 404 for anyone else.
42 */
43 function reportKeyRead(ctx: Parameters<Handler>[0]): Response {
44 const repo = getRepo(ctx.app.db, ctx.params.owner!, ctx.params.name!);
45 const user = ctx.auth.user;
46 if (repo === undefined || user === undefined || !canAdmin(ctx.app.db, repo, user)) return apiError("not_found", 404, "no repository by that name, or it has no report key");
47 const denied = requireScope(ctx, "ro");
48 if (denied) return denied;
49 return json({ made: reportKeyMadeAt(ctx.app.db, repo.id) ?? null, line: keyLine(ctx.app, repo) }, 200, { "cache-control": "no-store" });
50 }
51
3852 /**
3953 * Make or rotate (POST) or revoke (DELETE) the report key (#18224): the same actions, and so the
4054 * same words, as the settings page. The key is shown once; 404 when the caller does not administer
@@ -42,11 +56,15 @@ export function crashReportRoutes(router: Router<Handler>): void {
4256 */
4357 function reportKeyWrite(make: boolean) {
4458 return (ctx: Parameters<Handler>[0]): Response => {
59 // the admin check comes first, so a caller who is no admin (signed in or not) cannot tell this
60 // door from a missing repository
61 const repo = getRepo(ctx.app.db, ctx.params.owner!, ctx.params.name!);
62 const user = ctx.auth.user;
63 if (repo === undefined || user === undefined || !canAdmin(ctx.app.db, repo, user)) return apiError("not_found", 404, "no repository by that name, or it has no report key");
4564 const denied = requireScope(ctx, "rw");
4665 if (denied) return denied;
47 const repo = getRepo(ctx.app.db, ctx.params.owner!, ctx.params.name!);
48 const done = repo === undefined ? undefined : make ? makeKey(ctx.app, ctx.auth.user, repo) : revokeKey(ctx.app, ctx.auth.user, repo);
49 if (done === undefined || !done.ok || done.value === false) return apiError("not_found", 404, "no repository by that name, or it has no report key");
66 const done = make ? makeKey(ctx.app, user, repo) : revokeKey(ctx.app, user, repo);
67 if (!done.ok || done.value === false) return apiError("not_found", 404, "no repository by that name, or it has no report key");
5068 const noStore = { "cache-control": "no-store" };
5169 return make
5270 ? json({ key: done.value, message: "Shown once: put it in the build as X-Report-Key. Making another replaces it at once." }, 201, noStore)
src/http/lazy_lists.ts +1
@@ -1275,6 +1275,7 @@ export const API_ROUTES = [
12751275 ["POST", "/api/v1/telemetry/apple"],
12761276 ["POST", "/api/v1/repos/:owner/:name/crash-reports"],
12771277 ["POST", "/api/v1/apps/:owner/:name/crash-reports"],
1278 ["GET", "/api/v1/repos/:owner/:name/report-key"],
12781279 ["POST", "/api/v1/repos/:owner/:name/report-key"],
12791280 ["DELETE", "/api/v1/repos/:owner/:name/report-key"],
12801281 ["GET", "/api/v1/version"],
test/crash-report-key.test.ts +37
@@ -190,4 +190,41 @@ describe("report key", () => {
190190 expect(res.status).toBe(204);
191191 expect(app.db.query("SELECT COUNT(*) AS n FROM issue").get()).toEqual(before);
192192 });
193
194 test("the JSON API makes, rotates, revokes and reads the key; a non-admin gets the identical 404", async () => {
195 // a repository of its own: the report limit counts per repository and address
196 const owner = getUserByName(app.db, "alice")!;
197 const fresh = await createRepo(app, owner, { name: "fresh", visibility: "private" });
198 if (!fresh.ok) throw new Error(fresh.error);
199 const reports = (h: Record<string, string>) => post(real, h, `${servers.url}/api/v1/repos/alice/fresh/crash-reports`);
200 const door = `${servers.url}/api/v1/repos/alice/fresh/report-key`;
201 const call = (method: string, cookie?: string) => fetch(door, { method, headers: cookie ? { cookie } : {} });
202 const made = await call("POST", aliceCookie);
203 expect(made.status).toBe(201);
204 const first = ((await made.json()) as { key: string }).key;
205 expect((await reports({ "x-report-key": first })).status).toBe(202);
206 const got = await call("GET", aliceCookie);
207 const gotText = await got.text();
208 expect(got.status).toBe(200);
209 expect(gotText).toContain("A report key was made");
210 expect(gotText).not.toContain(first);
211 const second = ((await (await call("POST", aliceCookie)).json()) as { key: string }).key;
212 expect(second).not.toBe(first);
213 expect((await reports({ "x-report-key": first })).status).toBe(404);
214 expect((await reports({ "x-report-key": second })).status).toBe(202);
215 const revoked = await call("DELETE", aliceCookie);
216 expect(revoked.status).toBe(200);
217 expect(((await (await call("GET", aliceCookie)).json()) as { line: string }).line).toContain("No report key");
218 expect((await reports({ "x-report-key": second })).status).toBe(404);
219 const missing = await fetch(`${servers.url}/api/v1/repos/alice/nope/report-key`, { method: "POST", headers: { cookie: aliceCookie } });
220 const gone = await missing.text();
221 for (const method of ["GET", "POST", "DELETE"]) {
222 for (const cookie of [bobCookie, undefined]) {
223 const res = await call(method, cookie);
224 expect(res.status).toBe(404);
225 expect(await res.text()).toBe(gone);
226 }
227 }
228 expect(app.db.query("SELECT 1 FROM report_key WHERE repo_id = ?1").get(getRepo(app.db, "alice", "fresh")!.id)).toBeNull();
229 });
193230 });
test/route-timing-coverage.test.ts +1
@@ -28,6 +28,7 @@ const UNTIMED: Record<string, string> = {
2828 "/api/v1/templates": "the marked repositories, one indexed read; no page to phase",
2929 "/api/v1/repos/:owner/:name/drop": "an app's drop listing, one indexed read and a directory scan; no page to phase",
3030 "/api/v1/repos/:owner/:name/crew-devices": "a repository admin's device list, one indexed read; no page to phase",
31 "/api/v1/repos/:owner/:name/report-key": "a repository admin's report-key state, one indexed read; no page to phase",
3132 "/api/v1/repos/:owner/:name/app/registry": "an app's installed registry items, one indexed read; no page to phase",
3233 "/api/v1/repos/:owner/:name/app/registry/updates": "an app's items behind the registry, one indexed read and a catalogue compare; no page to phase",
3334 "/api/v1/admin/plans": "an admin's plans with the measured median, two indexed reads; no page to phase",

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs