mirror of
https://github.com/Govor-team/Govor.git
synced 2026-11-18 05:15:06 +00:00
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:
@@ -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;
|
||||
|
||||
|
||||
@@ -48,7 +48,8 @@ public class MessageSendingService : IMessageSendingService
|
||||
{
|
||||
var media = sendParams.Media?.ToArray() ?? [];
|
||||
if ((string.IsNullOrWhiteSpace(sendParams.EncryptContent) && media.Length == 0) ||
|
||||
(sendParams.EncryptContent?.Length ?? 0) > E2eeProtocol.MaxEnvelopeLength || media.Length > 100)
|
||||
(sendParams.EncryptContent?.Length ?? 0) > E2eeProtocol.MaxEnvelopeLength || media.Length > 100 ||
|
||||
media.Any(m => m.MediaId == Guid.Empty) || media.Select(m => m.MediaId).Distinct().Count() != media.Length)
|
||||
return new SendMessageResult(false, new ArgumentException("Invalid message content or attachment count."), default);
|
||||
|
||||
var validationResult = sendParams.RecipientType switch
|
||||
@@ -67,7 +68,11 @@ public class MessageSendingService : IMessageSendingService
|
||||
var completed = await _dbContext.Messages.AsNoTracking().Include(m => m.MediaAttachments)
|
||||
.ThenInclude(m => m.MediaFile).FirstOrDefaultAsync(m => m.Id == operationId);
|
||||
if (completed is not null)
|
||||
return completed.SenderId == sendParams.FromUserId && completed.EncryptedContent == sendParams.EncryptContent
|
||||
return completed.SenderId == sendParams.FromUserId && completed.EncryptedContent == sendParams.EncryptContent &&
|
||||
completed.RecipientId == sendParams.RecipientId && completed.RecipientType == sendParams.RecipientType &&
|
||||
completed.ReplyToMessageId == sendParams.ReplyToMessageId &&
|
||||
completed.MediaAttachments.Select(a => a.MediaFileId).Order().SequenceEqual(media.Select(a => a.MediaId).Order()) &&
|
||||
(!E2eeProtocol.IsEncrypted(sendParams.EncryptContent) || media.All(m => string.IsNullOrEmpty(m.EncryptedKey)))
|
||||
? new SendMessageResult(true, null, completed)
|
||||
: new SendMessageResult(false, new ArgumentException("Message ID already exists."), default);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user