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): encode some urls inside style tags (#35705)

Dmitry Gozmanedd25e3b83f0
diff --git a/packages/trace-viewer/src/sw/snapshotRenderer.ts b/packages/trace-viewer/src/sw/snapshotRenderer.ts
index c2b014ac3f..a0980ac7b1 100644
--- a/packages/trace-viewer/src/sw/snapshotRenderer.ts
+++ b/packages/trace-viewer/src/sw/snapshotRenderer.ts
@@ -82,7 +82,7 @@ export class SnapshotRenderer {
// Best-effort Electron support: rewrite custom protocol in url() links in stylesheets.
// Old snapshotter was sending lower-case.
if (parentTag === 'STYLE' || parentTag === 'style')
- result.push(rewriteURLsInStyleSheetForCustomProtocol(n));
+ result.push(escapeURLsInStyleSheet(rewriteURLsInStyleSheetForCustomProtocol(n)));
else
result.push(escapeHTML(n));
return;
@@ -618,6 +618,21 @@ function rewriteURLsInStyleSheetForCustomProtocol(text: string): string {
});
}
+// url() inside a <style> tag can mess up with html parsing, so we encode some of them.
+// As an example, the following url will close the </style> tag:
+// url('data:image/svg+xml,<svg><defs><style>.a{fill:none}</style></defs><g class="a"></g></svg>')
+const urlToEscapeRegex1 = /url\(\s*'([^']*)'\s*\)/ig;
+const urlToEscapeRegex2 = /url\(\s*"([^"]*)"\s*\)/ig;
+function escapeURLsInStyleSheet(text: string): string {
+ const replacer = (match: string, url: string) => {
+ // Conservatively encode only urls with a closing tag.
+ if (url.includes('</'))
+ return match.replace(url, encodeURI(url));
+ return match;
+ };
+ return text.replace(urlToEscapeRegex1, replacer).replace(urlToEscapeRegex2, replacer);
+}
+
// <base>/snapshot.html?r=<snapshotUrl> is used for "pop out snapshot" feature.
export function unwrapPopoutUrl(url: string) {
const u = new URL(url);
diff --git a/tests/library/trace-viewer.spec.ts b/tests/library/trace-viewer.spec.ts
index 5e795b8913..039d9cbc05 100644
--- a/tests/library/trace-viewer.spec.ts
+++ b/tests/library/trace-viewer.spec.ts
@@ -1840,3 +1840,39 @@ test("shouldn't render not-blob trace received from message", async ({ showTrace
await expect(traceViewer.page.locator('.drop-target')).toBeVisible();
await expect(traceViewer.actionTitles).not.toBeVisible();
});
+
+test('should not trip over complex urls in style tags', {
+ annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/35681' },
+}, async ({ runAndTrace, page }) => {
+ const traceViewer = await runAndTrace(async () => {
+ await page.setContent(`
+ <html>
+ <head>
+ <style>
+ </style>
+ </head>
+ <body>
+ <div>hello</div>
+ <span>world</span>
+ </body>
+ </html>
+ `);
+ const css = `
+ div {
+ background-image: url( 'data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><defs><style>.cls-1{fill:none}</style></defs><title>Custom Title</title><polyline class="cls-1" points="1 2 3 4"/></svg>');
+ color: rgb(1, 2, 3);
+ }
+ span {
+ color: rgb(4, 5, 6);
+ background-image: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'><defs><style>.cls-1{fill:none}</style></defs><title>Custom Title</title><polyline class='cls-1' points='1 2 3 4'/></svg>" );
+ }
+ `;
+ await page.evaluate(css => document.querySelector('style').textContent = css, css);
+ });
+
+ const snapshot = await traceViewer.snapshotFrame('page.evaluate');
+ // Just "hello world", no gibberish from the <style>.
+ await expect(snapshot.locator('body')).toHaveText('hello world');
+ await expect(snapshot.locator('div')).toHaveCSS('color', 'rgb(1, 2, 3)');
+ await expect(snapshot.locator('span')).toHaveCSS('color', 'rgb(4, 5, 6)');
+});