fix: Delete emoji attachment row in transaction and prevent shared attachments (#14768)

This commit is contained in:
Tom Moor authored and GitHub committed 2026-11-26 19:16:40 -05:00
1 parent de93d83041
commit 63de3fcd37
2 files changed
+79 -9

No files matched your search

+38 -9
View File
@@ -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<Emoji>
): Promise<void> {
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<void> {
const attachment = await Attachment.findByPk(model.attachmentId, {
transaction: options.transaction,
});
await attachment?.destroy({ transaction: options.transaction });
}
}
+41
View File
@@ -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();
});
});