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

Merged opened this

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

Merged

fix/runpage → main 0 commits 2 threads · 2 unresolved

  • Merged. fix/runpage was merged into main as 25f192c.

Review

Sign in to review this change.
3 files changed +44 −1
src/web/asset.ts +24
@@ -21,6 +21,7 @@ import { existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, s
2121 import { join } from "node:path";
2222 import { sha256 } from "../kernel/hash.ts";
2323 import { encodeOnce, MIN_BYTES, negotiate } from "../http/compress.ts";
24 import { defect } from "../kernel/error.ts";
2425 import { createLog } from "../kernel/log.ts";
2526 import { beaconJs } from "./beacon.ts";
2627 import { shellJs } from "./client.ts";
@@ -265,6 +266,7 @@ export function pageScriptAsset(js: string, dataDir?: string): FileAsset {
265266 const key = sha256(body);
266267 let asset = pageScripts.get(key);
267268 if (asset === undefined) {
269 parses(body);
268270 asset = fileAsset("page", "js", body, "text/javascript; charset=utf-8");
269271 remember(key, asset);
270272 }
@@ -272,6 +274,28 @@ export function pageScriptAsset(js: string, dataDir?: string): FileAsset {
272274 return asset;
273275 }
274276
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);
outdated

new Function(body) is eval-equivalent, and the security floor says no eval. 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.

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
275299 function remember(key: string, asset: FileAsset): void {
276300 pageScripts.set(key, asset);
277301 pageScriptsByName.set(asset.name, asset);
src/web/client.ts +1 −1
@@ -371,7 +371,7 @@ export const studioRunJs = /* js */ `
371371 fetch(logUrl, { credentials: 'same-origin' })
372372 .then(function (r) { return r.ok ? r.text() : ''; })
373373 .then(function (body) {
374 var all = body.split('\n');
374 var all = body.split('\\n');
375375 if (all[all.length - 1] === '') all.pop();
376376 all.slice(have).forEach(put);
377377 })
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();

This repeats the new Function check. Drop the line, since pageScriptAsset(studioRunJs) on the next line already covers it, so the test does not keep an eval-style call in the repository.

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 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs