Repository: ice-lab/icestark
Pull request #580 (2022-04-25 06:59:30)

Revert "Feat/sandbox inject custom proxy (#577)"
This reverts commit a44977a286864ca50ce09ffd908406c0f75b71ba. closes https://github.com/ice-lab/icestark/issues/192


Patch:
diff --git a/packages/sandbox/CHANGELOG.md b/packages/sandbox/CHANGELOG.md
index 0526bbe..6fc1735 100644
--- a/packages/sandbox/CHANGELOG.md
+++ b/packages/sandbox/CHANGELOG.md
@@ -1,5 +1,9 @@
 # Changelog
 
+## 1.1.5
+
+- [fix] exposes `injection` through constructor.
+
 ## 1.1.4
 
 - [fix] simply copy callable funtions's extra properties.
diff --git a/packages/sandbox/package.json b/packages/sandbox/package.json
index 9433e00..2fcf808 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": {
diff --git a/packages/sandbox/src/index.ts b/packages/sandbox/src/index.ts
index b91cf98..34a5fe8 100644
--- a/packages/sandbox/src/index.ts
+++ b/packages/sandbox/src/index.ts
@@ -1,5 +1,6 @@
 export interface SandboxProps {
   multiMode?: boolean;
+  injection?: Record<PropertyKey, any>;
 }
 
 export interface SandboxConstructor {
@@ -33,6 +34,8 @@ export default class Sandbox {
 
   private multiMode = false;
 
+  private injection = {};
+
   private eventListeners = {};
 
   private timeoutIds: number[] = [];
@@ -46,7 +49,7 @@ export default class Sandbox {
   public sandboxDisabled: boolean;
 
   constructor(props: SandboxProps = {}) {
-    const { multiMode } = props;
+    const { multiMode, injection } = props;
     if (!window.Proxy) {
       console.warn('proxy sandbox is not support by current browser');
       this.sandboxDisabled = true;
@@ -54,8 +57,13 @@ export default class Sandbox {
     // enable multiMode in case of create mulit sandbox in same time
     this.multiMode = multiMode;
     this.sandbox = null;
+    this.injection = injection;
   }
 
+  /**
+   * create proxy sandbox
+   * @param injection @deprecated will be deprecated in the future
+   */
   createProxySandbox(injection?: object) {
     const { propertyAdded, originalValues, multiMode } = this;
     const proxyWindow = Object.create(null) as Window;
@@ -65,6 +73,9 @@ export default class Sandbox {
     const originalSetInterval = window.setInterval;
     const originalSetTimeout = window.setTimeout;
 
+    // `this` in Proxy traps will retarget to the trap.
+    const _self = this;
+
     // hijack addEventListener
     proxyWindow.addEventListener = (eventName, fn, ...rest) => {
       this.eventListeners[eventName] = (this.eventListeners[eventName] || []);
@@ -135,7 +146,8 @@ export default class Sandbox {
         }
 
         // search from injection
-        const injectionValue = injection && injection[p];
+        const injectionValue = _self.injection?.[p] ?? injection?.[p];
+
         if (injectionValue) {
           return injectionValue;
         }
@@ -159,6 +171,7 @@ export default class Sandbox {
 
           // Axios, Moment, and other callable functions may have additional properties.
           // Simply copy them into boundValue.
+          // eslint-disable-next-line guard-for-in
           for (const key in value) {
             boundValue[key] = value[key];
           }


Test patch:
diff --git a/packages/sandbox/__tests__/index.spec.ts b/packages/sandbox/__tests__/index.spec.ts
index fea1ee9..06d6c6a 100644
--- a/packages/sandbox/__tests__/index.spec.ts
+++ b/packages/sandbox/__tests__/index.spec.ts
@@ -151,3 +151,31 @@ describe('callable functions in sandbox', () => {
     expect(error).toBe(null);
   });
 });
+
+describe('inject value to sandbox', () => {
+  const testObject = { a: 1 };
+  const testProxy = new Proxy(testObject, {
+    get(target, p, receiver) {
+        if(p === 'a') {
+          return 2;
+        }
+        return Reflect.get(target, p);
+    }
+  })
+  // in order to pass value outof sandbox to validate, set multiMode false 
+  const sandbox = new Sandbox({multiMode: false, injection: {testObject: testProxy}});
+  test('inject proxy object in sandbox', () => {
+    // @ts-ignore
+    window.testObject = testObject;
+    // @ts-ignore
+    window.result = undefined;
+    sandbox.execScriptInSandbox(
+      `
+        window.result = window.testObject.a;
+      `,
+    );
+
+    // @ts-ignore
+    expect(window.result).toBe(2);
+  })
+})