Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Improve smtp server logic (#117)
Leendert de Borst486dc67f9458
diff --git a/src/Services/AliasVault.SmtpService/Handlers/DatabaseMessageStore.cs b/src/Services/AliasVault.SmtpService/Handlers/DatabaseMessageStore.csindex 9cdad7ed2..0b464fa63 100644--- a/src/Services/AliasVault.SmtpService/Handlers/DatabaseMessageStore.cs+++ b/src/Services/AliasVault.SmtpService/Handlers/DatabaseMessageStore.cs@@ -40,108 +40,42 @@ public class DatabaseMessageStore(ILogger<DatabaseMessageStore> logger, Config c{try{- await using var stream = new MemoryStream();-- var position = buffer.GetPosition(0);- while (buffer.TryGet(ref position, out var memory))- {- stream.Write(memory.Span);- }-- // Max email filesize limit: 10MB. If the mail is larger in size, reject it.- // Because of base64 encoding which has approx 33% increase in binary size- // we multiply the limit by 1.4 to be safe.+ // Check email size limitvar maxEmailSizeInMegabytes = 10;- if (stream.Length > ((maxEmailSizeInMegabytes * 1024 * 1024) * 1.4))+ var maxEmailSizeInBytes = (long)((maxEmailSizeInMegabytes * 1024 * 1024) * 1.4);+ if (buffer.Length > maxEmailSizeInBytes){return SmtpResponse.SizeLimitExceeded;}- stream.Position = 0;- var message = await MimeMessage.LoadAsync(stream, cancellationToken);+ var message = await LoadMessageFromBuffer(buffer, cancellationToken);// Retrieve all addresses from the SMTP transaction which should contain all recipients for this mail instance.var allAddresses = transaction.To.Distinct().ToList();- // Limit list to 15 addresses maximum (to prevent mailbomb spam abuse).+ // Limit list to 15 addresses maximum to prevent mailbomb/spam abuse.var toAddresses = allAddresses.Take(15).ToList();+ var toAddressesCount = toAddresses.Count;+ var toAddressesFailCount = 0;foreach (var toAddress in toAddresses){- // Check if toAddress domain is allowed.- if (toAddress is null || !config.AllowedToDomains.Contains(toAddress.Host.ToLowerInvariant()))+ // Process the email for each recipient separately.+ var process = await ProcessEmailForRecipient(message, toAddress);+ if (!process){- // ToAddress domain is not allowed.- if (toAddresses.Count > 1)- {- // If more recipients, silently skip this one.- continue;- }-- // If only one recipient, return error.- logger.LogWarning(- "Rejected email: email for {ToAddress} is not allowed. Domain not in allowed domain list.",- toAddress?.User + "@" + toAddress?.Host);- return SmtpResponse.NoValidRecipientsGiven;- }-- // Check if the local part of the toAddress is a known alias (claimed by a user)- var dbContext = await dbContextFactory.CreateDbContextAsync(CancellationToken.None);- var toAddressLocal = toAddress.User.ToLowerInvariant();- var toAddressDomain = toAddress.Host.ToLowerInvariant();- var userEmailClaim = await dbContext.UserEmailClaims- .FirstOrDefaultAsync(- x =>- x.AddressLocal == toAddressLocal &&- x.AddressDomain == toAddressDomain,- CancellationToken.None);-- if (userEmailClaim is null)- {- // Email address has no user claim with corresponding encryption key so we cannot process it.- if (toAddresses.Count > 1)- {- // If more recipients, silently skip this one.- continue;- }-- // If only one recipient, return error.- logger.LogWarning(- "Rejected email: email for {ToAddress} is not allowed. No user claim on this ToAddress.",- toAddress.User + "@" + toAddress.Host);- return SmtpResponse.NoValidRecipientsGiven;+ toAddressesFailCount++;}- // Retrieve user public encryption key from database- var userPublicKey = await dbContext.UserEncryptionKeys.FirstOrDefaultAsync(- x =>- x.UserId == userEmailClaim.UserId && x.IsPrimary,- CancellationToken.None);-- if (userPublicKey is null)+ // If all recipients failed, return error to sender.+ if (toAddressesFailCount == toAddressesCount){- // Email address has no user claim with corresponding encryption key so we cannot process it.- if (toAddresses.Count > 1)- {- // If more recipients, silently skip this one.- continue;- }-- // If only one recipient, return error.- logger.LogCritical(- "Rejected email: email for {ToAddress} cannot be processed. No primary encryption key found for this user.",- toAddress.User + "@" + toAddress.Host);+ // No valid recipients given.+ logger.LogWarning("No valid recipients in email, returning error to sender.");return SmtpResponse.NoValidRecipientsGiven;}-- // Set the "to" for the email to the actual one we are looping through now.- var insertedId = await InsertEmailIntoDatabase(message, new MailAddress(toAddress.AsAddress()), userPublicKey);- logger.LogInformation(- "Email for {ToAddress} successfully saved into database with ID {insertedId}.",- toAddress.User + "@" + toAddress.Host,- insertedId);}return SmtpResponse.Ok;@@ -153,6 +87,26 @@ public class DatabaseMessageStore(ILogger<DatabaseMessageStore> logger, Config c}}+ /// <summary>+ /// Load the email message from the buffer.+ /// </summary>+ /// <param name="buffer">Buffer which contains the email contents.</param>+ /// <param name="cancellationToken">CancellationToken instance.</param>+ /// <returns>MimeMessage.</returns>+ private static async Task<MimeMessage> LoadMessageFromBuffer(ReadOnlySequence<byte> buffer, CancellationToken cancellationToken)+ {+ await using var stream = new MemoryStream();++ var position = buffer.GetPosition(0);+ while (buffer.TryGet(ref position, out var memory))+ {+ stream.Write(memory.Span);+ }++ stream.Position = 0;+ return await MimeMessage.LoadAsync(stream, cancellationToken);+ }+/// <summary>/// Convert MimeMessage to Email database object./// </summary>@@ -328,6 +282,68 @@ public class DatabaseMessageStore(ILogger<DatabaseMessageStore> logger, Config c}}+ /// <summary>+ /// Process email for recipient separately.+ /// </summary>+ /// <param name="message">MimeMessage.</param>+ /// <param name="toAddress">ToAddress.</param>+ /// <returns>True if success or silent skip, false if SmtpResponse.NoValidRecipientsGiven should be triggered.</returns>+ private async Task<bool> ProcessEmailForRecipient(MimeMessage message, IMailbox toAddress)+ {+ // Check if toAddress domain is allowed.+ if (!config.AllowedToDomains.Contains(toAddress.Host.ToLowerInvariant()))+ {+ // ToAddress domain is not allowed.+ logger.LogWarning(+ "Rejected email: email for {ToAddress} is not allowed. Domain not in allowed domain list.",+ toAddress?.User + "@" + toAddress?.Host);+ return false;+ }++ // Check if the local part of the toAddress is a known alias (claimed by a user)+ var dbContext = await dbContextFactory.CreateDbContextAsync(CancellationToken.None);+ var toAddressLocal = toAddress.User.ToLowerInvariant();+ var toAddressDomain = toAddress.Host.ToLowerInvariant();+ var userEmailClaim = await dbContext.UserEmailClaims+ .FirstOrDefaultAsync(+ x =>+ x.AddressLocal == toAddressLocal &&+ x.AddressDomain == toAddressDomain,+ CancellationToken.None);++ if (userEmailClaim is null)+ {+ // Email address has no user claim with corresponding encryption key so we cannot process it.+ logger.LogWarning(+ "Rejected email: email for {ToAddress} is not allowed. No user claim on this ToAddress.",+ toAddress.User + "@" + toAddress.Host);+ return false;+ }++ // Retrieve user public encryption key from database+ var userPublicKey = await dbContext.UserEncryptionKeys.FirstOrDefaultAsync(+ x =>+ x.UserId == userEmailClaim.UserId && x.IsPrimary,+ CancellationToken.None);++ if (userPublicKey is null)+ {+ // Email address has no user claim with corresponding encryption key so we cannot process it.+ logger.LogCritical(+ "Rejected email: email for {ToAddress} cannot be processed. No primary encryption key found for this user.",+ toAddress.User + "@" + toAddress.Host);+ return false;+ }++ // Set the "to" for the email to the actual one we are looping through now.+ var insertedId = await InsertEmailIntoDatabase(message, new MailAddress(toAddress.AsAddress()), userPublicKey);+ logger.LogInformation(+ "Email for {ToAddress} successfully saved into database with ID {insertedId}.",+ toAddress.User + "@" + toAddress.Host,+ insertedId);+ return true;+ }+/// <summary>/// Insert email into database./// </summary>diff --git a/src/Tests/AliasVault.E2ETests/Tests/Client/EmailDecryptionTest.cs b/src/Tests/AliasVault.E2ETests/Tests/Client/EmailDecryptionTest.csindex 366368d6c..890c2eb71 100644--- a/src/Tests/AliasVault.E2ETests/Tests/Client/EmailDecryptionTest.cs+++ b/src/Tests/AliasVault.E2ETests/Tests/Client/EmailDecryptionTest.cs@@ -68,7 +68,7 @@ public class EmailDecryptionTest : ClientPlaywrightTest// Assert that the users public key was created on the server.var publicKey = await ApiDbContext.UserEncryptionKeys.Where(x => x.UserId == claim.UserId).FirstOrDefaultAsync();Assert.That(publicKey, Is.Not.Null, "Public key for user not found in database. Check if public key creation is working correctly.");- Assert.That(publicKey.PublicKey.Length, Is.GreaterThanOrEqualTo(100), "Public key exists but length does not match expected. Check if public key creation is working correctly.");+ Assert.That(publicKey.PublicKey, Has.Length.GreaterThanOrEqualTo(100), "Public key exists but length does not match expected. Check if public key creation is working correctly.");// Email the SMTP server which will save the email in encrypted form in the database..var message = new MimeMessage();diff --git a/src/Tests/AliasVault.UnitTests/Utilities/RsaEncryptionTests.cs b/src/Tests/AliasVault.UnitTests/Utilities/RsaEncryptionTests.csindex 30b2f3add..fe5bb0b0e 100644--- a/src/Tests/AliasVault.UnitTests/Utilities/RsaEncryptionTests.cs+++ b/src/Tests/AliasVault.UnitTests/Utilities/RsaEncryptionTests.cs@@ -62,13 +62,16 @@ public class RsaEncryptionTests[Test]public void GenerateRsaKeyPair_ShouldReturnValidKeyPair(){- Assert.That(PublicKey, Is.Not.Null);- Assert.That(PrivateKey, Is.Not.Null);- Assert.That(PublicKey, Is.Not.EqualTo(PrivateKey));-- // Verify that the keys are in valid JSON format- Assert.That(() => JsonSerializer.Deserialize<Dictionary<string, object>>(PublicKey), Throws.Nothing);- Assert.That(() => JsonSerializer.Deserialize<Dictionary<string, object>>(PrivateKey), Throws.Nothing);+ Assert.Multiple(() =>+ {+ Assert.That(PublicKey, Is.Not.Null);+ Assert.That(PrivateKey, Is.Not.Null);+ Assert.That(PublicKey, Is.Not.EqualTo(PrivateKey));++ // Verify that the keys are in valid JSON format+ Assert.That(() => JsonSerializer.Deserialize<Dictionary<string, object>>(PublicKey), Throws.Nothing);+ Assert.That(() => JsonSerializer.Deserialize<Dictionary<string, object>>(PrivateKey), Throws.Nothing);+ });}/// <summary>@@ -78,7 +81,7 @@ public class RsaEncryptionTestspublic void GenerateRandomSymmetricKey_ReturnsCorrectLength(){var key = Encryption.GenerateRandomSymmetricKey();- Assert.That(key.Length, Is.EqualTo(32), "The generated key should be 32 bytes (256 bits) long.");+ Assert.That(key, Has.Length.EqualTo(32), "The generated key should be 32 bytes (256 bits) long.");}/// <summary>