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): avoid regex backtracking in media names

DavidShen476e5d7fdba7
diff --git a/packages/lizi-im/src/wecom/__tests__/media.test.ts b/packages/lizi-im/src/wecom/__tests__/media.test.ts
index 386c2ad5e6..24f3c5c9fc 100644
--- a/packages/lizi-im/src/wecom/__tests__/media.test.ts
+++ b/packages/lizi-im/src/wecom/__tests__/media.test.ts
@@ -24,6 +24,9 @@ afterEach(async () => {
describe("WeCom media helpers", () => {
it("removes path traversal and reserved filename characters", () => {
expect(safeWecomFilename("../../bad:name?.png")).toBe("bad_name_.png");
+ expect(safeWecomFilename(`report${" ".repeat(20_000)}..`)).toBe(
+ "report",
+ );
});
it("persists a downloaded file below the channel directory", async () => {
diff --git a/packages/lizi-im/src/wecom/media.ts b/packages/lizi-im/src/wecom/media.ts
index a499ebd9aa..abd45d330a 100644
--- a/packages/lizi-im/src/wecom/media.ts
+++ b/packages/lizi-im/src/wecom/media.ts
@@ -32,11 +32,19 @@ export function safeWecomFilename(
filename: string | undefined,
fallbackExtension = "",
): string {
- const base = path
+ const sanitized = path
.basename(filename?.trim() || `attachment${fallbackExtension}`)
.replace(/[<>:"/\\|?*\u0000-\u001f]/g, "_")
- .replace(/[. ]+$/g, "")
.slice(0, 160);
+ let end = sanitized.length;
+ while (
+ end > 0 &&
+ (sanitized.charCodeAt(end - 1) === 0x2e ||
+ sanitized.charCodeAt(end - 1) === 0x20)
+ ) {
+ end -= 1;
+ }
+ const base = sanitized.slice(0, end);
return base || `attachment${fallbackExtension}`;
}