Public: anyone can read this; the crew can change it.
Crash reports: a private app's owner makes a report key, and a build with it can file reports without a session #18242
Closed 5h 23m ago
scout/forge:scout/18224-crash-reports-a-private-app-s-owner-make → main 5 commits fork
18 files
changed +372
−5
- docs/features/crash-reports.md
- docs/features/report-key.md
- src/actions/report_keys.ts
- src/contract/openapi/routes/deleteApiV1AppsOwnerNameReportKey.ts
- src/contract/openapi/routes/index.ts
- src/contract/openapi/routes/postApiV1AppsOwnerNameReportKey.ts
- src/core/migration-pins/20261204120000-report-key.pin
- src/core/migrations/20261204120000-report-key.ts
- src/core/report_keys.ts
- src/http/api.ts
- src/http/api/crash_reports.ts
- src/http/lazy_lists.ts
- src/http/routes/repo_settings.ts
- src/tui/screens/reportkey.ts
- src/tui/screens/workspace.ts
- src/web/pages/report_key.ts
- src/web/pages/settings.ts
- test/report-key.test.ts
docs/features/crash-reports.md +2 −1
| @@ -4,11 +4,12 @@ row: Crash reports | ||
| 4 | 4 | --- |
| 5 | 5 | # Crash reports |
| 6 | 6 | |
| 7 | 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/apps/<owner>/<name>/crash-reports`, no token for a public app (a tester cannot always open an issue, so the forge files it for them). A private app answers only a signed-in person who can write to it; everyone else gets the same 404 as for an app that does not exist, so the door never says which private apps there are. | |
| 7 | 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/apps/<owner>/<name>/crash-reports`, no token for a public app (a tester cannot always open an issue, so the forge files it for them). A private app answers only a signed-in person who can write to it, or a build that sends the app's report key (below); everyone else gets the same 404 as for an app that does not exist, so the door never says which private apps there are. | |
| 8 | 8 | |
| 9 | 9 | - **The body.** `{"v":1,"build":"50","kind":"crash","frames":["Forge +1445064","SwiftUI +7"],"breadcrumbs":["12:00:01 tab repos"]}`. `kind` is `crash`, `hang`, `cpu` or `disk` (default `crash`); up to 12 frames and 200 breadcrumbs, each cut at 200 characters. The Apple app's shape is accepted too: `{"v":1,"kind":"crash","app":"Forge","os":"iOS 19.1","device":"iPhone17,1","at":"2026-12-04T10:00:00Z","previousRun":true,"findings":[{"kind":"crash","frames":["Forge +1"]}]}` (build is then optional: a report without one is counted but never reopens a closed group, and a build is never invented for it; `app` and `at` are checked and then dropped, the forge stamping its own time; up to 8 findings, each its own group and issue, answered in `findings`; `os` and `device` go in the issue, 64 characters at most, nothing that names a person). Any other field is a 422 in words; over 64 KB is a 413. Nothing about the sender is kept. |
| 10 | 10 | - **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. |
| 11 | 11 | - **Reopening.** A group closed for an older build reopens when a report comes from a newer build than any seen. A report from an older build only adds to the count. |
| 12 | 12 | - **The queue.** Any open issue labelled `bug`, or titled "Diagnostics from forge ...", goes before feature work in the crew's queue (the title counts only when someone who can write to the repository wrote it); an order a person gave (first, ranked, limbs) still wins. The issue asks the builder to say what it found with file and line, to open a fix pull that closes it, and to open no guess when it finds no cause. |
| 13 | 13 | - **Where to see it.** The app's page lists its open crash groups with their issues and the pull that closes each. In the terminal, "Crash groups of a repository" in the palette lists the same rows (reports, builds, fix pull); the issues themselves are in the issues screen like any other. |
| 14 | - **The report key.** A private app's owner makes a key so TestFlight testers' builds can report without a session: in the app's settings (Report key), in the terminal ("Report key" in the palette: `m` makes or rotates, `x` revokes), or `POST` and `DELETE /api/v1/apps/<owner>/<name>/report-key`. The key is shown once and only its hash is kept; making another stops the old one. A build sends it as the `x-report-key` header. It opens this one door and nothing else, the limits below still hold, and a wrong, revoked or missing key is the same 404 as an app that does not exist. | |
| 14 | 15 | - **Limits.** 5 reports a minute from one address and 60 in all, each counted per app. A repository remembers 200 groups; past that the least recently seen closed group is forgotten (its issue stays) and no report is refused. |
docs/features/report-key.md added +11
| @@ -0,0 +1,11 @@ | ||
| 1 | --- | |
| 2 | topic: When something is wrong | |
| 3 | row: Report key | |
| 4 | --- | |
| 5 | # Report key | |
| 6 | ||
| 7 | A private app answers crash reports only from a signed-in writer, so a TestFlight tester's build gets the same 404 as for an app that does not exist. The app's owner (whoever administers it) makes a report key so the build can file reports anyway. | |
| 8 | ||
| 9 | - **Make or rotate.** In the app's settings, "Report key": Make a key. In the terminal, "Report key" in the palette: `m`. Through the API: `POST /api/v1/apps/<owner>/<name>/report-key` (rw token). The key is shown once and only its hash is kept; making another stops the old one. A public app needs none. | |
| 10 | - **Use.** The build sends it as the `x-report-key` header of `POST /api/v1/apps/<owner>/<name>/crash-reports` (see Crash reports). The key opens that one door and nothing else, and the same limits hold. | |
| 11 | - **Revoke.** "Revoke" in settings, `x` in the terminal, `DELETE` on the same API route. A revoked, wrong or missing key gets the identical 404 of an app that is not there. |
src/actions/report_keys.ts added +42
| @@ -0,0 +1,42 @@ | ||
| 1 | /** | |
| 2 | * Make, rotate and revoke a private app's report key (#18224), for the owner (whoever administers | |
| 3 | * the repository) and for no one else. The settings page, the terminal and the API call these and | |
| 4 | * say `REPORT_KEY_WORDS` alike. | |
| 5 | */ | |
| 6 | import type { App } from "../app.ts"; | |
| 7 | import { dropReportKey, makeReportKey, reportKeyMadeAt } from "../core/report_keys.ts"; | |
| 8 | import { canAdmin, type Repo } from "../core/repos.ts"; | |
| 9 | import type { User } from "../core/users.ts"; | |
| 10 | import { getAppByRepo } from "../core/apps.ts"; | |
| 11 | import { err, ok, type Result } from "../kernel/result.ts"; | |
| 12 | ||
| 13 | export const REPORT_KEY_WORDS = { | |
| 14 | made: "Report key made. Copy it now: it is never shown again. A build sends it as the x-report-key header; making another stops this one.", | |
| 15 | revoked: "Report key revoked. A build that still sends it is turned away.", | |
| 16 | none: "This app has no report key.", | |
| 17 | off: "A report key is for a private app, which only a signed-in writer may otherwise report to.", | |
| 18 | not_found: "No such app.", | |
| 19 | } as const; | |
| 20 | ||
| 21 | export type ReportKeyError = "not_found" | "off" | "none"; | |
| 22 | ||
| 23 | const gate = (app: App, user: User | undefined, repo: Repo): Result<void, ReportKeyError> => | |
| 24 | user === undefined || !canAdmin(app.db, repo, user) || getAppByRepo(app.db, repo.id) === undefined ? err("not_found") : ok(undefined); | |
| 25 | ||
| 26 | export function makeKey(app: App, user: User | undefined, repo: Repo): Result<string, ReportKeyError> { | |
| 27 | const g = gate(app, user, repo); | |
| 28 | if (!g.ok) return g; | |
| 29 | if (repo.visibility === "public") return err("off"); | |
| 30 | return ok(makeReportKey(app.db, repo.id)); | |
| 31 | } | |
| 32 | ||
| 33 | export function revokeKey(app: App, user: User | undefined, repo: Repo): Result<void, ReportKeyError> { | |
| 34 | const g = gate(app, user, repo); | |
| 35 | if (!g.ok) return g; | |
| 36 | return dropReportKey(app.db, repo.id) ? ok(undefined) : err("none"); | |
| 37 | } | |
| 38 | ||
| 39 | /** When the key was made, for the owner to see; undefined when none or not the owner's to see. */ | |
| 40 | export function keyState(app: App, user: User | undefined, repo: Repo): string | undefined { | |
| 41 | return gate(app, user, repo).ok ? reportKeyMadeAt(app.db, repo.id) : undefined; | |
| 42 | } |
src/contract/openapi/routes/deleteApiV1AppsOwnerNameReportKey.ts added +10
| @@ -0,0 +1,10 @@ | ||
| 1 | import type { Route } from "../shared.ts"; | |
| 2 | ||
| 3 | export const route: Route = { | |
| 4 | method: "delete", | |
| 5 | path: "/api/v1/apps/{owner}/{name}/report-key", | |
| 6 | summary: "Revoke a private app's report key (rw scope, the app's admin); 404 when there is none", | |
| 7 | auth: true, | |
| 8 | role: "member", | |
| 9 | refuses: [404], | |
| 10 | }; |
src/contract/openapi/routes/index.ts +2
| @@ -4,6 +4,7 @@ export { route as deleteApiV1AdminAccountsUsername } from "./deleteApiV1AdminAcc | ||
| 4 | 4 | export { route as deleteApiV1AdminInvitesId } from "./deleteApiV1AdminInvitesId.ts"; |
| 5 | 5 | export { route as deleteApiV1AdminRunnersId } from "./deleteApiV1AdminRunnersId.ts"; |
| 6 | 6 | export { route as deleteApiV1AgentsUsername } from "./deleteApiV1AgentsUsername.ts"; |
| 7 | export { route as deleteApiV1AppsOwnerNameReportKey } from "./deleteApiV1AppsOwnerNameReportKey.ts"; | |
| 7 | 8 | export { route as deleteApiV1BoardsSlug } from "./deleteApiV1BoardsSlug.ts"; |
| 8 | 9 | export { route as deleteApiV1CodesId } from "./deleteApiV1CodesId.ts"; |
| 9 | 10 | export { route as deleteApiV1ConnectionsId } from "./deleteApiV1ConnectionsId.ts"; |
| @@ -370,6 +371,7 @@ export { route as postApiV1AdminRunnersIdRevoke } from "./postApiV1AdminRunnersI | ||
| 370 | 371 | export { route as postApiV1Agents } from "./postApiV1Agents.ts"; |
| 371 | 372 | export { route as postApiV1Apps } from "./postApiV1Apps.ts"; |
| 372 | 373 | export { route as postApiV1AppsOwnerNameCrashReports } from "./postApiV1AppsOwnerNameCrashReports.ts"; |
| 374 | export { route as postApiV1AppsOwnerNameReportKey } from "./postApiV1AppsOwnerNameReportKey.ts"; | |
| 373 | 375 | export { route as postApiV1AsksAdmin } from "./postApiV1AsksAdmin.ts"; |
| 374 | 376 | export { route as postApiV1AsksBulk } from "./postApiV1AsksBulk.ts"; |
| 375 | 377 | export { route as postApiV1AsksBulkIdUndo } from "./postApiV1AsksBulkIdUndo.ts"; |
src/contract/openapi/routes/postApiV1AppsOwnerNameReportKey.ts added +13
| @@ -0,0 +1,13 @@ | ||
| 1 | import type { Route } from "../shared.ts"; | |
| 2 | ||
| 3 | export const route: Route = { | |
| 4 | method: "post", | |
| 5 | path: "/api/v1/apps/{owner}/{name}/report-key", | |
| 6 | summary: "Make or rotate a private app's report key; shown once (rw scope, the app's admin)", | |
| 7 | detail: "The key lets a build file crash reports to this private app without a session: send it as the `x-report-key` header of `POST /api/v1/apps/{owner}/{name}/crash-reports`. It opens that one door. Only its hash is kept; making another stops the old one. Answers 201 `{key, message}`.", | |
| 8 | auth: true, | |
| 9 | role: "member", | |
| 10 | status: 201, | |
| 11 | refusals: { off: "the app is public: it needs no key (422)", not_found: "no such app, or you do not administer it (404)" }, | |
| 12 | refuses: [404], | |
| 13 | }; |
src/core/migration-pins/20261204120000-report-key.pin added +1
| @@ -0,0 +1,1 @@ | ||
| 1 | c3d6f52b7b716e3a7897484308334f2d33b431c1ecaed7fe015991afb015ef97 |
src/core/migrations/20261204120000-report-key.ts added +10
| @@ -0,0 +1,10 @@ | ||
| 1 | // A private app's report key (#18224): one row per app, the hash of a secret the owner hands to a | |
| 2 | // build so it can file crash reports without a session. It opens that one door and nothing else. | |
| 3 | // Gone with the repository. Additive. | |
| 4 | export default ` | |
| 5 | CREATE TABLE report_key ( | |
| 6 | repo_id TEXT PRIMARY KEY REFERENCES repo(id) ON DELETE CASCADE, | |
| 7 | key_hash TEXT NOT NULL UNIQUE, | |
| 8 | created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ','now')) | |
| 9 | ); | |
| 10 | `; |
src/core/report_keys.ts added +37
| @@ -0,0 +1,37 @@ | ||
| 1 | /** | |
| 2 | * A report key (#18224): the secret a private app's owner puts in a build so its crash reports | |
| 3 | * (`http/api/crash_reports.ts`) are taken without a session. Made like a token (`core/tokens.ts`): | |
| 4 | * shown once, only its hash kept, revoked by deleting it, rotated by making another. It is not a | |
| 5 | * token row because a token acts as a person across the whole API; this opens one door. | |
| 6 | */ | |
| 7 | import type { Database } from "bun:sqlite"; | |
| 8 | import { sha256 } from "../kernel/hash.ts"; | |
| 9 | import { secret } from "../kernel/id.ts"; | |
| 10 | ||
| 11 | export const REPORT_KEY_PREFIX = "rk_"; | |
| 12 | ||
| 13 | /** Makes the app's key, replacing any it had (rotation). The secret is returned this once. */ | |
| 14 | export function makeReportKey(db: Database, repoId: string): string { | |
| 15 | const key = `${REPORT_KEY_PREFIX}${secret()}`; | |
| 16 | db.query( | |
| 17 | `INSERT INTO report_key (repo_id, key_hash) VALUES (?1, ?2) | |
| 18 | ON CONFLICT (repo_id) DO UPDATE SET key_hash = excluded.key_hash, created_at = strftime('%Y-%m-%dT%H:%M:%fZ','now')`, | |
| 19 | ).run(repoId, sha256(key)); | |
| 20 | return key; | |
| 21 | } | |
| 22 | ||
| 23 | /** Whether a key was deleted. */ | |
| 24 | export function dropReportKey(db: Database, repoId: string): boolean { | |
| 25 | return db.query("DELETE FROM report_key WHERE repo_id = ?1").run(repoId).changes > 0; | |
| 26 | } | |
| 27 | ||
| 28 | /** When the app's key was made, or undefined when it has none. */ | |
| 29 | export function reportKeyMadeAt(db: Database, repoId: string): string | undefined { | |
| 30 | return db.query<{ created_at: string }, [string]>("SELECT created_at FROM report_key WHERE repo_id = ?1").get(repoId)?.created_at; | |
| 31 | } | |
| 32 | ||
| 33 | /** Whether `presented` is this app's current key. */ | |
| 34 | export function reportKeyOpens(db: Database, repoId: string, presented: string): boolean { | |
| 35 | if (presented.length === 0 || presented.length > 200) return false; | |
| 36 | return db.query("SELECT 1 FROM report_key WHERE repo_id = ?1 AND key_hash = ?2").get(repoId, sha256(presented)) !== null; | |
| 37 | } |
src/http/api.ts +2 −1
| @@ -363,7 +363,7 @@ import { takeOrder } from "../crew/take_order.ts"; | ||
| 363 | 363 | import { forgeCarries } from "../origins/words.ts"; |
| 364 | 364 | import { forgeOriginRoutes } from "./api/forge_origin.ts"; |
| 365 | 365 | import { appleTelemetryRoutes } from "./api/telemetry_apple.ts"; |
| 366 | import { crashReportRoutes } from "./api/crash_reports.ts"; | |
| 366 | import { crashReportRoutes, reportKeyRoutes } from "./api/crash_reports.ts"; | |
| 367 | 367 | import { pushDeviceRoutes } from "./api/push_devices.ts"; |
| 368 | 368 | import { userRoutes } from "./api/user.ts"; |
| 369 | 369 | import { appRoutes } from "./api/app.ts"; |
| @@ -1291,6 +1291,7 @@ export function apiRoutes(router: Router<Handler>, isPage: (path: string) => boo | ||
| 1291 | 1291 | pushDeviceRoutes(router); |
| 1292 | 1292 | appleTelemetryRoutes(router); |
| 1293 | 1293 | crashReportRoutes(router); |
| 1294 | reportKeyRoutes(router); | |
| 1294 | 1295 | forgeOriginRoutes(router); |
| 1295 | 1296 | adminRoutes(router); |
| 1296 | 1297 | runnerRoutes(router); |
src/http/api/crash_reports.ts +38 −3
| @@ -2,7 +2,8 @@ | ||
| 2 | 2 | * `POST /api/v1/apps/:owner/:name/crash-reports` (#18081): an app, or its tester, reports a crash |
| 3 | 3 | * and it becomes work: one standard `bug` issue on the app's repository per group of reports |
| 4 | 4 | * (`actions/crash_reports.ts`). Open to anyone, signed in or not, for a public repository. A |
| 5 | * private one answers only a signed-in person who can write to it, and answers everyone else 404 | |
| 5 | * private one answers only a signed-in person who can write to it, or a build that sends its | |
| 6 | * report key (`x-report-key`, #18224), and answers everyone else 404 | |
| 6 | 7 | * in the very words of an app that does not exist, so the door never says which private apps |
| 7 | 8 | * there are. Whoever sends it is never read. The address only counts a minute's tries (of |
| 8 | 9 | * reports with a valid shape) in memory, per app, and is dropped with the request. Strict shape, |
| @@ -11,14 +12,16 @@ | ||
| 11 | 12 | |
| 12 | 13 | import type { App } from "../../app.ts"; |
| 13 | 14 | import { reportCrashes } from "../../actions/crash_reports.ts"; |
| 15 | import { makeKey, REPORT_KEY_WORDS, revokeKey } from "../../actions/report_keys.ts"; | |
| 14 | 16 | import { getAppByRepo } from "../../core/apps.ts"; |
| 15 | 17 | import { parseReport, REPORT_ERRORS } from "../../core/crash_groups.ts"; |
| 18 | import { reportKeyOpens } from "../../core/report_keys.ts"; | |
| 16 | 19 | import { RateLimiter } from "../../core/ratelimit.ts"; |
| 17 | 20 | import { canWrite, getRepo } from "../../core/repos.ts"; |
| 18 | 21 | import type { Handler } from "../context.ts"; |
| 19 | 22 | import { BODY_CAPS, RATE_CAPS, readBounded } from "../limits.ts"; |
| 20 | 23 | import { clientBucket } from "../peer.ts"; |
| 21 | import { apiError, json, tryAgain } from "../respond.ts"; | |
| 24 | import { apiError, json, requireScope, tryAgain } from "../respond.ts"; | |
| 22 | 25 | import type { Router } from "../router.ts"; |
| 23 | 26 | |
| 24 | 27 | const limiter = (perMinute: number) => new RateLimiter({ perMinute, adminPerMinute: perMinute, enabled: true }); |
| @@ -28,7 +31,14 @@ export function crashReportRoutes(router: Router<Handler>): void { | ||
| 28 | 31 | router.add("POST", "/api/v1/apps/:owner/:name/crash-reports", async (ctx) => { |
| 29 | 32 | const { request, app } = ctx; |
| 30 | 33 | const repo = getRepo(app.db, ctx.params.owner!, ctx.params.name!); |
| 31 | const open = repo !== undefined && (repo.visibility === "public" || (ctx.auth.user !== undefined && canWrite(app.db, repo, ctx.auth.user))); | |
| 34 | // A private app also takes a report that carries its report key (`core/report_keys.ts`): that | |
| 35 | // opens this door and no other, and a wrong or missing one is the 404 of an app that is not | |
| 36 | // there. | |
| 37 | const open = | |
| 38 | repo !== undefined && | |
| 39 | (repo.visibility === "public" || | |
| 40 | (ctx.auth.user !== undefined && canWrite(app.db, repo, ctx.auth.user)) || | |
| 41 | reportKeyOpens(app.db, repo.id, request.headers.get("x-report-key") ?? "")); | |
| 32 | 42 | if (!open || getAppByRepo(app.db, repo.id) === undefined) return apiError("not_found", 404, "no app by that name"); |
| 33 | 43 | const cap = BODY_CAPS.crashReport.bytes; |
| 34 | 44 | const bytes = await readBounded(request, cap); |
| @@ -59,3 +69,28 @@ export function crashReportRoutes(router: Router<Handler>): void { | ||
| 59 | 69 | return json({ ...row(one!), ...(read.value.findings === undefined ? {} : { findings: filed.value.map(row) }) }, 202, { "cache-control": "no-store" }); |
| 60 | 70 | }); |
| 61 | 71 | } |
| 72 | ||
| 73 | /** | |
| 74 | * `POST` and `DELETE /api/v1/apps/:owner/:name/report-key` (#18224): the owner of a private app | |
| 75 | * makes (or rotates) its report key, shown once, and revokes it; the same words as the settings | |
| 76 | * page. | |
| 77 | */ | |
| 78 | export function reportKeyRoutes(router: Router<Handler>): void { | |
| 79 | const target = (ctx: Parameters<Handler>[0]) => getRepo(ctx.app.db, ctx.params.owner!, ctx.params.name!); | |
| 80 | router.add("POST", "/api/v1/apps/:owner/:name/report-key", (ctx) => { | |
| 81 | const denied = requireScope(ctx, "rw"); | |
| 82 | if (denied) return denied; | |
| 83 | const repo = target(ctx); | |
| 84 | const made = repo === undefined ? undefined : makeKey(ctx.app, ctx.auth.user, repo); | |
| 85 | if (made === undefined || !made.ok) return apiError(made?.error ?? "not_found", made?.error === "off" ? 422 : 404, REPORT_KEY_WORDS[made?.error ?? "not_found"]); | |
| 86 | return json({ key: made.value, message: REPORT_KEY_WORDS.made }, 201, { "cache-control": "no-store" }); | |
| 87 | }); | |
| 88 | router.add("DELETE", "/api/v1/apps/:owner/:name/report-key", (ctx) => { | |
| 89 | const denied = requireScope(ctx, "rw"); | |
| 90 | if (denied) return denied; | |
| 91 | const repo = target(ctx); | |
| 92 | const gone = repo === undefined ? undefined : revokeKey(ctx.app, ctx.auth.user, repo); | |
| 93 | if (gone === undefined || !gone.ok) return apiError(gone?.error ?? "not_found", 404, REPORT_KEY_WORDS[gone?.error ?? "not_found"]); | |
| 94 | return json({ message: REPORT_KEY_WORDS.revoked }, 200, { "cache-control": "no-store" }); | |
| 95 | }); | |
| 96 | } |
src/http/lazy_lists.ts +4
| @@ -1063,6 +1063,8 @@ export const REPO_SETTINGS_ROUTES = [ | ||
| 1063 | 1063 | ["GET", "/:owner/:name/settings/app/flags/:flag"], |
| 1064 | 1064 | ["POST", "/:owner/:name/settings/app/secrets/grant"], |
| 1065 | 1065 | ["POST", "/:owner/:name/settings/app/secrets/revoke"], |
| 1066 | ["POST", "/:owner/:name/settings/app/report-key"], | |
| 1067 | ["POST", "/:owner/:name/settings/app/report-key/revoke"], | |
| 1066 | 1068 | ["POST", "/:owner/:name/settings/app/env/:var/delete"], |
| 1067 | 1069 | ["POST", "/:owner/:name/settings/app/domains"], |
| 1068 | 1070 | ["POST", "/:owner/:name/settings/app/domains/:host/check"], |
| @@ -1267,6 +1269,8 @@ export const API_ROUTES = [ | ||
| 1267 | 1269 | ["DELETE", "/api/v1/user/devices/:id"], |
| 1268 | 1270 | ["POST", "/api/v1/telemetry/apple"], |
| 1269 | 1271 | ["POST", "/api/v1/apps/:owner/:name/crash-reports"], |
| 1272 | ["POST", "/api/v1/apps/:owner/:name/report-key"], | |
| 1273 | ["DELETE", "/api/v1/apps/:owner/:name/report-key"], | |
| 1270 | 1274 | ["GET", "/api/v1/version"], |
| 1271 | 1275 | ["GET", "/api/v1/user/repos"], |
| 1272 | 1276 | ["GET", "/api/v1/admin/pinned"], |
src/http/routes/repo_settings.ts +3
| @@ -6,6 +6,7 @@ import { chainPage, severSubmit, modeSubmit, takeSubmit, proposeParentSubmit, ch | ||
| 6 | 6 | import { followingSubmit } from "../../web/pages/following.ts"; |
| 7 | 7 | import { appFlagEditPage } from "../../web/pages/app_flag_edit.ts"; |
| 8 | 8 | import { appFlagSet } from "../../web/pages/app_flags.ts"; |
| 9 | import { reportKeyMake, reportKeyRevoke } from "../../web/pages/report_key.ts"; | |
| 9 | 10 | import { appSecretGrant, appSecretRevoke } from "../../web/pages/app_secrets.ts"; |
| 10 | 11 | import { crewDevicesPage, crewDeviceAdd, crewDeviceGrant, crewDeviceRemove } from "../../web/pages/crew_devices.ts"; |
| 11 | 12 | import { crewSettingsPage, crewAdd, crewSubscriptionsSave, crewRemove, crewRoleUpdate, pillarUpdate } from "../../web/pages/crew-settings.ts"; |
| @@ -86,6 +87,8 @@ export function repoSettingsRoutes(router: Router<Handler>): void { | ||
| 86 | 87 | router.add("GET", "/:owner/:name/settings/app/flags/:flag", appFlagEditPage); |
| 87 | 88 | router.add("POST", "/:owner/:name/settings/app/secrets/grant", appSecretGrant); |
| 88 | 89 | router.add("POST", "/:owner/:name/settings/app/secrets/revoke", appSecretRevoke); |
| 90 | router.add("POST", "/:owner/:name/settings/app/report-key", reportKeyMake); | |
| 91 | router.add("POST", "/:owner/:name/settings/app/report-key/revoke", reportKeyRevoke); | |
| 89 | 92 | router.add( |
| 90 | 93 | "POST", |
| 91 | 94 | "/:owner/:name/settings/app/env/:var/delete", |
src/tui/screens/reportkey.ts added +68
| @@ -0,0 +1,68 @@ | ||
| 1 | /** | |
| 2 | * A private app's report key in the terminal (#18224): what the settings page says. `m` makes or | |
| 3 | * rotates it (the key is shown once, here, until the screen closes), `x` revokes it; the same | |
| 4 | * actions and words as the page and the API. | |
| 5 | */ | |
| 6 | ||
| 7 | import { keyState, makeKey, REPORT_KEY_WORDS, revokeKey } from "../../actions/report_keys.ts"; | |
| 8 | import { getAppByRepo } from "../../core/apps.ts"; | |
| 9 | import { canAdmin, type Repo } from "../../core/repos.ts"; | |
| 10 | import type { Ctx, Screen } from "../app/app.ts"; | |
| 11 | import { binding } from "../app/keys.ts"; | |
| 12 | import type { Command } from "../app/palette.ts"; | |
| 13 | import type { Session } from "../session.ts"; | |
| 14 | import { style } from "../term/style.ts"; | |
| 15 | import { wrap } from "../term/width.ts"; | |
| 16 | ||
| 17 | /** The palette's "Report key", for an admin of a private repository that is an app. */ | |
| 18 | export function reportKeyCommands(f: Session, ctx: Ctx, repo: Repo | undefined): Command[] { | |
| 19 | if (repo === undefined || f.user === undefined || repo.visibility !== "private") return []; | |
| 20 | if (getAppByRepo(f.app.db, repo.id) === undefined || !canAdmin(f.app.db, repo, f.user)) return []; | |
| 21 | return [ | |
| 22 | { | |
| 23 | id: "report-key", | |
| 24 | title: `${repo.ownerName}/${repo.name} — Report key`, | |
| 25 | hint: "lets a build file crash reports without signing in", | |
| 26 | run: () => ctx.push(reportKeyScreen(f, ctx, repo)), | |
| 27 | }, | |
| 28 | ]; | |
| 29 | } | |
| 30 | ||
| 31 | export function reportKeyScreen(f: Session, ctx: Ctx, repo: Repo): Screen { | |
| 32 | let note = ""; | |
| 33 | let shown = ""; | |
| 34 | return { | |
| 35 | title: "Report key", | |
| 36 | keys: [binding(["m"], "make or rotate"), binding(["x"], "revoke")], | |
| 37 | render(area, s) { | |
| 38 | const c = ctx.theme.c; | |
| 39 | const madeAt = keyState(f.app, f.user, repo); | |
| 40 | const lines = [ | |
| 41 | "A build sends the key as the x-report-key header to file crash reports to this private app without signing in. It can do nothing else.", | |
| 42 | madeAt === undefined ? REPORT_KEY_WORDS.none : `Made ${madeAt}. Only its hash is kept.`, | |
| 43 | ...(note === "" ? [] : [note]), | |
| 44 | ...(shown === "" ? [] : [shown]), | |
| 45 | ]; | |
| 46 | let y = area.y; | |
| 47 | for (const line of lines) for (const w of wrap(line, Math.max(1, area.w))) if (y < area.y + area.h) s.text(area.x, y++, w, style(c.mutedFg, c.bg), area.x + area.w); | |
| 48 | }, | |
| 49 | key(e) { | |
| 50 | if (e.ctrl || e.alt) return false; | |
| 51 | if (e.key === "m") { | |
| 52 | const made = makeKey(f.app, f.user, repo); | |
| 53 | note = made.ok ? REPORT_KEY_WORDS.made : REPORT_KEY_WORDS[made.error]; | |
| 54 | shown = made.ok ? made.value : ""; | |
| 55 | ctx.invalidate(); | |
| 56 | return true; | |
| 57 | } | |
| 58 | if (e.key === "x") { | |
| 59 | const gone = revokeKey(f.app, f.user, repo); | |
| 60 | note = gone.ok ? REPORT_KEY_WORDS.revoked : REPORT_KEY_WORDS[gone.error]; | |
| 61 | shown = ""; | |
| 62 | ctx.invalidate(); | |
| 63 | return true; | |
| 64 | } | |
| 65 | return false; | |
| 66 | }, | |
| 67 | }; | |
| 68 | } |
src/tui/screens/workspace.ts +2
| @@ -154,6 +154,7 @@ import { iterationsCommands } from "./iterations.ts"; | ||
| 154 | 154 | import { labelsCommands } from "./labels.ts"; |
| 155 | 155 | import { appEnvCommands } from "./appenv.ts"; |
| 156 | 156 | import { appSecretCommands } from "./appsecrets.ts"; |
| 157 | import { reportKeyCommands } from "./reportkey.ts"; | |
| 157 | 158 | import { appFlagCommands } from "./appflags.ts"; |
| 158 | 159 | import { appRegistryCommands } from "./appregistry.ts"; |
| 159 | 160 | import { changesCommands } from "./app_changes.ts"; |
| @@ -1405,6 +1406,7 @@ export function workspaceScreen( | ||
| 1405 | 1406 | ...displayCommands(f, ctx, repo), |
| 1406 | 1407 | ...appEnvCommands(f, ctx, repo), |
| 1407 | 1408 | ...appSecretCommands(f, ctx, repo), |
| 1409 | ...reportKeyCommands(f, ctx, repo), | |
| 1408 | 1410 | ...appFlagCommands(f, ctx, repo), |
| 1409 | 1411 | ...appRegistryCommands(f, ctx, repo), |
| 1410 | 1412 | ...changesCommands(f, ctx, repo), |
src/web/pages/report_key.ts added +46
| @@ -0,0 +1,46 @@ | ||
| 1 | /** | |
| 2 | * A private app's report key, as its settings say it (#18224): make or rotate it (shown once, in a | |
| 3 | * one-shot flash), revoke it. The words are `actions/report_keys.ts`'s, the terminal's alike. | |
| 4 | */ | |
| 5 | ||
| 6 | import { makeKey, REPORT_KEY_WORDS, revokeKey } from "../../actions/report_keys.ts"; | |
| 7 | import type { Ctx } from "../../http/context.ts"; | |
| 8 | import { adminForm } from "../admin_form.ts"; | |
| 9 | import { repoBase } from "../chrome.ts"; | |
| 10 | import { html, when, type Html } from "../html.ts"; | |
| 11 | import { flashTo } from "../layout.ts"; | |
| 12 | import { btn } from "../ui.ts"; | |
| 13 | ||
| 14 | /** The settings block; `madeAt` is when the key was made, undefined when there is none. */ | |
| 15 | export function reportKeyView(base: string, madeAt: string | undefined): Html { | |
| 16 | return html`<section class="settings-block" id="report-key"> | |
| 17 | <h2>Report key</h2> | |
| 18 | <p class="muted small">A build sends this key as <span class="mono">x-report-key</span> to file crash reports to this private app without signing in. It can do nothing else.</p> | |
| 19 | <p class="muted small">${madeAt === undefined ? REPORT_KEY_WORDS.none : html`Made ${madeAt}. Only its hash is kept.`}</p> | |
| 20 | <form class="row-actions" method="post" action="${base}/settings/app/report-key"> | |
| 21 | ${btn(madeAt === undefined ? "Make a key" : "Rotate", { size: "sm", icon: "plus", label: madeAt === undefined ? "Make a report key" : "Rotate the report key" })} | |
| 22 | </form> | |
| 23 | ${when(madeAt !== undefined, () => html`<form class="row-actions" method="post" action="${base}/settings/app/report-key/revoke"> | |
| 24 | ${btn("Revoke", { size: "sm", icon: "x", label: "Revoke the report key" })} | |
| 25 | </form>`)} | |
| 26 | </section>`; | |
| 27 | } | |
| 28 | ||
| 29 | /** POST /:owner/:name/settings/app/report-key */ | |
| 30 | export async function reportKeyMake(ctx: Ctx): Promise<Response> { | |
| 31 | const gate = await adminForm(ctx); | |
| 32 | if (gate instanceof Response) return gate; | |
| 33 | const { repo } = gate; | |
| 34 | const made = makeKey(ctx.app, ctx.auth.user, repo); | |
| 35 | const back = `${repoBase(repo)}/settings/app#report-key`; | |
| 36 | return flashTo(ctx, back, made.ok ? { message: REPORT_KEY_WORDS.made, code: made.value } : { tone: "danger", message: REPORT_KEY_WORDS[made.error] }); | |
| 37 | } | |
| 38 | ||
| 39 | /** POST /:owner/:name/settings/app/report-key/revoke */ | |
| 40 | export async function reportKeyRevoke(ctx: Ctx): Promise<Response> { | |
| 41 | const gate = await adminForm(ctx); | |
| 42 | if (gate instanceof Response) return gate; | |
| 43 | const { repo } = gate; | |
| 44 | const gone = revokeKey(ctx.app, ctx.auth.user, repo); | |
| 45 | return flashTo(ctx, `${repoBase(repo)}/settings/app#report-key`, gone.ok ? { message: REPORT_KEY_WORDS.revoked } : { tone: "danger", message: REPORT_KEY_WORDS[gone.error] }); | |
| 46 | } |
src/web/pages/settings.ts +3
| @@ -104,6 +104,8 @@ import { settingsLead, teamJs } from "../studio/crew.ts"; | ||
| 104 | 104 | import { pageTitle } from "../studio/nouns.ts"; |
| 105 | 105 | import { appHref } from "./apps.ts"; |
| 106 | 106 | import { appSecretsView } from "./app_secrets.ts"; |
| 107 | import { reportKeyView } from "./report_key.ts"; | |
| 108 | import { keyState } from "../../actions/report_keys.ts"; | |
| 107 | 109 | import { appFlagsView } from "./app_flags.ts"; |
| 108 | 110 | import { servedFlags } from "../../actions/app_flags.ts"; |
| 109 | 111 | import type { AppFlagRow } from "../../core/app_flags.ts"; |
| @@ -907,6 +909,7 @@ function appSettingsView( | ||
| 907 | 909 | </div> |
| 908 | 910 | ${when(flags !== undefined, () => html`<div class="mt">${appFlagsView(base, flags!)}</div>`)} |
| 909 | 911 | ${when(needs !== undefined, () => html`<div class="mt">${appSecretsView(base, needs!.needs, ctx.auth.user?.isAdmin === true, needs!.grantable)}</div>`)} |
| 912 | ${when(repo.visibility === "private", () => html`<div class="mt">${reportKeyView(base, keyState(ctx.app, ctx.auth.user, repo))}</div>`)} | |
| 910 | 913 | <div class="mt"> |
| 911 | 914 | ${appDomainsView(base, domains.ok ? domains.value : undefined)} |
| 912 | 915 | </div> |
test/report-key.test.ts added +78
| @@ -0,0 +1,78 @@ | ||
| 1 | /** A private app's report key (#18224): files a report without a session, and nothing else. */ | |
| 2 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 3 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import { createApp, type App } from "../src/app.ts"; | |
| 7 | import { createApp as insertApp } from "../src/core/apps.ts"; | |
| 8 | import { groupCount } from "../src/core/crash_groups.ts"; | |
| 9 | import { createRepo, type Repo } from "../src/core/repos.ts"; | |
| 10 | import { createUser } from "../src/core/users.ts"; | |
| 11 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 12 | import { tokenFor } from "./helpers/tokens.ts"; | |
| 13 | ||
| 14 | const report = { v: 1, build: "50", frames: ["Vault +1", "UIKit +2"] }; | |
| 15 | ||
| 16 | describe("report key", () => { | |
| 17 | let dir = ""; | |
| 18 | let app: App; | |
| 19 | let servers: Servers; | |
| 20 | let vault: Repo; | |
| 21 | const url = (p: string) => `${servers.url}${p}`; | |
| 22 | const file = (key?: string, path = "/api/v1/apps/bob/vault/crash-reports") => | |
| 23 | fetch(url(path), { method: "POST", body: JSON.stringify(report), headers: key === undefined ? {} : { "x-report-key": key } }); | |
| 24 | const keyCall = (method: string, user: string) => | |
| 25 | fetch(url("/api/v1/apps/bob/vault/report-key"), { method, headers: { authorization: `Bearer ${tokenFor(app, user, "rw")}` } }); | |
| 26 | ||
| 27 | beforeAll(async () => { | |
| 28 | dir = await mkdtemp(join(tmpdir(), "forge-rk-")); | |
| 29 | app = createApp({ dataDir: dir, port: 0 }); | |
| 30 | servers = await serve(app); | |
| 31 | for (const u of ["bob", "eve"]) await createUser(app.db, { username: u, email: `${u}@forge.local`, password: "a-good-passphrase", origin: "seed", trusted: true }); | |
| 32 | const bob = app.db.query<{ id: string }, []>("SELECT id FROM user WHERE username = 'bob'").get()!; | |
| 33 | const made = await createRepo(app.db, app.reposRoot, { ownerId: bob.id, name: "vault", visibility: "private" }); | |
| 34 | if (!made.ok) throw new Error(made.error); | |
| 35 | vault = made.value; | |
| 36 | const a = insertApp(app.db, { repoId: vault.id, slug: "vault", access: "private" }); | |
| 37 | if (!a.ok) throw new Error(a.error); | |
| 38 | }); | |
| 39 | afterAll(async () => { | |
| 40 | servers.stop(); | |
| 41 | await rm(dir, { recursive: true, force: true }); | |
| 42 | }); | |
| 43 | ||
| 44 | test("only the owner makes a key; it files a report, is refused after revoke, and a wrong key is the 404 of a missing app", async () => { | |
| 45 | expect((await keyCall("POST", "eve")).status).toBe(404); | |
| 46 | const made = await keyCall("POST", "bob"); | |
| 47 | expect(made.status).toBe(201); | |
| 48 | const { key } = (await made.json()) as { key: string }; | |
| 49 | const hash = app.db.query<{ key_hash: string }, []>("SELECT key_hash FROM report_key").get()!.key_hash; | |
| 50 | expect(hash).not.toContain(key); | |
| 51 | ||
| 52 | const missingBody = await (await file(undefined, "/api/v1/apps/bob/nothing/crash-reports")).text(); | |
| 53 | const wrong = await file("rk_wrong"); | |
| 54 | expect(wrong.status).toBe(404); | |
| 55 | expect(await wrong.text()).toBe(missingBody); | |
| 56 | expect((await file()).status).toBe(404); | |
| 57 | expect(groupCount(app.db, vault.id)).toBe(0); | |
| 58 | ||
| 59 | expect((await file(key)).status).toBe(202); | |
| 60 | expect(groupCount(app.db, vault.id)).toBe(1); | |
| 61 | // the key does nothing else | |
| 62 | const other = await fetch(url("/api/v1/apps/bob/vault/report-key"), { method: "POST", headers: { "x-report-key": key } }); | |
| 63 | expect(other.status).toBe(401); | |
| 64 | expect((await fetch(url("/bob/vault"), { headers: { "x-report-key": key } })).status).toBe(404); | |
| 65 | ||
| 66 | // rotating stops the old key | |
| 67 | const next = ((await (await keyCall("POST", "bob")).json()) as { key: string }).key; | |
| 68 | expect((await file(key)).status).toBe(404); | |
| 69 | expect((await file(next)).status).toBe(202); | |
| 70 | ||
| 71 | expect((await keyCall("DELETE", "eve")).status).toBe(404); | |
| 72 | expect((await keyCall("DELETE", "bob")).status).toBe(200); | |
| 73 | const after = await file(next); | |
| 74 | expect(after.status).toBe(404); | |
| 75 | expect(await after.text()).toBe(missingBody); | |
| 76 | expect((await keyCall("DELETE", "bob")).status).toBe(404); | |
| 77 | }); | |
| 78 | }); |
new above
offline