From 17b092f2e404882c9705f4bf87fe8d1c6c77b864 Mon Sep 17 00:00:00 2001 From: TIANYAO ZHANG <88520881+ZHANGTIANYAO1@users.noreply.github.com> Date: Wed, 18 Nov 2026 16:53:40 +0800 Subject: [PATCH] fix(teamspeak): fence chat commands to their connection --- src/bot/instance.test.ts | 35 +++++++++++++++++++++++++++++++++++ src/bot/instance.ts | 10 +++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/bot/instance.test.ts b/src/bot/instance.test.ts index 5b02d0f..fcd8a8f 100644 --- a/src/bot/instance.test.ts +++ b/src/bot/instance.test.ts @@ -1975,6 +1975,41 @@ describe("BotInstance — OpenProtocol & command routing", () => { expect(ctx.tsClient.sendTextMessage).toHaveBeenCalledWith("ok:now:"); }); + it("drops a private command reply when the connection changed during execution", async () => { + const ctx = makeGateCtx({}); + ctx.lifecycleGeneration = 1; + const executed = deferred(); + const reply = deferred(); + ctx.executeCommand = vi.fn(() => { + executed.resolve(); + return reply.promise; + }); + + const pending = handleTextMessage.call(ctx, { ...makeMsg("!now"), targetMode: 1 }); + await executed.promise; + ctx.lifecycleGeneration++; + reply.resolve("Prior connection's queue"); + await pending; + + expect(ctx.tsClient.sendTextMessage).not.toHaveBeenCalled(); + }); + + it("drops a command from a previous connection after the group check", async () => { + const ctx = makeGateCtx({ adminGroups: [6] }); + ctx.lifecycleGeneration = 1; + const groups = deferred(); + ctx.tsClient.getClientServerGroups.mockReturnValue(groups.promise); + + const pending = handleTextMessage.call(ctx, { ...makeMsg("!stop"), targetMode: 1 }); + expect(ctx.tsClient.getClientServerGroups).toHaveBeenCalled(); + ctx.lifecycleGeneration++; + groups.resolve(["6"]); + await pending; + + expect(ctx.executeCommand).not.toHaveBeenCalled(); + expect(ctx.tsClient.sendTextMessage).not.toHaveBeenCalled(); + }); + it("routes permission denied and execution errors privately when triggered via DM", async () => { const ctx = makeGateCtx({ adminGroups: [6], lookupGroups: [] }); const dmStop: TS3TextMessage = { diff --git a/src/bot/instance.ts b/src/bot/instance.ts index 484c408..bcb7b8d 100755 --- a/src/bot/instance.ts +++ b/src/bot/instance.ts @@ -830,7 +830,13 @@ export class BotInstance extends EventEmitter { ); if (!parsed) return; - if (!(await this.isCommandAllowed(parsed.name, msg))) { + // A numeric invoker ID only belongs to this TeamSpeak connection. + const generation = this.lifecycleGeneration; + const isCurrent = () => this.lifecycleGeneration === generation; + const allowed = await this.isCommandAllowed(parsed.name, msg); + if (!isCurrent()) return; + + if (!allowed) { this.logger.info( { command: parsed.name, invoker: msg.invokerName }, "Command denied: invoker not in adminGroups" @@ -854,10 +860,12 @@ export class BotInstance extends EventEmitter { // A single long reply (e.g. full lyrics) would exceed TeamSpeak's // per-message byte cap, so split it and send the chunks in order. for (const chunk of splitTextIntoChunks(response)) { + if (!isCurrent()) return; await sendTextReply(this.tsClient, msg, chunk); } } } catch (err) { + if (!isCurrent()) return; this.logger.error({ err, command: parsed.name }, "Command execution error"); try { await sendTextReply(this.tsClient, msg, `Error: ${(err as Error).message}`);