From 63de3fcd3701c253e9c2d2e6baff4a4a9486392e Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Thu, 26 Nov 2026 19:16:40 -0500 Subject: [PATCH] fix: Delete emoji attachment row in transaction and prevent shared attachments (#14768) --- server/models/Emoji.ts | 47 ++++++++++++++++++++----- server/routes/api/emojis/emojis.test.ts | 41 +++++++++++++++++++++ 2 files changed, 79 insertions(+), 9 deletions(-) diff --git a/server/models/Emoji.ts b/server/models/Emoji.ts index 6c8ac52329..070b77fb2a 100644 --- a/server/models/Emoji.ts +++ b/server/models/Emoji.ts @@ -1,8 +1,13 @@ -import type { InferAttributes, InferCreationAttributes } from "sequelize"; -import { type SaveOptions } from "sequelize"; +import type { + InferAttributes, + InferCreationAttributes, + InstanceDestroyOptions, +} from "sequelize"; +import { Op, type SaveOptions } from "sequelize"; import { + AfterDestroy, BeforeCreate, - BeforeDestroy, + BeforeSave, BelongsTo, Column, DataType, @@ -16,7 +21,6 @@ import Team from "./Team"; import User from "./User"; import IdModel from "./base/IdModel"; import Length from "./validators/Length"; -import FileStorage from "@server/storage/files"; import Attachment from "./Attachment"; @Table({ tableName: "emojis", modelName: "emoji" }) @@ -76,12 +80,37 @@ class Emoji extends IdModel< } } - @BeforeDestroy - static async deleteAttachmentFromS3(model: Emoji) { - const attachment = await Attachment.findByPk(model.attachmentId); - if (attachment) { - await FileStorage.deleteFile(attachment.key); + @BeforeSave + static async checkUniqueAttachment( + model: Emoji, + options: SaveOptions + ): Promise { + if (!model.changed("attachmentId")) { + return; } + + const existingEmoji = await this.findOne({ + where: { + attachmentId: model.attachmentId, + id: { [Op.ne]: model.id }, + }, + transaction: options.transaction, + }); + + if (existingEmoji) { + throw ValidationError("Attachment is already used by another emoji"); + } + } + + @AfterDestroy + static async deleteAttachment( + model: Emoji, + options: InstanceDestroyOptions + ): Promise { + const attachment = await Attachment.findByPk(model.attachmentId, { + transaction: options.transaction, + }); + await attachment?.destroy({ transaction: options.transaction }); } } diff --git a/server/routes/api/emojis/emojis.test.ts b/server/routes/api/emojis/emojis.test.ts index 4e3a712e07..57bafa2fb5 100644 --- a/server/routes/api/emojis/emojis.test.ts +++ b/server/routes/api/emojis/emojis.test.ts @@ -179,6 +179,29 @@ describe("#emojis.update", () => { expect(await Attachment.findByPk(emoji.attachmentId)).not.toBeNull(); }); + it("should not allow using another emoji's attachment", async () => { + const user = await buildUser(); + const emoji = await buildEmoji({ + teamId: user.teamId, + createdById: user.id, + }); + const other = await buildEmoji({ + teamId: user.teamId, + createdById: user.id, + }); + + const res = await server.post("/api/emojis.update", user, { + body: { + id: emoji.id, + attachmentId: other.attachmentId, + }, + }); + + expect(res.status).toEqual(400); + expect(await Emoji.findByPk(other.id)).not.toBeNull(); + expect(await Attachment.findByPk(emoji.attachmentId)).not.toBeNull(); + }); + it("should return 404 for non-existent attachment", async () => { const user = await buildUser(); const emoji = await buildEmoji({ @@ -260,3 +283,21 @@ describe("#emojis.create", () => { expect(await Emoji.count({ where: { teamId: user.teamId } })).toEqual(0); }); }); + +describe("#emojis.delete", () => { + it("should delete the emoji and its attachment", async () => { + const user = await buildUser(); + const emoji = await buildEmoji({ + teamId: user.teamId, + createdById: user.id, + }); + + const res = await server.post("/api/emojis.delete", user, { + body: { id: emoji.id }, + }); + + expect(res.status).toEqual(200); + expect(await Emoji.findByPk(emoji.id)).toBeNull(); + expect(await Attachment.findByPk(emoji.attachmentId)).toBeNull(); + }); +});