Harden media uploads and message attachments

Enforce upload limits and safer media metadata, validate attachment ownership and encrypted-message references, and make message attachment binding idempotent. Add gallery media round-trip coverage and a regression test for closing older sessions.
This commit is contained in:
Artemy
2026-10-24 17:29:22 +07:00
parent a049a80e04
commit 55cc9538f7
6 changed files with 177 additions and 17 deletions
+16
View File
@@ -1,5 +1,7 @@
using Govor.Application.Storage;
using Govor.Domain.Models;
using Govor.Domain.Models.Messages;
using Govor.Application.E2ee;
using Govor.Domain;
using Govor.Domain.Common;
using Microsoft.EntityFrameworkCore;
@@ -169,6 +171,20 @@ public class MediaService : IMediaService
);
}
var message = await _dbContext.Messages.FirstOrDefaultAsync(m => m.Id == messageId);
if (message is null)
return Result<Unit, Error>.Failure(Error.NotFound("File.AttachToMessage", "Message not found."));
if (message.SenderId != mediaFile.UploaderId)
return Result<Unit, Error>.Failure(Error.Forbidden("File.AttachToMessage", "Only the message author's uploads can be attached."));
if (E2eeProtocol.IsEncrypted(message.EncryptedContent) &&
(mediaFile.MineType != E2eeProtocol.EncryptedMimeType ||
!E2eeProtocol.Inspect(message.EncryptedContent).MediaIds.Contains(mediaId)))
return Result<Unit, Error>.Failure(Error.Validation("File.AttachToMessage", "Attachment is not authenticated by the encrypted message."));
if (!await _dbContext.MediaAttachments.AnyAsync(a => a.MediaFileId == mediaId && a.MessageId == messageId))
_dbContext.MediaAttachments.Add(new MediaAttachments
{ Id = Guid.NewGuid(), MediaFileId = mediaId, MessageId = messageId });
mediaFile.OwnerType = MediaOwnerType.Message;
mediaFile.OwnerId = messageId;