Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 🐛 simply copy callable funtions' extra properties #557

Merged
merged 3 commits into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/sandbox/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.1.4

- [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))
Expand All @@ -9,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`.
Expand Down
22 changes: 22 additions & 0 deletions packages/sandbox/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,25 @@ describe('eval in sandbox', () => {
});
});

describe('callable functions in sandbox', () => {
const sandbox = new Sandbox({ multiMode: true });

test('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);
});
});
2 changes: 1 addition & 1 deletion packages/sandbox/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
13 changes: 11 additions & 2 deletions packages/sandbox/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 7 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.