Public: anyone can read this; the crew can change it.
The forge toolbar is never added to non-HTML responses or twice to one page, proven by a test #13455
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/13446-the-forge-toolbar-is-never-added-to-non → main 3 commits fork
1 file
changed +161
−0
test/apps-toolbar-passthrough.test.ts added +161
| @@ -0,0 +1,161 @@ | ||
| 1 | /** | |
| 2 | * What the pill must not touch: non-HTML bodies byte for byte, and a page that has the tag once | |
| 3 | * (#13446). | |
| 4 | */ | |
| 5 | import { afterAll, beforeAll, describe, expect, spyOn, test } from "bun:test"; | |
| 6 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 7 | import { tmpdir } from "node:os"; | |
| 8 | import { join } from "node:path"; | |
| 9 | import { gzipSync } from "node:zlib"; | |
| 10 | import { createApp, type App } from "../src/app.ts"; | |
| 11 | import { newApp } from "../src/actions/apps.ts"; | |
| 12 | import { AppsRuntime, type EnvStatus } from "../src/apps/runtime.ts"; | |
| 13 | import { TOOLBAR_ASSET_PATH } from "../src/apps/toolbar.ts"; | |
| 14 | import { createUser } from "../src/core/users.ts"; | |
| 15 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 16 | import { quietLogs } from "./helpers/globals.ts"; | |
| 17 | ||
| 18 | quietLogs(); | |
| 19 | ||
| 20 | const DOMAIN = "apps.test"; | |
| 21 | /** Every byte value, so no text decoding can round-trip it by luck. */ | |
| 22 | const BINARY = Uint8Array.from({ length: 512 }, (_, i) => i % 256); | |
| 23 | const HTML = "<html><body><h1>hi</h1></body></html>"; | |
| 24 | const WITH_TAG = `<html><body>hi<script src="${TOOLBAR_ASSET_PATH}" defer></script></body></html>`; | |
| 25 | const enc = new TextEncoder(); | |
| 26 | /** The same bytes sent chunked, with no content-length. */ | |
| 27 | const chunked = (body: Uint8Array | string, headers: Record<string, string>): Response => { | |
| 28 | const bytes = typeof body === "string" ? enc.encode(body) : body; | |
| 29 | const half = Math.floor(bytes.length / 2); | |
| 30 | let sent = 0; | |
| 31 | return new Response( | |
| 32 | // One chunk per pull, so the body arrives in two pieces. | |
| 33 | new ReadableStream({ | |
| 34 | pull(c) { | |
| 35 | if (sent++ === 0) c.enqueue(bytes.slice(0, half)); | |
| 36 | else { | |
| 37 | c.enqueue(bytes.slice(half)); | |
| 38 | c.close(); | |
| 39 | } | |
| 40 | }, | |
| 41 | }), | |
| 42 | { headers }, | |
| 43 | ); | |
| 44 | }; | |
| 45 | ||
| 46 | describe("the forge pill leaves what is not a plain page alone", () => { | |
| 47 | let dataDir: string; | |
| 48 | let app: App; | |
| 49 | let servers: Servers; | |
| 50 | let runtime: AppsRuntime; | |
| 51 | let backend: ReturnType<typeof Bun.serve>; | |
| 52 | let status: ReturnType<typeof spyOn>; | |
| 53 | ||
| 54 | beforeAll(async () => { | |
| 55 | backend = Bun.serve({ | |
| 56 | port: 0, | |
| 57 | fetch(request) { | |
| 58 | const path = new URL(request.url).pathname; | |
| 59 | const html = { "content-type": "text/html; charset=utf-8" }; | |
| 60 | if (path === "/json") return Response.json({ a: 1, html: "</body>" }); | |
| 61 | if (path === "/json-stream") return chunked('{"a":1,"html":"</body>"}', { "content-type": "application/json" }); | |
| 62 | if (path === "/text") return new Response("plain </body> text", { headers: { "content-type": "text/plain" } }); | |
| 63 | if (path === "/bin") return new Response(BINARY, { headers: { "content-type": "image/png" } }); | |
| 64 | if (path === "/bin-stream") return chunked(BINARY, { "content-type": "application/octet-stream" }); | |
| 65 | if (path === "/download") | |
| 66 | return new Response(HTML, { headers: { ...html, "content-disposition": 'attachment; filename="a.html"' } }); | |
| 67 | if (path === "/download-stream") | |
| 68 | return chunked(HTML, { ...html, "content-disposition": 'attachment; filename="a.html"' }); | |
| 69 | if (path === "/gz-json") | |
| 70 | return new Response(gzipSync('{"a":1}'), { headers: { "content-type": "application/json", "content-encoding": "gzip" } }); | |
| 71 | if (path === "/tag") return new Response(WITH_TAG, { headers: html }); | |
| 72 | if (path === "/tag-stream") return chunked(WITH_TAG, html); | |
| 73 | if (path === "/bare-stream") return chunked("<p>no body tag</p>", html); | |
| 74 | return new Response(HTML, { headers: html }); | |
| 75 | }, | |
| 76 | }); | |
| 77 | dataDir = await mkdtemp(join(tmpdir(), "forge-app-toolbar-")); | |
| 78 | app = createApp({ dataDir, port: 0, host: "forge.test" }); | |
| 79 | servers = await serve(app); | |
| 80 | runtime = new AppsRuntime(app, { | |
| 81 | domain: DOMAIN, | |
| 82 | scheme: "http", | |
| 83 | publicPort: servers.port, | |
| 84 | forgeUrl: servers.url, | |
| 85 | concurrency: 1, | |
| 86 | }); | |
| 87 | await runtime.start(); | |
| 88 | status = spyOn(AppsRuntime.prototype, "envStatus").mockImplementation( | |
| 89 | () => | |
| 90 | ({ | |
| 91 | env: "production", | |
| 92 | state: "running", | |
| 93 | port: backend.port, | |
| 94 | commitOid: "a".repeat(40), | |
| 95 | toolbar: undefined, | |
| 96 | }) as EnvStatus, | |
| 97 | ); | |
| 98 | const a = await createUser(app.db, { | |
| 99 | origin: "seed", | |
| 100 | trusted: true, | |
| 101 | username: "alice", | |
| 102 | email: "[email protected]", | |
| 103 | password: "a-good-passphrase", | |
| 104 | adminIfFirst: true, | |
| 105 | }); | |
| 106 | if (!a.ok) throw new Error(a.error); | |
| 107 | const made = await newApp(app, a.value, { | |
| 108 | name: "notes", | |
| 109 | description: "A place for words", | |
| 110 | visibility: "public", | |
| 111 | }); | |
| 112 | if (!made.ok) throw new Error(String(made.error)); | |
| 113 | }, 120_000); | |
| 114 | ||
| 115 | afterAll(async () => { | |
| 116 | status.mockRestore(); | |
| 117 | await runtime.stop(); | |
| 118 | servers.stop(); | |
| 119 | backend.stop(true); | |
| 120 | app.close(); | |
| 121 | await rm(dataDir, { recursive: true, force: true }); | |
| 122 | }); | |
| 123 | ||
| 124 | const at = (path: string) => | |
| 125 | fetch(`${servers.url}${path}`, { | |
| 126 | headers: { host: `notes.${DOMAIN}`, accept: "*/*" }, | |
| 127 | redirect: "manual", | |
| 128 | decompress: false, | |
| 129 | } as RequestInit); | |
| 130 | const bytes = async (path: string) => new Uint8Array(await (await at(path)).arrayBuffer()); | |
| 131 | const count = (s: string) => s.split(TOOLBAR_ASSET_PATH).length - 1; | |
| 132 | ||
| 133 | test("JSON, text, images and downloads arrive byte for byte, sized and chunked", async () => { | |
| 134 | expect(await bytes("/json")).toEqual(enc.encode(JSON.stringify({ a: 1, html: "</body>" }))); | |
| 135 | expect(await bytes("/json-stream")).toEqual(enc.encode('{"a":1,"html":"</body>"}')); | |
| 136 | expect(await bytes("/text")).toEqual(enc.encode("plain </body> text")); | |
| 137 | expect(await bytes("/bin")).toEqual(BINARY); | |
| 138 | expect(await bytes("/bin-stream")).toEqual(BINARY); | |
| 139 | expect(await bytes("/download")).toEqual(enc.encode(HTML)); | |
| 140 | expect(await bytes("/download-stream")).toEqual(enc.encode(HTML)); | |
| 141 | const dl = await at("/download"); | |
| 142 | expect(dl.headers.get("content-disposition")).toBe('attachment; filename="a.html"'); | |
| 143 | }); | |
| 144 | ||
| 145 | test("a compressed answer reaches the client still compressed, unchanged", async () => { | |
| 146 | const res = await at("/gz-json"); | |
| 147 | expect(res.headers.get("content-encoding")).toBe("gzip"); | |
| 148 | expect(new Uint8Array(await res.arrayBuffer())).toEqual(new Uint8Array(gzipSync('{"a":1}'))); | |
| 149 | }); | |
| 150 | ||
| 151 | test("a page that carries the tag keeps exactly that one, sized or chunked", async () => { | |
| 152 | expect(await (await at("/tag")).text()).toBe(WITH_TAG); | |
| 153 | expect(await (await at("/tag-stream")).text()).toBe(WITH_TAG); | |
| 154 | }); | |
| 155 | ||
| 156 | test("a page with no </body> gets the tag once, at the end, sized or chunked", async () => { | |
| 157 | const got = await (await at("/bare-stream")).text(); | |
| 158 | expect(got.startsWith("<p>no body tag</p><script")).toBe(true); | |
| 159 | expect(count(got)).toBe(1); | |
| 160 | }); | |
| 161 | }); |
new above
offline