Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(wecom): sanitize Windows device filenames

DavidShen431d2fff3022
diff --git a/packages/lizi-im/src/wecom/__tests__/media.test.ts b/packages/lizi-im/src/wecom/__tests__/media.test.ts
index 24f3c5c9fc..7b61dc7fca 100644
--- a/packages/lizi-im/src/wecom/__tests__/media.test.ts
+++ b/packages/lizi-im/src/wecom/__tests__/media.test.ts
@@ -29,6 +29,15 @@ describe("WeCom media helpers", () => {
);
});
+ it("prefixes Windows device names while preserving similar filenames", () => {
+ expect(safeWecomFilename("CON")).toBe("_CON");
+ expect(safeWecomFilename("nul.txt")).toBe("_nul.txt");
+ expect(safeWecomFilename("COM1.log")).toBe("_COM1.log");
+ expect(safeWecomFilename("LPT9.csv")).toBe("_LPT9.csv");
+ expect(safeWecomFilename("console.txt")).toBe("console.txt");
+ expect(safeWecomFilename("COM10.log")).toBe("COM10.log");
+ });
+
it("persists a downloaded file below the channel directory", async () => {
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "cindy-wecom-"));
tempDirs.push(dir);
diff --git a/packages/lizi-im/src/wecom/media.ts b/packages/lizi-im/src/wecom/media.ts
index abd45d330a..ea80fb88fd 100644
--- a/packages/lizi-im/src/wecom/media.ts
+++ b/packages/lizi-im/src/wecom/media.ts
@@ -4,6 +4,30 @@ import path from "node:path";
const MAX_MEDIA_BYTES = 50 * 1024 * 1024;
const MAX_IMAGE_BYTES = 10 * 1024 * 1024;
+const WINDOWS_RESERVED_FILENAME_STEMS = new Set([
+ "CON",
+ "PRN",
+ "AUX",
+ "NUL",
+ "COM1",
+ "COM2",
+ "COM3",
+ "COM4",
+ "COM5",
+ "COM6",
+ "COM7",
+ "COM8",
+ "COM9",
+ "LPT1",
+ "LPT2",
+ "LPT3",
+ "LPT4",
+ "LPT5",
+ "LPT6",
+ "LPT7",
+ "LPT8",
+ "LPT9",
+]);
const MIME_BY_EXTENSION: Readonly<Record<string, string>> = {
".gif": "image/gif",
@@ -45,7 +69,13 @@ export function safeWecomFilename(
end -= 1;
}
const base = sanitized.slice(0, end);
- return base || `attachment${fallbackExtension}`;
+ if (!base) return `attachment${fallbackExtension}`;
+
+ const stemEnd = base.indexOf(".");
+ const stem = (stemEnd === -1 ? base : base.slice(0, stemEnd)).toUpperCase();
+ return WINDOWS_RESERVED_FILENAME_STEMS.has(stem)
+ ? `_${base}`.slice(0, 160)
+ : base;
}
export async function persistWecomDownload(args: {