fix(code-runtime-python): cap the unknown-binding preview before JSON.stringify

The reviewer's standing item: the unknown-binding reply ran JSON.stringify on
the WHOLE capped target (global + '.' + name, each up to maxValueBytes code
units), allocating the escaped form — up to ~6x under control-heavy input, a
multi-hundred-MB spike near the maxValueBytes ceiling that no hostile-peer
bound would have admitted. The escaped preview is now built from a 1 KiB
prefix of the target (enough to identify the binding); capMessage still
enforces the reply budget. A forged huge-name case drives the path.
This commit is contained in:
Chinesezjc authored and Tianyi Cui committed 2026-08-31 15:06:50 +08:00
1 parent 6719e287de
commit a6678610b8
2 files changed
+31 -1

No files matched your search

@@ -1676,7 +1676,14 @@ export class PythonCodeRuntime extends CodeRuntime {
// error.
const cap = this.config.maxValueBytes
const target = `${message.global.slice(0, cap)}.${message.name.slice(0, cap)}`
sendReply({ type: 'reply', id: message.id, ok: false, message: capMessage(`unknown binding ${JSON.stringify(target)}`, cap) })
// JSON.stringify on the WHOLE capped target would still allocate
// the escaped form — up to ~6x under control-heavy input, a
// multi-hundred-MB spike near the maxValueBytes ceiling that no
// hostile-peer bound would have admitted. The message only needs
// to identify the binding, so the escaped form is built from a
// 1 KiB prefix; capMessage then enforces the reply budget.
const preview = JSON.stringify(target.slice(0, 1024))
sendReply({ type: 'reply', id: message.id, ok: false, message: capMessage(`unknown binding ${preview}`, cap) })
return
}
void (async () => {