fix: scope session state and confirm chat destinations

This commit is contained in:
illusia committed 2026-10-01 15:46:52 +08:00
1 parent c16d1d4cfb
commit db08fbda6b
18 files changed
+969 -206

No files matched your search

+12
View File
@@ -19,6 +19,18 @@ test("all command payloads preserve request IDs through the shared parser", () =
}
});
test("optional chat destination guards reject malformed values and preserve legacy payloads", () => {
for (const clientUid of ["", 7, "x".repeat(129)]) {
assert.ok("error" in parseClientCommand(JSON.stringify({ type: "sendPrivateMessage", payload: { clientId: 2, message: "Hello", clientUid } })));
}
for (const channelId of ["", "-1", 1, "x".repeat(21)]) {
assert.ok("error" in parseClientCommand(JSON.stringify({ type: "sendTextMessage", payload: { message: "Hello", channelId } })));
}
for (const payload of [{ clientId: 2, message: "Hello" }, { clientId: 2, clientUid: "user", message: "Hello" }]) {
assert.deepEqual(parseClientCommand(JSON.stringify({ type: "sendPrivateMessage", payload })), { type: "sendPrivateMessage", payload });
}
});
test("malformed commands keep existing public error codes", () => {
const cases = [
[{ type: "moveClient", payload: { clientId: 0, channelId: "1" } }, "INVALID_CLIENT_ID"],
+8 -2
View File
@@ -2,9 +2,9 @@
export interface ClientCommandPayloads {
switchChannel: { channelId: string; password?: string };
moveClient: { clientId: number; channelId: string; password?: string };
sendTextMessage: { message: string };
sendTextMessage: { message: string; channelId?: string };
sendServerMessage: { message: string };
sendPrivateMessage: { clientId: number; message: string };
sendPrivateMessage: { clientId: number; message: string; clientUid?: string };
poke: { clientId: number; message: string };
setAway: { away: boolean; message?: string };
setWhisperTargets: { targetIds: number[] };
@@ -61,6 +61,12 @@ export function parseClientCommand(raw: string): ClientCommandResult {
if (value.type === "sendTextMessage" && (typeof value.payload.message !== "string" || value.payload.message.length > 500)) {
return { error: { code: "INVALID_TEXT_MESSAGE", message: "文字消息无效" } };
}
if (value.type === "sendTextMessage" && value.payload.channelId !== undefined && (typeof value.payload.channelId !== "string" || !/^\d{1,20}$/.test(value.payload.channelId))) {
return { error: { code: "INVALID_CHANNEL_ID", message: "频道标识无效" } };
}
if (value.type === "sendPrivateMessage" && value.payload.clientUid !== undefined && (typeof value.payload.clientUid !== "string" || !value.payload.clientUid || value.payload.clientUid.length > 128)) {
return { error: { code: "INVALID_CLIENT_ID", message: "成员标识无效" } };
}
if ((value.type === "sendServerMessage" || value.type === "sendPrivateMessage") && (typeof value.payload.message !== "string" || value.payload.message.length > 500)) {
return { error: { code: "INVALID_TEXT_MESSAGE", message: "文字消息无效" } };
}
+3
View File
@@ -27,6 +27,9 @@ export interface ChatMessage {
scope: "channel" | "server" | "private" | "system";
targetId?: string;
conversationId?: string;
/** Browser-local identity scope and display snapshot for private history. */
conversationKey?: string;
conversationName?: string;
senderId?: number;
senderUid?: string;
invokerName: string;