|
| 1 | +/** |
| 2 | + * objectui#13511 — the Console document turns zod's JIT off before any module |
| 3 | + * runs. |
| 4 | + * |
| 5 | + * zod v4 probes for `eval` with `new Function('')` the first time an object |
| 6 | + * schema is built. Under the Console's content-security-policy that probe is |
| 7 | + * refused, and reported on every load, unless the react-pages tier is on. The |
| 8 | + * fix is one inline classic script in `apps/console/index.html`, so, following |
| 9 | + * `bootSplash.test.ts`, these tests read that file through `?raw` and run the |
| 10 | + * script itself rather than a copy of it. |
| 11 | + * |
| 12 | + * Two halves: the script is ahead of the module entry (a `z.config()` call in |
| 13 | + * the entry runs too late, because the bundle evaluates the vendor chunks that |
| 14 | + * build schemas before the entry's body), and running it before the installed |
| 15 | + * zod means zod builds and parses with no `Function` call, against a control |
| 16 | + * that the probe is seen without it. The second half is what notices a zod |
| 17 | + * upgrade that stops reading the global. |
| 18 | + * |
| 19 | + * The zod half runs in a child Node process: this worker's setup has already |
| 20 | + * loaded zod, which caches the probe's answer once per process. |
| 21 | + */ |
| 22 | +import indexHtml from '../../index.html?raw'; |
| 23 | +import { describe, it, expect } from 'vitest'; |
| 24 | +import { spawnSync } from 'node:child_process'; |
| 25 | +import path from 'node:path'; |
| 26 | + |
| 27 | +const SCRIPT_MARKER = '__zod_globalConfig'; |
| 28 | +const APP_ENTRY = '/src/main.tsx'; |
| 29 | + |
| 30 | +/** Every `<script>` open tag plus its inline body, in document order, comments removed. */ |
| 31 | +function scriptsOf(html: string) { |
| 32 | + return [...html.replace(/<!--[\s\S]*?-->/g, '').matchAll(/<script\b([^>]*)>([\s\S]*?)<\/script>/g)].map( |
| 33 | + (match) => ({ attrs: match[1] ?? '', body: match[2] ?? '' }), |
| 34 | + ); |
| 35 | +} |
| 36 | + |
| 37 | +/** The jitless script exactly as it ships; exactly one, or the test fails rather than test nothing. */ |
| 38 | +function jitlessScript(): string { |
| 39 | + const found = scriptsOf(indexHtml).filter((script) => script.body.includes(SCRIPT_MARKER)); |
| 40 | + if (found.length !== 1) throw new Error(`expected exactly 1 inline ${SCRIPT_MARKER} script in index.html, found ${found.length}`); |
| 41 | + return found[0].body; |
| 42 | +} |
| 43 | + |
| 44 | +/** Counts `Function` calls while zod builds and parses one object schema, after `prelude`. */ |
| 45 | +function probe(prelude: string): { calls: string[]; parsed: unknown } { |
| 46 | + const source = ` |
| 47 | +const calls = []; |
| 48 | +const RealFunction = globalThis.Function; |
| 49 | +globalThis.Function = new Proxy(RealFunction, { |
| 50 | + construct(target, args) { calls.push(args.join(',')); return Reflect.construct(target, args); }, |
| 51 | + apply(target, self, args) { calls.push(args.join(',')); return Reflect.apply(target, self, args); }, |
| 52 | +}); |
| 53 | +globalThis.window = globalThis; |
| 54 | +${prelude} |
| 55 | +const { z } = await import('zod'); |
| 56 | +const schema = z.object({ name: z.string(), count: z.number().optional() }); |
| 57 | +const parsed = schema.parse({ name: 'a', count: 1 }); |
| 58 | +process.stdout.write(JSON.stringify({ calls, parsed })); |
| 59 | +`; |
| 60 | + const run = spawnSync(process.execPath, ['--input-type=module', '-e', source], { |
| 61 | + cwd: path.resolve(__dirname, '..'), |
| 62 | + encoding: 'utf8', |
| 63 | + }); |
| 64 | + if (run.status !== 0) throw new Error(`probe exited ${run.status}\n${run.stderr}`); |
| 65 | + return JSON.parse(run.stdout); |
| 66 | +} |
| 67 | + |
| 68 | +describe('apps/console/index.html — zod runs without its JIT (objectui#13511)', () => { |
| 69 | + it('sets it in a classic script ahead of the module entry', () => { |
| 70 | + const scripts = scriptsOf(indexHtml); |
| 71 | + const jitless = scripts.findIndex((script) => script.body.includes(SCRIPT_MARKER)); |
| 72 | + const entry = scripts.findIndex((script) => script.attrs.includes(APP_ENTRY)); |
| 73 | + expect(jitless).toBeGreaterThanOrEqual(0); |
| 74 | + expect(entry).toBeGreaterThan(jitless); |
| 75 | + expect(scripts[jitless].attrs).not.toMatch(/type\s*=\s*["']?module|\bsrc\s*=|\b(async|defer)\b/); |
| 76 | + }); |
| 77 | + |
| 78 | + it('control: without it, the installed zod calls Function when it builds an object schema', () => { |
| 79 | + const { calls, parsed } = probe(''); |
| 80 | + expect(parsed).toEqual({ name: 'a', count: 1 }); |
| 81 | + expect(calls.length).toBeGreaterThan(0); |
| 82 | + }); |
| 83 | + |
| 84 | + it('with it run first, the installed zod builds and parses an object schema without calling Function', () => { |
| 85 | + const { calls, parsed } = probe(jitlessScript()); |
| 86 | + expect(parsed).toEqual({ name: 'a', count: 1 }); |
| 87 | + expect(calls).toEqual([]); |
| 88 | + }); |
| 89 | +}); |
0 commit comments