From fcb5f7934d4d323b981292b94f31f2575da308d7 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Wed, 23 Sep 2026 18:56:59 -0400 Subject: [PATCH] fix: Keep attachments private when S3 ACLs are disabled (#13867) --- .../models/helpers/AttachmentHelper.test.ts | 33 +++++++++++++++++++ server/models/helpers/AttachmentHelper.ts | 4 +-- .../api/attachments/attachments.test.ts | 33 +++++++++++++++++++ 3 files changed, 68 insertions(+), 2 deletions(-) diff --git a/server/models/helpers/AttachmentHelper.test.ts b/server/models/helpers/AttachmentHelper.test.ts index 958f45b45c..20e0322f28 100644 --- a/server/models/helpers/AttachmentHelper.test.ts +++ b/server/models/helpers/AttachmentHelper.test.ts @@ -1,6 +1,39 @@ +import { AttachmentPreset } from "@shared/types"; +import env from "@server/env"; import AttachmentHelper from "./AttachmentHelper"; describe("AttachmentHelper", () => { + describe("presetToAcl", () => { + it("uses a private attachment ACL when S3 object ACLs are disabled", () => { + const original = env.AWS_S3_ACL; + env.AWS_S3_ACL = ""; + + try { + expect( + AttachmentHelper.presetToAcl(AttachmentPreset.DocumentAttachment) + ).toBe("private"); + expect(AttachmentHelper.presetToAcl(AttachmentPreset.Avatar)).toBe( + "public-read" + ); + } finally { + env.AWS_S3_ACL = original; + } + }); + + it("uses a configured attachment ACL", () => { + const original = env.AWS_S3_ACL; + env.AWS_S3_ACL = "public-read"; + + try { + expect( + AttachmentHelper.presetToAcl(AttachmentPreset.DocumentAttachment) + ).toBe("public-read"); + } finally { + env.AWS_S3_ACL = original; + } + }); + }); + describe("getKey", () => { it("should return the correct key for a private attachment", () => { const key = AttachmentHelper.getKey({ diff --git a/server/models/helpers/AttachmentHelper.ts b/server/models/helpers/AttachmentHelper.ts index 49c61fb008..ee073c0d41 100644 --- a/server/models/helpers/AttachmentHelper.ts +++ b/server/models/helpers/AttachmentHelper.ts @@ -70,14 +70,14 @@ export default class AttachmentHelper { * Get the ACL to use for a given attachment preset * * @param preset The preset to use - * @returns A valid S3 ACL + * @returns A valid attachment ACL */ static presetToAcl(preset: AttachmentPreset) { switch (preset) { case AttachmentPreset.Avatar: return "public-read"; default: - return env.AWS_S3_ACL; + return env.AWS_S3_ACL || "private"; } } diff --git a/server/routes/api/attachments/attachments.test.ts b/server/routes/api/attachments/attachments.test.ts index 79cdf7d39e..b52058211e 100644 --- a/server/routes/api/attachments/attachments.test.ts +++ b/server/routes/api/attachments/attachments.test.ts @@ -167,6 +167,39 @@ describe("#attachments.create", () => { expect(res.status).toEqual(200); }); + it("should create a private document attachment when S3 object ACLs are disabled", async () => { + const original = env.AWS_S3_ACL; + env.AWS_S3_ACL = ""; + + try { + const user = await buildUser(); + const document = await buildDocument({ + teamId: user.teamId, + userId: user.id, + }); + + const res = await server.post("/api/attachments.create", user, { + body: { + name: "test.png", + contentType: "image/png", + size: 1000, + documentId: document.id, + preset: AttachmentPreset.DocumentAttachment, + }, + }); + + expect(res.status).toEqual(200); + const body = await res.json(); + const attachment = await Attachment.findByPk(body.data.attachment.id, { + rejectOnEmpty: true, + }); + expect(attachment.acl).toBe("private"); + expect(body.data.form).toBeDefined(); + } finally { + env.AWS_S3_ACL = original; + } + }); + it("should return PUT data when AWS_S3_UPLOAD_METHOD is put", async () => { const original = env.AWS_S3_UPLOAD_METHOD; env.AWS_S3_UPLOAD_METHOD = "put";