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

A scaffolded app passes webspec() on its first boot, proven by a test #3012

Parked opened this
Closed without merging

scout/forge:scout/3011-a-scaffolded-app-passes-webspec-on-its-f → main 1 commit fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
2 files changed +111 −15
src/apps/scaffold.ts +48 −15
@@ -2096,11 +2096,16 @@ export const routes: readonly Route[] = [
20962096 handle: (c) => {
20972097 const { notice, clear } = readNotice(c.request);
20982098 const notes = listNotes(c.db, c.who);
2099 return html(
2100 home({ who: c.who, notes: notes.ok ? notes.value : [], notice }),
2101 200,
2102 clear,
2103 );
2099 const page = home({ who: c.who, notes: notes.ok ? notes.value : [], notice });
2100 // The page is the same bytes until a note changes: a revisit costs a 304.
2101 const etag = \`"\${Bun.hash(page).toString(36)}"\`;
2102 const caching = { etag, "cache-control": "private, no-cache" };
2103 if (c.request.headers.get("if-none-match") === etag)
2104 return new Response(null, {
2105 status: 304,
2106 headers: { ...caching, ...SECURITY },
2107 });
2108 return html(page, 200, { ...clear, ...caching });
21042109 },
21052110 },
21062111 {
@@ -2377,16 +2382,31 @@ export const routes: readonly Route[] = [
23772382 {
23782383 method: "GET",
23792384 path: "/.well-known/security.txt",
2385 handle: () => {
2386 if (forgeUrl === undefined) return text("not found", 404);
2387 const now = new Date();
2388 const expires = new Date(Date.UTC(now.getUTCFullYear() + 1, 0, 1));
2389 return text(
2390 [
2391 \`Contact: \${new URL("/.well-known/security.txt", forgeUrl)}\`,
2392 \`Expires: \${expires.toISOString().replace(/\\.\\d{3}Z$/, "Z")}\`,
2393 \`Canonical: \${publicOrigin}/.well-known/security.txt\`,
2394 "",
2395 ].join("\\n"),
2396 );
2397 },
2398 },
2399 // Passwords live at the forge that signs people in: a password manager is sent there.
2400 {
2401 method: "GET",
2402 path: "/.well-known/change-password",
23802403 handle: () =>
23812404 forgeUrl === undefined
23822405 ? text("not found", 404)
23832406 : new Response(null, {
23842407 status: 302,
23852408 headers: {
2386 location: new URL(
2387 "/.well-known/security.txt",
2388 forgeUrl,
2389 ).toString(),
2409 location: new URL("/settings/password", forgeUrl).toString(),
23902410 ...SECURITY,
23912411 },
23922412 }),
@@ -4149,16 +4169,29 @@ test("what the web expects: robots.txt, and security.txt from the forge that run
41494169 expect(robots).toBe("User-agent: *\\nDisallow: /\\n");
41504170 expect(noindex).toBe("noindex");
41514171 }
4152 const security = await fetch(base + "/.well-known/security.txt", {
4172 const security = await fetch(base + "/.well-known/security.txt");
4173 const password = await fetch(base + "/.well-known/change-password", {
41534174 redirect: "manual",
41544175 });
4155 if (process.env.FORGE_URL === undefined) expect(security.status).toBe(404);
4156 else {
4157 expect(security.status).toBe(302);
4158 expect(security.headers.get("location")).toBe(
4159 new URL("/.well-known/security.txt", process.env.FORGE_URL).toString(),
4176 if (process.env.FORGE_URL === undefined) {
4177 expect(security.status).toBe(404);
4178 expect(password.status).toBe(404);
4179 } else {
4180 expect(security.status).toBe(200);
4181 expect(await security.text()).toContain(
4182 \`Contact: \${new URL("/.well-known/security.txt", process.env.FORGE_URL)}\\n\`,
4183 );
4184 expect(password.status).toBe(302);
4185 expect(password.headers.get("location")).toBe(
4186 new URL("/settings/password", process.env.FORGE_URL).toString(),
41604187 );
41614188 }
4189 // The page can be revalidated: same bytes, same ETag, a 304.
4190 const first = await fetch(base + "/");
4191 const etag = first.headers.get("etag") ?? "";
4192 expect(etag).not.toBe("");
4193 const again = await fetch(base + "/", { headers: { "if-none-match": etag } });
4194 expect(again.status).toBe(304);
41624195 });
41634196 `;
41644197
test/scaffold-webspec.test.ts added +63
@@ -0,0 +1,63 @@
1 /**
2 * A scaffolded app passes webspec() on its first boot (#3011): it is written
3 * to a temp dir, started on a loopback port, and the site-wide report on `/`
4 * comes back with no findings. The server is stopped in teardown.
5 */
6 import { afterAll, beforeAll, expect, test } from "bun:test";
7 import { mkdtemp, mkdir, rm, writeFile } from "node:fs/promises";
8 import { tmpdir } from "node:os";
9 import { dirname, join } from "node:path";
10 import { scaffoldFiles } from "../src/apps/scaffold.ts";
11 import { webspec } from "../src/release/webspec.ts";
12
13 let dir: string;
14 let child: ReturnType<typeof Bun.spawn> | undefined;
15 let forge: ReturnType<typeof Bun.serve> | undefined;
16 let base = "";
17
18 beforeAll(async () => {
19 dir = await mkdtemp(join(tmpdir(), "forge-scaffold-webspec-"));
20 for (const file of scaffoldFiles({ name: "field-notes", description: "" })) {
21 const path = join(dir, file.path);
22 await mkdir(dirname(path), { recursive: true });
23 await writeFile(path, file.content as string);
24 }
25 // Stands in for the forge that runs the app: only its address is used.
26 forge = Bun.serve({ port: 0, hostname: "127.0.0.1", fetch: () => new Response("") });
27 const probe = Bun.serve({ port: 0, hostname: "127.0.0.1", fetch: () => new Response("") });
28 const port = probe.port;
29 await probe.stop(true);
30 base = `http://127.0.0.1:${port}`;
31 child = Bun.spawn([process.execPath, "run", "src/server.ts"], {
32 cwd: dir,
33 env: {
34 PATH: process.env.PATH ?? "/usr/bin:/bin",
35 HOME: dir,
36 PORT: String(port),
37 HOST: "127.0.0.1",
38 FORGE_APP_DATA: join(dir, "data"),
39 FORGE_APP_URL: base,
40 FORGE_APP_ENV: "production",
41 FORGE_URL: `http://127.0.0.1:${forge.port}`,
42 },
43 stdout: "ignore",
44 stderr: "ignore",
45 });
46 for (let i = 0; i < 100; i++) {
47 if (await fetch(`${base}/healthz`).then((r) => r.ok, () => false)) return;
48 await Bun.sleep(50);
49 }
50 throw new Error("the scaffolded app did not boot");
51 }, 30_000);
52
53 afterAll(async () => {
54 child?.kill();
55 await forge?.stop(true);
56 await child?.exited;
57 await rm(dir, { recursive: true, force: true });
58 });
59
60 test("a scaffolded app has no webspec findings on its first boot", async () => {
61 const findings = await webspec(base, ["/"], fetch, { site: true });
62 expect(findings).toEqual([]);
63 }, 30_000);

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs