|
| 1 | +/** |
| 2 | + * Cross-runtime proof that a `reflect: true` property holding a function never |
| 3 | + * writes that function into its attribute, under WHICHEVER runtime executes |
| 4 | + * this file (#1169). Run under both: |
| 5 | + * |
| 6 | + * node test/bun/reflect-function-guard.mjs |
| 7 | + * bun test/bun/reflect-function-guard.mjs |
| 8 | + * |
| 9 | + * The reflect path is plain DOM-shim use rather than a serializer / listener / |
| 10 | + * stream surface, so a divergence in the GUARD itself is not what this is |
| 11 | + * watching for. What makes it worth proving on both runtimes is the thing the |
| 12 | + * guard removes: the severity of the leak was runtime-dependent. |
| 13 | + * |
| 14 | + * `String(fn)` is `Function.prototype.toString`, and what that returns is not |
| 15 | + * the same on both. On Node it is the source text as written, so a module-scope |
| 16 | + * `const` the body reads appears only as its identifier. Bun transpiles a |
| 17 | + * module before the engine sees it and can fold a module-scope string literal |
| 18 | + * straight into the body, so the same function can report the SECRET ITSELF |
| 19 | + * where Node reports the name it was read through. That is a transpiler's |
| 20 | + * internal choice rather than a documented boundary, so it is not a rule to |
| 21 | + * build a habit on, which is exactly why refusing to stringify is the fix |
| 22 | + * rather than sanitizing whatever came back. |
| 23 | + * |
| 24 | + * So this asserts the strong claim on both runtimes: neither the identifier |
| 25 | + * nor the folded value reaches the output, because nothing is stringified at |
| 26 | + * all. A plain assert script (not `*.test.mjs`, so the runner does not |
| 27 | + * double-run it), exiting non-zero on failure. Run from the repo root so the |
| 28 | + * bare `@webjsdev/core` specifier resolves to the workspace package. |
| 29 | + */ |
| 30 | +import assert from 'node:assert/strict'; |
| 31 | +import { html, WebComponent, prop } from '@webjsdev/core'; |
| 32 | +import { renderToString } from '@webjsdev/core/server'; |
| 33 | + |
| 34 | +const runtime = process.versions.bun ? `bun ${process.versions.bun}` : `node ${process.versions.node}`; |
| 35 | + |
| 36 | +// A module-scope literal the action closes over. This is the binding Bun may |
| 37 | +// fold into the function body and Node will not, so both spellings are checked |
| 38 | +// below and neither may appear. |
| 39 | +const VENDOR_API_KEY = 'sk_live_REFLECT_FOLDED_MARKER'; |
| 40 | + |
| 41 | +async function leakyAction() { |
| 42 | + const header = `Bearer ${VENDOR_API_KEY}`; |
| 43 | + const inline = 'REFLECT_INLINE_MARKER'; |
| 44 | + return header + inline; |
| 45 | +} |
| 46 | + |
| 47 | +class ReflectProbe extends WebComponent({ |
| 48 | + // The String branch, which is where the leak lived. |
| 49 | + label: prop(String, { reflect: true }), |
| 50 | + // Untyped, so the same fall-through is reached without a declared type. |
| 51 | + payload: prop({ reflect: true }), |
| 52 | + // JSON.stringify(fn) is `undefined`, which setAttribute wrote as that |
| 53 | + // literal four-character string. No source leak, same defect. |
| 54 | + config: prop(Object, { reflect: true }), |
| 55 | + // An ordinary value, to prove the guard did not break reflection. |
| 56 | + ok: prop(String, { reflect: true }), |
| 57 | + // An author-supplied converter runs first and is deliberately untouched. |
| 58 | + conv: prop(String, { reflect: true, converter: { toAttribute: (v) => `custom:${typeof v}` } }), |
| 59 | +}) { |
| 60 | + constructor() { |
| 61 | + super(); |
| 62 | + this.label = leakyAction; |
| 63 | + this.payload = leakyAction; |
| 64 | + this.config = leakyAction; |
| 65 | + this.ok = 'plain-string'; |
| 66 | + this.conv = leakyAction; |
| 67 | + } |
| 68 | + render() { return html`<span>x</span>`; } |
| 69 | +} |
| 70 | +ReflectProbe.register('bun-reflect-probe'); |
| 71 | + |
| 72 | +// The guard warns on every drop, and a passing run should not print a wall of |
| 73 | +// warnings that read as failures. Capture them and assert on the count instead. |
| 74 | +// |
| 75 | +// This capture is what caught the warning being dead code in the SHIPPED |
| 76 | +// bundle. The check used to be gated on `process.env.NODE_ENV`, which |
| 77 | +// `scripts/build-framework-dist.js` folds to a constant (esbuild substitutes |
| 78 | +// it under `platform: 'browser'` + `minify`), so the SSR half never warned in |
| 79 | +// any published build. Running this through the bare `@webjsdev/core` |
| 80 | +// specifier, which resolves to `dist/`, is the only layer that sees it. |
| 81 | +const warnings = []; |
| 82 | +const originalWarn = console.warn; |
| 83 | +console.warn = (...args) => warnings.push(args.join(' ')); |
| 84 | +let out; |
| 85 | +try { |
| 86 | + out = await renderToString(html`<bun-reflect-probe></bun-reflect-probe>`); |
| 87 | +} finally { |
| 88 | + console.warn = originalWarn; |
| 89 | +} |
| 90 | + |
| 91 | +// The claim the whole guard exists for, in both spellings the two runtimes |
| 92 | +// produce. The inline marker covers a literal written inside the body (which |
| 93 | +// both runtimes emit), the folded marker covers the module-scope binding Bun |
| 94 | +// may inline, and the identifier covers what Node emits in its place. |
| 95 | +for (const marker of ['REFLECT_INLINE_MARKER', 'REFLECT_FOLDED_MARKER', 'VENDOR_API_KEY']) { |
| 96 | + assert.ok(!out.includes(marker), `[${runtime}] the function source reached the output via ${marker}: ${out}`); |
| 97 | +} |
| 98 | + |
| 99 | +// The attributes are absent, not merely emptied. An empty attribute would be a |
| 100 | +// different observable than the removal the guard promises. |
| 101 | +for (const attr of ['label=', 'payload=', 'config=']) { |
| 102 | + assert.ok(!out.includes(attr), `[${runtime}] ${attr} should be removed, not written: ${out}`); |
| 103 | +} |
| 104 | +assert.ok(!out.includes('undefined'), `[${runtime}] wrote a literal "undefined": ${out}`); |
| 105 | + |
| 106 | +// Ordinary reflection is unchanged, and the author override still wins. |
| 107 | +assert.ok(out.includes('ok="plain-string"'), `[${runtime}] a normal value must still reflect: ${out}`); |
| 108 | +assert.ok(out.includes('conv="custom:function"'), `[${runtime}] converter.toAttribute must still win: ${out}`); |
| 109 | + |
| 110 | +// One warning per dropped property, and none of them prints the value: the |
| 111 | +// warning path is the other place the source could escape, and a server log is |
| 112 | +// not always a private place. |
| 113 | +assert.equal(warnings.length, 3, `[${runtime}] expected one warning per dropped prop, got ${warnings.length}`); |
| 114 | +for (const message of warnings) { |
| 115 | + for (const marker of ['REFLECT_INLINE_MARKER', 'REFLECT_FOLDED_MARKER']) { |
| 116 | + assert.ok(!message.includes(marker), `[${runtime}] the warning leaked the source it refused to write: ${message}`); |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +console.log(`[${runtime}] reflect-function-guard: function props dropped, no source in output or warnings, string + converter intact ✓`); |
0 commit comments