fix(tracing): overwrite attr value specifies charset other than utf-8 (#10848)

This commit is contained in:
musou1500 2021-12-13 07:54:00 +09:00 committed by GitHub
parent 8b5e146b90
commit 81ab6b3fde
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 1 deletions

View file

@ -196,6 +196,24 @@ export function frameSnapshotStreamer(snapshotStreamer: string) {
visitNode(this._fakeBase);
}
private __sanitizeMetaAttribute(name: string, value: string, httpEquiv: string) {
if (name === 'charset')
return 'utf-8';
if (httpEquiv.toLowerCase() !== 'content-type' || name !== 'content')
return value;
const [type, ...params] = value.split(';');
if (type !== 'text/html' || params.length <= 0)
return value;
const charsetParamIdx = params.findIndex(param => param.trim().startsWith('charset='));
if (charsetParamIdx > -1)
params[charsetParamIdx] = 'charset=utf-8';
return `${type}; ${params.join('; ')}`;
}
private _sanitizeUrl(url: string): string {
if (url.startsWith('javascript:'))
return '';
@ -420,7 +438,9 @@ export function frameSnapshotStreamer(snapshotStreamer: string) {
if (nodeName === 'IFRAME' && (name === 'src' || name === 'sandbox'))
continue;
let value = element.attributes[i].value;
if (name === 'src' && (nodeName === 'IMG'))
if (nodeName === 'META')
value = this.__sanitizeMetaAttribute(name, value, (node as HTMLMetaElement).httpEquiv);
else if (name === 'src' && (nodeName === 'IMG'))
value = this._sanitizeUrl(value);
else if (name === 'srcset' && (nodeName === 'IMG'))
value = this._sanitizeSrcSet(value);

View file

@ -103,6 +103,20 @@ it.describe('snapshots', () => {
expect(distillSnapshot(snapshot)).toBe('<!DOCTYPE foo>hi');
});
it('should replace meta charset attr that specifies charset', async ({ page, server, toImpl, snapshotter }) => {
await page.goto(server.EMPTY_PAGE);
await page.setContent('<meta charset="shift-jis" />');
const snapshot = await snapshotter.captureSnapshot(toImpl(page), 'snapshot');
expect(distillSnapshot(snapshot)).toBe('<META charset="utf-8">');
});
it('should replace meta content attr that specifies charset', async ({ page, server, toImpl, snapshotter }) => {
await page.goto(server.EMPTY_PAGE);
await page.setContent('<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">');
const snapshot = await snapshotter.captureSnapshot(toImpl(page), 'snapshot');
expect(distillSnapshot(snapshot)).toBe('<META http-equiv="Content-Type" content="text/html; charset=utf-8">');
});
it('should respect subresource CSSOM change', async ({ page, server, toImpl, snapshotter }) => {
await page.goto(server.EMPTY_PAGE);
await page.route('**/style.css', route => {