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): escape script-context interpolation in snapshot renderer (#40497)

Pavel Feldman37ff94b56272
diff --git a/CLAUDE.md b/CLAUDE.md
index 9110c0b092..76da6a4b9c 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -121,6 +121,7 @@ fix(proxy): handle SOCKS proxy authentication
Fixes: https://github.com/microsoft/playwright/issues/39562
EOF
)"
+# **Never `git push` without an explicit instruction to push.**
git push origin fix-39562
gh pr create --repo microsoft/playwright --head username:fix-39562 \
--title "fix(proxy): handle SOCKS proxy authentication" \
diff --git a/packages/isomorphic/trace/snapshotRenderer.ts b/packages/isomorphic/trace/snapshotRenderer.ts
index 72bf324bb5..368bd33448 100644
--- a/packages/isomorphic/trace/snapshotRenderer.ts
+++ b/packages/isomorphic/trace/snapshotRenderer.ts
@@ -568,7 +568,10 @@ function snapshotScript(viewport: ViewportSize, ...targetIds: (string | undefine
win.addEventListener('DOMContentLoaded', onDOMContentLoaded);
}
- return `\n(${applyPlaywrightAttributes.toString()})(${JSON.stringify(blankSnapshotUrl)},${JSON.stringify(viewport)}${targetIds.map(id => `, "${id}"`).join('')})`;
+ // Trace data is untrusted; escape `<` so attacker-controlled targetIds/viewport
+ // cannot terminate the surrounding <script> tag with "</script>".
+ const safe = (value: unknown) => JSON.stringify(value).replace(/</g, '\\u003c');
+ return `\n(${applyPlaywrightAttributes.toString()})(${safe(blankSnapshotUrl)},${safe(viewport)}${targetIds.map(id => `, ${safe(String(id))}`).join('')})`;
}
diff --git a/tests/library/snapshot-renderer.spec.ts b/tests/library/snapshot-renderer.spec.ts
new file mode 100644
index 0000000000..c92fcc0871
--- /dev/null
+++ b/tests/library/snapshot-renderer.spec.ts
@@ -0,0 +1,48 @@
+/**
+ * Copyright (c) Microsoft Corporation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import { test, expect } from '@playwright/test';
+import { SnapshotRenderer } from '../../packages/isomorphic/trace/snapshotRenderer';
+import { LRUCache } from '../../packages/isomorphic/lruCache';
+import type { FrameSnapshot } from '../../packages/trace/src/snapshot';
+
+function makeSnapshot(overrides: Partial<FrameSnapshot> = {}): FrameSnapshot {
+ return {
+ callId: 'call-1',
+ pageId: 'page-1',
+ frameId: 'frame-1',
+ frameUrl: 'http://example.com/',
+ timestamp: 0,
+ collectionTime: 0,
+ html: ['HTML', {}, ['BODY', {}]],
+ resourceOverrides: [],
+ viewport: { width: 1280, height: 720 },
+ isMainFrame: true,
+ ...overrides,
+ };
+}
+
+for (const [name, overrides] of [
+ ['callId', { callId: '</script><script>window.__pwned__=true;//' }],
+ ['snapshotName', { callId: 'call-1', snapshotName: '</script><img src=x onerror=alert(1)>' }],
+] as const) {
+ test(`snapshot renderer escapes attacker-controlled ${name} in script context`, () => {
+ const renderer = new SnapshotRenderer(new LRUCache(1_000_000), [], [makeSnapshot(overrides)], [], 0);
+ const { html } = renderer.render();
+ expect(html.match(/<script>/g)).toHaveLength(1);
+ expect(html.match(/<\/script>/g)).toHaveLength(1);
+ });
+}