fix: eval serialization w/ overridden URL/RegExp/Date (#21112)

Fixes #21109
This commit is contained in:
Yury Semikhatsky 2023-02-22 15:45:42 -08:00 committed by GitHub
parent 000583e048
commit d6dae8ea1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 3 deletions

View File

@ -35,15 +35,27 @@ type VisitorInfo = {
export function source() {
function isRegExp(obj: any): obj is RegExp {
return obj instanceof RegExp || Object.prototype.toString.call(obj) === '[object RegExp]';
try {
return obj instanceof RegExp || Object.prototype.toString.call(obj) === '[object RegExp]';
} catch (error) {
return false;
}
}
function isDate(obj: any): obj is Date {
return obj instanceof Date || Object.prototype.toString.call(obj) === '[object Date]';
try {
return obj instanceof Date || Object.prototype.toString.call(obj) === '[object Date]';
} catch (error) {
return false;
}
}
function isURL(obj: any): obj is URL {
return obj instanceof URL || Object.prototype.toString.call(obj) === '[object URL]';
try {
return obj instanceof URL || Object.prototype.toString.call(obj) === '[object URL]';
} catch (error) {
return false;
}
}
function isError(obj: any): obj is Error {

View File

@ -703,6 +703,25 @@ it('should work with overridden globalThis.Window/Document/Node', async ({ page,
}
});
it('should work with overridden URL/Date/RegExp', async ({ page, server }) => {
const testCases = [
// @ts-ignore
() => globalThis.URL = 'foo',
// @ts-ignore
() => globalThis.RegExp = 'foo',
// @ts-ignore
() => globalThis.Date = 'foo',
];
for (const testCase of testCases) {
await it.step(testCase.toString(), async () => {
await page.goto(server.EMPTY_PAGE);
await page.evaluate(testCase);
expect(await page.evaluate('1+2')).toBe(3);
expect(await page.evaluate(() => ({ 'a': 2023 }))).toEqual({ 'a': 2023 });
});
}
});
it('should expose utilityScript', async ({ page }) => {
const result = await (page.mainFrame() as any)._evaluateExposeUtilityScript((utilityScript, { a }) => {
return { utils: 'parseEvaluationResultValue' in utilityScript, a };