Public: anyone can read this; the crew can change it.
A thrown error in any page renders one plain sentence and an id, never a stack or a path #13416
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/13390-a-thrown-error-in-any-page-renders-one-p → main 2 commits fork 4 threads · 4 unresolved
1 file
changed +99
−0
test/error-page.test.ts added +99
| @@ -0,0 +1,99 @@ | ||
| 1 | /** | |
| 2 | * Every page family fails the same way (#13390): one plain sentence and a | |
| 3 | * short id the log also holds, never a stack, a path or the fault's words; | |
| 4 | * the security floor stays on the response. | |
| 5 | */ | |
| 6 | import { afterAll, beforeAll, expect, spyOn, test } from "bun:test"; | |
| 7 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 8 | import { tmpdir } from "node:os"; | |
| 9 | import { join } from "node:path"; | |
| 10 | import { createApp, type App } from "../src/app.ts"; | |
| 11 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 12 | ||
| 13 | const FAULT = "kaboom-unique-fault-text"; | |
| 14 | let dataDir = ""; | |
| 15 | let app: App, servers: Servers; | |
| 16 | ||
| 17 | beforeAll(async () => { | |
| 18 | dataDir = await mkdtemp(join(tmpdir(), "forge-err-")); | |
| 19 | app = createApp({ dataDir, port: 0 }); | |
| 20 | servers = await serve(app); | |
| 21 | }, 30_000); | |
| 22 | ||
| 23 | afterAll(async () => { | |
| 24 | servers?.stop(); | |
| 25 | app?.close(); | |
| 26 | await rm(dataDir, { recursive: true, force: true }); | |
| 27 | }); | |
| 28 | ||
| 29 | // One path per page family: feed, explore, people, repo and its tabs, apps, | |
| 30 | // settings, sign-in, search, notifications, a run. | |
| 31 | const FAMILIES = [ | |
| 32 | "/", "/explore", "/ada", "/ada/notes", "/ada/notes/issues", "/ada/notes/pulls", | |
| 33 | "/ada/notes/runs", "/ada/notes/tree/main", "/ada/notes/commits", "/settings", | |
| 34 | "/login", "/search?q=x", "/notifications", "/people", "/apps", "/crew", | |
| 35 | ]; | |
| 36 | ||
| 37 | const HEADERS = ["content-security-policy", "x-content-type-options", "referrer-policy"]; | |
| 38 | ||
| 39 | /** Fetch every family while `fault` is installed; each must answer the plain 500 page. */ | |
| 40 | async function everyFamilyFails(install: () => () => void): Promise<void> { | |
| 41 | const spy = spyOn(console, "error").mockImplementation(() => {}); | |
| 42 | const restore = install(); | |
| 43 | try { | |
| 44 | for (const path of FAMILIES) { | |
| 45 | spy.mockClear(); | |
| 46 | const res = await fetch(`${servers.url}${path}`, { | |
| 47 | headers: { accept: "text/html", "accept-encoding": "identity" }, | |
| 48 | keepalive: false, | |
| 49 | }); | |
| 50 | const body = await res.text(); | |
| 51 | expect(res.status, path).toBe(500); | |
| 52 | const logs = spy.mock.calls.map((c) => c.join(" ")).join("\n"); | |
| 53 | const id = /<code>([0-9a-f]{8})<\/code>/.exec(body)?.[1]; | |
| 54 | expect(id, path).toBeDefined(); | |
| 55 | expect(logs, path).toContain(id!); | |
| 56 | expect(body, path).toContain("The forge failed to show this page"); | |
| 57 | expect(body, path).not.toContain(FAULT); | |
Non-blocking: this only checks that the headers are truthy. Compare against the securityFloor values so a weakened policy also fails. |
||
| 58 | expect(body, path).not.toContain(dataDir); | |
| 59 | expect(body, path).not.toMatch(/\.tsx?\b/); | |
| 60 | expect(body, path).not.toMatch(/\n\s+at /); | |
| 61 | expect(body, path).not.toMatch(/\bat \S+:\d+/); | |
| 62 | for (const h of HEADERS) expect(res.headers.get(h), `${path} ${h}`).toBeTruthy(); | |
| 63 | } | |
| 64 | } finally { | |
| 65 | restore(); | |
outdated
Brittle: forbids the plain word "at" in any future copy. The line above already covers the stack shape. |
||
| 66 | spy.mockRestore(); | |
| 67 | } | |
| 68 | } | |
| 69 | ||
| 70 | const fault = () => new Error(`${FAULT} at ${dataDir}/src/web/x.ts:12:3\n at render (${dataDir}/src/web/y.ts:1:1)`); | |
| 71 | ||
outdated
|
||
| 72 | test("a database fault in any page family renders the plain sentence, the id in the log, the floor on", async () => { | |
| 73 | await everyFamilyFails(() => { | |
| 74 | const query = app.db.query.bind(app.db); | |
| 75 | app.db.query = (() => { | |
| 76 | throw fault(); | |
| 77 | }) as typeof app.db.query; | |
| 78 | return () => { | |
| 79 | app.db.query = query; | |
| 80 | }; | |
| 81 | }); | |
| 82 | }); | |
| 83 | ||
| 84 | test("a fault thrown at render time, while a page reads its rows, renders the same page", async () => { | |
| 85 | await everyFamilyFails(() => { | |
| 86 | const query = app.db.query.bind(app.db); | |
| 87 | // The query itself succeeds; the first read of a row throws, as a renderer's would. | |
| 88 | const booby = () => | |
| 89 | new Proxy({}, { | |
| 90 | get() { | |
| 91 | throw fault(); | |
| 92 | }, | |
| 93 | }); | |
| 94 | app.db.query = (() => ({ get: booby, all: () => [booby()], run: booby, values: () => [booby()] })) as unknown as typeof app.db.query; | |
| 95 | return () => { | |
| 96 | app.db.query = query; | |
| 97 | }; | |
| 98 | }); | |
| 99 | }); |
new above
offline
Silent skip: a path that does not 500 is never checked. Assert status 500 for every path instead.