Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(email): keep attachment filenames within UTF-8 byte limits
Afloat6462aee7f7e8
diff --git a/server/email/attachment-names.test.ts b/server/email/attachment-names.test.tsnew file mode 100644index 0000000000..2e614d94c5--- /dev/null+++ b/server/email/attachment-names.test.ts@@ -0,0 +1,35 @@+import { mkdtempSync, readFileSync, rmSync } from "node:fs";+import { tmpdir } from "node:os";+import { basename, join } from "node:path";+import { afterAll, describe, expect, it } from "vitest";++import { safeAttachmentName, saveAttachment } from "./client.ts";++const root = mkdtempSync(join(tmpdir(), "omb-mail-names-"));+afterAll(() => rmSync(root, { recursive: true, force: true }));++describe("attachment filenames on disk", () => {+ it.each([+ { label: "CJK", stem: "报告".repeat(80), suffix: ".pdf" },+ { label: "supplementary letters", stem: "𠀀".repeat(80), suffix: ".json" },+ ])("saves long $label names without splitting characters", ({ stem, suffix }) => {+ const name = safeAttachmentName(`${stem}${suffix}`, 0);+ expect(Buffer.byteLength(name)).toBeLessThanOrEqual(252);+ expect(name.length).toBeLessThanOrEqual(120);+ expect(name.endsWith(suffix)).toBe(true);+ expect(Buffer.from(name).toString("utf8")).toBe(name);++ const folder = mkdtempSync(join(root, "files-"));+ const first = saveAttachment(folder, name, Buffer.from("first"));+ const next = saveAttachment(folder, name, Buffer.from("second"));+ expect(Buffer.byteLength(basename(next))).toBeLessThanOrEqual(255);+ expect(readFileSync(first, "utf8")).toBe("first");+ expect(readFileSync(next, "utf8")).toBe("second");+ });++ it("keeps ordinary names and the existing character limit", () => {+ expect(safeAttachmentName("Rechnung März 2026.pdf", 0)).toBe("Rechnung März 2026.pdf");+ expect(safeAttachmentName(`${"x".repeat(300)}.pdf`, 0)).toBe(`${"x".repeat(116)}.pdf`);+ expect(safeAttachmentName(undefined, 2)).toBe("attachment-3");+ });+});diff --git a/server/email/client.ts b/server/email/client.tsindex 0dba7adb58..318126acf0 100644--- a/server/email/client.ts+++ b/server/email/client.ts@@ -295,8 +295,15 @@ export interface ReadMessage extends MessageSummary {/** A file name that stays inside the folder, from a name a stranger wrote. */export function safeAttachmentName(raw: string | undefined, index: number): string {- const name = basename((raw ?? "").replaceAll("\\", "/")).replace(/[^\p{L}\p{N}._ -]+/gu, "_").replace(/^[.\s]+/u, "").slice(-120).trim();- return name || `attachment-${index + 1}`;+ const clean = basename((raw ?? "").replaceAll("\\", "/")).replace(/[^\p{L}\p{N}._ -]+/gu, "_").trim();+ let name = "", bytes = 0;+ for (const character of Array.from(clean).reverse()) {+ const size = Buffer.byteLength(character);+ if (name.length + character.length > 120 || bytes + size > 252) break;+ name = character + name;+ bytes += size;+ }+ return name.replace(/^[.\s]+/u, "") || `attachment-${index + 1}`;}/** Security: `folder` is the server's own and read-only in the box, so a bot can't plant a link in it, and the file is