Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Add SRP validate endpoint sanity checks to prevent potentially incorrect lockouts (#2340)
Leendert de Borst8988b4f3e7a2
diff --git a/apps/server/AliasVault.Api/Controllers/AuthController.cs b/apps/server/AliasVault.Api/Controllers/AuthController.csindex 9e65abc86..800583240 100644--- a/apps/server/AliasVault.Api/Controllers/AuthController.cs+++ b/apps/server/AliasVault.Api/Controllers/AuthController.cs@@ -883,10 +883,10 @@ public class AuthController(IAliasServerDbContextFactory dbContextFactory, UserM}// Validate the SRP session (actual password check).- var serverSession = AuthHelper.ValidateSrpSession(cache, user, model.ClientPublicEphemeral, model.ClientSessionProof);+ var (serverSession, activeSessionFound) = AuthHelper.ValidateSrpSession(cache, user, model.ClientPublicEphemeral, model.ClientSessionProof);if (serverSession is null){- await authLoggingService.LogAuthEventFailAsync(user.UserName!, AuthEventType.AccountDeletion, AuthFailureReason.InvalidPassword);+ await authLoggingService.LogAuthEventFailAsync(user.UserName!, AuthEventType.AccountDeletion, activeSessionFound ? AuthFailureReason.InvalidPassword : AuthFailureReason.SrpSessionNotFound);return BadRequest(ApiErrorCodeHelper.CreateValidationErrorResponse(ApiErrorCode.PASSWORD_MISMATCH, 400));}@@ -1046,13 +1046,17 @@ public class AuthController(IAliasServerDbContextFactory dbContextFactory, UserM}// Validate the SRP session (actual password check).- var serverSession = AuthHelper.ValidateSrpSession(cache, user, model.ClientPublicEphemeral, model.ClientSessionProof);+ var (serverSession, activeSessionFound) = AuthHelper.ValidateSrpSession(cache, user, model.ClientPublicEphemeral, model.ClientSessionProof);if (serverSession is null){- // Increment failed login attempts in order to lock out the account when the limit is reached.- await userManager.AccessFailedAsync(user);+ if (activeSessionFound)+ {+ // Incorrect password: increment failed login attempts which then locks out+ // the account when the limit is reached.+ await userManager.AccessFailedAsync(user);+ }- await authLoggingService.LogAuthEventFailAsync(user.UserName!, AuthEventType.Login, AuthFailureReason.InvalidPassword);+ await authLoggingService.LogAuthEventFailAsync(user.UserName!, AuthEventType.Login, activeSessionFound ? AuthFailureReason.InvalidPassword : AuthFailureReason.SrpSessionNotFound);return (null, null, BadRequest(ApiErrorCodeHelper.CreateValidationErrorResponse(ApiErrorCode.USER_NOT_FOUND, 400)));}diff --git a/apps/server/AliasVault.Api/Controllers/VaultController.cs b/apps/server/AliasVault.Api/Controllers/VaultController.csindex 6efd11c8e..590775997 100644--- a/apps/server/AliasVault.Api/Controllers/VaultController.cs+++ b/apps/server/AliasVault.Api/Controllers/VaultController.cs@@ -247,13 +247,17 @@ public class VaultController(ILogger<VaultController> logger, IAliasServerDbCont}// Validate the SRP session (actual password check).- var serverSession = AuthHelper.ValidateSrpSession(cache, user, model.CurrentClientPublicEphemeral, model.CurrentClientSessionProof);+ var (serverSession, activeSessionFound) = AuthHelper.ValidateSrpSession(cache, user, model.CurrentClientPublicEphemeral, model.CurrentClientSessionProof);if (serverSession is null){- // Increment failed login attempts in order to lock out the account when the limit is reached.- await GetUserManager().AccessFailedAsync(user);+ if (activeSessionFound)+ {+ // The password was wrong: increment failed login attempts which then locks out+ // the account when the limit is reached.+ await GetUserManager().AccessFailedAsync(user);+ }- await authLoggingService.LogAuthEventFailAsync(user.UserName!, AuthEventType.PasswordChange, AuthFailureReason.InvalidPassword);+ await authLoggingService.LogAuthEventFailAsync(user.UserName!, AuthEventType.PasswordChange, activeSessionFound ? AuthFailureReason.InvalidPassword : AuthFailureReason.SrpSessionNotFound);return BadRequest(ApiErrorCodeHelper.CreateValidationErrorResponse(ApiErrorCode.PASSWORD_MISMATCH, 400));}diff --git a/apps/server/AliasVault.Api/Helpers/AuthHelper.cs b/apps/server/AliasVault.Api/Helpers/AuthHelper.csindex d79b263ae..7319b3347 100644--- a/apps/server/AliasVault.Api/Helpers/AuthHelper.cs+++ b/apps/server/AliasVault.Api/Helpers/AuthHelper.cs@@ -35,15 +35,16 @@ public static class AuthHelper/// <param name="user">The user object.</param>/// <param name="clientEphemeral">The client ephemeral value.</param>/// <param name="clientSessionProof">The client session proof.</param>- /// <returns>SrpSession if validation succeeds, null otherwise.</returns>- public static SrpSession? ValidateSrpSession(IMemoryCache cache, AliasVaultUser user, string clientEphemeral, string clientSessionProof)+ /// <returns>Tuple with the SrpSession (null if validation failed) and whether an active SRP session existed.</returns>+ public static (SrpSession? Session, bool ActiveSessionFound) ValidateSrpSession(IMemoryCache cache, AliasVaultUser user, string clientEphemeral, string clientSessionProof){// Get or create SRP identity. For existing users without SrpIdentity, fall back to username (lowercase).var srpIdentity = user.SrpIdentity ?? user.UserName!.ToLowerInvariant();if (!cache.TryGetValue(CachePrefixEphemeral + srpIdentity, out var serverSecretEphemeral) || serverSecretEphemeral is not string){- return null;+ // No login was initiated for this user, or the server ephemeral has expired. Return false to indicate that no active session was found.+ return (null, false);}// Retrieve latest vault of user which contains the current salt and verifier.@@ -59,12 +60,8 @@ public static class AuthHelperlatestVaultEncryptionSettings.Verifier,clientSessionProof);- if (serverSession is null)- {- return null;- }-- return serverSession;+ // If validation failed, serverSession will be null here.+ return (serverSession, true);}/// <summary>diff --git a/apps/server/Databases/AliasServerDb/AuthLog.cs b/apps/server/Databases/AliasServerDb/AuthLog.csindex fa2589f68..14590d599 100644--- a/apps/server/Databases/AliasServerDb/AuthLog.cs+++ b/apps/server/Databases/AliasServerDb/AuthLog.cs@@ -62,11 +62,16 @@ public enum AuthFailureReason/// </summary>IpBlocked = 9,+ /// <summary>+ /// Indicates that no active SRP session existed for the user, meaning no login was initiated or it expired.+ /// </summary>+ SrpSessionNotFound = 10,+/// <summary>/// Indicates that the mobile login attempt was blocked because the client created too many/// mobile login requests in a short period./// </summary>- MobileLoginRateLimitExceeded = 10,+ MobileLoginRateLimitExceeded = 11,/// <summary>/// Indicates that the failure reason was unknown.diff --git a/apps/server/Utilities/Cryptography/AliasVault.Cryptography.Client/Srp.cs b/apps/server/Utilities/Cryptography/AliasVault.Cryptography.Client/Srp.csindex 7eaa749bf..fa6d0a466 100644--- a/apps/server/Utilities/Cryptography/AliasVault.Cryptography.Client/Srp.cs+++ b/apps/server/Utilities/Cryptography/AliasVault.Cryptography.Client/Srp.cs@@ -128,6 +128,11 @@ public static class Srp// Incorrect password provided, return null.return null;}+ catch (FormatException)+ {+ // Malformed (non-hexadecimal) ephemeral or proof provided, return null.+ return null;+ }}/// <summary>