From 8e850e7d8ef913a09cb8c696d440ee01ae44f840 Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Wed, 4 Nov 2026 15:04:14 -0800 Subject: [PATCH 1/5] fix(trace-viewer): reject unsafe snapshot attribute names Attribute names were written into the snapshot HTML without escaping. A crafted trace could use a name such as " onerror" to bypass the event handler filter, or smuggle extra attributes after a space. Drop names that contain whitespace, =, <, >, / or quotes, the same rule that already applies to tag names. --- packages/isomorphic/trace/snapshotRenderer.ts | 4 ++++ tests/library/snapshot-renderer.spec.ts | 10 ++++++++++ tests/registry.md | 1 + 3 files changed, 15 insertions(+) diff --git a/packages/isomorphic/trace/snapshotRenderer.ts b/packages/isomorphic/trace/snapshotRenderer.ts index 8e5d24a3264fe..e9d8d883fe147 100644 --- a/packages/isomorphic/trace/snapshotRenderer.ts +++ b/packages/isomorphic/trace/snapshotRenderer.ts @@ -138,6 +138,10 @@ export class SnapshotRenderer { const hasUnsafeHttpEquiv = isMeta && attrs.some(a => a[0].toLowerCase() === 'http-equiv' && !kAllowedMetaHttpEquivs.has(a[1].trim().toLowerCase())); for (const [attr, value] of attrs) { let attrName = attr; + // Reject names that would emit extra attributes (space, =, <, >, /, quotes). + // For example, a crafted trace could use " onerror" to bypass the "on" check below. + if (!attr || /[\s=<>\/"']/.test(attr)) + continue; // Strip event handler attributes. The capture side already empties these, // but a crafted trace file could include live handlers (e.g. onerror, onclick). // escapeHTMLAttribute does not help because payloads like "alert(1)" contain diff --git a/tests/library/snapshot-renderer.spec.ts b/tests/library/snapshot-renderer.spec.ts index 6c744d7aecd6e..ed433f854a705 100644 --- a/tests/library/snapshot-renderer.spec.ts +++ b/tests/library/snapshot-renderer.spec.ts @@ -164,6 +164,16 @@ test('TV-132 snapshot renderer drops unsafe tag names that smuggle attributes', expect(html).toContain('safe'); }); +test('TV-137 snapshot renderer drops unsafe attribute names that smuggle attributes', () => { + const renderer = new SnapshotRenderer(new LRUCache(1_000_000), [], [makeSnapshot({ + html: ['HTML', {}, ['BODY', {}, ['IMG', { ' onerror': 'alert(1)', 'x onmouseover=alert(2)': 'y', 'alt': 'safe' }]]], + })], [], 0); + const { html } = renderer.render(); + expect(html).not.toContain('alert(1)'); + expect(html).not.toContain('alert(2)'); + expect(html).toContain('safe'); +}); + test('TV-133 snapshot renderer still renders ordinary custom elements', () => { const renderer = new SnapshotRenderer(new LRUCache(1_000_000), [], [makeSnapshot({ html: ['HTML', {}, ['BODY', {}, diff --git a/tests/registry.md b/tests/registry.md index 2ea0103160cf4..9dc6583657c3b 100644 --- a/tests/registry.md +++ b/tests/registry.md @@ -5249,6 +5249,7 @@ subsystem, add a row to the Subsystems table. Then update this file: | `TV-134` | `library/snapshot-renderer.spec.ts` | stripAnsiEscapes should not exhibit polynomial backtracking | | `TV-135` | `library/snapshot-renderer.spec.ts` | stripAnsiEscapes handles common ANSI sequences after fix | | `TV-136` | `library/snapshot-renderer.spec.ts` | snapshot renderer selects the nearest out-of-order screencast frame | +| `TV-137` | `library/snapshot-renderer.spec.ts` | snapshot renderer drops unsafe attribute names that smuggle attributes | ### TZ — Timezone From 18d50bb36f6058c6251192709f3df97d3a1cfdf6 Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Wed, 4 Nov 2026 15:41:06 -0800 Subject: [PATCH 2/5] test(trace-viewer): fold unsafe attribute name cases into TV-125 --- tests/library/snapshot-renderer.spec.ts | 18 +++++------------- tests/registry.md | 1 - 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/tests/library/snapshot-renderer.spec.ts b/tests/library/snapshot-renderer.spec.ts index ed433f854a705..676c0acf7711b 100644 --- a/tests/library/snapshot-renderer.spec.ts +++ b/tests/library/snapshot-renderer.spec.ts @@ -91,11 +91,13 @@ test('TV-124 snapshot renderer nonces the bootstrap script', () => { test('TV-125 snapshot renderer strips event handler attributes', () => { const renderer = new SnapshotRenderer(new LRUCache(1_000_000), [], [makeSnapshot({ - html: ['HTML', {}, ['BODY', {}, ['IMG', { 'onerror': 'alert(1)', 'src': 'x' }]]], + html: ['HTML', {}, ['BODY', {}, ['IMG', { 'onerror': 'alert(1)', ' onerror': 'alert(2)', 'x onmouseover=alert(3)': 'y', 'src': 'x' }]]], })], [], 0); const { html } = renderer.render(); - expect(html).not.toContain('onerror="alert(1)"'); - expect(html).toContain('src="x"'); + expect(html).not.toContain('alert(1)'); + expect(html).not.toContain('alert(2)'); + expect(html).not.toContain('alert(3)'); + expect(html).toContain(''); }); test('TV-126 snapshot renderer strips onclick attributes', () => { @@ -164,16 +166,6 @@ test('TV-132 snapshot renderer drops unsafe tag names that smuggle attributes', expect(html).toContain('safe'); }); -test('TV-137 snapshot renderer drops unsafe attribute names that smuggle attributes', () => { - const renderer = new SnapshotRenderer(new LRUCache(1_000_000), [], [makeSnapshot({ - html: ['HTML', {}, ['BODY', {}, ['IMG', { ' onerror': 'alert(1)', 'x onmouseover=alert(2)': 'y', 'alt': 'safe' }]]], - })], [], 0); - const { html } = renderer.render(); - expect(html).not.toContain('alert(1)'); - expect(html).not.toContain('alert(2)'); - expect(html).toContain('safe'); -}); - test('TV-133 snapshot renderer still renders ordinary custom elements', () => { const renderer = new SnapshotRenderer(new LRUCache(1_000_000), [], [makeSnapshot({ html: ['HTML', {}, ['BODY', {}, diff --git a/tests/registry.md b/tests/registry.md index 9dc6583657c3b..2ea0103160cf4 100644 --- a/tests/registry.md +++ b/tests/registry.md @@ -5249,7 +5249,6 @@ subsystem, add a row to the Subsystems table. Then update this file: | `TV-134` | `library/snapshot-renderer.spec.ts` | stripAnsiEscapes should not exhibit polynomial backtracking | | `TV-135` | `library/snapshot-renderer.spec.ts` | stripAnsiEscapes handles common ANSI sequences after fix | | `TV-136` | `library/snapshot-renderer.spec.ts` | snapshot renderer selects the nearest out-of-order screencast frame | -| `TV-137` | `library/snapshot-renderer.spec.ts` | snapshot renderer drops unsafe attribute names that smuggle attributes | ### TZ — Timezone From ba3e3f1fc8bda5a9dc750fe2c31bda4916ea2d07 Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Wed, 4 Nov 2026 15:41:06 -0800 Subject: [PATCH 3/5] chore(trace-viewer): route all snapshot HTML writes through HtmlBuilder Escaping and name validation now live in one class, so no raw string from the trace reaches the HTML without going through it. --- packages/isomorphic/trace/snapshotRenderer.ts | 73 ++++++++++++++----- 1 file changed, 56 insertions(+), 17 deletions(-) diff --git a/packages/isomorphic/trace/snapshotRenderer.ts b/packages/isomorphic/trace/snapshotRenderer.ts index e9d8d883fe147..709a1346f535b 100644 --- a/packages/isomorphic/trace/snapshotRenderer.ts +++ b/packages/isomorphic/trace/snapshotRenderer.ts @@ -91,11 +91,11 @@ export class SnapshotRenderer { } render(): RenderedFrameSnapshot { - const result: string[] = []; + const result = new HtmlBuilder(); const visit = (n: NodeSnapshot, snapshotIndex: number, parentTag: string | undefined, parentAttrs: [string, string][] | undefined) => { // Text node. if (typeof n === 'string') { - result.push(escapeHTML(n)); + result.text(n); return; } @@ -114,17 +114,14 @@ export class SnapshotRenderer { // crafted trace file could include them to achieve XSS. if (name.toUpperCase() === 'SCRIPT') return; - // Reject names that would emit extra attributes (space, =, <, >, /). - // Allow Unicode custom elements such as math-α. - if (/[\s=<>\/]/.test(name)) - return; // Element node. // Note that