Expose chat read positions and E2EE errors

Add optional chat activity and read-position details to the unread-count endpoint. Return stable E2EE rejection codes for stale timestamps and untrusted signing devices, with tests for both behaviors.
This commit is contained in:
Artemy
2026-11-15 21:26:00 +07:00
parent def724e0ee
commit 31d704712c
9 changed files with 108 additions and 8 deletions
+2
View File
@@ -55,6 +55,8 @@ public static class E2eeProtocol
public const int MaxDevices = 500;
public const string EncryptedMimeType = "application/vnd.govor.encrypted";
public const string DevicesChangedError = "E2EE_DEVICES_CHANGED";
public const string TimestampOutOfRangeError = "E2EE_TIMESTAMP_OUT_OF_RANGE";
public const string SigningDeviceRejectedError = "E2EE_SIGNING_DEVICE_REJECTED";
public static readonly JsonSerializerOptions Json = new() { PropertyNameCaseInsensitive = true };
public static bool IsEncrypted(string? value)
+5 -4
View File
@@ -224,9 +224,10 @@ public sealed class E2eeService(GovorDbContext context)
envelope.RecipientType != (int)type || envelope.ReplyToMessageId != replyTo ||
envelope.IsEdit != editedMessage.HasValue ||
(editedMessage.HasValue && envelope.MessageId != editedMessage) ||
!envelope.MediaIds.Order().SequenceEqual(media.Order()) ||
Math.Abs((double)envelope.CreatedAt - DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()) > 300_000)
throw new ArgumentException("E2EE routing, attachment or time context does not match.");
!envelope.MediaIds.Order().SequenceEqual(media.Order()))
throw new ArgumentException("E2EE routing or attachment context does not match.");
if (Math.Abs((double)envelope.CreatedAt - DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()) > 300_000)
throw new ArgumentException(E2eeProtocol.TimestampOutOfRangeError);
var signingSession = await context.UserSessions.AsNoTracking().Include(s => s.CryptoSession)
.ThenInclude(c => c.SignedPreKey).SingleOrDefaultAsync(s => s.Id == envelope.Sender.SessionId &&
@@ -235,7 +236,7 @@ public sealed class E2eeService(GovorDbContext context)
ToDeviceKey(signingSession.CryptoSession, sender).Fingerprint != envelope.Sender.Fingerprint ||
signingSession.CryptoSession.AccountIdentityKey != envelope.Sender.AccountIdentityKey ||
signingSession.CryptoSession.Generation != envelope.Sender.Generation)
throw new UnauthorizedAccessException("E2EE signing device is unknown or revoked.");
throw new UnauthorizedAccessException(E2eeProtocol.SigningDeviceRejectedError);
var directory = await GetConversationKeysAsync(sender, conversation, type);
var expected = directory.Devices.Select(k => k.Fingerprint).Distinct().Order(StringComparer.Ordinal);
if (!expected.SequenceEqual(envelope.Recipients.Order(StringComparer.Ordinal)))