Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(trace-viewer): reject unsafe snapshot tag names (#42712)

Sebastien Tardif3cd102531124
diff --git a/packages/isomorphic/trace/snapshotRenderer.ts b/packages/isomorphic/trace/snapshotRenderer.ts
index 2a38aca7b6..63612c79ae 100644
--- a/packages/isomorphic/trace/snapshotRenderer.ts
+++ b/packages/isomorphic/trace/snapshotRenderer.ts
@@ -108,6 +108,10 @@ 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 <noscript> will not be rendered by default in the trace viewer, because
// JS is enabled. So rename it to <x-noscript>.
diff --git a/tests/library/snapshot-renderer.spec.ts b/tests/library/snapshot-renderer.spec.ts
index 9dce93b7d4..2bfa23ecab 100644
--- a/tests/library/snapshot-renderer.spec.ts
+++ b/tests/library/snapshot-renderer.spec.ts
@@ -126,6 +126,34 @@ test('snapshot renderer handles case-insensitive iframe tag names', () => {
expect(html).toContain('__playwright_src__');
});
+test('snapshot renderer drops unsafe tag names that smuggle attributes', () => {
+ const renderer = new SnapshotRenderer(new LRUCache(1_000_000), [], [makeSnapshot({
+ html: ['HTML', {}, ['BODY', {}, ['img src=x onerror=alert(1)', {}], 'safe']],
+ })], [], 0);
+ const { html } = renderer.render();
+ expect(html).not.toContain('onerror=alert(1)');
+ expect(html).not.toContain('<img src=x');
+ expect(html).toContain('safe');
+});
+
+test('snapshot renderer still renders ordinary custom elements', () => {
+ const renderer = new SnapshotRenderer(new LRUCache(1_000_000), [], [makeSnapshot({
+ html: ['HTML', {}, ['BODY', {},
+ ['MY-WIDGET', { 'id': 'w' }, 'hello'],
+ ['MY-WIDGET_V2', { 'id': 'w2' }, 'v2'],
+ ['MY-WIDGET.V2', { 'id': 'w3' }, 'dot'],
+ ['math-α', { 'id': 'm' }, 'alpha'],
+ ['my-élément', { 'id': 'e' }, 'elem'],
+ ]],
+ })], [], 0);
+ const { html } = renderer.render();
+ expect(html).toContain('<MY-WIDGET id="w">hello</MY-WIDGET>');
+ expect(html).toContain('<MY-WIDGET_V2 id="w2">v2</MY-WIDGET_V2>');
+ expect(html).toContain('<MY-WIDGET.V2 id="w3">dot</MY-WIDGET.V2>');
+ expect(html).toContain('<math-α id="m">alpha</math-α>');
+ expect(html).toContain('<my-élément id="e">elem</my-élément>');
+});
+
test('stripAnsiEscapes should not exhibit polynomial backtracking', () => {
// \x1b[ + 50000 semicolons + non-terminal character.
// Before the fix this took >3 seconds due to O(n^2) backtracking.