Skip to content

Commit

Permalink
fix(sandbox): optimize window proxy get ice-lab#748
Browse files Browse the repository at this point in the history
  • Loading branch information
apathyjade committed Aug 15, 2024
1 parent 10a882c commit ce10fc9
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions packages/sandbox/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,30 +113,34 @@ export default class Sandbox {
return true;
},
get(target: Window, p: PropertyKey): any {
const targetValue = target[p];
/**
* Falsy value like 0/ ''/ false should be trapped by proxy window.
*/
if (targetValue !== undefined) {
// case of addEventListener, removeEventListener, setTimeout, setInterval setted in sandbox
return targetValue;
}
if (p === Symbol.unscopables) {
return undefined;
}
if (['top', 'window', 'self', 'globalThis'].includes(p as string)) {
// eslint-disable-next-line no-param-reassign
target[p] = sandbox;
return sandbox;
}
// proxy hasOwnProperty, in case of proxy.hasOwnProperty value represented as originalWindow.hasOwnProperty
if (p === 'hasOwnProperty') {
// eslint-disable-next-line no-prototype-builtins
return (key: PropertyKey) => !!target[key] || originalWindow.hasOwnProperty(key);
}

const targetValue = target[p];
/**
* Falsy value like 0/ ''/ false should be trapped by proxy window.
*/
if (targetValue !== undefined) {
// case of addEventListener, removeEventListener, setTimeout, setInterval setted in sandbox
return targetValue;
// eslint-disable-next-line no-param-reassign, no-prototype-builtins
target[p] = (key: PropertyKey) => originalWindow.hasOwnProperty.call(target, key) || originalWindow.hasOwnProperty(key);
return target[p];
}

// search from injection
const injectionValue = injection && injection[p];
if (injectionValue) {
// eslint-disable-next-line no-param-reassign
target[p] = injectionValue;
return injectionValue;
}

Expand All @@ -149,6 +153,8 @@ export default class Sandbox {
* https://262.ecma-international.org/5.1/#sec-10.4.2
*/
if (p === 'eval') {
// eslint-disable-next-line no-param-reassign
target[p] = value;
return value;
}

Expand All @@ -159,15 +165,17 @@ 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];
}

// eslint-disable-next-line no-param-reassign
target[p] = boundValue;
return boundValue;
} else {
// case of window.clientWidth、new window.Object()
return value;
}
// eslint-disable-next-line no-param-reassign
target[p] = value;
return value;
},
has(target: Window, p: PropertyKey): boolean {
return p in target || p in originalWindow;
Expand Down

0 comments on commit ce10fc9

Please sign in to comment.