fix: ensure that errors thrown in the context bridge are created in the correct context (#24534)

This commit is contained in:
Samuel Attard 2020-07-23 14:32:38 -07:00 committed by GitHub
parent b500294c1d
commit 5cfe956fe1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 8 deletions

View file

@ -500,6 +500,39 @@ describe('contextBridge', () => {
expect(threw).to.equal(true);
});
it('should copy thrown errors into the other context', async () => {
await makeBindingWindow(() => {
contextBridge.exposeInMainWorld('example', {
throwNormal: () => {
throw new Error('whoops');
},
throwWeird: () => {
throw 'this is no error...'; // eslint-disable-line no-throw-literal
},
throwNotClonable: () => {
return Object(Symbol('foo'));
},
argumentConvert: () => {}
});
});
const result = await callWithBindings((root: any) => {
const getError = (fn: Function) => {
try {
fn();
} catch (e) {
return e;
}
return null;
};
const normalIsError = Object.getPrototypeOf(getError(root.example.throwNormal)) === Error.prototype;
const weirdIsError = Object.getPrototypeOf(getError(root.example.throwWeird)) === Error.prototype;
const notClonableIsError = Object.getPrototypeOf(getError(root.example.throwNotClonable)) === Error.prototype;
const argumentConvertIsError = Object.getPrototypeOf(getError(() => root.example.argumentConvert(Object(Symbol('test'))))) === Error.prototype;
return [normalIsError, weirdIsError, notClonableIsError, argumentConvertIsError];
});
expect(result).to.deep.equal([true, true, true, true], 'should all be errors in the current context');
});
it('should not leak prototypes', async () => {
await makeBindingWindow(() => {
contextBridge.exposeInMainWorld('example', {