fix(voice): read server banner from client handshake

This commit is contained in:
illusia committed 2026-10-11 20:40:01 +08:00
1 parent 398a4d6c7f
commit 6c57868903
5 files changed
+39 -33

No files matched your search

+18 -13
View File
@@ -18,8 +18,9 @@ const transfer = { size: 6n };
class SdkStub extends EventEmitter {
inputCommands: string[] = [];
fileTransfers: Array<{ channelId: bigint; path: string; password: string }> = [];
serverInfo: { name: string; iconId: number | null } | null = { name: "兄弟们的teamspeak", iconId: 123456 };
serverPresentationRows: Array<Record<string, string>> = [];
serverInfo: { name: string; iconId: number | null; hostBannerGfxUrl: string | null; welcomeMessage: string } | null = {
name: "兄弟们的teamspeak", iconId: 123456, hostBannerGfxUrl: null, welcomeMessage: "",
};
inputUpdate = async (_command: string): Promise<void> => {};
async execCommand(command: string) { this.inputCommands.push(command); await this.inputUpdate(command); }
initialized = 0;
@@ -29,7 +30,7 @@ class SdkStub extends EventEmitter {
download = async (destination: Writable) => { destination.write(Buffer.from("GIF89a")); };
execCommandWithResponse(command = "") {
this.inputCommands.push(command);
return command === "serverinfo" ? Promise.resolve(this.serverPresentationRows) : this.metadata();
return this.metadata();
}
async fileTransferInitDownload(channelId: bigint, path: string, password: string) {
this.initialized++;
@@ -67,26 +68,30 @@ test("server info uses the name and icon id captured by the SDK handshake", asyn
const f = fixture();
assert.deepEqual(await f.client.getServerInfo(), { name: "兄弟们的teamspeak", iconId: 123456 });
assert.deepEqual(f.sdk.inputCommands, [], "server identity does not issue a ServerQuery-only command over the client session");
f.sdk.serverInfo = { name: "No icon", iconId: null };
f.sdk.serverInfo = { name: "No icon", iconId: null, hostBannerGfxUrl: null, welcomeMessage: "" };
assert.deepEqual(await f.client.getServerInfo(), { name: "No icon", iconId: null });
});
test("server presentation reads bounded public banner and welcome fields", async () => {
const f = fixture();
f.sdk.serverPresentationRows = [{
virtualserver_hostbanner_gfx_url: "https://assets.example.invalid/teamspeak/banner.png",
virtualserver_welcomemessage: "欢迎来到服务器!\n请遵守频道规则。",
}];
f.sdk.serverInfo = {
name: "兄弟们的teamspeak",
iconId: 123456,
hostBannerGfxUrl: "https://assets.example.invalid/teamspeak/banner.png",
welcomeMessage: "欢迎来到服务器!\n请遵守频道规则。",
};
assert.deepEqual(await f.client.getServerPresentation(), {
bannerUrl: "https://assets.example.invalid/teamspeak/banner.png",
welcomeMessage: "欢迎来到服务器!\n请遵守频道规则。",
});
assert.deepEqual(f.sdk.inputCommands, ["serverinfo"]);
assert.deepEqual(f.sdk.inputCommands, [], "server presentation comes from the connection handshake without extra permissions");
f.sdk.serverPresentationRows = [{
virtualserver_hostbanner_gfx_url: "javascript:alert(1)",
virtualserver_welcomemessage: "x".repeat(5_000),
}];
f.sdk.serverInfo = {
name: "兄弟们的teamspeak",
iconId: null,
hostBannerGfxUrl: "javascript:alert(1)",
welcomeMessage: "x".repeat(5_000),
};
const sanitized = await f.client.getServerPresentation();
assert.equal(sanitized.bannerUrl, null);
assert.equal(sanitized.welcomeMessage.length, 4_096);
+8 -15
View File
@@ -295,7 +295,8 @@ export class TSClient extends EventEmitter {
if (this.client !== client || !this.connected || this.connectionGeneration !== generation) {
throw new Error("TeamSpeak session changed while reading server information");
}
return client.serverInfo ?? { name: "", iconId: null };
const info = client.serverInfo;
return info ? { name: info.name, iconId: info.iconId } : { name: "", iconId: null };
}
/** Read only the public banner and welcome message from the active virtual server. */
@@ -305,20 +306,12 @@ export class TSClient extends EventEmitter {
const generation = this.connectionGeneration;
const isCurrent = () => this.client === client && this.connected && this.connectionGeneration === generation;
const empty: TSClientServerPresentation = { bannerUrl: null, welcomeMessage: "" };
try {
const rows = await client.execCommandWithResponse("serverinfo", 5_000);
if (!isCurrent()) throw new Error("TeamSpeak session changed while reading server presentation");
const row = rows[0] ?? {};
return {
bannerUrl: normalizeTeamSpeakBannerUrl(row.virtualserver_hostbanner_gfx_url),
welcomeMessage: normalizeTeamSpeakWelcomeMessage(row.virtualserver_welcomemessage),
};
} catch (error: unknown) {
if (!isCurrent()) throw error;
// Some servers restrict serverinfo. Keep the voice connection usable and
// omit the optional presentation when the fields cannot be read.
return empty;
}
const info = client.serverInfo;
if (!isCurrent() || !info) return empty;
return {
bannerUrl: normalizeTeamSpeakBannerUrl(info.hostBannerGfxUrl),
welcomeMessage: normalizeTeamSpeakWelcomeMessage(info.welcomeMessage),
};
}
/** Download the public virtual-server icon using TeamSpeak's normal file-transfer port. */