Skip to content

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

fix(files): prove an attachment's folder before deleting it

DeryFerdd93d653f03db
diff --git a/server/attachments.test.ts b/server/attachments.test.ts
index ab6a14060e..11b9779933 100644
--- a/server/attachments.test.ts
+++ b/server/attachments.test.ts
@@ -57,6 +57,7 @@ const {
offerAttachmentClear,
cleanupStaleAttachmentPartials,
deleteAttachment,
+ deleteAttachments,
extensionForFileMime,
extensionForMime,
parseAudioRange,
@@ -354,6 +355,17 @@ describe("aggregate attachment storage", () => {
expect(existsSync(orphan)).toBe(false);
});
+ it("leaves a path outside the attachments folder alone", () => {
+ mkdirSync(ATTACHMENTS_DIR, { recursive: true, mode: 0o700 });
+ const outside = join(DATA_ROOT, "notes.txt");
+ writeFileSync(outside, "keep me");
+
+ deleteAttachments([outside, `${ATTACHMENTS_DIR}/../notes.txt`]);
+
+ expect(existsSync(outside)).toBe(true);
+ expect(readFileSync(outside, "utf8")).toBe("keep me");
+ });
+
it("frees quota after deleteAttachment, without a rescan, for a file the cache already knows about", () => {
const first = saveImage(Buffer.from("x"), "image/png");
truncateSync(first.path, ATTACHMENTS_MAX_BYTES - 2);
diff --git a/server/attachments.ts b/server/attachments.ts
index 89c579737f..6181167b8b 100644
--- a/server/attachments.ts
+++ b/server/attachments.ts
@@ -226,9 +226,11 @@ export function deleteAttachment(path: string): void {
deleteAttachments([path]);
}
-/** deleteAttachment for many files at once. An upload still on disk keeps its box copies; one gone loses them. */
+/** deleteAttachment for many files at once. An upload still on disk keeps its box copies; one gone loses them. A path
+ * outside the attachments folder is not ours to remove, so it is left alone before anything is unlinked. */
export function deleteAttachments(paths: readonly string[]): void {
- const gone = paths.filter((path) => unlinkAttachment(path) && resolve(dirname(path)) === resolve(ATTACHMENTS_DIR)).map((path) => basename(path));
+ const ours = paths.filter((path) => resolve(dirname(path)) === resolve(ATTACHMENTS_DIR));
+ const gone = ours.filter((path) => unlinkAttachment(path)).map((path) => basename(path));
dropBoxUploadCopies(gone);
dropKeptOriginals(gone);
}