🐛 fix nullish bug in proxy sandbox (#1053)

This commit is contained in:
Kuitos 2020-11-05 17:37:04 +08:00 committed by GitHub
parent 3fa008be96
commit 3b98d6d708
2 changed files with 22 additions and 4 deletions

View File

@ -7,6 +7,12 @@ import { isBoundedFunction } from '../../utils';
import { getCurrentRunningSandboxProxy } from '../common'; import { getCurrentRunningSandboxProxy } from '../common';
import ProxySandbox from '../proxySandbox'; import ProxySandbox from '../proxySandbox';
declare global {
interface Window {
[p: string]: any;
}
}
beforeAll(() => { beforeAll(() => {
Object.defineProperty(window, 'mockTop', { value: window, writable: false, configurable: false, enumerable: false }); Object.defineProperty(window, 'mockTop', { value: window, writable: false, configurable: false, enumerable: false });
Object.defineProperty(window, 'mockSafariTop', { Object.defineProperty(window, 'mockSafariTop', {
@ -215,10 +221,8 @@ test('document and eval accessing should modify the attachDocProxySymbol value e
expect(d1).toBe(d2); expect(d1).toBe(d2);
expect(d1).toBe(document); expect(d1).toBe(document);
// @ts-ignore
const eval1 = proxy3.eval; const eval1 = proxy3.eval;
expect(getCurrentRunningSandboxProxy()).toBe(proxy3); expect(getCurrentRunningSandboxProxy()).toBe(proxy3);
// @ts-ignore
const eval2 = proxy4.eval; const eval2 = proxy4.eval;
expect(getCurrentRunningSandboxProxy()).toBe(proxy4); expect(getCurrentRunningSandboxProxy()).toBe(proxy4);
@ -293,3 +297,13 @@ test('some native window property was defined with getter in safari and firefox,
const { proxy } = new ProxySandbox('object-define-property-target-test'); const { proxy } = new ProxySandbox('object-define-property-target-test');
expect((<any>proxy).mockSafariGetterProperty).toBe('getterPropertyInSafariWindow'); expect((<any>proxy).mockSafariGetterProperty).toBe('getterPropertyInSafariWindow');
}); });
test('falsy values should return as expected', () => {
const { proxy } = new ProxySandbox('falsy-value-test');
proxy.falsevar = false;
proxy.nullvar = null;
proxy.zero = 0;
expect(proxy.falsevar).toBe(false);
expect(proxy.nullvar).toBeNull();
expect(proxy.zero).toBe(0);
});

View File

@ -238,8 +238,12 @@ export default class ProxySandbox implements SandBox {
} }
} }
// eslint-disable-next-line no-bitwise // eslint-disable-next-line no-nested-ternary
const value = propertiesWithGetter.has(p) ? (rawWindow as any)[p] : (target as any)[p] || (rawWindow as any)[p]; const value = propertiesWithGetter.has(p)
? (rawWindow as any)[p]
: p in target
? (target as any)[p]
: (rawWindow as any)[p];
return getTargetValue(rawWindow, value); return getTargetValue(rawWindow, value);
}, },