From 9f4fbf7ec033004c9281bc22564802ff63840b0d Mon Sep 17 00:00:00 2001 From: maoxiaoke Date: Fri, 25 Mar 2022 22:21:56 +0800 Subject: [PATCH 1/3] =?UTF-8?q?fix:=20=F0=9F=90=9B=20simply=20copy=20calla?= =?UTF-8?q?ble=20funtions's=20extra=20properties?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/sandbox/CHANGELOG.md | 4 ++++ packages/sandbox/__tests__/index.spec.ts | 22 ++++++++++++++++++++++ packages/sandbox/package.json | 2 +- packages/sandbox/src/index.ts | 13 +++++++++++-- 4 files changed, 38 insertions(+), 3 deletions(-) diff --git a/packages/sandbox/CHANGELOG.md b/packages/sandbox/CHANGELOG.md index f815e66e..45f3341e 100644 --- a/packages/sandbox/CHANGELOG.md +++ b/packages/sandbox/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 1.1.3 + +- [fix] simply copy callable funtions's extra properties. + ## 1.1.2 - [fix] hijacked eventListener were not been removed after sandbox unload. ([#295](https://github.com/ice-lab/icestark/issues/295)) diff --git a/packages/sandbox/__tests__/index.spec.ts b/packages/sandbox/__tests__/index.spec.ts index eb5f76e2..5e5f3ed0 100644 --- a/packages/sandbox/__tests__/index.spec.ts +++ b/packages/sandbox/__tests__/index.spec.ts @@ -129,3 +129,25 @@ describe('eval in sandbox', () => { }); }); +describe('callable functions in sandbox', () => { + const sandbox = new Sandbox({ multiMode: true }); + + test.only('callable function with extra properties', () => { + // @ts-ignore + window.axios = function(){}; + // @ts-ignore + axios.create = function(){}; + let error = null; + try { + sandbox.execScriptInSandbox( + ` + axios.create(); + `, + ); + } catch (e) { + error = e.message; + } + + expect(error).toBe(null); + }); +}); diff --git a/packages/sandbox/package.json b/packages/sandbox/package.json index 1349b93f..9433e009 100644 --- a/packages/sandbox/package.json +++ b/packages/sandbox/package.json @@ -1,6 +1,6 @@ { "name": "@ice/sandbox", - "version": "1.1.3", + "version": "1.1.4", "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 6f0b81c8..b91cf98e 100644 --- a/packages/sandbox/src/index.ts +++ b/packages/sandbox/src/index.ts @@ -153,8 +153,17 @@ export default class Sandbox { } if (isWindowFunction(value)) { - // fix Illegal invocation - return value.bind(originalWindow); + // When run into some window's functions, such as `console.table`, + // an illegal invocation exception is thrown. + const boundValue = value.bind(originalWindow); + + // Axios, Moment, and other callable functions may have additional properties. + // Simply copy them into boundValue. + for (const key in value) { + boundValue[key] = value[key]; + } + + return boundValue; } else { // case of window.clientWidth、new window.Object() return value; From b76c21f88f49f116f2fe2200a58e7ce73e4a6449 Mon Sep 17 00:00:00 2001 From: maoxiaoke Date: Mon, 28 Mar 2022 11:17:26 +0800 Subject: [PATCH 2/3] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20free=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/sandbox/CHANGELOG.md | 3 ++- packages/sandbox/__tests__/index.spec.ts | 2 +- packages/sandbox/package.json | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/sandbox/CHANGELOG.md b/packages/sandbox/CHANGELOG.md index 45f3341e..0526bbe6 100644 --- a/packages/sandbox/CHANGELOG.md +++ b/packages/sandbox/CHANGELOG.md @@ -1,6 +1,6 @@ # Changelog -## 1.1.3 +## 1.1.4 - [fix] simply copy callable funtions's extra properties. @@ -13,6 +13,7 @@ ## 1.1.1 - [fix] falsy values except `undefined` would be trapped by proxy window. ([#156](https://github.com/ice-lab/icestark/issues/156)) + ## 1.1.0 - [feat] mark access to all properties added to local window by using method `getAddedProperties`. diff --git a/packages/sandbox/__tests__/index.spec.ts b/packages/sandbox/__tests__/index.spec.ts index 5e5f3ed0..fea1ee99 100644 --- a/packages/sandbox/__tests__/index.spec.ts +++ b/packages/sandbox/__tests__/index.spec.ts @@ -132,7 +132,7 @@ describe('eval in sandbox', () => { describe('callable functions in sandbox', () => { const sandbox = new Sandbox({ multiMode: true }); - test.only('callable function with extra properties', () => { + test('callable function with extra properties', () => { // @ts-ignore window.axios = function(){}; // @ts-ignore diff --git a/packages/sandbox/package.json b/packages/sandbox/package.json index 9433e009..2b788e61 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.4-0", "description": "sandbox for execute scripts", "main": "lib/index.js", "scripts": { From a6b196fac51c9dc701770580ba974d67eb975155 Mon Sep 17 00:00:00 2001 From: maoxiaoke Date: Mon, 28 Mar 2022 15:00:43 +0800 Subject: [PATCH 3/3] =?UTF-8?q?fix:=20=F0=9F=90=9B=20version?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/sandbox/package.json | 2 +- pnpm-lock.yaml | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/sandbox/package.json b/packages/sandbox/package.json index 2b788e61..9433e009 100644 --- a/packages/sandbox/package.json +++ b/packages/sandbox/package.json @@ -1,6 +1,6 @@ { "name": "@ice/sandbox", - "version": "1.1.4-0", + "version": "1.1.4", "description": "sandbox for execute scripts", "main": "lib/index.js", "scripts": { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 506ea685..60c2abf6 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -74,7 +74,7 @@ importers: typescript: ^4.3.5 url-parse: ^1.1.9 dependencies: - '@ice/sandbox': link:../sandbox + '@ice/sandbox': 1.1.3 lodash.isempty: 4.4.0 lodash.isequal: 4.5.0 path-to-regexp: 1.8.0 @@ -106,7 +106,7 @@ importers: react-dom: ^16.13.1 typescript: ^3.8.3 dependencies: - '@ice/sandbox': link:../sandbox + '@ice/sandbox': 1.1.3 devDependencies: react: 16.14.0 react-dom: 16.14.0_react@16.14.0 @@ -625,6 +625,10 @@ packages: resolution: {integrity: sha1-tSBSnsIdjllFoYUd/Rwy6U45/0U=, tarball: '@humanwhocodes/object-schema/download/@humanwhocodes/object-schema-1.2.1.tgz'} dev: true + /@ice/sandbox/1.1.3: + resolution: {integrity: sha512-qC+1wZXLVPmneOuQ4nlMDhnmcq9L/JZFEahsyktZ0DveuWkhomREoM5fodGsyDpwDXregywEhzGatcU55AOtWw==} + dev: false + /@iceworks/eslint-plugin-best-practices/0.2.10_b0505e73fb80a469b12939b6b7fdc1f5: resolution: {integrity: sha512-5VyGibvZ9bkwBnijjG1oSo+iAuMi7dxlPUmPDiG2sYsENjQueMLjh7E180a7Ija2n7dWYRWhFpsAP/c93W8gcA==, tarball: '@iceworks/eslint-plugin-best-practices/download/@iceworks/eslint-plugin-best-practices-0.2.10.tgz'} dependencies: @@ -3136,7 +3140,7 @@ packages: dev: true /fsevents/1.2.13: - resolution: {integrity: sha1-8yXLBFVZJCi88Rs4M3DvcOO/zDg=, tarball: fsevents/download/fsevents-1.2.13.tgz} + resolution: {integrity: sha512-oWb1Z6mkHIskLzEJ/XWX0srkpkTQ7vaopMQkyaEIoq0fmtFVxOthb8cCxeT+p3ynTdkk/RZwbgG4brR5BeWECw==} engines: {node: '>= 4.0'} os: [darwin] deprecated: fsevents 1 will break on node v14+ and could be using insecure binaries. Upgrade to fsevents 2.