Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions packages/isomorphic/trace/snapshotRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ export class SnapshotRenderer {
// we will be using the currentSrc instead.
attrName = '_' + attrName;
}
if (upperName === 'TEMPLATE' && (attr.toLowerCase() === 'shadowrootmode' || attr.toLowerCase() === 'shadowroot')) {
// The capture side never records declarative shadow roots. A closed shadow root
// would hide its links from the click listener in the snapshot script.
attrName = '_' + attr;
}
if (hasUnsafeHttpEquiv && (attr.toLowerCase() === 'http-equiv' || attr.toLowerCase() === 'content')) {
// Neutralize the META directive by renaming the attribute so the browser ignores it.
attrName = '_' + attr;
Expand Down Expand Up @@ -329,6 +334,17 @@ function snapshotScript(viewport: ViewportSize, ...targetIds: (string | undefine
function applyPlaywrightAttributes(blankSnapshotUrl: string, viewport: ViewportSize, ...targetIds: (string | undefined)[]) {
// eslint-disable-next-line no-restricted-globals
const win = window;

// The snapshot must not navigate: the snapshot content could open a page from the trace viewer origin.
// Install this listener first, so that snapshot content that makes the code below throw cannot skip it.
// A middle click sends 'auxclick' and opens the link in a new tab.
for (const eventName of ['click', 'auxclick']) {
win.addEventListener(eventName, event => {
if (event.composedPath().some(target => (target as Element).localName === 'a' || (target as Element).localName === 'area'))
event.preventDefault();
}, true);
}

const searchParams = new URLSearchParams(win.location.search);
const shouldPopulateCanvasFromScreenshot = searchParams.has('shouldPopulateCanvasFromScreenshot');
const isUnderTest = searchParams.has('isUnderTest');
Expand Down Expand Up @@ -444,8 +460,13 @@ function snapshotScript(viewport: ViewportSize, ...targetIds: (string | undefine
const body = root.querySelector(`body[__playwright_custom_elements__]`);
if (body && win.customElements) {
const customElements = (body.getAttribute('__playwright_custom_elements__') || '').split(',');
for (const elementName of customElements)
win.customElements.define(elementName, class extends HTMLElement {});
for (const elementName of customElements) {
try {
win.customElements.define(elementName, class extends HTMLElement {});
} catch {
// The snapshot can have names that are not valid custom element names.
}
}
}
}

Expand All @@ -457,9 +478,6 @@ function snapshotScript(viewport: ViewportSize, ...targetIds: (string | undefine
visit(shadowRoot);
}

for (const element of root.querySelectorAll('a'))
element.addEventListener('click', event => { event.preventDefault(); });

if ('adoptedStyleSheets' in (root as any)) {
const adoptedSheets: CSSStyleSheet[] = [...(root as any).adoptedStyleSheets];
for (const element of root.querySelectorAll(`template[__playwright_style_sheet_]`)) {
Expand Down
4 changes: 2 additions & 2 deletions packages/isomorphic/trace/snapshotServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ export class SnapshotServer {
status: 200,
headers: {
'Content-Type': 'text/html; charset=utf-8',
// Only allow our own bootstrap script.
'Content-Security-Policy': `script-src 'nonce-${renderedSnapshot.scriptNonce}'; object-src 'none'`,
// Only allow our own bootstrap script. Do not submit forms: the popout window has no iframe sandbox.
'Content-Security-Policy': `script-src 'nonce-${renderedSnapshot.scriptNonce}'; object-src 'none'; form-action 'none'`,
},
});
}
Expand Down
8 changes: 8 additions & 0 deletions tests/library/snapshot-renderer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ test('TV-128 snapshot renderer neutralizes iframe sandbox', () => {
expect(html).toContain('__playwright_sandbox__');
});

test('TV-137 snapshot renderer neutralizes declarative shadow roots', () => {
const renderer = new SnapshotRenderer(new LRUCache(1_000_000), [], [makeSnapshot({
html: ['HTML', {}, ['BODY', {}, ['DIV', {}, ['TEMPLATE', { 'shadowrootmode': 'closed', 'SHADOWROOT': 'closed' }, ['A', { href: 'x' }, 'link']]]]],
})], [], 0);
const { html } = renderer.render();
expect(html).toContain('<TEMPLATE _shadowrootmode="closed" _SHADOWROOT="closed">');
});

test('TV-129 snapshot renderer neutralizes object data attribute', () => {
const renderer = new SnapshotRenderer(new LRUCache(1_000_000), [], [makeSnapshot({
html: ['HTML', {}, ['BODY', {}, ['OBJECT', { 'data': '/sha1/malicious', 'type': 'text/html' }]]],
Expand Down
17 changes: 14 additions & 3 deletions tests/library/trace-viewer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2598,10 +2598,15 @@ test('TV-110 should survive service worker restart', async ({ page, runAndTrace,
test('TV-111 should not navigate on anchor clicks', async ({ runAndTrace, page, server }) => {
const url = server.EMPTY_PAGE;
const traceViewer = await runAndTrace(async () => {
// An invalid custom element name must not stop the snapshot script before it guards the links.
await page.setContent(`
<a href="${url}">link1</a>
<a href="${url}" target="_blank">link2</a>
<a href="${url}" target="_top">link3</a>
<body __playwright_custom_elements__="not a valid name">
<a href="${url}">link1</a>
<a href="${url}" target="_blank">link2</a>
<a href="${url}" target="_top">link3</a>
<map name="map"><area href="${url}" alt="link4" shape="default"></map>
<img usemap="#map" width="100" height="100">
</body>
`);
});
const snapshot = await traceViewer.snapshotFrame('Set content');
Expand All @@ -2613,6 +2618,8 @@ test('TV-111 should not navigate on anchor clicks', async ({ runAndTrace, page,
newPageOpened = true;
});
await snapshot.getByRole('link', { name }).click();
// A middle click opens a link in a new tab.
await snapshot.getByRole('link', { name }).click({ button: 'middle' });
await traceViewer.page.waitForTimeout(500);
await expect(traceViewer.page).not.toHaveURL(url, { timeout: 500 });
expect(frame.url()).not.toBe(url);
Expand All @@ -2622,6 +2629,10 @@ test('TV-111 should not navigate on anchor clicks', async ({ runAndTrace, page,
await checkLink('link1');
await checkLink('link2');
await checkLink('link3');

await snapshot.locator('area').dispatchEvent('click');
await traceViewer.page.waitForTimeout(500);
expect(frame.url()).not.toBe(url);
});

test('TV-112 should respect CSSOM changes', async ({ runAndTrace, page, server }) => {
Expand Down
1 change: 1 addition & 0 deletions tests/registry.md
Original file line number Diff line number Diff line change
Expand Up @@ -5255,6 +5255,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 neutralizes declarative shadow roots |

### TZ — Timezone

Expand Down
Loading