Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Let chat attach archives (.zip, .tar.gz, .7z, ...) as plain files

AdityaUmale73e9f69624f7
diff --git a/server/attachments.test.ts b/server/attachments.test.ts
index 9086118c6a..3096a58dac 100644
--- a/server/attachments.test.ts
+++ b/server/attachments.test.ts
@@ -535,11 +535,12 @@ describe("shared files", () => {
resetDir();
});
- it("allows useful document mimes but not executables, archives, or active markup", () => {
+ it("allows useful document and archive mimes but not executables or active markup", () => {
expect(extensionForFileMime("text/plain; charset=utf-8")).toBe(".txt");
expect(extensionForFileMime("application/pdf")).toBe(".pdf");
expect(extensionForFileMime("application/vnd.openxmlformats-officedocument.wordprocessingml.document")).toBe(".docx");
- expect(extensionForFileMime("application/zip")).toBeNull();
+ expect(extensionForFileMime("application/zip")).toBe(".zip");
+ expect(extensionForFileMime("application/x-compressed-tar")).toBe(".tgz");
expect(extensionForFileMime("application/x-msdownload")).toBeNull();
expect(extensionForFileMime("application/octet-stream")).toBeNull();
expect(extensionForFileMime("text/html")).toBeNull();
@@ -552,7 +553,8 @@ describe("shared files", () => {
expect(() => sanitizeSharedFileName("../../secret.txt", "text/plain")).toThrow(/filename, not a path/);
expect(() => sanitizeSharedFileName("..\\..\\secret.txt", "text/plain")).toThrow(/filename, not a path/);
expect(() => sanitizeSharedFileName("..%2F..%2Fsecret.txt", "text/plain")).toThrow(/filename, not a path/);
- expect(() => sanitizeSharedFileName("notes.txt", "application/zip")).toThrow(/supported document/);
+ expect(sanitizeSharedFileName("logs.tar.gz", "application/gzip")).toBe("logs.tar.gz");
+ expect(() => sanitizeSharedFileName("notes.txt", "application/x-msdownload")).toThrow(/supported document/);
});
it("streams a file under a generated name with private permissions", async () => {
diff --git a/server/attachments.ts b/server/attachments.ts
index 072380e8ca..cfc7bc7ece 100644
--- a/server/attachments.ts
+++ b/server/attachments.ts
@@ -69,7 +69,7 @@ const IMAGE_MIMES: Record<string, string> = {
"image/webp": ".webp",
};
-/** No archives, binaries, HTML, SVG or scripts. The claimed mime picks the extension; an attacker's filename never does. */
+/** No binaries, HTML, SVG or scripts; archives are kept as sent, never unpacked. The claimed mime picks the extension; an attacker's filename never does. */
const FILE_MIMES: Readonly<Record<string, string>> = {
"video/mp4": ".mp4",
"video/webm": ".webm",
@@ -103,6 +103,14 @@ const FILE_MIMES: Readonly<Record<string, string>> = {
"application/vnd.oasis.opendocument.text": ".odt",
"application/vnd.oasis.opendocument.spreadsheet": ".ods",
"application/vnd.oasis.opendocument.presentation": ".odp",
+ "application/zip": ".zip",
+ "application/vnd.rar": ".rar",
+ "application/x-7z-compressed": ".7z",
+ "application/x-tar": ".tar",
+ "application/gzip": ".gz",
+ "application/x-compressed-tar": ".tgz",
+ "application/x-bzip2": ".bz2",
+ "application/x-xz": ".xz",
};
export function extensionForMime(mime: string | undefined): string | null {
diff --git a/src/lib/attach-notice.test.ts b/src/lib/attach-notice.test.ts
index aa2f88e131..cacf1c83a1 100644
--- a/src/lib/attach-notice.test.ts
+++ b/src/lib/attach-notice.test.ts
@@ -14,17 +14,16 @@ describe("attachment notices", () => {
expect(attachNoticeText([{ kind: "too-large", name: "big.png", limitMb: 10 }])).toBe("big.png exceeds 10 MB");
});
- it("keeps image limits first, then unsupported files, archives and upload failures", () => {
+ it("keeps image limits first, then unsupported files and upload failures", () => {
const problems = [
{ kind: "upload-failed" as const, name: "c.pdf", reason: "disk full" },
- { kind: "archives" as const, names: ["a.zip"] },
{ kind: "unsupported" as const, names: ["b.bin"] },
{ kind: "image-limit" as const, names: ["left.png"] },
];
- expect(attachNoticeText(problems)).toBe("A message takes up to 10 images, 40 MB in all. left.png wasn't attached. Unable to attach b.bin. Choose a supported image, document, or audio file. Archives can't be attached: unzip a.zip and attach the files inside. (c.pdf: disk full)");
+ expect(attachNoticeText(problems)).toBe("A message takes up to 10 images, 40 MB in all. left.png wasn't attached. Unable to attach b.bin. Choose a supported image, document, or audio file. (c.pdf: disk full)");
expect(attachNoticeText([{ kind: "image-limit", names: ["a.png", "b.png"] }])).toBe("A message takes up to 10 images, 40 MB in all. a.png, b.png weren't attached.");
setLocale("pt-br");
- expect(attachNoticeText(problems)).toBe("Uma mensagem aceita até 10 imagens, com 40 MB no total. O arquivo left.png não foi anexado. Não foi possível anexar b.bin. Escolha um arquivo compatível: imagem, documento ou áudio. Arquivos compactados não podem ser anexados: descompacte a.zip e anexe os arquivos de dentro. (c.pdf: não foi possível enviar. Motivo: disk full)");
+ expect(attachNoticeText(problems)).toBe("Uma mensagem aceita até 10 imagens, com 40 MB no total. O arquivo left.png não foi anexado. Não foi possível anexar b.bin. Escolha um arquivo compatível: imagem, documento ou áudio. (c.pdf: não foi possível enviar. Motivo: disk full)");
expect(attachNoticeText([{ kind: "image-limit", names: ["a.png", "b.png"] }])).toContain("Os arquivos a.png, b.png não foram anexados.");
});
diff --git a/src/lib/attach-notice.ts b/src/lib/attach-notice.ts
index 9eb9f2ba0a..22bd1c1dfe 100644
--- a/src/lib/attach-notice.ts
+++ b/src/lib/attach-notice.ts
@@ -21,8 +21,6 @@ function problemText(problem: AttachProblem): string {
return t("attach.tooLarge", { name: problem.name, limit: numberText(problem.limitMb) });
case "unsupported":
return t("attach.unsupported", { names: problem.names.join(", ") });
- case "archives":
- return t("attach.archivesUnsupported", { names: problem.names.join(", ") });
case "image-limit":
return t(problem.names.length === 1 ? "attach.imagesLeftOutOne" : "attach.imagesLeftOutMany", { count: TURN_IMAGE_MAX_COUNT, megabytes: TURN_IMAGE_MAX_BYTES / (1024 * 1024), names: problem.names.join(", ") });
case "upload-failed":
@@ -32,7 +30,7 @@ function problemText(problem: AttachProblem): string {
export function attachNoticeText(problems: readonly AttachProblem[]): string | null {
const limited = problems.filter((problem) => problem.kind === "image-limit").map(problemText).join(" ");
- const rejected = ["unsupported", "archives"].flatMap((kind) => problems.filter((problem) => problem.kind === kind).map(problemText)).join(" ");
+ const rejected = problems.filter((problem) => problem.kind === "unsupported").map(problemText).join(" ");
const failed = problems.filter((problem) => problem.kind === "too-large" || problem.kind === "upload-failed").map(problemText).join("; ");
const notice = rejected && failed ? `${rejected} (${failed})` : rejected || failed;
return [limited, notice].filter(Boolean).join(" ") || null;
diff --git a/src/lib/composer-attachments.test.ts b/src/lib/composer-attachments.test.ts
index fec40fbe57..808fd5ea41 100644
--- a/src/lib/composer-attachments.test.ts
+++ b/src/lib/composer-attachments.test.ts
@@ -547,6 +547,27 @@ describe("private document intake", () => {
expect(documentMime({ name: "archive.zip", type: "application/zip" })).toBeNull();
});
+ it("uploads archives as plain files, typed by their filename", async () => {
+ const fetch = vi.spyOn(globalThis, "fetch").mockImplementation(async () => new Response(JSON.stringify({
+ path: "/private/attachments/1.zip", name: "x", bytes: 3,
+ }), { status: 201, headers: { "content-type": "application/json" } }));
+ try {
+ for (const [name, type, mime] of [
+ ["Project.zip", "application/x-zip-compressed", "application/zip"],
+ ["logs.TAR.GZ", "", "application/gzip"],
+ ["src.tgz", "application/octet-stream", "application/x-compressed-tar"],
+ ["dump.7z", "", "application/x-7z-compressed"],
+ ] as const) {
+ const file = new File([new Uint8Array([1, 2, 3])], name, { type });
+ await expect(fileAttachmentFromFile(file)).resolves.toMatchObject({ kind: "file", path: "/private/attachments/1.zip" });
+ expect(fetch).toHaveBeenLastCalledWith(expect.stringMatching(/^\/api\/files\?/), expect.objectContaining({ headers: { "content-type": mime } }));
+ }
+ await expect(fileAttachmentFromFile(new File([new Uint8Array([1])], "zip-renamed.bin", { type: "application/zip" }))).resolves.toBeNull();
+ } finally {
+ fetch.mockRestore();
+ }
+ });
+
it("uses the server path and safe name returned by the private store", async () => {
const fetch = vi.spyOn(globalThis, "fetch").mockResolvedValue(new Response(JSON.stringify({
path: "/private/attachments/123.pdf",
diff --git a/src/lib/composer-attachments.ts b/src/lib/composer-attachments.ts
index ff8e1fc683..5cad7066dc 100644
--- a/src/lib/composer-attachments.ts
+++ b/src/lib/composer-attachments.ts
@@ -138,12 +138,9 @@ export function imagesThatFit<T extends { name: string; size: number }>(images:
return kept;
}
-const ARCHIVE_NAME = /\.(?:zip|rar|7z|tar|gz|tgz|bz2|xz)$/i;
-
export type AttachProblem =
| { kind: "too-large"; name: string; limitMb: number }
| { kind: "unsupported"; names: string[] }
- | { kind: "archives"; names: string[] }
| { kind: "image-limit"; names: string[] }
| { kind: "upload-failed"; name: string; reason: string };
@@ -193,6 +190,18 @@ const AUDIO_MIMES_BY_EXTENSION: Readonly<Record<string, string>> = {
flac: "audio/flac",
webm: "audio/webm",
};
+/** Sent as plain files and never unpacked; typed by name alone, since browsers declare archives inconsistently. */
+const ARCHIVE_MIMES_BY_EXTENSION: Readonly<Record<string, string>> = {
+ zip: "application/zip",
+ rar: "application/vnd.rar",
+ "7z": "application/x-7z-compressed",
+ tar: "application/x-tar",
+ gz: "application/gzip",
+ tgz: "application/x-compressed-tar",
+ bz2: "application/x-bzip2",
+ xz: "application/x-xz",
+};
+
const ACCEPTED_AUDIO_MIMES = new Set([
...Object.values(AUDIO_MIMES_BY_EXTENSION),
"audio/x-m4a", "audio/x-wav", "audio/wave", "audio/x-flac",
@@ -364,7 +373,8 @@ export async function fileAttachmentFromFile(file: File): Promise<FileAttachment
const declared = file.type.split(";", 1)[0]!.trim().toLowerCase();
const extension = file.name.split(".").at(-1)?.toLowerCase() ?? "";
const mime = documentMime(file) ??
- (ACCEPTED_AUDIO_MIMES.has(declared) ? declared : AUDIO_MIMES_BY_EXTENSION[extension]);
+ (ACCEPTED_AUDIO_MIMES.has(declared) ? declared : AUDIO_MIMES_BY_EXTENSION[extension]) ??
+ ARCHIVE_MIMES_BY_EXTENSION[extension];
if (!mime) return null;
if (file.size > FILE_MAX_BYTES) throw new AttachError(file.name, FILE_MAX_BYTES);
const uploadId = newUploadId();
@@ -805,13 +815,10 @@ export async function intakeFiles<T extends DroppedFile & { type: string }>(
}));
const attachments = results.flatMap((result) => result.attachments);
const rejectedNames = results.flatMap((result) => result.rejectedNames);
- const archives = rejectedNames.filter((name) => ARCHIVE_NAME.test(name));
- const unsupported = rejectedNames.filter((name) => !ARCHIVE_NAME.test(name));
const uploadErrors = results.flatMap((result) => result.uploadError ? [result.uploadError] : []);
- const pathless = [
- unsupported.length ? `Unable to attach ${unsupported.join(", ")}. Choose a supported image, document, or audio file.` : "",
- archives.length ? `Archives can't be attached: unzip ${archives.join(", ")} and attach the files inside.` : "",
- ].filter(Boolean).join(" ") || null;
+ const pathless = rejectedNames.length
+ ? `Unable to attach ${rejectedNames.join(", ")}. Choose a supported image, document, or audio file.`
+ : null;
const uploadErrorText = results.flatMap((result) => result.uploadErrorText ? [result.uploadErrorText] : []).join("; ") || null;
const notice = pathless && uploadErrorText ? `${pathless} (${uploadErrorText})` : (pathless ?? uploadErrorText);
const limited = leftOut.length ? imageLimitNotice(leftOut) : null;
@@ -819,8 +826,7 @@ export async function intakeFiles<T extends DroppedFile & { type: string }>(
attachments,
problems: [
...(leftOut.length ? [{ kind: "image-limit" as const, names: leftOut }] : []),
- ...(unsupported.length ? [{ kind: "unsupported" as const, names: unsupported }] : []),
- ...(archives.length ? [{ kind: "archives" as const, names: archives }] : []),
+ ...(rejectedNames.length ? [{ kind: "unsupported" as const, names: rejectedNames }] : []),
...uploadErrors,
],
notice: limited && notice ? `${limited} ${notice}` : (limited ?? notice),
diff --git a/src/lib/intake-files.test.ts b/src/lib/intake-files.test.ts
index 15440a495d..6317d7f97d 100644
--- a/src/lib/intake-files.test.ts
+++ b/src/lib/intake-files.test.ts
@@ -173,14 +173,12 @@ describe("intakeFiles", () => {
expect(out.notice).toBeNull();
});
- it("explains that an archive needs unzipping instead of calling it unsupported", async () => {
- const out = await intakeFiles([file("아카이브.zip", "application/zip"), file("run.exe", "application/octet-stream")], {
+ it("names a file it can't take as unsupported", async () => {
+ const out = await intakeFiles([file("run.exe", "application/octet-stream")], {
allowImages: true, getPath: () => "", uploadImage: upload, uploadFile: async () => null,
});
expect(out.attachments).toEqual([]);
- expect(out.notice).toBe(
- "Unable to attach run.exe. Choose a supported image, document, or audio file. Archives can't be attached: unzip 아카이브.zip and attach the files inside.",
- );
+ expect(out.notice).toBe("Unable to attach run.exe. Choose a supported image, document, or audio file.");
});
it("counts the images already in the draft, and names pasted images past the slots left", () => {
diff --git a/src/locales/en.json b/src/locales/en.json
index a310f7f335..000dbbcebe 100644
--- a/src/locales/en.json
+++ b/src/locales/en.json
@@ -9913,7 +9913,6 @@
"server.routines.cron.nthWeekend.last": "Monthly on the last {weekday} at {time}",
"attach.imagesLeftOutOne": "A message takes up to {count} images, {megabytes} MB in all. {names} wasn't attached.",
"attach.imagesLeftOutMany": "A message takes up to {count} images, {megabytes} MB in all. {names} weren't attached.",
- "attach.archivesUnsupported": "Archives can't be attached: unzip {names} and attach the files inside.",
"native.home.ownCopyNotReady": "OpenMausBot on this computer could not go back to its own copy yet.",
"native.home.ownCopyNotRunning": "This computer's own copy isn't running.",
"native.cloud.connectedAppsStay": "Your connected apps stay on this computer: that server has no connected-apps service of its own. Connect them again there in Apps.",
diff --git a/src/locales/pt-br.json b/src/locales/pt-br.json
index 3d0ebf4da6..94e5ed37c8 100644
--- a/src/locales/pt-br.json
+++ b/src/locales/pt-br.json
@@ -9866,7 +9866,6 @@
"server.routines.cron.nthWeekend.last": "Todo mês no último {weekday} às {time}",
"attach.imagesLeftOutOne": "Uma mensagem aceita até {count} imagens, com {megabytes} MB no total. O arquivo {names} não foi anexado.",
"attach.imagesLeftOutMany": "Uma mensagem aceita até {count} imagens, com {megabytes} MB no total. Os arquivos {names} não foram anexados.",
- "attach.archivesUnsupported": "Arquivos compactados não podem ser anexados: descompacte {names} e anexe os arquivos de dentro.",
"native.home.ownCopyNotReady": "O OpenMausBot neste computador ainda não conseguiu voltar à própria cópia.",
"native.home.ownCopyNotRunning": "A cópia deste computador não está rodando.",
"native.cloud.connectedAppsStay": "Seus apps conectados ficam neste computador: esse servidor não tem um serviço próprio de apps conectados. Conecte-os novamente lá, em “Apps”.",
diff --git a/src/locales/source-hashes.json b/src/locales/source-hashes.json
index d090924706..213626d29a 100644
--- a/src/locales/source-hashes.json
+++ b/src/locales/source-hashes.json
@@ -35194,7 +35194,6 @@
"server.routines.cron.nthWeekend.last": "046ffe9386c0b114850e60f39089b81d443c4a6490b183b550db7d5192cc7d29",
"attach.imagesLeftOutOne": "402e21d809621935f18273f84e146eaf628bcf4876ff041bb53452570a338070",
"attach.imagesLeftOutMany": "e71ffd37f0f2b5342652c635957c44a9b13c0db6ef30129618960a524875bf8f",
- "attach.archivesUnsupported": "b12cc0cb95d16bba1e3ebc915466c1bf00632807ee80572d8ed16b39cc27c71c",
"native.home.ownCopyNotReady": "ce74be60b4fec9f004309f847527956c28a564fda61283f4558109cf0afcbce3",
"native.home.ownCopyNotRunning": "5fd02c26e265964371db2ef1579da50d97ab84471c5f8ea5b72d5fdfcdb6ce5c",
"native.cloud.connectedAppsStay": "c3a421ad14dca5dfd60cbabc57adef08f915dbafd17b810f32183caae51c25ab",
@@ -53938,7 +53937,6 @@
"server.routines.cron.nthWeekend.last": "046ffe9386c0b114850e60f39089b81d443c4a6490b183b550db7d5192cc7d29",
"attach.imagesLeftOutOne": "402e21d809621935f18273f84e146eaf628bcf4876ff041bb53452570a338070",
"attach.imagesLeftOutMany": "e71ffd37f0f2b5342652c635957c44a9b13c0db6ef30129618960a524875bf8f",
- "attach.archivesUnsupported": "b12cc0cb95d16bba1e3ebc915466c1bf00632807ee80572d8ed16b39cc27c71c",
"native.home.ownCopyNotReady": "ce74be60b4fec9f004309f847527956c28a564fda61283f4558109cf0afcbce3",
"native.home.ownCopyNotRunning": "5fd02c26e265964371db2ef1579da50d97ab84471c5f8ea5b72d5fdfcdb6ce5c",
"native.cloud.connectedAppsStay": "c3a421ad14dca5dfd60cbabc57adef08f915dbafd17b810f32183caae51c25ab",
diff --git a/src/locales/zh-tw.json b/src/locales/zh-tw.json
index 6c7a711aab..475560b51e 100644
--- a/src/locales/zh-tw.json
+++ b/src/locales/zh-tw.json
@@ -9439,7 +9439,6 @@
"server.routines.cron.nthWeekend.last": "每月最後一個{weekday} {time}",
"attach.imagesLeftOutOne": "一則訊息最多可附加 {count} 張圖片,總計 {megabytes} MB。{names} 沒有附加。",
"attach.imagesLeftOutMany": "一則訊息最多可附加 {count} 張圖片,總計 {megabytes} MB。{names} 沒有附加。",
- "attach.archivesUnsupported": "無法附加壓縮檔:請解壓縮 {names},再附加裡面的檔案。",
"native.home.ownCopyNotReady": "這台電腦上的 OpenMausBot 還無法切回自己的副本。",
"native.home.ownCopyNotRunning": "這台電腦自己的副本沒有在執行。",
"native.cloud.connectedAppsStay": "你已連線的應用程式會留在這台電腦上:那台伺服器沒有自己的已連線應用程式服務。請到那裡的「應用程式」中重新連線。",