Skip to content

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

fix(files): refuse an attachment name that is a path, not a filename

DeryFerdaea459a1be54
diff --git a/server/attachments.test.ts b/server/attachments.test.ts
index ab6a14060e..fb6aadc5b7 100644
--- a/server/attachments.test.ts
+++ b/server/attachments.test.ts
@@ -66,6 +66,7 @@ const {
saveFile,
saveImage,
saveImageUpload,
+ saveNamedAttachment,
validateAttachmentUploadId,
} = await import("./attachments.ts");
@@ -483,6 +484,16 @@ describe("readAttachment name lock", () => {
});
});
+describe("saveNamedAttachment", () => {
+ it("refuses a name that is not a bare filename, so a synced attachment cannot land outside the folder", async () => {
+ const chunks = (async function* () {
+ yield Buffer.from("x");
+ })();
+
+ await expect(saveNamedAttachment("../escape.png", chunks)).rejects.toThrow(/filename/);
+ });
+});
+
describe("parseAudioRange", () => {
const SIZE = 1000;
diff --git a/server/attachments.ts b/server/attachments.ts
index 89c579737f..8b35be004c 100644
--- a/server/attachments.ts
+++ b/server/attachments.ts
@@ -571,6 +571,10 @@ export async function saveFile(
* upload (507 when it is full), written under a partial name cleanup knows, never over a file already here, and never
* leaving a partial file behind. `stored`: false when the same file was here already. */
export async function saveNamedAttachment(name: string, chunks: AsyncIterable<Uint8Array>, expectedBytes?: number): Promise<{ stored: boolean }> {
+ // Security: the name arrives from another computer, and this join is the only thing between it and the data folder.
+ if (!name || name === "." || name === ".." || name !== basename(name) || name.includes("/") || name.includes("\\")) {
+ throw statusError(400, "name must be a filename, not a path");
+ }
if (expectedBytes !== undefined && expectedBytes > FILE_MAX_BYTES) throw statusError(413, `attachments are limited to ${FILE_MAX_BYTES} bytes`);
ensureAttachmentsDir();
const path = join(ATTACHMENTS_DIR, name);