fix: exceptions during function/promise result conversions live in calling world (#37904)

This commit is contained in:
Samuel Attard 2023-04-11 02:57:48 -07:00 committed by GitHub
parent c65632d404
commit c01dff8d89
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 115 additions and 16 deletions

View file

@ -814,10 +814,21 @@ describe('contextBridge', () => {
bad: Object(Symbol('foo'))
};
},
argumentConvert: () => {}
throwDynamic: () => {
return {
get bad () {
throw new Error('damm');
}
};
},
argumentConvert: () => {},
rejectNotClonable: async () => {
throw Object(Symbol('foo'));
},
resolveNotClonable: async () => Object(Symbol('foo'))
});
});
const result = await callWithBindings((root: any) => {
const result = await callWithBindings(async (root: any) => {
const getError = (fn: Function) => {
try {
fn();
@ -826,15 +837,26 @@ describe('contextBridge', () => {
}
return null;
};
const getAsyncError = async (fn: Function) => {
try {
await 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 notClonableNestedArrayIsError = Object.getPrototypeOf(getError(root.example.throwNotClonableNestedArray)) === Error.prototype;
const notClonableNestedObjectIsError = Object.getPrototypeOf(getError(root.example.throwNotClonableNestedObject)) === Error.prototype;
const dynamicIsError = Object.getPrototypeOf(getError(root.example.throwDynamic)) === Error.prototype;
const argumentConvertIsError = Object.getPrototypeOf(getError(() => root.example.argumentConvert(Object(Symbol('test'))))) === Error.prototype;
return [normalIsError, weirdIsError, notClonableIsError, notClonableNestedArrayIsError, notClonableNestedObjectIsError, argumentConvertIsError];
const rejectNotClonableIsError = Object.getPrototypeOf(await getAsyncError(root.example.rejectNotClonable)) === Error.prototype;
const resolveNotClonableIsError = Object.getPrototypeOf(await getAsyncError(root.example.resolveNotClonable)) === Error.prototype;
return [normalIsError, weirdIsError, notClonableIsError, notClonableNestedArrayIsError, notClonableNestedObjectIsError, dynamicIsError, argumentConvertIsError, rejectNotClonableIsError, resolveNotClonableIsError];
});
expect(result).to.deep.equal([true, true, true, true, true, true], 'should all be errors in the current context');
expect(result).to.deep.equal([true, true, true, true, true, true, true, true, true], 'should all be errors in the current context');
});
it('should not leak prototypes', async () => {