diff --git a/src/sandbox/__tests__/proxySandbox.test.ts b/src/sandbox/__tests__/proxySandbox.test.ts index fb0242b..4b22118 100644 --- a/src/sandbox/__tests__/proxySandbox.test.ts +++ b/src/sandbox/__tests__/proxySandbox.test.ts @@ -347,3 +347,16 @@ it('should return true while [[GetPrototypeOf]] invoked by proxy object', () => expect(Reflect.getPrototypeOf(proxy)).toBe(Reflect.getPrototypeOf(window)); expect(Reflect.getPrototypeOf(proxy)).toBe(Object.getPrototypeOf(window)); }); + +it('native window function calling should always be bound with window', () => { + const { proxy } = new ProxySandbox('mustBeBoundWithWindowReference'); + proxy.nativeWindowFunction = function nativeWindowFunction(this: any) { + if (this !== undefined && this !== window) { + throw new Error('Illegal Invocation!'); + } + + return 'success'; + }; + + expect(proxy.nativeWindowFunction()).toBe('success'); +}); diff --git a/src/sandbox/common.ts b/src/sandbox/common.ts index 4e8633b..8366d10 100644 --- a/src/sandbox/common.ts +++ b/src/sandbox/common.ts @@ -16,7 +16,8 @@ export function setCurrentRunningSandboxProxy(proxy: WindowProxy | null) { export function getTargetValue(target: any, value: any): any { /* - 仅绑定 isCallable && !isBoundedFunction && !isConstructable 的函数对象,如 window.console、window.atob 这类。目前没有完美的检测方式,这里通过 prototype 中是否还有可枚举的拓展方法的方式来判断 + 仅绑定 isCallable && !isBoundedFunction && !isConstructable 的函数对象,如 window.console、window.atob 这类,不然微应用中调用时会抛出 Illegal invocation 异常 + 目前没有完美的检测方式,这里通过 prototype 中是否还有可枚举的拓展方法的方式来判断 @warning 这里不要随意替换成别的判断方式,因为可能触发一些 edge case(比如在 lodash.isFunction 在 iframe 上下文中可能由于调用了 top window 对象触发的安全异常) */ if (isCallable(value) && !isBoundedFunction(value) && !isConstructable(value)) {