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

The API's egress grants read names its Server-Timing phases: session, grants, render #3951

Merged opened this

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

Merged

scout/forge:scout/3938-the-api-s-egress-grants-read-names-its-s → main 2 commits fork

  • Merged. scout/3938-the-api-s-egress-grants-read-names-its-s was merged into main as 297dab3.

Review

Sign in to review this change.
2 files changed +96 −2
src/http/api.ts +7 −2
@@ -5336,13 +5336,18 @@ export function apiRoutes(router: Router<Handler>): void {
53365336 // (#3433). Reading takes admin over the repository; changing takes a site
53375337 // admin. The destination is the exact `host:port` forge.json names.
53385338 router.add("GET", "/api/v1/repos/:owner/:name/egress-grants", async (ctx) => {
5339 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
53395340 const denied = requireScope(ctx, "admin");
53405341 if (denied) return denied;
53415342 const repo = loadRepo(ctx);
53425343 if (!repo) return notFound();
5343 const view = await egressView(ctx.app, ctx.auth.user, repo);
5344 const view = await timePhaseAsync(ctx.request, "grants", () =>
5345 egressView(ctx.app, ctx.auth.user, repo),
5346 );
53445347 if (!view.ok) return respondAccess(view.error, 422);
5345 return json({ items: view.value.rows, changes: view.value.changes, next_cursor: null });
5348 return timePhase(ctx.request, "render", () =>
5349 json({ items: view.value.rows, changes: view.value.changes, next_cursor: null }),
5350 );
53465351 });
53475352
53485353 router.add("POST", "/api/v1/repos/:owner/:name/egress-grants", async (ctx) => {
test/api-egress-grants-timing.test.ts added +89
@@ -0,0 +1,89 @@
1 /** The API's egress grants read names its Server-Timing phases (#3938). */
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 { commitToBranch, createRepo } from "../src/actions/repos.ts";
8 import { createSession } from "../src/core/sessions.ts";
9 import { createUser } from "../src/core/users.ts";
10 import { serve, type Servers } from "../src/http/server.ts";
11 import { quietLogs } from "./helpers/globals.ts";
12
13 quietLogs();
14
15 let dataDir: string;
16 let app: App;
17 let servers: Servers;
18 let cookie = "";
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-egress-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 const repo = await createRepo(app, alice, { name: "r", visibility: "public" });
38 if (!repo.ok) throw new Error(String(repo.error));
39 const done = await commitToBranch(app, alice, repo.value, {
40 message: "change",
41 files: [
42 {
43 path: "forge.json",
44 content: JSON.stringify({
45 app: { run: "bun run src/server.ts" },
46 egress: ["100.89.95.68:8101"],
47 }),
48 },
49 ],
50 });
51 if (!done.ok) throw new Error(JSON.stringify(done.error));
52 });
53
54 afterAll(async () => {
55 await servers.stop();
56 app.close();
57 if (cells === undefined) delete process.env["FORGE_CELLS"];
58 else process.env["FORGE_CELLS"] = cells;
59 await rm(dataDir, { recursive: true, force: true });
60 });
61
62 async function phases(
63 path: string,
64 headers: Record<string, string> = { cookie },
65 ): Promise<{ status: number; names: string[]; body: string }> {
66 const res = await fetch(`${servers.url}${path}`, { headers });
67 const body = await res.text();
68 const names = (res.headers.get("server-timing") ?? "").split(", ").map((p) => p.split(";")[0]!);
69 return { status: res.status, names: names.slice(names.indexOf("app") + 1), body };
70 }
71
72 test("the egress grants read names session, grants and render in order", async () => {
73 const got = await phases("/api/v1/repos/alice/r/egress-grants");
74 expect(got.status).toBe(200);
75 expect(got.names).toEqual(["session", "grants", "render"]);
76 expect(JSON.parse(got.body).next_cursor).toBeNull();
77 });
78
79 test("a missing repository still names session", async () => {
80 const got = await phases("/api/v1/repos/alice/nope/egress-grants");
81 expect(got.status).toBe(404);
82 expect(got.names).toEqual(["session"]);
83 });
84
85 test("an anonymous read is refused and names session", async () => {
86 const got = await phases("/api/v1/repos/alice/r/egress-grants", {});
87 expect(got.status).toBe(401);
88 expect(got.names).toEqual(["session"]);
89 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs