From 6544b17f9b508c304aa4ad11522914890d01621b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=87=A4=E5=AE=9D=28Ted=29?= Date: Thu, 20 Oct 2022 10:55:40 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20proxy=20sandboxshould=20not=20de?= =?UTF-8?q?lete=20untouched=20globals=20(#2310)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sandbox/__tests__/proxySandbox.test.ts | 18 ++++++++++++++++++ src/sandbox/proxySandbox.ts | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/sandbox/__tests__/proxySandbox.test.ts b/src/sandbox/__tests__/proxySandbox.test.ts index b670fedbf..387e4be96 100644 --- a/src/sandbox/__tests__/proxySandbox.test.ts +++ b/src/sandbox/__tests__/proxySandbox.test.ts @@ -502,6 +502,24 @@ describe('variables in whitelist', () => { sandbox.inactive(); expect('__REACT_ERROR_OVERLAY_GLOBAL_HOOK__' in window).toBeFalsy(); }); + + it('should not modify those global context that have not been touched during the app running', () => { + const original = { + iframeReady: function t1() {}, + }; + window.__REACT_ERROR_OVERLAY_GLOBAL_HOOK__ = original; + const sandbox = new ProxySandbox('whitelist-variables'); + const { proxy } = sandbox; + sandbox.active(); + // globals in whitelist + proxy.System = {}; + expect(window.System).toEqual({}); + // regular globals + proxy.someOther = {}; + expect('someOther' in window).toBeFalsy(); + sandbox.inactive(); + expect(window.__REACT_ERROR_OVERLAY_GLOBAL_HOOK__).toBe(original); + }); }); describe('should work with nest sandbox', () => { diff --git a/src/sandbox/proxySandbox.ts b/src/sandbox/proxySandbox.ts index 053b96b5b..9d5a9a365 100644 --- a/src/sandbox/proxySandbox.ts +++ b/src/sandbox/proxySandbox.ts @@ -149,7 +149,7 @@ export default class ProxySandbox implements SandBox { if (process.env.NODE_ENV === 'test' || --activeSandboxCount === 0) { // reset the global value to the prev value - globalVariableWhiteList.forEach((p) => { + Object.keys(this.globalWhitelistPrevDescriptor).forEach((p) => { const descriptor = this.globalWhitelistPrevDescriptor[p]; if (descriptor) { Object.defineProperty(this.globalContext, p, descriptor);