From 667012d7103428d0b17b4fc704de79e5d0dd9328 Mon Sep 17 00:00:00 2001 From: Moepchi Date: Thu, 17 Sep 2026 11:06:16 +0000 Subject: [PATCH] Fix WS message handler crash and stdin command injection in gateway socket.on("message") parsed JSON and ran the whole handler switch with no try/catch. Since the callback is async, any throw - malformed JSON from any connected browser tab, or a handler throwing on unexpected input - became an unhandled rejection and crashed the entire gateway process, dropping every other connected user's live session. Now wrapped so one bad message only errors back to that socket. connection.ts wrote several fields (permission scope/ids, whisper targets) straight into the connector's line-based stdin protocol without stripping embedded CR/LF, unlike the sanitization already applied to every other free-text field in the file - a newline in one of them could smuggle in a second, attacker-chosen connector command. All stdin writes now go through one writeLine() helper that strips embedded CR/LF, closing this for every call site at once instead of patching each one individually. Bumps the tsclientlib submodule for the matching SSRF/panic fixes there. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01Hu2cuPLqpuKVfd9wWFFakU --- gateway/src/index.ts | 26 ++++++- gateway/src/ts3/connection.ts | 127 +++++++++++++++++++--------------- tsclientlib | 2 +- 3 files changed, 96 insertions(+), 59 deletions(-) diff --git a/gateway/src/index.ts b/gateway/src/index.ts index a7e26fb..624a36d 100644 --- a/gateway/src/index.ts +++ b/gateway/src/index.ts @@ -515,8 +515,31 @@ wss.on("connection", (socket: WebSocket) => { }); socket.on("message", async (raw) => { - const msg = JSON.parse(raw.toString()); + // Everything below runs inside this try/catch on purpose: any exception + // here - malformed JSON, a handler throwing on unexpected input - would + // otherwise become an unhandled rejection (this callback is async) and + // take the whole process down with it, dropping every other connected + // user's live session. One bad message from one tab must never do that. + let msg: any; + try { + msg = JSON.parse(raw.toString()); + } catch { + socket.send(JSON.stringify({ type: "error", message: "Invalid JSON" })); + return; + } + try { + await handleMessage(msg); + } catch (err) { + console.error("[gateway] Error handling WS message:", err); + try { + socket.send(JSON.stringify({ type: "error", message: "Internal error handling message" })); + } catch { + /* socket already gone */ + } + } + + async function handleMessage(msg: any): Promise { switch (msg.type) { case "connect": { // Replacing a connection on the same socket: tear down the previous @@ -819,6 +842,7 @@ wss.on("connection", (socket: WebSocket) => { JSON.stringify({ type: "error", message: `Unknown message type: ${msg.type}` }) ); } + } }); socket.on("close", () => { diff --git a/gateway/src/ts3/connection.ts b/gateway/src/ts3/connection.ts index 23673b2..dbfae2a 100644 --- a/gateway/src/ts3/connection.ts +++ b/gateway/src/ts3/connection.ts @@ -248,6 +248,16 @@ export class Ts3Connection { for (const listener of this.listeners) listener(event); } + /** Every connector command is exactly one line on this stdin protocol. + * Several call sites forward fields (permission scope/ids, whisper + * targets, ...) without stripping embedded CR/LF first - routing every + * write through here means a stray newline in any of them can never + * smuggle in a second, attacker-chosen command. */ + private writeLine(line: string): void { + if (!this.child || this.child.killed) return; + this.child.stdin.write(`${line.replace(/[\r\n]+/g, " ")}\n`); + } + async connect(): Promise { const args = ["--address", this.options.host, "--nickname", this.options.nickname]; if (this.options.serverPassword) args.push("--server-password", this.options.serverPassword); @@ -576,7 +586,7 @@ export class Ts3Connection { // The password travels base64-encoded on this line-based stdin protocol // so it can safely contain spaces or other characters. const passwordArg = channelPassword ? ` ${Buffer.from(channelPassword, "utf8").toString("base64")}` : ""; - this.child?.stdin.write(`switch ${id}${passwordArg}\n`); + this.writeLine(`switch ${id}${passwordArg}`); } async moveClient(clientId: number, channelId: number, channelPassword?: string): Promise { @@ -584,30 +594,30 @@ export class Ts3Connection { const cid = Number(channelId); if (!Number.isFinite(clid) || !Number.isFinite(cid)) return; const passwordArg = channelPassword ? ` ${Buffer.from(channelPassword, "utf8").toString("base64")}` : ""; - this.child?.stdin.write(`moveclient ${clid} ${cid}${passwordArg}\n`); + this.writeLine(`moveclient ${clid} ${cid}${passwordArg}`); } async getClientConnectionInfo(clientId: number): Promise { - this.child?.stdin.write(`clientconninfo ${clientId}\n`); + this.writeLine(`clientconninfo ${clientId}`); } async getServerConnectionInfo(): Promise { - this.child?.stdin.write(`serverconninfo\n`); + this.writeLine(`serverconninfo`); } async kickFromChannel(clientId: number, reason: string): Promise { const sanitized = reason.replace(/[\r\n]+/g, " ").trim(); - this.child?.stdin.write(`kickchannel ${clientId} ${sanitized}\n`); + this.writeLine(`kickchannel ${clientId} ${sanitized}`); } async kickFromServer(clientId: number, reason: string): Promise { const sanitized = reason.replace(/[\r\n]+/g, " ").trim(); - this.child?.stdin.write(`kickserver ${clientId} ${sanitized}\n`); + this.writeLine(`kickserver ${clientId} ${sanitized}`); } async banClient(clientId: number, seconds: number, reason: string): Promise { const sanitized = reason.replace(/[\r\n]+/g, " ").trim(); - this.child?.stdin.write(`banclient ${clientId} ${Math.max(0, Math.floor(seconds))} ${sanitized}\n`); + this.writeLine(`banclient ${clientId} ${Math.max(0, Math.floor(seconds))} ${sanitized}`); } /** Every field is optional - only send what actually changed. JSON.stringify @@ -631,95 +641,95 @@ export class Ts3Connection { phoneticName?: string; codecEncryptionMode?: string; }): Promise { - this.child?.stdin.write(`serveredit ${JSON.stringify(payload)}\n`); + this.writeLine(`serveredit ${JSON.stringify(payload)}`); } async getServerLog(): Promise { - this.child?.stdin.write(`serverlog\n`); + this.writeLine(`serverlog`); } async getBanList(): Promise { - this.child?.stdin.write(`banlist\n`); + this.writeLine(`banlist`); } async deleteBan(banId: number): Promise { - this.child?.stdin.write(`bandel ${banId}\n`); + this.writeLine(`bandel ${banId}`); } async deleteAllBans(): Promise { - this.child?.stdin.write(`bandelall\n`); + this.writeLine(`bandelall`); } async getComplainList(): Promise { - this.child?.stdin.write(`complainlist\n`); + this.writeLine(`complainlist`); } async deleteComplaint(targetClientDbId: number, fromClientDbId: number): Promise { - this.child?.stdin.write(`complaindel ${targetClientDbId} ${fromClientDbId}\n`); + this.writeLine(`complaindel ${targetClientDbId} ${fromClientDbId}`); } async deleteAllComplaintsFor(targetClientDbId: number): Promise { - this.child?.stdin.write(`complaindelall ${targetClientDbId}\n`); + this.writeLine(`complaindelall ${targetClientDbId}`); } async getOfflineMessageList(): Promise { - this.child?.stdin.write(`messagelist\n`); + this.writeLine(`messagelist`); } async getOfflineMessage(messageId: number): Promise { - this.child?.stdin.write(`messageget ${messageId}\n`); + this.writeLine(`messageget ${messageId}`); } async sendOfflineMessage(clientUid: string, subject: string, message: string): Promise { const sanitize = (s: string) => s.replace(/[\r\n\t]+/g, " ").trim(); - this.child?.stdin.write(`messageadd ${sanitize(clientUid)}\t${sanitize(subject)}\t${sanitize(message)}\n`); + this.writeLine(`messageadd ${sanitize(clientUid)}\t${sanitize(subject)}\t${sanitize(message)}`); } async deleteOfflineMessage(messageId: number): Promise { - this.child?.stdin.write(`messagedel ${messageId}\n`); + this.writeLine(`messagedel ${messageId}`); } async markOfflineMessageRead(messageId: number): Promise { - this.child?.stdin.write(`messageupdateflag ${messageId} 1\n`); + this.writeLine(`messageupdateflag ${messageId} 1`); } async getChannelGroupList(): Promise { - this.child?.stdin.write(`channelgrouplist\n`); + this.writeLine(`channelgrouplist`); } async getServerGroupList(): Promise { - this.child?.stdin.write(`servergrouplist\n`); + this.writeLine(`servergrouplist`); } async setChannelGroup(channelGroupId: number, channelId: number, clientDbId: number): Promise { - this.child?.stdin.write(`setchannelgroup ${channelGroupId} ${channelId} ${clientDbId}\n`); + this.writeLine(`setchannelgroup ${channelGroupId} ${channelId} ${clientDbId}`); } async addServerGroup(serverGroupId: number, clientDbId: number): Promise { - this.child?.stdin.write(`addservergroup ${serverGroupId} ${clientDbId}\n`); + this.writeLine(`addservergroup ${serverGroupId} ${clientDbId}`); } async removeServerGroup(serverGroupId: number, clientDbId: number): Promise { - this.child?.stdin.write(`delservergroup ${serverGroupId} ${clientDbId}\n`); + this.writeLine(`delservergroup ${serverGroupId} ${clientDbId}`); } async serverQueryLogin(username: string, password: string): Promise { const u = Buffer.from(username, "utf8").toString("base64"); const p = Buffer.from(password, "utf8").toString("base64"); - this.child?.stdin.write(`serverquerylogin ${u} ${p}\n`); + this.writeLine(`serverquerylogin ${u} ${p}`); } async getPermissionOverview(): Promise { - this.child?.stdin.write(`permoverview\n`); + this.writeLine(`permoverview`); } async getPermissionCatalog(): Promise { - this.child?.stdin.write(`permissionlist\n`); + this.writeLine(`permissionlist`); } async getPermList(scope: PermScope, id1: number, id2?: number): Promise { const args = id2 !== undefined ? `${scope} ${id1} ${id2}` : `${scope} ${id1}`; - this.child?.stdin.write(`permlist ${args}\n`); + this.writeLine(`permlist ${args}`); } /** `negated`/`skip` only apply to the "server" and "client" scopes - the @@ -736,73 +746,73 @@ export class Ts3Connection { let args = `${scope} ${ids.join(" ")} ${permId} ${value}`; if (scope === "server") args += ` ${negated ? 1 : 0} ${skip ? 1 : 0}`; else if (scope === "client") args += ` ${skip ? 1 : 0}`; - this.child?.stdin.write(`permadd ${args}\n`); + this.writeLine(`permadd ${args}`); } async removePermission(scope: PermScope, ids: number[], permId: number): Promise { - this.child?.stdin.write(`permdel ${scope} ${ids.join(" ")} ${permId}\n`); + this.writeLine(`permdel ${scope} ${ids.join(" ")} ${permId}`); } async getFileList(channelId: number, path: string): Promise { const sanitized = (path || "/").replace(/[\r\n]+/g, " ").trim() || "/"; - this.child?.stdin.write(`ftlist ${channelId} ${sanitized}\n`); + this.writeLine(`ftlist ${channelId} ${sanitized}`); } async createDirectory(channelId: number, dirname: string): Promise { const sanitized = dirname.replace(/[\r\n]+/g, " ").trim(); - if (sanitized) this.child?.stdin.write(`ftmkdir ${channelId} ${sanitized}\n`); + if (sanitized) this.writeLine(`ftmkdir ${channelId} ${sanitized}`); } async deleteFile(channelId: number, name: string): Promise { const sanitized = name.replace(/[\r\n]+/g, " ").trim(); - if (sanitized) this.child?.stdin.write(`ftdelete ${channelId} ${sanitized}\n`); + if (sanitized) this.writeLine(`ftdelete ${channelId} ${sanitized}`); } async renameFile(channelId: number, oldName: string, newName: string): Promise { const sanitize = (s: string) => s.replace(/[\r\n\t]+/g, " ").trim(); - this.child?.stdin.write(`ftrename ${channelId}\t${sanitize(oldName)}\t${sanitize(newName)}\n`); + this.writeLine(`ftrename ${channelId}\t${sanitize(oldName)}\t${sanitize(newName)}`); } async downloadFile(channelId: number, path: string): Promise { const sanitized = path.replace(/[\r\n]+/g, " ").trim(); - if (sanitized) this.child?.stdin.write(`ftdownload ${channelId} ${sanitized}\n`); + if (sanitized) this.writeLine(`ftdownload ${channelId} ${sanitized}`); } /** `dataBase64` is the raw file content, base64-encoded - the browser reads * the picked File as a data URL/ArrayBuffer and sends it up already encoded. */ async uploadFile(channelId: number, path: string, dataBase64: string): Promise { const sanitizedPath = path.replace(/[\r\n\t]+/g, " ").trim(); - this.child?.stdin.write(`ftupload ${channelId}\t${sanitizedPath}\t${dataBase64}\n`); + this.writeLine(`ftupload ${channelId}\t${sanitizedPath}\t${dataBase64}`); } async sendChatMessage(message: string): Promise { const sanitized = message.replace(/[\r\n]+/g, " ").trim(); - if (sanitized) this.child?.stdin.write(`chat ${sanitized}\n`); + if (sanitized) this.writeLine(`chat ${sanitized}`); } async sendServerMessage(message: string): Promise { const sanitized = message.replace(/[\r\n]+/g, " ").trim(); - if (sanitized) this.child?.stdin.write(`serverchat ${sanitized}\n`); + if (sanitized) this.writeLine(`serverchat ${sanitized}`); } async sendPrivateMessage(clientId: number, message: string): Promise { const sanitized = message.replace(/[\r\n]+/g, " ").trim(); - if (sanitized) this.child?.stdin.write(`pm ${clientId} ${sanitized}\n`); + if (sanitized) this.writeLine(`pm ${clientId} ${sanitized}`); } async sendPoke(clientId: number, message: string): Promise { const sanitized = message.replace(/[\r\n]+/g, " ").trim(); - this.child?.stdin.write(`poke ${clientId} ${sanitized}\n`); + this.writeLine(`poke ${clientId} ${sanitized}`); } async sendAudio(pcmBase64: string): Promise { - this.child?.stdin.write(`audio ${pcmBase64}\n`); + this.writeLine(`audio ${pcmBase64}`); } /** Pulls a streaming client's stream details; answered with a * `notifystreaminfo` streamEvent carrying the stream id. */ async requestStreamInfo(clientId: number): Promise { - this.child?.stdin.write(`streaminfo ${clientId}\n`); + this.writeLine(`streaminfo ${clientId}`); } /** Ask the client publishing a TS6 stream to let us watch. The answer comes @@ -811,12 +821,12 @@ export class Ts3Connection { async joinStream(streamId: string, clientId: number, message = ""): Promise { const id = streamId.replace(/[\s]+/g, ""); const sanitized = message.replace(/[\r\n]+/g, " ").trim(); - if (id) this.child?.stdin.write(`streamjoin ${id} ${clientId} ${sanitized}\n`); + if (id) this.writeLine(`streamjoin ${id} ${clientId} ${sanitized}`); } async leaveStream(streamId: string, clientId: number): Promise { const id = streamId.replace(/[\s]+/g, ""); - if (id) this.child?.stdin.write(`streamleave ${id} ${clientId}\n`); + if (id) this.writeLine(`streamleave ${id} ${clientId}`); } /** Relays one signaling payload (SDP answer, ICE candidate) to a stream peer. @@ -826,7 +836,7 @@ export class Ts3Connection { * stdin protocol. */ async sendStreamSignal(streamId: string, clientId: number, payload: unknown): Promise { const id = streamId.replace(/[\s]+/g, ""); - if (id) this.child?.stdin.write(`streamsignal ${id} ${clientId} ${JSON.stringify(payload)}\n`); + if (id) this.writeLine(`streamsignal ${id} ${clientId} ${JSON.stringify(payload)}`); } /** Announces a stream we publish. The server assigns the id and reports it @@ -841,7 +851,7 @@ export class Ts3Connection { viewerLimit: options.viewerLimit, audio: options.audio, }; - this.child?.stdin.write(`streamsetup ${JSON.stringify(payload)}\n`); + this.writeLine(`streamsetup ${JSON.stringify(payload)}`); } /** Accepts or refuses a viewer that asked to watch our stream. On accept, @@ -863,44 +873,47 @@ export class Ts3Connection { offer, decision: accept ? 1 : 0, }; - this.child?.stdin.write(`streamrespond ${JSON.stringify(payload)}\n`); + this.writeLine(`streamrespond ${JSON.stringify(payload)}`); } async stopStream(streamId: string, reason = ""): Promise { const id = streamId.replace(/[\s]+/g, ""); const sanitized = reason.replace(/[\r\n]+/g, " ").trim(); - if (id) this.child?.stdin.write(`streamstop ${id} ${sanitized}\n`); + if (id) this.writeLine(`streamstop ${id} ${sanitized}`); } async setAway(away: boolean, message: string): Promise { if (away) { const sanitized = message.replace(/[\r\n]+/g, " ").trim(); - this.child?.stdin.write(`away ${sanitized}\n`); + this.writeLine(`away ${sanitized}`); } else { - this.child?.stdin.write("unaway\n"); + this.writeLine("unaway"); } } async setInputMuted(muted: boolean): Promise { - this.child?.stdin.write(`muteinput ${muted ? "1" : "0"}\n`); + this.writeLine(`muteinput ${muted ? "1" : "0"}`); } async setOutputMuted(muted: boolean): Promise { - this.child?.stdin.write(`muteoutput ${muted ? "1" : "0"}\n`); + this.writeLine(`muteoutput ${muted ? "1" : "0"}`); } async setNickname(nickname: string): Promise { const sanitized = nickname.replace(/[\r\n]+/g, " ").trim(); - if (sanitized) this.child?.stdin.write(`nickname ${sanitized}\n`); + if (sanitized) this.writeLine(`nickname ${sanitized}`); } /** Empty channelIds/clientIds clears whisper mode, returning outgoing * voice to the normal current-channel broadcast. */ async setWhisperTargets(channelIds: number[], clientIds: number[]): Promise { - if (channelIds.length === 0 && clientIds.length === 0) { - this.child?.stdin.write("unwhisper\n"); + const chans = channelIds.map(Number); + const clients = clientIds.map(Number); + if (chans.some((n) => !Number.isFinite(n)) || clients.some((n) => !Number.isFinite(n))) return; + if (chans.length === 0 && clients.length === 0) { + this.writeLine("unwhisper"); } else { - this.child?.stdin.write(`whisper ${channelIds.join(",")};${clientIds.join(",")}\n`); + this.writeLine(`whisper ${chans.join(",")};${clients.join(",")}`); } } diff --git a/tsclientlib b/tsclientlib index 6df8ba1..7361a8d 160000 --- a/tsclientlib +++ b/tsclientlib @@ -1 +1 @@ -Subproject commit 6df8ba1bcea3a115f61b0cfd01c29f052805cee5 +Subproject commit 7361a8d71d3d3b0dc4588191b1753a101f6d9e5d