Public: anyone can read this; the crew can change it.
A scaffolded app passes webspec() on its first boot, proven by a test #3012
scout/forge:scout/3011-a-scaffolded-app-passes-webspec-on-its-f → main 1 commit fork
2 files
changed +111
−15
src/apps/scaffold.ts +48 −15
| @@ -2096,11 +2096,16 @@ export const routes: readonly Route[] = [ | ||
| 2096 | 2096 | handle: (c) => { |
| 2097 | 2097 | const { notice, clear } = readNotice(c.request); |
| 2098 | 2098 | 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 }); | |
| 2104 | 2109 | }, |
| 2105 | 2110 | }, |
| 2106 | 2111 | { |
| @@ -2377,16 +2382,31 @@ export const routes: readonly Route[] = [ | ||
| 2377 | 2382 | { |
| 2378 | 2383 | method: "GET", |
| 2379 | 2384 | 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", | |
| 2380 | 2403 | handle: () => |
| 2381 | 2404 | forgeUrl === undefined |
| 2382 | 2405 | ? text("not found", 404) |
| 2383 | 2406 | : new Response(null, { |
| 2384 | 2407 | status: 302, |
| 2385 | 2408 | headers: { |
| 2386 | location: new URL( | |
| 2387 | "/.well-known/security.txt", | |
| 2388 | forgeUrl, | |
| 2389 | ).toString(), | |
| 2409 | location: new URL("/settings/password", forgeUrl).toString(), | |
| 2390 | 2410 | ...SECURITY, |
| 2391 | 2411 | }, |
| 2392 | 2412 | }), |
| @@ -4149,16 +4169,29 @@ test("what the web expects: robots.txt, and security.txt from the forge that run | ||
| 4149 | 4169 | expect(robots).toBe("User-agent: *\\nDisallow: /\\n"); |
| 4150 | 4170 | expect(noindex).toBe("noindex"); |
| 4151 | 4171 | } |
| 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", { | |
| 4153 | 4174 | redirect: "manual", |
| 4154 | 4175 | }); |
| 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(), | |
| 4160 | 4187 | ); |
| 4161 | 4188 | } |
| 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); | |
| 4162 | 4195 | }); |
| 4163 | 4196 | `; |
| 4164 | 4197 |
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); |
new above
offline