From 08f810a28b2c6c09c3012da0c878dee4e832cc88 Mon Sep 17 00:00:00 2001 From: Kuitos Date: Tue, 25 May 2021 11:50:04 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20add=20getPrototypeOf=20trap=20in?= =?UTF-8?q?=20sandbox=20to=20keep=20the=20`window=20instanceof=20Window`?= =?UTF-8?q?=20returns=20true=20in=20microapp=20(#1465)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sandbox/__tests__/proxySandbox.test.ts | 15 +++++++++++++++ src/sandbox/proxySandbox.ts | 5 +++++ 2 files changed, 20 insertions(+) diff --git a/src/sandbox/__tests__/proxySandbox.test.ts b/src/sandbox/__tests__/proxySandbox.test.ts index 1c3694a..fb0242b 100644 --- a/src/sandbox/__tests__/proxySandbox.test.ts +++ b/src/sandbox/__tests__/proxySandbox.test.ts @@ -332,3 +332,18 @@ test('falsy values should return as expected', () => { expect(proxy.nullvar).toBeNull(); expect(proxy.zero).toBe(0); }); + +it('should return true while [[GetPrototypeOf]] invoked by proxy object', () => { + // window.__proto__ not equals window prototype in jest environment + // eslint-disable-next-line no-proto + expect(window.__proto__ === Object.getPrototypeOf(window)).toBeFalsy(); + // we must to set the prototype of window as jest modified window `__proto__` property but not changed it internal [[Prototype]] property + // eslint-disable-next-line no-proto + Object.setPrototypeOf(window, window.__proto__); + + const { proxy } = new ProxySandbox('window-prototype'); + expect(proxy instanceof Window).toBeTruthy(); + expect(Object.getPrototypeOf(proxy)).toBe(Object.getPrototypeOf(window)); + expect(Reflect.getPrototypeOf(proxy)).toBe(Reflect.getPrototypeOf(window)); + expect(Reflect.getPrototypeOf(proxy)).toBe(Object.getPrototypeOf(window)); +}); diff --git a/src/sandbox/proxySandbox.ts b/src/sandbox/proxySandbox.ts index 79c43bc..c46fa7f 100644 --- a/src/sandbox/proxySandbox.ts +++ b/src/sandbox/proxySandbox.ts @@ -332,6 +332,11 @@ export default class ProxySandbox implements SandBox { return true; }, + + // makes sure `window instanceof Window` returns truthy in micro app + getPrototypeOf() { + return Reflect.getPrototypeOf(rawWindow); + }, }); this.proxy = proxy;