fix(code-runtime): contain hostile boundary failures

This commit is contained in:
Tianyi Cui
2026-07-29 09:28:50 +08:00
parent f84f63ccd7
commit 71e0eeac40
2 changed files with 26 additions and 1 deletions

View File

@@ -49,7 +49,11 @@ export type RuntimeBindingReply =
* @returns the caller-facing diagnostic text.
*/
export function runtimeErrorMessage(error: unknown): string {
return error instanceof Error ? error.message : String(error)
try {
return error instanceof Error ? error.message : String(error)
} catch {
return 'binding rejected with an unrenderable value'
}
}
/**

View File

@@ -597,6 +597,27 @@ describe('WorkerCodeRuntime — hostile programs (real workers)', () => {
expect(result.value).toEqual({ name: 'ToolCallError', toolName: 'bad', message: 'binding resolution must be lossless JSON' })
})
it('contains binding rejections whose thrown values cannot be rendered', async () => {
const { runtime } = await setup()
const result = await runtime.run({
program: 'try { await tools.bad({}) } catch (error) { return { name: error.name, toolName: error.toolName, message: error.message } }',
bindings: tools({
bad: async () => {
const hostile = new Error('hidden')
Object.defineProperty(hostile, 'message', {
get() { throw new Error('message getter failed') },
})
throw hostile
},
}),
})
expect(result.value).toEqual({
name: 'ToolCallError',
toolName: 'bad',
message: 'binding rejected with an unrenderable value',
})
})
it('rejects lossy binding arguments in the worker before invoking the host binding', async () => {
const { runtime } = await setup()
let calls = 0