From 12b34b1082db53493cd8ed77c88ae273f799acc7 Mon Sep 17 00:00:00 2001 From: ClarkXia Date: Mon, 1 Aug 2022 18:06:44 +0800 Subject: [PATCH 1/2] chore: version --- packages/sandbox/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/sandbox/package.json b/packages/sandbox/package.json index 9433e009..2fcf8084 100644 --- a/packages/sandbox/package.json +++ b/packages/sandbox/package.json @@ -1,6 +1,6 @@ { "name": "@ice/sandbox", - "version": "1.1.4", + "version": "1.1.5", "description": "sandbox for execute scripts", "main": "lib/index.js", "scripts": { From abd0e63a5e765388603a842aa8c1dcb30064ec72 Mon Sep 17 00:00:00 2001 From: HuColin Date: Mon, 1 Aug 2022 18:08:33 +0800 Subject: [PATCH 2/2] fix: record changed properties in sandbox (#612) Co-authored-by: huhaojie --- packages/sandbox/__tests__/index.spec.ts | 44 ++++++++++++++++++++++++ packages/sandbox/src/index.ts | 13 ++++--- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/packages/sandbox/__tests__/index.spec.ts b/packages/sandbox/__tests__/index.spec.ts index fea1ee99..5c7d27ff 100644 --- a/packages/sandbox/__tests__/index.spec.ts +++ b/packages/sandbox/__tests__/index.spec.ts @@ -151,3 +151,47 @@ describe('callable functions in sandbox', () => { expect(error).toBe(null); }); }); + +describe('mock function cacheDeps', () => { + // mock window + const windowAsAny = (window as any); + test('load second dependency with first dependency in multiMode', () => { + windowAsAny['test'] = { a: 123, b: 456 }; + + let sandbox = new Sandbox({ multiMode: true }); + + sandbox.execScriptInSandbox(` + window['test'] = { a: 234, b: 456 }; + `); + const firstDepPropertyAdded = sandbox.getAddedProperties(); + sandbox.clear(); + sandbox = new Sandbox({ multiMode: true }); + sandbox.createProxySandbox(firstDepPropertyAdded); + sandbox.execScriptInSandbox(` + expect(window.test.a).toBe(234); + `); + sandbox.clear(); + expect(windowAsAny.test.a).toBe(123); + + }); + + test('load second dependency with first dependency in singleMode', () => { + windowAsAny['test'] = { a: 123, b: 456 }; + + let sandbox = new Sandbox({ multiMode: false }); + + sandbox.execScriptInSandbox(` + window['test'] = { a: 234, b: 456 }; + `); + const firstDepPropertyAdded = sandbox.getAddedProperties(); + sandbox.clear(); + sandbox = new Sandbox({ multiMode: true }); + sandbox.createProxySandbox(firstDepPropertyAdded); + sandbox.execScriptInSandbox(` + expect(window.test.a).toBe(234); + `); + sandbox.clear(); + expect(windowAsAny.test.a).toBe(123); + }) + +}) diff --git a/packages/sandbox/src/index.ts b/packages/sandbox/src/index.ts index b91cf98e..cbb57b62 100644 --- a/packages/sandbox/src/index.ts +++ b/packages/sandbox/src/index.ts @@ -103,6 +103,8 @@ export default class Sandbox { } else if (!originalValues.hasOwnProperty(p)) { // if it is already been setted in original window, record it's original value originalValues[p] = originalWindow[p]; + // because target in sandbox will be changed, record the change in propertyAdded, for user can get what has been changed in last-loading + propertyAdded[p] = value; } // set new value to original window in case of jsonp, js bundle which will be execute outof sandbox if (!multiMode) { @@ -214,13 +216,16 @@ export default class Sandbox { // clear timeout this.timeoutIds.forEach((id) => window.clearTimeout(id)); this.intervalIds.forEach((id) => window.clearInterval(id)); - // recover original values - Object.keys(this.originalValues).forEach((key) => { - window[key] = this.originalValues[key]; - }); + // some properties has been in original window, when they're loaded in sandbox + // the change has been recorded in propertyAdded + // delete the modified properties Object.keys(this.propertyAdded).forEach((key) => { delete window[key]; }); + // then recover original values + Object.keys(this.originalValues).forEach((key) => { + window[key] = this.originalValues[key]; + }); } } }