Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Update username validation (#1006)
Leendert de Borstcbacd7486af0
diff --git a/apps/browser-extension/src/i18n/locales/de.json b/apps/browser-extension/src/i18n/locales/de.jsonindex 1ace2fde1..3d3a907c0 100644--- a/apps/browser-extension/src/i18n/locales/de.json+++ b/apps/browser-extension/src/i18n/locales/de.json@@ -128,7 +128,6 @@"USERNAME_EMPTY_OR_WHITESPACE": "Username cannot be empty or whitespace.","USERNAME_TOO_SHORT": "Username too short: must be at least 3 characters long.","USERNAME_TOO_LONG": "Username too long: cannot be longer than 40 characters.",- "USERNAME_ADMIN_NOT_ALLOWED": "Username 'admin' is not allowed.","USERNAME_INVALID_EMAIL": "Invalid email address.","USERNAME_INVALID_CHARACTERS": "Username is invalid, can only contain letters or digits.","VAULT_NOT_UP_TO_DATE": "Your vault is not up-to-date. Please synchronize your vault and try again.",diff --git a/apps/browser-extension/src/i18n/locales/en.json b/apps/browser-extension/src/i18n/locales/en.jsonindex 1ace2fde1..3d3a907c0 100644--- a/apps/browser-extension/src/i18n/locales/en.json+++ b/apps/browser-extension/src/i18n/locales/en.json@@ -128,7 +128,6 @@"USERNAME_EMPTY_OR_WHITESPACE": "Username cannot be empty or whitespace.","USERNAME_TOO_SHORT": "Username too short: must be at least 3 characters long.","USERNAME_TOO_LONG": "Username too long: cannot be longer than 40 characters.",- "USERNAME_ADMIN_NOT_ALLOWED": "Username 'admin' is not allowed.","USERNAME_INVALID_EMAIL": "Invalid email address.","USERNAME_INVALID_CHARACTERS": "Username is invalid, can only contain letters or digits.","VAULT_NOT_UP_TO_DATE": "Your vault is not up-to-date. Please synchronize your vault and try again.",diff --git a/apps/browser-extension/src/i18n/locales/es.json b/apps/browser-extension/src/i18n/locales/es.jsonindex 1ace2fde1..3d3a907c0 100644--- a/apps/browser-extension/src/i18n/locales/es.json+++ b/apps/browser-extension/src/i18n/locales/es.json@@ -128,7 +128,6 @@"USERNAME_EMPTY_OR_WHITESPACE": "Username cannot be empty or whitespace.","USERNAME_TOO_SHORT": "Username too short: must be at least 3 characters long.","USERNAME_TOO_LONG": "Username too long: cannot be longer than 40 characters.",- "USERNAME_ADMIN_NOT_ALLOWED": "Username 'admin' is not allowed.","USERNAME_INVALID_EMAIL": "Invalid email address.","USERNAME_INVALID_CHARACTERS": "Username is invalid, can only contain letters or digits.","VAULT_NOT_UP_TO_DATE": "Your vault is not up-to-date. Please synchronize your vault and try again.",diff --git a/apps/browser-extension/src/i18n/locales/fr.json b/apps/browser-extension/src/i18n/locales/fr.jsonindex 1ace2fde1..3d3a907c0 100644--- a/apps/browser-extension/src/i18n/locales/fr.json+++ b/apps/browser-extension/src/i18n/locales/fr.json@@ -128,7 +128,6 @@"USERNAME_EMPTY_OR_WHITESPACE": "Username cannot be empty or whitespace.","USERNAME_TOO_SHORT": "Username too short: must be at least 3 characters long.","USERNAME_TOO_LONG": "Username too long: cannot be longer than 40 characters.",- "USERNAME_ADMIN_NOT_ALLOWED": "Username 'admin' is not allowed.","USERNAME_INVALID_EMAIL": "Invalid email address.","USERNAME_INVALID_CHARACTERS": "Username is invalid, can only contain letters or digits.","VAULT_NOT_UP_TO_DATE": "Your vault is not up-to-date. Please synchronize your vault and try again.",diff --git a/apps/browser-extension/src/i18n/locales/nl.json b/apps/browser-extension/src/i18n/locales/nl.jsonindex 5a5d1e520..1f578bfe2 100644--- a/apps/browser-extension/src/i18n/locales/nl.json+++ b/apps/browser-extension/src/i18n/locales/nl.json@@ -127,7 +127,6 @@"USERNAME_EMPTY_OR_WHITESPACE": "Gebruikersnaam mag niet leeg zijn of alleen uit spaties bestaan.","USERNAME_TOO_SHORT": "Gebruikersnaam te kort: moet minimaal 3 tekens lang zijn.","USERNAME_TOO_LONG": "Gebruikersnaam te lang: mag niet langer zijn dan 40 tekens.",- "USERNAME_ADMIN_NOT_ALLOWED": "Gebruikersnaam 'admin' is niet toegestaan.","USERNAME_INVALID_EMAIL": "Ongeldig e-mailadres.","USERNAME_INVALID_CHARACTERS": "Gebruikersnaam is ongeldig, mag alleen letters of cijfers bevatten.","VAULT_NOT_UP_TO_DATE": "Uw kluis is niet up-to-date. Synchroniseer uw kluis en probeer het opnieuw.",diff --git a/apps/browser-extension/src/i18n/locales/uk.json b/apps/browser-extension/src/i18n/locales/uk.jsonindex 1ace2fde1..3d3a907c0 100644--- a/apps/browser-extension/src/i18n/locales/uk.json+++ b/apps/browser-extension/src/i18n/locales/uk.json@@ -128,7 +128,6 @@"USERNAME_EMPTY_OR_WHITESPACE": "Username cannot be empty or whitespace.","USERNAME_TOO_SHORT": "Username too short: must be at least 3 characters long.","USERNAME_TOO_LONG": "Username too long: cannot be longer than 40 characters.",- "USERNAME_ADMIN_NOT_ALLOWED": "Username 'admin' is not allowed.","USERNAME_INVALID_EMAIL": "Invalid email address.","USERNAME_INVALID_CHARACTERS": "Username is invalid, can only contain letters or digits.","VAULT_NOT_UP_TO_DATE": "Your vault is not up-to-date. Please synchronize your vault and try again.",diff --git a/apps/mobile-app/i18n/locales/de.json b/apps/mobile-app/i18n/locales/de.jsonindex 944389dde..4c96ed434 100644--- a/apps/mobile-app/i18n/locales/de.json+++ b/apps/mobile-app/i18n/locales/de.json@@ -276,7 +276,6 @@"USERNAME_EMPTY_OR_WHITESPACE": "Username cannot be empty or whitespace.","USERNAME_TOO_SHORT": "Username too short: must be at least 3 characters long.","USERNAME_TOO_LONG": "Username too long: cannot be longer than 40 characters.",- "USERNAME_ADMIN_NOT_ALLOWED": "Username 'admin' is not allowed.","USERNAME_INVALID_EMAIL": "Invalid email address.","USERNAME_INVALID_CHARACTERS": "Username is invalid, can only contain letters or digits.","VAULT_NOT_UP_TO_DATE": "Your vault is not up-to-date. Please synchronize your vault and try again.",diff --git a/apps/mobile-app/i18n/locales/en.json b/apps/mobile-app/i18n/locales/en.jsonindex 944389dde..4c96ed434 100644--- a/apps/mobile-app/i18n/locales/en.json+++ b/apps/mobile-app/i18n/locales/en.json@@ -276,7 +276,6 @@"USERNAME_EMPTY_OR_WHITESPACE": "Username cannot be empty or whitespace.","USERNAME_TOO_SHORT": "Username too short: must be at least 3 characters long.","USERNAME_TOO_LONG": "Username too long: cannot be longer than 40 characters.",- "USERNAME_ADMIN_NOT_ALLOWED": "Username 'admin' is not allowed.","USERNAME_INVALID_EMAIL": "Invalid email address.","USERNAME_INVALID_CHARACTERS": "Username is invalid, can only contain letters or digits.","VAULT_NOT_UP_TO_DATE": "Your vault is not up-to-date. Please synchronize your vault and try again.",diff --git a/apps/mobile-app/i18n/locales/es.json b/apps/mobile-app/i18n/locales/es.jsonindex 944389dde..4c96ed434 100644--- a/apps/mobile-app/i18n/locales/es.json+++ b/apps/mobile-app/i18n/locales/es.json@@ -276,7 +276,6 @@"USERNAME_EMPTY_OR_WHITESPACE": "Username cannot be empty or whitespace.","USERNAME_TOO_SHORT": "Username too short: must be at least 3 characters long.","USERNAME_TOO_LONG": "Username too long: cannot be longer than 40 characters.",- "USERNAME_ADMIN_NOT_ALLOWED": "Username 'admin' is not allowed.","USERNAME_INVALID_EMAIL": "Invalid email address.","USERNAME_INVALID_CHARACTERS": "Username is invalid, can only contain letters or digits.","VAULT_NOT_UP_TO_DATE": "Your vault is not up-to-date. Please synchronize your vault and try again.",diff --git a/apps/mobile-app/i18n/locales/fr.json b/apps/mobile-app/i18n/locales/fr.jsonindex 944389dde..4c96ed434 100644--- a/apps/mobile-app/i18n/locales/fr.json+++ b/apps/mobile-app/i18n/locales/fr.json@@ -276,7 +276,6 @@"USERNAME_EMPTY_OR_WHITESPACE": "Username cannot be empty or whitespace.","USERNAME_TOO_SHORT": "Username too short: must be at least 3 characters long.","USERNAME_TOO_LONG": "Username too long: cannot be longer than 40 characters.",- "USERNAME_ADMIN_NOT_ALLOWED": "Username 'admin' is not allowed.","USERNAME_INVALID_EMAIL": "Invalid email address.","USERNAME_INVALID_CHARACTERS": "Username is invalid, can only contain letters or digits.","VAULT_NOT_UP_TO_DATE": "Your vault is not up-to-date. Please synchronize your vault and try again.",diff --git a/apps/mobile-app/i18n/locales/nl.json b/apps/mobile-app/i18n/locales/nl.jsonindex 9564acf6b..29a569db8 100644--- a/apps/mobile-app/i18n/locales/nl.json+++ b/apps/mobile-app/i18n/locales/nl.json@@ -276,7 +276,6 @@"USERNAME_EMPTY_OR_WHITESPACE": "Gebruikersnaam mag niet leeg zijn of alleen uit spaties bestaan.","USERNAME_TOO_SHORT": "Gebruikersnaam te kort: moet minimaal 3 tekens lang zijn.","USERNAME_TOO_LONG": "Gebruikersnaam te lang: mag niet langer zijn dan 40 tekens.",- "USERNAME_ADMIN_NOT_ALLOWED": "Gebruikersnaam 'admin' is niet toegestaan.","USERNAME_INVALID_EMAIL": "Ongeldig e-mailadres.","USERNAME_INVALID_CHARACTERS": "Gebruikersnaam is ongeldig, mag alleen letters of cijfers bevatten.","VAULT_NOT_UP_TO_DATE": "Uw kluis is niet up-to-date. Synchroniseer uw kluis en probeer het opnieuw.",diff --git a/apps/mobile-app/i18n/locales/uk.json b/apps/mobile-app/i18n/locales/uk.jsonindex 944389dde..4c96ed434 100644--- a/apps/mobile-app/i18n/locales/uk.json+++ b/apps/mobile-app/i18n/locales/uk.json@@ -276,7 +276,6 @@"USERNAME_EMPTY_OR_WHITESPACE": "Username cannot be empty or whitespace.","USERNAME_TOO_SHORT": "Username too short: must be at least 3 characters long.","USERNAME_TOO_LONG": "Username too long: cannot be longer than 40 characters.",- "USERNAME_ADMIN_NOT_ALLOWED": "Username 'admin' is not allowed.","USERNAME_INVALID_EMAIL": "Invalid email address.","USERNAME_INVALID_CHARACTERS": "Username is invalid, can only contain letters or digits.","VAULT_NOT_UP_TO_DATE": "Your vault is not up-to-date. Please synchronize your vault and try again.",diff --git a/apps/server/AliasVault.Api/Controllers/AuthController.cs b/apps/server/AliasVault.Api/Controllers/AuthController.csindex 1534ea948..bc0ad43dd 100644--- a/apps/server/AliasVault.Api/Controllers/AuthController.cs+++ b/apps/server/AliasVault.Api/Controllers/AuthController.cs@@ -386,7 +386,7 @@ public class AuthController(IAliasServerDbContextFactory dbContextFactory, UserM}// Validate the username.- var (isValid, apiErrorCode) = ValidateUsername(model.Username);+ var (isValid, apiErrorCode) = await ValidateUsername(model.Username);if (!isValid){return BadRequest(ApiErrorCodeHelper.CreateValidationErrorResponse(apiErrorCode, 400));@@ -484,7 +484,7 @@ public class AuthController(IAliasServerDbContextFactory dbContextFactory, UserM}// Validate the username- var (isValid, apiErrorCode) = ValidateUsername(normalizedUsername);+ var (isValid, apiErrorCode) = await ValidateUsername(normalizedUsername);if (!isValid){@@ -581,60 +581,6 @@ public class AuthController(IAliasServerDbContextFactory dbContextFactory, UserMreturn username.ToLowerInvariant().Trim();}- /// <summary>- /// Validates if a given username meets the required criteria.- /// </summary>- /// <param name="username">The username to validate.</param>- /// <returns>A tuple containing a boolean indicating if the username is valid, and an error message if it's invalid.</returns>- private static (bool IsValid, ApiErrorCode ApiErrorCode) ValidateUsername(string username)- {- const int minimumUsernameLength = 3;- const int maximumUsernameLength = 40;- const string adminUsername = "admin";-- if (string.IsNullOrWhiteSpace(username))- {- return (false, ApiErrorCode.USERNAME_EMPTY_OR_WHITESPACE);- }-- if (username.Length < minimumUsernameLength)- {- return (false, ApiErrorCode.USERNAME_TOO_SHORT);- }-- if (username.Length > maximumUsernameLength)- {- return (false, ApiErrorCode.USERNAME_TOO_LONG);- }-- if (string.Equals(username, adminUsername, StringComparison.OrdinalIgnoreCase))- {- return (false, ApiErrorCode.USERNAME_ADMIN_NOT_ALLOWED);- }-- // Check if it's a valid email address- if (username.Contains('@'))- {- try- {- var addr = new System.Net.Mail.MailAddress(username);- return (addr.Address == username, ApiErrorCode.USERNAME_INVALID_EMAIL);- }- catch- {- return (false, ApiErrorCode.USERNAME_INVALID_EMAIL);- }- }-- // If it's not an email, check if it only contains letters and digits- if (!username.All(char.IsLetterOrDigit))- {- return (false, ApiErrorCode.USERNAME_INVALID_CHARACTERS);- }-- return (true, ApiErrorCode.USERNAME_AVAILABLE);- }-/// <summary>/// Generate a device identifier based on request headers. This is used to associate refresh tokens/// with a specific device for a specific user.@@ -713,6 +659,68 @@ public class AuthController(IAliasServerDbContextFactory dbContextFactory, UserMreturn principal;}+ /// <summary>+ /// Validates if a given username meets the required criteria.+ /// </summary>+ /// <param name="username">The username to validate.</param>+ /// <returns>A tuple containing a boolean indicating if the username is valid, and an error message if it's invalid.</returns>+ private async Task<(bool IsValid, ApiErrorCode ApiErrorCode)> ValidateUsername(string username)+ {+ const int minimumUsernameLength = 3;+ const int maximumUsernameLength = 40;+ const string adminUsername = "admin";++ if (string.IsNullOrWhiteSpace(username))+ {+ return (false, ApiErrorCode.USERNAME_EMPTY_OR_WHITESPACE);+ }++ if (username.Length < minimumUsernameLength)+ {+ return (false, ApiErrorCode.USERNAME_TOO_SHORT);+ }++ if (username.Length > maximumUsernameLength)+ {+ return (false, ApiErrorCode.USERNAME_TOO_LONG);+ }++ // Disallow admin username to prevent conflicts with the default admin user+ if (string.Equals(username, adminUsername, StringComparison.OrdinalIgnoreCase))+ {+ return (false, ApiErrorCode.USERNAME_ALREADY_IN_USE);+ }++ // Check if username is not taken already+ var existingUser = await userManager.FindByNameAsync(username);+ if (existingUser != null)+ {+ return (false, ApiErrorCode.USERNAME_ALREADY_IN_USE);+ }++ // Check if it's a valid email address+ if (username.Contains('@'))+ {+ try+ {+ var addr = new System.Net.Mail.MailAddress(username);+ return (addr.Address == username, ApiErrorCode.USERNAME_INVALID_EMAIL);+ }+ catch+ {+ return (false, ApiErrorCode.USERNAME_INVALID_EMAIL);+ }+ }++ // If it's not an email, check if it only contains letters and digits+ if (!username.All(char.IsLetterOrDigit))+ {+ return (false, ApiErrorCode.USERNAME_INVALID_CHARACTERS);+ }++ return (true, ApiErrorCode.USERNAME_AVAILABLE);+ }+/// <summary>/// Validates the user and SRP session (password). If the user is not found or the password is invalid an/// action result is returned with the appropriate error message. If everything is valid nothing is returned.diff --git a/apps/server/AliasVault.Client/Resources/ApiErrors.en.resx b/apps/server/AliasVault.Client/Resources/ApiErrors.en.resxindex ad752d5f3..5956bc387 100644--- a/apps/server/AliasVault.Client/Resources/ApiErrors.en.resx+++ b/apps/server/AliasVault.Client/Resources/ApiErrors.en.resx@@ -167,9 +167,6 @@<data name="USERNAME_TOO_LONG" xml:space="preserve"><value>Username too long: cannot be longer than 40 characters.</value></data>- <data name="USERNAME_ADMIN_NOT_ALLOWED" xml:space="preserve">- <value>Username 'admin' is not allowed.</value>- </data><data name="USERNAME_INVALID_EMAIL" xml:space="preserve"><value>Invalid email address.</value></data>diff --git a/apps/server/Shared/AliasVault.RazorComponents/Alerts/AlertMessageError.razor b/apps/server/Shared/AliasVault.RazorComponents/Alerts/AlertMessageError.razorindex 72dbdf86f..c73dd2883 100644--- a/apps/server/Shared/AliasVault.RazorComponents/Alerts/AlertMessageError.razor+++ b/apps/server/Shared/AliasVault.RazorComponents/Alerts/AlertMessageError.razor@@ -2,7 +2,7 @@@if (!string.IsNullOrEmpty(Message)){- <div class="p-4 mb-4 text-sm text-red-800 rounded-lg bg-red-50 border-2 dark:bg-red-800 dark:text-white dark:border-red-500 dark:border" role="alert">+ <div class="p-4 mt-4 text-sm text-red-800 rounded-lg bg-red-50 border-2 dark:bg-red-800 dark:text-white dark:border-red-500 dark:border" role="alert">@Message</div>}diff --git a/apps/server/Shared/AliasVault.Shared/Models/Enums/ApiErrorCode.cs b/apps/server/Shared/AliasVault.Shared/Models/Enums/ApiErrorCode.csindex 6e10f95d6..612ffe41b 100644--- a/apps/server/Shared/AliasVault.Shared/Models/Enums/ApiErrorCode.cs+++ b/apps/server/Shared/AliasVault.Shared/Models/Enums/ApiErrorCode.cs@@ -104,11 +104,6 @@ public enum ApiErrorCode/// </summary>USERNAME_TOO_LONG,- /// <summary>- /// Username 'admin' is not allowed.- /// </summary>- USERNAME_ADMIN_NOT_ALLOWED,-/// <summary>/// Username is not a valid email address./// </summary>diff --git a/apps/server/Tests/AliasVault.E2ETests/Tests/Client/Shard4/AuthTests.cs b/apps/server/Tests/AliasVault.E2ETests/Tests/Client/Shard4/AuthTests.csindex d7e17246a..86f56438b 100644--- a/apps/server/Tests/AliasVault.E2ETests/Tests/Client/Shard4/AuthTests.cs+++ b/apps/server/Tests/AliasVault.E2ETests/Tests/Client/Shard4/AuthTests.cs@@ -246,7 +246,7 @@ public class AuthTests : ClientPlaywrightTestawait Register(checkForSuccess: false);var warning = await Page.TextContentAsync("div[role='alert']");- Assert.That(warning, Does.Contain("is already taken."), "No visible warning when registering with existing email address.");+ Assert.That(warning, Does.Contain("is already in use."), "No visible warning when registering with existing email address.");}/// <summary>