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

A forge asleep for a client shows a plain 'Waking up' page with a retry that needs no script #14094

Merged opened this

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

Merged

scout/forge:scout/14084-a-forge-asleep-for-a-client-shows-a-plai → main 8 commits fork 3 threads · 3 unresolved

  • Merged. scout/14084-a-forge-asleep-for-a-client-shows-a-plai was merged into main as 879df48.

Review

Sign in to review this change.
11 files changed +126 −58
src/kernel/destroy_reach.ts −1
@@ -93,7 +93,6 @@ export const ALLOWED_REMOVALS: readonly { unit: string; why: string }[] = [
9393 { unit: "src/actions/spawn.ts:unmake", why: "removes the directory and ledger of a child whose first attempt failed before it was ever finished, and only that call's own (never a finished or resumed one): nothing of anyone's work is in it (#13135)" },
9494 { unit: "src/spawn/own_code.ts:checkout", why: "removes a half-made export of the forge's own code, keyed by commit" },
9595 { unit: "src/spawn/own_code.ts:recordGood", why: "removes exports of the forge's own code for other commits than the good one" },
96 { unit: "src/web/asset.ts:prune", why: "removes cached page scripts older than their keep time" },
9796 ];
9897
9998 const REMOVERS = ["rmSync", "rmdirSync", "unlinkSync", "rm", "rmdir", "unlink"];
src/spawn/host.ts +9 −9
@@ -620,10 +620,10 @@ export async function wakeGate(parent: App, request: Request): Promise<Response
620620 if (ledger === undefined || !ledger.done) return undefined;
621621 // Being moved into its own cell: closed here and not yet proven there. It answers again in a
622622 // moment, and the page asks by itself.
623 if (ledger.moving) return wakingResponse(parent, request, name);
623 if (ledger.moving) return wakingResponse(request, name);
624624 // Placed in this process on a host that opens none there: it waits for its move, and takes no
625625 // place among the forges waking.
626 if (ledger.placement !== "cell" && isSealed(parent)) return wakingResponse(parent, request, name, waitsToMove(name));
626 if (ledger.placement !== "cell" && isSealed(parent)) return wakingResponse(request, name, waitsToMove(name));
627627 keepSweeping(parent);
628628 keepWatchingFloor(parent);
629629 // Below the disk floor an awake child is put to sleep now, not at the next look,
@@ -664,7 +664,7 @@ export async function wakeGate(parent: App, request: Request): Promise<Response
664664 if (!reserved) waking.set(parent, (reserved = new Map()));
665665 if (reservedCount(parent) > 0 && reserved.has(ledger.bodyId)) return undefined;
666666 const verdict = takeDoor(parent, "api", request);
667 if (!verdict.allowed) return wakingResponse(parent, request, name, "too many requests to sleeping forges from your address; try again in a minute");
667 if (!verdict.allowed) return wakingResponse(request, name, "too many requests to sleeping forges from your address; try again in a minute");
668668 let rates = wakeRates.get(parent);
669669 if (!rates) wakeRates.set(parent, (rates = new WakeRate()));
670670 const at = gateClock.now();
@@ -675,11 +675,11 @@ export async function wakeGate(parent: App, request: Request): Promise<Response
675675 const own = isParentsOwn(ledger, request);
676676 const wait = own ? 0 : people.waitS(budget.key, max, at);
677677 if (wait > 0)
678 return wakingResponse(parent, request, name, budgetRefusal({ name, budget, max, waitS: wait, limitsUrl: `${parentOrigin(parent.config)}/admin/limits` }), wait);
678 return wakingResponse(request, name, budgetRefusal({ name, budget, max, waitS: wait, limitsUrl: `${parentOrigin(parent.config)}/admin/limits` }), wait);
679679 const why = (await roomFor(parent)) ?? (known ? undefined : rates.refusal(ledger.bodyId, at));
680680 if (why !== undefined) {
681681 const stop = lastStop(parent, ledger.bodyId);
682 return wakingResponse(parent, request, name, stop === undefined ? why : stopSentence(stop));
682 return wakingResponse(request, name, stop === undefined ? why : stopSentence(stop));
683683 }
684684 if (!known) rates.note(ledger.bodyId, at);
685685 if (!own) people.note(budget.key, at);
@@ -1093,8 +1093,8 @@ export const hostedChildApps = childAppsSummary;
10931093 export async function answerFromCell(parent: App, request: Request, named?: string): Promise<Response> {
10941094 const name = named ?? parseHostedChildName(request.headers.get("host") ?? "", childHost(parent));
10951095 const ledger = name === undefined ? undefined : readLedgerSync(parent, name);
1096 if (ledger !== undefined && ledger.moving) return wakingResponse(parent, request, ledger.name);
1097 if (ledger !== undefined && ledger.placement !== "cell" && isSealed(parent)) return wakingResponse(parent, request, ledger.name, waitsToMove(ledger.name));
1096 if (ledger !== undefined && ledger.moving) return wakingResponse(request, ledger.name);
1097 if (ledger !== undefined && ledger.placement !== "cell" && isSealed(parent)) return wakingResponse(request, ledger.name, waitsToMove(ledger.name));
10981098 if (ledger === undefined || ledger.placement !== "cell") return new Response("not found", { status: 404 });
10991099 const cells = cellChildren(parent);
11001100 const child = cells.get(ledger.bodyId);
@@ -1113,7 +1113,7 @@ export async function answerFromCell(parent: App, request: Request, named?: stri
11131113 // Only so many wait on one cell: the rest are told it is waking at once, so a crowd cannot pin
11141114 // connections and bodies.
11151115 const waiting = waiters.get(ledger.bodyId) ?? 0;
1116 if (waiting >= wakeLimits.waiters) return wakingResponse(parent, request, ledger.name, undefined, 5, ledger.wakeMs);
1116 if (waiting >= wakeLimits.waiters) return wakingResponse(request, ledger.name, undefined, 5, ledger.wakeMs);
11171117 waiters.set(ledger.bodyId, waiting + 1);
11181118 let woke: CellChild | undefined;
11191119 try {
@@ -1134,7 +1134,7 @@ export async function answerFromCell(parent: App, request: Request, named?: stri
11341134 // A cell that did not start says so, with when the host tries again: never "waking up" without
11351135 // end.
11361136 const stop = cells.failure(ledger.bodyId) === undefined ? undefined : lastStop(parent, ledger.bodyId);
1137 return wakingResponse(parent, request, ledger.name, stop === undefined ? undefined : stopSentence(stop), 5, ledger.wakeMs);
1137 return wakingResponse(request, ledger.name, stop === undefined ? undefined : stopSentence(stop), 5, ledger.wakeMs);
11381138 }
11391139
11401140 /**
src/web/pages/waking.ts +46 −31
@@ -1,44 +1,59 @@
11 /**
22 * What a visitor sees at a sleeping forge's address: it is waking, or the host
3 * will not wake it right now. Drawn with the page shell and its components, so
4 * it follows the theme; a browser asks again by itself (`refresh`), a git
5 * client or an API call gets a sentence (`spawn/sleep.ts` `wakingText`).
3 * will not wake it right now. A client must never see a raw error, so the page
4 * is one sentence in a document of its own: no script, one hashed style, no shell (the
5 * shell's palette lists the parent's apps), and no name, host or id. The
6 * browser asks again by itself (`<meta http-equiv="refresh">`) and a link
7 * retries by hand; a git client
8 * or an API call gets a sentence (`spawn/sleep.ts` `wakingText`).
69 */
710
8 import type { App } from "../../app.ts";
9 import type { Ctx } from "../../http/context.ts";
11 import { createHash } from "node:crypto";
12 import { THEMES } from "../../design/themes.ts";
13 import { securityFloor } from "../../http/respond.ts";
1014 import { wakingText } from "../../spawn/sleep.ts";
11 import { btn, empty } from "../ui.ts";
12 import { html } from "../html.ts";
13 import { page } from "../layout.ts";
14 import { wakeSeconds, wakingSentence } from "../retry_words.ts";
15 import { escapeHtml } from "../html.ts";
16 import { triesAgain, wakeSeconds } from "../retry_words.ts";
17
18 const light = THEMES.light.tokens;
19 const dark = THEMES.dark.tokens;
20 /**
outdated

Unused _app parameter: remove it and the callers' argument.

21 * The forge's tokens inlined (the page cannot load the stylesheet it stands in for): a narrow
22 * measure, generous rhythm.
23 */
24 const STYLE =
25 `:root{color-scheme:light dark;--bg:${light.bg};--fg:${light.fg};--accent:${light.accent}}` +
26 `@media(prefers-color-scheme:dark){:root{--bg:${dark.bg};--fg:${dark.fg};--accent:${dark.accent}}}` +
27 `body{margin:0;background:var(--bg);color:var(--fg);font:14px/1.5 system-ui,-apple-system,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif}` +
28 `main{max-width:36rem;margin:0 auto;padding:4rem 16px}` +
outdated

Principles 3, 4, 5: no style, so no --bg/--fg tokens, no margins or measure, and no way to reach a retry on a phone. Add tokenised styling and a plain <a href> retry link; the 429 page especially has no way forward as it is.

29 `h1{font-size:20px;font-weight:620;letter-spacing:-.02em;line-height:1.25;margin:0 0 .75rem}` +
30 `p{margin:0 0 1.5rem}a{color:var(--accent)}`;
31 /** Nothing loads, nothing runs: the one stylesheet is admitted by hash. */
32 const POLICY = `default-src 'none'; style-src 'sha256-${createHash("sha256").update(STYLE).digest("base64")}'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'`;
1533
1634 /** Waking (`why` undefined, 503) or refused (`why` a sentence, 429), as a page when the visitor asks for HTML. */
17 export function wakingResponse(app: App, request: Request, name: string, why?: string, retryS = 5, wakeMs?: number): Response {
35 export function wakingResponse(request: Request, name: string, why?: string, retryS = 5, wakeMs?: number): Response {
1836 // One number answers "when": the words, Refresh and Retry-After. A refusal keeps the caller's
1937 // own.
2038 const aboutS = why === undefined ? wakeSeconds(wakeMs) : retryS;
2139 if (!(request.headers.get("accept") ?? "").includes("text/html")) return wakingText(name, why, aboutS);
22 const url = new URL(request.url);
23 const ctx: Ctx = { app, request, url, params: {}, auth: { user: undefined, scope: undefined, via: "none" }, theme: undefined };
24 const response = page(
25 {
26 ctx,
27 bare: true,
28 title: why === undefined ? "Waking up" : "Not now",
29 body: html`<div class="narrow-wide">
30 ${empty({
31 icon: "history",
32 title: why === undefined ? "Waking up" : "Not now",
33 note: why ?? wakingSentence(name, wakeMs),
34 action: btn("Try again", { href: url.pathname, variant: "primary" }),
35 })}
36 </div>`,
40 const waking = why === undefined;
41 const title = waking ? "Waking up" : "Not now";
42 const sentence = why ?? `This forge was asleep and is waking up. ${triesAgain(aboutS)}`;
43 // A link navigates where a refresh may be blocked; the path is the request's own, escaped.
44 const here = escapeHtml(new URL(request.url).pathname);
45 const body = `<!doctype html><html lang="en"><head><meta charset="utf-8">${
46 waking ? `<meta http-equiv="refresh" content="${aboutS}">` : ""
47 }<meta name="viewport" content="width=device-width, initial-scale=1"><title>${title}</title><style>${STYLE}</style></head><body><main><h1>${title}</h1><p>${escapeHtml(sentence)}</p><p><a href="${here}">Try again</a></p></main></body></html>`;
48 const response = new Response(body, {
49 status: waking ? 503 : 429,
50 headers: {
51 "content-type": "text/html; charset=utf-8",
52 "content-security-policy": POLICY,
53 "retry-after": String(aboutS),
54 "cache-control": "no-store",
55 ...(waking ? { refresh: String(aboutS) } : {}),
3756 },
38 why === undefined ? 503 : 429,
39 );
40 response.headers.set("retry-after", String(aboutS));
41 response.headers.set("cache-control", "no-store");
42 if (why === undefined) response.headers.set("refresh", String(aboutS));
43 return response;
57 });
58 return securityFloor(response);
4459 }
src/web/retry_words.ts −8
@@ -16,11 +16,3 @@ export function wakeSeconds(ms: unknown): number {
1616 export function triesAgain(seconds: number): string {
1717 return `This page tries again in about ${seconds} second${seconds === 1 ? "" : "s"}.`;
1818 }
19
20 /**
21 * `homie was asleep and is starting. This page tries again in about 8 seconds.` (`ms`: the last
22 * measured start).
23 */
24 export function wakingSentence(name: string, ms?: number): string {
25 return `${name} was asleep and is starting. ${triesAgain(wakeSeconds(ms))}`;
26 }
test/first-ten-minutes.test.ts +1 −1
@@ -202,7 +202,7 @@ describe("a request while a cell wakes", () => {
202202 const res = await answerFromCell(parent, post());
203203 expect(woken).toBe(false); // answered while the wake is still under way
204204 expect(res.status).toBe(503);
205 expect(await res.text()).toContain("was asleep and is starting");
205 expect(await res.text()).toContain("is waking up");
206206 } finally {
207207 wake.open(undefined);
208208 wakeLimits.waitMs = was;
test/forge-sleep.test.ts +2 −2
@@ -163,7 +163,7 @@ describe("a hosted forge scales to zero", () => {
163163 rates.note("b", 2000);
164164 expect(rates.refusal("b", 3000, env)).toContain("woken 2 times");
165165 expect(rates.refusal("b", 3000 + 3_600_000, env)).toBeUndefined();
166 const refused = wakingResponse(parent, new Request("https://x"), "homie", "too many");
166 const refused = wakingResponse(new Request("https://x"), "homie", "too many");
167167 expect(refused.status).toBe(429);
168168 expect(await refused.text()).toBe("too many");
169169 });
@@ -182,7 +182,7 @@ describe("a hosted forge scales to zero", () => {
182182 });
183183
184184 test("a waking page is plain for a browser, and the gate lets an awake forge through", async () => {
185 const page = wakingResponse(parent, new Request("https://forge.test/", { headers: { accept: "text/html" } }), "homie");
185 const page = wakingResponse(new Request("https://forge.test/", { headers: { accept: "text/html" } }), "homie");
186186 expect(page.status).toBe(503);
187187 expect(page.headers.get("refresh")).toBe("5");
188188 const text = await page.text();
test/loading-chrome.looks.test.ts +1 −1
@@ -49,7 +49,7 @@ describe.skipIf(chromium === undefined)("loading, watched in Chromium", () => {
4949 port: 0,
5050 fetch: async (request) => {
5151 const url = new URL(request.url);
52 if (url.pathname === "/__waking") return wakingResponse(app, request, "demo");
52 if (url.pathname === "/__waking") return wakingResponse(request, "demo");
5353 const headers = new Headers(request.headers);
5454 headers.delete("host");
5555 headers.set("accept-encoding", "identity");
test/no-polling.test.ts +1
@@ -101,6 +101,7 @@ async function render(base: string, path: string, cookie: string): Promise<{ sta
101101 const ALLOWED: Record<string, { allow: string[]; why: string }> = {
102102 "src/web/sw.ts": { allow: ['http-equiv="refresh"'], why: "the restart page: the forge is down, so there is no /events to hear" },
103103 "src/web/pages/offline.ts": { allow: ['http-equiv="refresh"'], why: "the offline page: the forge is unreachable, so there is no /events to hear" },
104 "src/web/pages/waking.ts": { allow: ['http-equiv="refresh"'], why: "the waking page: the forge is asleep, so there is no /events to hear" },
104105 "src/web/pages/talk.ts": { allow: ["setInterval"], why: "server side: the stream of one turn watches the run's row, which has no event to hear" },
105106 "src/web/client.ts": { allow: ["setInterval"], why: "clocks that only redraw text" },
106107 "src/web/space_client.ts": { allow: ["setInterval"], why: "the map clock label only redraws text; it fetches and reloads nothing" },
test/retry-words.test.ts +3 −3
@@ -5,7 +5,7 @@ import { tmpdir } from "node:os";
55 import { join } from "node:path";
66 import { offlineHtml } from "../src/web/pages/offline.ts";
77 import { wakingResponse } from "../src/web/pages/waking.ts";
8 import { triesAgain, wakeSeconds, wakingSentence } from "../src/web/retry_words.ts";
8 import { triesAgain, wakeSeconds } from "../src/web/retry_words.ts";
99
1010 describe("when the forge will answer", () => {
1111 test("a measured start becomes whole seconds, clamped; none measured is the default", () => {
@@ -19,11 +19,11 @@ describe("when the forge will answer", () => {
1919 test("the waking page says about how long, and refreshes once after that long", async () => {
2020 const app = createApp({ dataDir: mkdtempSync(join(tmpdir(), "forge-wake-")), port: 0 });
2121 try {
22 const res = wakingResponse(app, new Request("https://x/", { headers: { accept: "text/html" } }), "homie", undefined, 5, 7400);
22 const res = wakingResponse(new Request("https://x/", { headers: { accept: "text/html" } }), "homie", undefined, 5, 7400);
2323 expect(res.headers.get("refresh")).toBe("8");
2424 expect(res.headers.get("retry-after")).toBe("8");
2525 const text = await res.text();
26 expect(text).toContain(wakingSentence("homie", 7400));
26 expect(text).toContain("This page tries again in about 8 seconds.");
2727 expect(text).not.toContain("Starting, about");
2828 expect(text.match(/tries again/g)?.length).toBe(1);
2929 } finally {
test/spawn-cell-stays.test.ts +1 −2
@@ -16,7 +16,6 @@ import { cellChildren, type CellChild } from "../src/spawn/cell.ts";
1616 import { answerFromCell, bootHostedChild, closeHostedChildren, NOT_STARTED, provideCellsForTest, ranOwnCode, resolveHostedChild, wakeLimits } from "../src/spawn/host.ts";
1717 import { readOwnCode, switchOwnCode } from "../src/spawn/own_code.ts";
1818 import { cancelRestarts, lastStop, stopSentence } from "../src/spawn/stopped.ts";
19 import { wakingSentence } from "../src/web/retry_words.ts";
2019 import { quietLogs } from "./helpers/globals.ts";
2120
2221 quietLogs();
@@ -167,7 +166,7 @@ describe("how long a start took", () => {
167166 expect(waking.status).toBe(503);
168167 expect(waking.headers.get("refresh")).toBe("8");
169168 expect(waking.headers.get("retry-after")).toBe("8");
170 expect(await waking.text()).toContain(wakingSentence("alpha", 7500));
169 expect(await waking.text()).toContain("This page tries again in about 8 seconds.");
171170 } finally {
172171 Date.now = realNow;
173172 wakeLimits.waitMs = waitMs;
test/waking-plain.test.ts added +62
@@ -0,0 +1,62 @@
1 import { describe, expect, test } from "bun:test";
2 import { wakingResponse } from "../src/web/pages/waking.ts";
3
4 const ask = () =>
5 wakingResponse(new Request("https://homie--forge.internal.test/x", { headers: { accept: "text/html" } }), "homie-secret-name");
6
7 describe("the waking page is plain", () => {
8 test("one sentence and a meta refresh, no script needed to retry", async () => {
9 const res = ask();
10 expect(res.status).toBe(503);
11 const text = await res.text();
12 expect(text).toContain('http-equiv="refresh" content="5"');
13 expect(text.match(/<p>/g)?.length).toBe(2); // the sentence and the link
14 expect(text).toContain('<a href="/x">Try again</a>');
15 expect(text).toContain("is waking up");
16 expect(text).not.toContain("<script");
17 });
18
19 test("it shows no stack, host or internal id", async () => {
20 const text = await ask().text();
21 for (const leak of ["homie-secret-name", "homie--forge", "internal.test", "at Object", ".ts:", "Error:", "stack"])
22 expect(text).not.toContain(leak);
23 });
24
25 test("the security headers are kept", async () => {
26 const res = ask();
27 expect(res.headers.get("content-security-policy")).toBeTruthy();
28 expect(res.headers.get("x-content-type-options")).toBe("nosniff");
29 expect(res.headers.get("referrer-policy")).toBeTruthy();
30 expect(res.headers.get("cache-control")).toBe("no-store");
31 });
32
33 test("a refusal is 429, escaped, names no forge, has no refresh and a link to tap", async () => {
outdated

Add a test for the 429 path with a hostile why: it must be escaped, status 429, no refresh, and carry a retry link.

34 const hostile = "<script>alert(1)</script> & \"quoted\"";
35 const res = wakingResponse(
36 new Request("https://homie--forge.internal.test/x?a=1", { headers: { accept: "text/html" } }),
37 "homie-secret-name",
38 hostile,
39 30,
40 );
41 expect(res.status).toBe(429);
42 expect(res.headers.get("refresh")).toBeNull();
43 expect(res.headers.get("retry-after")).toBe("30");
44 const text = await res.text();
45 expect(text).not.toContain("<script");
46 expect(text).toContain("&lt;script&gt;alert(1)&lt;/script&gt;");
47 expect(text).not.toContain("http-equiv");
48 expect(text).toContain('<a href="/x">Try again</a>');
49 for (const leak of ["homie-secret-name", "homie--forge", "internal.test"]) expect(text).not.toContain(leak);
50 });
51
52 test("the page uses the forge's tokens in one hashed style, and no script is allowed", async () => {
53 const res = ask();
54 const csp = res.headers.get("content-security-policy") ?? "";
55 expect(csp).toMatch(/style-src 'sha256-[A-Za-z0-9+/=]+'/);
56 expect(csp).not.toContain("script-src");
57 expect(csp).toContain("default-src 'none'");
58 const text = await res.text();
59 expect(text).toContain("var(--bg)");
60 expect(text).toContain("max-width:36rem");
61 });
62 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs