From c3a5f810ce493bf5863e150ab9b2c1906db12d0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=A5=87?= <16399091+binaricat@users.noreply.github.com> Date: Mon, 26 Oct 2026 14:46:30 +0000 Subject: [PATCH] fix(plugin-ai): validate context references before publishing turns --- electron/plugins/ai/modelInvocationBroker.cjs | 13 ++++++++--- .../plugins/ai/modelInvocationBroker.test.cjs | 22 +++++++++++++++++++ .../context/selectedContextAuthority.cjs | 6 ++++- .../context/selectedContextAuthority.test.cjs | 10 +++++++++ 4 files changed, 47 insertions(+), 4 deletions(-) diff --git a/electron/plugins/ai/modelInvocationBroker.cjs b/electron/plugins/ai/modelInvocationBroker.cjs index 23d98eaf5..7c339657b 100644 --- a/electron/plugins/ai/modelInvocationBroker.cjs +++ b/electron/plugins/ai/modelInvocationBroker.cjs @@ -105,12 +105,20 @@ class ModelInvocationBroker { || contexts.length > this.limits.contexts || Buffer.byteLength(prompt) > this.limits.inputBytes) { throw fail("invalidArgument", "Invalid model input"); } + const references = contexts.map((reference) => { + if (!reference || Array.isArray(reference) || Object.keys(reference).some((key) => !["handle", "version"].includes(key)) + || typeof reference.handle !== "string" || !reference.handle || reference.handle.length > 128 + || typeof reference.version !== "string" || !reference.version || reference.version.length > 128) { + throw fail("invalidArgument", "Invalid context reference"); + } + return Object.freeze({ handle: reference.handle, version: reference.version }); + }); const chat = this.chats.get(chatHandle); if (!chat) throw fail("permissionDenied", "Plugin chat is unavailable"); await this.#check(chat, context); if (chat.turn) throw fail("resourceExhausted", "A turn is already active"); const controller = new AbortController(); - const turn = { handle: randomUUID(), chat, controller, reading: false, bytes: 0, sequence: 0, iterator: null, references: structuredClone(contexts) }; + const turn = { handle: randomUUID(), chat, controller, reading: false, bytes: 0, sequence: 0, iterator: null, references: Object.freeze(references) }; chat.turn = turn; this.turns.set(turn.handle, turn); const abort = () => this.#end(turn, fail("cancelled", "Model turn cancelled")); @@ -121,8 +129,7 @@ class ModelInvocationBroker { await this.#guard(turn, context); const selected = []; let bytes = Buffer.byteLength(prompt); - for (const reference of contexts) { - if (!reference || Object.keys(reference).some((key) => !["handle", "version"].includes(key))) throw fail("invalidArgument", "Invalid context reference"); + for (const reference of references) { const result = await abortable(this.contexts.read(reference, { ...context, signal: controller.signal }), controller.signal); bytes += Buffer.byteLength(result.text); if (bytes > this.limits.inputBytes) throw fail("resourceExhausted", "Model input byte budget exceeded"); diff --git a/electron/plugins/ai/modelInvocationBroker.test.cjs b/electron/plugins/ai/modelInvocationBroker.test.cjs index 2a05220e1..86b1465c7 100644 --- a/electron/plugins/ai/modelInvocationBroker.test.cjs +++ b/electron/plugins/ai/modelInvocationBroker.test.cjs @@ -187,3 +187,25 @@ test("event and handle caps prevent cheap empty streams and unbounded allocation assert.equal((await f.broker.read(turn.turnHandle, f.context)).text, ""); await assert.rejects(f.broker.read(turn.turnHandle, f.context), (e) => e.code === RPC_ERRORS.resourceExhausted); f.broker.shutdown(); }); + +test("malformed context cannot publish a turn or interrupt two-handle runtime revocation", async (t) => { + const entered = deferred(); const resume = deferred(); let checks = 0; + const f = fixture({ model: { isCurrent: async () => { if (++checks === 3) { entered.resolve(); await resume.promise; } return true; } } }); + t.after(() => { resume.resolve(); f.broker.shutdown(); }); + const selection = { scope: "note", title: "Synthetic", version: "1", readText: () => "Synthetic", isCurrent: () => true }; + await f.contexts.share(selection, f.context); await f.contexts.share(selection, f.context); + const chat = await f.broker.open(f.context); + const start = f.broker.start({ chatHandle: chat.chatHandle, prompt: "Hi", contexts: [null] }, f.context); + const outcome = start.then(() => "accepted", (error) => error); + // On the old implementation, reach the reported post-publication model guard. + // On the fixed implementation, validation rejects before that await exists. + await Promise.race([entered.promise, outcome]); + assert.doesNotThrow(() => f.contexts.releaseRuntime("r1")); + assert.equal(f.contexts.handles.size, 0); + resume.resolve(); + const error = await outcome; + assert.equal(error.code, RPC_ERRORS.invalidArgument); + assert.equal(f.broker.turns.size, 0); + assert.equal(f.calls.length, 0); + f.broker.shutdown(); +}); diff --git a/electron/plugins/context/selectedContextAuthority.cjs b/electron/plugins/context/selectedContextAuthority.cjs index d65787ff2..63bce9dad 100644 --- a/electron/plugins/context/selectedContextAuthority.cjs +++ b/electron/plugins/context/selectedContextAuthority.cjs @@ -147,7 +147,11 @@ class SelectedContextAuthority { } release(handle) { if (!this.handles.delete(handle)) return; - for (const listener of this.listeners) listener(handle); + for (const listener of [...this.listeners]) { + // Revocation must finish even when an observer fails. Never log selected + // text or let an observer exception retain another runtime handle. + try { listener(handle); } catch {} + } } releaseRuntime(runtimeId) { for (const [handle, entry] of this.handles) if (entry.runtimeId === runtimeId) this.release(handle); diff --git a/electron/plugins/context/selectedContextAuthority.test.cjs b/electron/plugins/context/selectedContextAuthority.test.cjs index 35d7bd2ff..afb4df8e1 100644 --- a/electron/plugins/context/selectedContextAuthority.test.cjs +++ b/electron/plugins/context/selectedContextAuthority.test.cjs @@ -75,3 +75,13 @@ test("content reader has a deadline and respects request cancellation", async () const controller = new AbortController(); const reading = authority.read(note, { ...context, signal: controller.signal }); controller.abort(); await assert.rejects(reading); authority.shutdown(); }); + +test("one failing invalidation listener cannot interrupt other listeners or handle cleanup", async () => { + const f = fixture(); const first = await f.authority.share(f.selection, f.context); const second = await f.authority.share(f.selection, f.context); + const notified = []; + f.authority.onDidInvalidate(() => { throw Error("listener failed"); }); + f.authority.onDidInvalidate((handle) => notified.push(handle)); + assert.doesNotThrow(() => f.authority.releaseRuntime("r1")); + assert.equal(f.authority.handles.size, 0); + assert.deepEqual(notified, [first.handle, second.handle]); +});