From d9502f5795489711398af341b15c697a2eaa5928 Mon Sep 17 00:00:00 2001 From: Kuitos Date: Wed, 5 Jul 2023 15:15:56 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20compatiable=20with=20react=20dev?= =?UTF-8?q?elopment=20for=20event=20rewrite=20scenarios=20(#2545)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sandbox/proxySandbox.ts | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/src/sandbox/proxySandbox.ts b/src/sandbox/proxySandbox.ts index 5649602aa..e69f7a6b0 100644 --- a/src/sandbox/proxySandbox.ts +++ b/src/sandbox/proxySandbox.ts @@ -33,6 +33,8 @@ const variableWhiteListInDev = // for react hot reload // see https://github.com/facebook/create-react-app/blob/66bf7dfc43350249e2f09d138a20840dae8a0a4a/packages/react-error-overlay/src/index.js#L180 '__REACT_ERROR_OVERLAY_GLOBAL_HOOK__', + // for react development event issue, see https://github.com/umijs/qiankun/issues/2375 + 'event', ] : []; // who could escape the sandbox @@ -209,25 +211,26 @@ export default class ProxySandbox implements SandBox { set: (target: FakeWindow, p: PropertyKey, value: any): boolean => { if (this.sandboxRunning) { this.registerRunningApp(name, proxy); - // We must keep its description while the property existed in globalContext before - if (!target.hasOwnProperty(p) && globalContext.hasOwnProperty(p)) { - const descriptor = Object.getOwnPropertyDescriptor(globalContext, p); - const { writable, configurable, enumerable, set } = descriptor!; - // only writable property can be overwritten - // here we ignored accessor descriptor of globalContext as it makes no sense to trigger its logic(which might make sandbox escaping instead) - // we force to set value by data descriptor - if (writable || set) { - Object.defineProperty(target, p, { configurable, enumerable, writable: true, value }); - } - } else { - target[p] = value; - } // sync the property to globalContext if (typeof p === 'string' && globalVariableWhiteList.indexOf(p) !== -1) { this.globalWhitelistPrevDescriptor[p] = Object.getOwnPropertyDescriptor(globalContext, p); // @ts-ignore globalContext[p] = value; + } else { + // We must keep its description while the property existed in globalContext before + if (!target.hasOwnProperty(p) && globalContext.hasOwnProperty(p)) { + const descriptor = Object.getOwnPropertyDescriptor(globalContext, p); + const { writable, configurable, enumerable, set } = descriptor!; + // only writable property can be overwritten + // here we ignored accessor descriptor of globalContext as it makes no sense to trigger its logic(which might make sandbox escaping instead) + // we force to set value by data descriptor + if (writable || set) { + Object.defineProperty(target, p, { configurable, enumerable, writable: true, value }); + } + } else { + target[p] = value; + } } updatedValueSet.add(p); @@ -281,6 +284,11 @@ export default class ProxySandbox implements SandBox { return eval; } + if (p === 'string' && globalVariableWhiteList.indexOf(p) !== -1) { + // @ts-ignore + return globalContext[p]; + } + const actualTarget = propertiesWithGetter.has(p) ? globalContext : p in target ? target : globalContext; const value = actualTarget[p];