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

A page that failed to render shows what failed, a Retry link to the same address and the request id on one line #16061

Merged opened this

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

Merged

scout/forge:scout/16045-a-page-that-failed-to-render-shows-what → main 0 commits fork 5 threads · 5 unresolved

  • Merged. scout/16045-a-page-that-failed-to-render-shows-what was merged into main as 262b718.

Review

Sign in to review this change.
5 files changed +55 −9
src/web/failed_page.ts +9 −4
@@ -45,10 +45,15 @@ export function parentOf(pathname: string): string {
4545 return segments.length <= 1 ? "/" : `/${segments.slice(0, -1).join("/")}`;
4646 }
4747
48 /** Where Try again goes: this path for a read, home for anything that wrote. */
48 /**
49 * Where Retry goes: the same address for a read — this path and the request's
outdated

Box 1: Retry should reload the same address, query included. Take the request URL's search as well as the pathname, keep the '//' guard, and add a test with a query string.

50 * query — and home for anything that wrote. A path that starts `//` would
51 * leave the origin, so it goes home too.
52 */
4953 export function retryTarget(request: Request, pathname: string): string {
5054 const read = request.method === "GET" || request.method === "HEAD";
51 return read && !pathname.startsWith("//") ? pathname : "/";
55 if (!read || !pathname.startsWith("/") || pathname.startsWith("//")) return "/";
56 return pathname + new URL(request.url).search;
5257 }
5358
5459 export function failedPage(request: Request, pathname: string, id: string): Response {
@@ -59,9 +64,9 @@ export function failedPage(request: Request, pathname: string, id: string): Resp
5964 ${crumbs.length === 0 ? null : crumbNav(crumbs, { ariaLabel: "Location" })}
6065 </header>
6166 <main class="shell" id="main">
62 <div class="page-head"><h1>Something broke on our side. Reference: <code>${id}</code></h1></div>
67 <div class="page-head"><h1>Could not render <code>${pathname.length > 120 ? `${pathname.slice(0, 120)}…` : pathname}</code>. Reference: <code>${id}</code></h1></div>
6368 <p class="muted">This is the forge's fault, not yours. <a href="/tell">Tell the crew</a> and quote the reference.</p>
64 <p class="actions">${btn("Try again", { href: retryTarget(request, pathname), variant: "primary" })} ${btn("Go home", { href: "/" })}</p>
69 <p class="actions">${btn("Retry", { href: retryTarget(request, pathname), variant: "primary" })} ${btn("Go home", { href: "/" })}</p>
6570 </main>`;
6671 const text = `<!doctype html><html lang="en"><head><meta charset="utf-8" />
6772 <meta name="viewport" content="width=device-width, initial-scale=1" />
test/error-page-500.test.ts +7 −2
@@ -54,7 +54,7 @@ test("a throwing page answers 500 with chrome, the way back and an id found in t
5454 expect(body).toContain('class="topbar"');
5555 expect(body).toContain('class="crumbs"');
5656 expect(body).toContain('href="/ada/notes"'); // the parent
57 expect(body).toContain("Try again");
57 expect(body).toContain("Retry");
5858 expect(body).toContain("the forge's fault");
5959 expect(body).toContain('href="/tell"');
6060 expect(body).toContain('href="/"');
@@ -103,9 +103,14 @@ test("crumbs and parent come from the path alone", () => {
103103 expect(parentOf("/a")).toBe("/");
104104 });
105105
106 test("Try again re-requests a read, and sends anything that wrote home", () => {
106 test("Retry re-requests a read, and sends anything that wrote home", () => {
107107 const req = (method: string) => new Request("http://x/a/b", { method });
108108 expect(retryTarget(req("GET"), "/a/b")).toBe("/a/b");
109109 expect(retryTarget(req("POST"), "/a/b")).toBe("/");
110110 expect(retryTarget(req("GET"), "//evil.example")).toBe("/");
111 const query = (method: string) => new Request("http://x/ada/notes?tab=open&q=a%20b", { method });
112 expect(retryTarget(query("GET"), "/ada/notes")).toBe("/ada/notes?tab=open&q=a%20b");
113 expect(retryTarget(query("HEAD"), "/ada/notes")).toBe("/ada/notes?tab=open&q=a%20b");
114 expect(retryTarget(query("POST"), "/ada/notes")).toBe("/");
115 expect(retryTarget(query("GET"), "//evil.example")).toBe("/");
111116 });
test/error-page.test.ts +2 −2
@@ -50,10 +50,10 @@ async function everyFamilyFails(install: () => () => void): Promise<void> {
5050 const body = await res.text();
5151 expect(res.status, path).toBe(500);
5252 const logs = spy.mock.calls.map((c) => c.join(" ")).join("\n");
53 const id = /<code>([0-9a-f]{8})<\/code>/.exec(body)?.[1];
53 const id = /Reference: <code>([0-9a-f]{8})<\/code>/.exec(body)?.[1];
5454 expect(id, path).toBeDefined();
5555 expect(logs, path).toContain(id!);
56 expect(body, path).toContain("Something broke on our side. Reference:");
56 expect(body, path).toContain("Could not render");
5757 expect(body, path).not.toContain(FAULT);
5858 expect(body, path).not.toContain(dataDir);
5959 expect(body, path).not.toMatch(/\.tsx?\b/);
test/error-reference.test.ts +1 −1
@@ -43,7 +43,7 @@ const leaks = (b: string) => {
4343 test("the page says one line with the reference, the log has the same id", async () => {
4444 const { res, body, logs } = await failing("/ada/notes/issues", "text/html");
4545 expect(res.status).toBe(500);
46 const ref = /Something broke on our side\. Reference: <code>([0-9a-f]{8})<\/code>/.exec(body)?.[1];
46 const ref = /Reference: <code>([0-9a-f]{8})<\/code>/.exec(body)?.[1];
4747 expect(ref).toBeDefined();
4848 expect(logs).toContain(ref!);
4949 leaks(body);
test/failed-page.test.ts added +36
@@ -0,0 +1,36 @@
1 import { expect, test } from "bun:test";
2 import { failedPage } from "../src/web/failed_page.ts";
3
4 test("a failed page names the escaped path once, retries it, and prints the id once", async () => {
5 const path = "/a/<script>alert(1)</script>";
6 const res = failedPage(new Request("http://x/a/b"), path, "deadbeef");
7 const body = await res.text();
8 expect(res.status).toBe(500);
9 expect(body).toContain("Could not render <code>");
10 expect(body).not.toContain("<script>alert");
11 expect(body).toContain("&lt;script&gt;");

This regex matches the crumb link href="/a" (or the escaped-path crumb) and then lazily runs to Retry, so it passes whatever Retry points at. Fix: assert the Retry anchor itself, e.g. /<a[^>]href="([^"])"[^>]*>Retry</ and check the captured href equals the escaped path. The request URL is /a but the path passed is different, so also make them consistent.

12 const retry = body.match(/<a[^>]*href="([^"]*)"[^>]*>\s*Retry</);

The Retry href assertion only covers a plain path. Add a case with a query and a POST (which should go home) so the test would fail if the same-address behaviour broke.

13 expect(retry?.[1]).toBe("/a/&lt;script&gt;alert(1)&lt;/script&gt;");
14 expect(body.split("deadbeef").length - 1).toBe(1);
15 expect(res.headers.get("x-request-id")).toBe("deadbeef");
16 expect(res.headers.get("content-security-policy")).toBeTruthy();
17 });
18
19 const retryOf = (body: string) => body.match(/<a[^>]*href="([^"]*)"[^>]*>\s*Retry</)?.[1];
20
21 test("Retry reloads the same address, query included, and an escaped one", async () => {
22 const res = failedPage(new Request("http://x/ada/notes?tab=open&q=%3Cb%3E"), "/ada/notes", "cafef00d");
23 expect(retryOf(await res.text())).toBe("/ada/notes?tab=open&amp;q=%3Cb%3E");
24 });
25
26 test("Retry after a POST goes home, not back to the write", async () => {
27 const req = new Request("http://x/ada/notes?tab=open", { method: "POST" });
28 expect(retryOf(await failedPage(req, "/ada/notes", "cafef00d").text())).toBe("/");
29 });
30
31 test("a long unbroken path in the heading wraps instead of widening a phone", async () => {
32 const { components } = await import("../src/web/style/components.ts");
33 const body = await failedPage(new Request("http://x/a"), `/${"x".repeat(200)}`, "cafef00d").text();
34 expect(body).toMatch(/<div class="page-head"><h1>Could not render <code>/);
35 expect(components).toContain(".page-head h1 { min-width: 0; overflow-wrap: anywhere; }");
36 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs