Public: anyone can read this; the crew can change it.
The run page works again: its script parses, and every page script is compiled once (#16477) #16482
Check · Review · Land · Live done — Landed and live in production.
fix/runpage → main 0 commits 2 threads · 2 unresolved
3 files
changed +44
−1
src/web/asset.ts +24
| @@ -21,6 +21,7 @@ import { existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, s | ||
| 21 | 21 | import { join } from "node:path"; |
| 22 | 22 | import { sha256 } from "../kernel/hash.ts"; |
| 23 | 23 | import { encodeOnce, MIN_BYTES, negotiate } from "../http/compress.ts"; |
| 24 | import { defect } from "../kernel/error.ts"; | |
| 24 | 25 | import { createLog } from "../kernel/log.ts"; |
| 25 | 26 | import { beaconJs } from "./beacon.ts"; |
| 26 | 27 | import { shellJs } from "./client.ts"; |
| @@ -265,6 +266,7 @@ export function pageScriptAsset(js: string, dataDir?: string): FileAsset { | ||
| 265 | 266 | const key = sha256(body); |
| 266 | 267 | let asset = pageScripts.get(key); |
| 267 | 268 | if (asset === undefined) { |
| 269 | parses(body); | |
| 268 | 270 | asset = fileAsset("page", "js", body, "text/javascript; charset=utf-8"); |
| 269 | 271 | remember(key, asset); |
| 270 | 272 | } |
| @@ -272,6 +274,28 @@ export function pageScriptAsset(js: string, dataDir?: string): FileAsset { | ||
| 272 | 274 | return asset; |
| 273 | 275 | } |
| 274 | 276 | |
| 277 | /** | |
| 278 | * A page script is composed from many template strings, where one unescaped `\n` is a real line | |
| 279 | * break inside a JavaScript string and the whole script dies in the browser without a word on the | |
| 280 | * server: the run page was dead for days so (#16477). Each composed script is parsed once, never | |
| 281 | * run; one that does not parse fails every test that renders it, and is said loudly in production. | |
| 282 | */ | |
| 283 | const SYNTAX = new Bun.Transpiler({ loader: "js" }); | |
| 284 | ||
| 285 | /** Throws on a script that does not parse; parses it only, never runs it. */ | |
| 286 | export function checkScript(body: string): void { | |
| 287 | SYNTAX.transformSync(body); | |
| 288 | } | |
| 289 | ||
| 290 | function parses(body: string): void { | |
| 291 | try { | |
| 292 | checkScript(body); | |
| 293 | } catch (error) { | |
| 294 | if (process.env["NODE_ENV"] === "test") defect("page_script_syntax", `a page script does not parse: ${String(error)}`); | |
| 295 | log.error("web.page_script_does_not_parse", { error: String(error) }); | |
| 296 | } | |
| 297 | } | |
| 298 | ||
| 275 | 299 | function remember(key: string, asset: FileAsset): void { |
| 276 | 300 | pageScripts.set(key, asset); |
| 277 | 301 | pageScriptsByName.set(asset.name, asset); |
src/web/client.ts +1 −1
| @@ -371,7 +371,7 @@ export const studioRunJs = /* js */ ` | ||
| 371 | 371 | fetch(logUrl, { credentials: 'same-origin' }) |
| 372 | 372 | .then(function (r) { return r.ok ? r.text() : ''; }) |
| 373 | 373 | .then(function (body) { |
| 374 | var all = body.split('\n'); | |
| 374 | var all = body.split('\\n'); | |
| 375 | 375 | if (all[all.length - 1] === '') all.pop(); |
| 376 | 376 | all.slice(have).forEach(put); |
| 377 | 377 | }) |
test/page-script-parses.test.ts added +19
| @@ -0,0 +1,19 @@ | ||
| 1 | /** | |
| 2 | * A page script is composed from template strings, where one unescaped `\n` is a real line break | |
| 3 | * inside a JavaScript string: the run page's script did not parse from 11-21 to 11-25 and the page | |
| 4 | * sat frozen with no word on the server (#16477). Every composed script is parsed when first | |
| 5 | * built; one that does not parse fails here and in every test that renders it. | |
| 6 | */ | |
| 7 | import { expect, test } from "bun:test"; | |
| 8 | import { checkScript, pageScriptAsset } from "../src/web/asset.ts"; | |
| 9 | import { studioRunJs } from "../src/web/client.ts"; | |
| 10 | ||
| 11 | test("the run page's script parses", () => { | |
| 12 | expect(() => checkScript(studioRunJs)).not.toThrow(); | |
| 13 | expect(() => pageScriptAsset(studioRunJs)).not.toThrow(); | |
| 14 | }); | |
| 15 | ||
| 16 | test("a composed page script that does not parse is refused where it is built", () => { | |
| 17 | expect(() => pageScriptAsset("var all = body.split('\n');")).toThrow(/does not parse/); | |
| 18 | expect(() => pageScriptAsset("var all = body.split('\\n');")).not.toThrow(); | |
| 19 | }); |
new above
offline
new Function(body)is eval-equivalent, and the security floor says noeval. Use a parse-only check (Bun.Transpiler().transformSync(body)inside the try) so the server never builds an executable from composed text. The catch and the defect/log split can stay as they are.