Public: anyone can read this; the crew can change it.
The API's SSH keys list names its Server-Timing phases: session, keys, render #4956
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/4942-the-api-s-ssh-keys-list-names-its-server → main 1 commit fork
2 files
changed +65
−5
src/http/api.ts +7 −5
| @@ -1938,11 +1938,13 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 1938 | 1938 | |
| 1939 | 1939 | // SSH keys — what the SSH listener authenticates against. Public keys are |
| 1940 | 1940 | // public, so the list returns them whole. |
| 1941 | router.add("GET", "/api/v1/user/keys", (ctx) => | |
| 1942 | ctx.auth.user | |
| 1943 | ? json(listKeys(ctx.app.db, ctx.auth.user.id).map(publicKey)) | |
| 1944 | : unauthorized(), | |
| 1945 | ); | |
| 1941 | router.add("GET", "/api/v1/user/keys", (ctx) => { | |
| 1942 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 1943 | const user = ctx.auth.user; | |
| 1944 | if (!user) return unauthorized(); | |
| 1945 | const keys = timePhase(ctx.request, "keys", () => listKeys(ctx.app.db, user.id)); | |
| 1946 | return timePhase(ctx.request, "render", () => json(keys.map(publicKey))); | |
| 1947 | }); | |
| 1946 | 1948 | router.add("POST", "/api/v1/user/keys", async (ctx) => { |
| 1947 | 1949 | const denied = requireScope(ctx, "rw"); |
| 1948 | 1950 | if (denied) return denied; |
test/api-keys-timing.test.ts added +58
| @@ -0,0 +1,58 @@ | ||
| 1 | /** The API SSH keys list names its Server-Timing phases (#4942). */ | |
| 2 | import { afterAll, beforeAll, 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 { createSession } from "../src/core/sessions.ts"; | |
| 8 | import { createUser } from "../src/core/users.ts"; | |
| 9 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 10 | import { quietLogs } from "./helpers/globals.ts"; | |
| 11 | ||
| 12 | quietLogs(); | |
| 13 | ||
| 14 | let dataDir: string; | |
| 15 | let app: App; | |
| 16 | let servers: Servers; | |
| 17 | let cookie = ""; | |
| 18 | ||
| 19 | const cells = process.env["FORGE_CELLS"]; | |
| 20 | ||
| 21 | beforeAll(async () => { | |
| 22 | process.env["FORGE_CELLS"] = "off"; | |
| 23 | dataDir = await mkdtemp(join(tmpdir(), "forge-api-keys-timing-")); | |
| 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(String(made.error)); | |
| 35 | const alice = made.value; | |
| 36 | cookie = `forge_session=${createSession(app.db, alice.id).token}`; | |
| 37 | }); | |
| 38 | afterAll(async () => { | |
| 39 | await servers.stop(); | |
| 40 | app.close(); | |
| 41 | if (cells === undefined) delete process.env["FORGE_CELLS"]; | |
| 42 | else process.env["FORGE_CELLS"] = cells; | |
| 43 | await rm(dataDir, { recursive: true, force: true }); | |
| 44 | }); | |
| 45 | ||
| 46 | async function phases(path: string): Promise<{ status: number; names: string[]; body: string }> { | |
| 47 | const res = await fetch(`${servers.url}${path}`, { headers: { cookie } }); | |
| 48 | const body = await res.text(); | |
| 49 | const names = (res.headers.get("server-timing") ?? "").split(", ").map((p) => p.split(";")[0]!); | |
| 50 | return { status: res.status, names: names.slice(names.indexOf("app") + 1), body }; | |
| 51 | } | |
| 52 | ||
| 53 | test("the keys list names session, keys, render in order", async () => { | |
| 54 | const got = await phases("/api/v1/user/keys"); | |
| 55 | expect(got.status).toBe(200); | |
| 56 | expect(got.names).toEqual(["session", "keys", "render"]); | |
| 57 | expect(Array.isArray(JSON.parse(got.body))).toBe(true); | |
| 58 | }); |
new above
offline