Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
63 changes: 50 additions & 13 deletions packages/isomorphic/trace/snapshotRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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 <noscript> will not be rendered by default in the trace viewer, because
// JS is enabled. So rename it to <x-noscript>.
const upperName = name.toUpperCase();
const nodeName = upperName === 'NOSCRIPT' ? 'X-NOSCRIPT' : name;
const attrs = Object.entries(nodeAttrs || {});
result.push('<', nodeName);
if (!result.openTag(nodeName))
return;
const kCurrentSrcAttribute = '__playwright_current_src__';
const isFrame = upperName === 'IFRAME' || upperName === 'FRAME';
const isAnchor = upperName === 'A';
Expand Down Expand Up @@ -176,20 +173,21 @@ export class SnapshotRenderer {
let attrValue = value;
if (!isAnchor && (attr.toLowerCase() === 'href' || attr.toLowerCase() === 'src' || attr === kCurrentSrcAttribute))
attrValue = rewriteURLForCustomProtocol(value);
result.push(' ', attrName, '="', escapeHTMLAttribute(attrValue), '"');
result.attribute(attrName, attrValue);
}
if (upperName === 'STYLE') {
// Style has always exactly one child which is a text node.
const styleContent = typeof children[0] === 'string' ? children[0] : '';
result.push(' ', '__playwright_style_content__', '="', escapeHTMLAttribute(rewriteURLsInStyleSheetForCustomProtocol(styleContent)), '"');
result.push('></', nodeName, '>');
result.attribute('__playwright_style_content__', rewriteURLsInStyleSheetForCustomProtocol(styleContent));
result.endOpenTag();
result.closeTag(nodeName);
return;
}
result.push('>');
result.endOpenTag();
for (const child of children)
visit(child, snapshotIndex, nodeName, attrs);
if (!autoClosing.has(nodeName))
result.push('</', nodeName, '>');
result.closeTag(nodeName);
return;
} else {
// Why are we here? Let's not throw, just in case.
Expand All @@ -211,7 +209,7 @@ export class SnapshotRenderer {
// Hide the document in order to prevent flickering. We will unhide once script has processed shadow.
'<style>*,*::before,*::after { visibility: hidden }</style>',
`<script nonce="${scriptNonce}">${snapshotScript(this.viewport(), this._callId, this.snapshotName)}</script>`
].join('') + result.join('');
].join('') + result.toString();
return { value: { html, scriptNonce }, size: html.length };
});

Expand Down Expand Up @@ -277,6 +275,45 @@ export class SnapshotRenderer {
}
}

// All trace-controlled data goes through this class, so escaping and name checks live in one place.
class HtmlBuilder {
private _chunks: string[] = [];

text(text: string) {
this._chunks.push(escapeHTML(text));
}

openTag(name: string): boolean {
if (!isSafeName(name))
return false;
this._chunks.push('<', name);
return true;
}

attribute(name: string, value: string) {
if (!isSafeName(name))
return;
this._chunks.push(' ', name, '="', escapeHTMLAttribute(value), '"');
Comment thread
yury-s marked this conversation as resolved.
Resolved
}

endOpenTag() {
this._chunks.push('>');
}

closeTag(name: string) {
this._chunks.push('</', name, '>');
}

toString() {
return this._chunks.join('');
}
}

// Reject names that would end the tag or attribute name early. Allow Unicode custom elements such as math-α.
function isSafeName(name: string): boolean {
return !!name && !/[\s=<>\/"']/.test(name);
}

const autoClosing = new Set(['AREA', 'BASE', 'BR', 'COL', 'COMMAND', 'EMBED', 'HR', 'IMG', 'INPUT', 'KEYGEN', 'LINK', 'MENUITEM', 'META', 'PARAM', 'SOURCE', 'TRACK', 'WBR']);

// Whitelist of META http-equiv directives that are safe to render in the trace viewer.
Expand Down
8 changes: 5 additions & 3 deletions tests/library/snapshot-renderer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('<IMG src="x">');
});

test('TV-126 snapshot renderer strips onclick attributes', () => {
Expand Down
Loading