Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Add local password unlock rate limit to browser extension (#1824)

Leendert de Borst8cf06a597005
diff --git a/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts b/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts
index 9a8ce106d..e4f50f6bd 100644
--- a/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts
+++ b/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts
@@ -293,6 +293,9 @@ export async function handleClearSession(): Promise<messageBoolResponse> {
'session:navigationHistory',
]);
+ // Reset password unlock failed attempts counter on logout
+ await LocalPreferencesService.resetPasswordUnlockFailedAttempts();
+
// Clear cached client since session ended
cachedSqliteClient = null;
cachedVaultBlob = null;
diff --git a/apps/browser-extension/src/entrypoints/popup/pages/auth/Unlock.tsx b/apps/browser-extension/src/entrypoints/popup/pages/auth/Unlock.tsx
index ba96abc5e..3d6810a8b 100644
--- a/apps/browser-extension/src/entrypoints/popup/pages/auth/Unlock.tsx
+++ b/apps/browser-extension/src/entrypoints/popup/pages/auth/Unlock.tsx
@@ -61,6 +61,11 @@ const Unlock: React.FC = () => {
// Password unlock state
const [password, setPassword] = useState('');
const [showPassword, setShowPassword] = useState(false);
+ const [passwordFailedAttempts, setPasswordFailedAttempts] = useState(0);
+
+ // Brute force protection constants
+ const MAX_PASSWORD_ATTEMPTS = 10;
+ const PASSWORD_WARNING_THRESHOLD = 5;
// PIN unlock state
const [pin, setPin] = useState('');
@@ -138,6 +143,10 @@ const Unlock: React.FC = () => {
setUnlockMode('password');
}
+ // Load password failed attempts counter
+ const storedAttempts = await LocalPreferencesService.getPasswordUnlockFailedAttempts();
+ setPasswordFailedAttempts(storedAttempts);
+
// Then check API status
await checkStatus();
};
@@ -294,8 +303,10 @@ const Unlock: React.FC = () => {
// Clear dismiss until
await LocalPreferencesService.setVaultLockedDismissUntil(0);
- // Reset PIN failed attempts on successful password unlock
+ // Reset PIN and password failed attempts on successful unlock
await resetFailedAttempts();
+ await LocalPreferencesService.resetPasswordUnlockFailedAttempts();
+ setPasswordFailedAttempts(0);
// Navigate to reinitialize which will call syncVault to sync with server
navigate('/reinitialize', { replace: true });
@@ -307,13 +318,13 @@ const Unlock: React.FC = () => {
// Check if it's a decryption failure (E-203): this means wrong password
const errorCode = extractErrorCode(getErrorMessage(err, ''));
if (errorCode === AppErrorCode.VAULT_DECRYPT_FAILED) {
- setError(t('common.errors.wrongPassword'));
+ await handlePasswordFailedAttempt();
} else {
// Other error codes, show the formatted message as-is
setError(getErrorMessage(err, t('common.errors.wrongPassword')));
}
} else {
- setError(t('common.errors.wrongPassword'));
+ await handlePasswordFailedAttempt();
}
console.error('Unlock error:', err);
} finally {
@@ -445,6 +456,35 @@ const Unlock: React.FC = () => {
}
};
+ /**
+ * Handle failed password attempt with brute force protection
+ */
+ const handlePasswordFailedAttempt = async (): Promise<void> => {
+ const newAttempts = passwordFailedAttempts + 1;
+ setPasswordFailedAttempts(newAttempts);
+ await LocalPreferencesService.setPasswordUnlockFailedAttempts(newAttempts);
+
+ const remainingAttempts = MAX_PASSWORD_ATTEMPTS - newAttempts;
+
+ // Clear password field
+ setPassword('');
+
+ if (newAttempts >= MAX_PASSWORD_ATTEMPTS) {
+ // Max attempts reached - logout user
+ setError(t('auth.maxAttemptsReached'));
+ // Delay to let user read the message
+ setTimeout(async () => {
+ await authContext.clearAuthUserInitiated();
+ }, 2000);
+ } else if (newAttempts >= PASSWORD_WARNING_THRESHOLD) {
+ // Show warning about remaining attempts
+ setError(t('auth.passwordAttemptsWarning', { remainingAttempts }));
+ } else {
+ // Show standard incorrect password error
+ setError(t('common.errors.wrongPassword'));
+ }
+ };
+
/**
* Handle logout click - opens the logout confirmation modal.
*/
@@ -514,8 +554,10 @@ const Unlock: React.FC = () => {
// Clear dismiss until
await LocalPreferencesService.setVaultLockedDismissUntil(0);
- // Reset PIN failed attempts on successful unlock
+ // Reset PIN and password failed attempts on successful unlock
await resetFailedAttempts();
+ await LocalPreferencesService.resetPasswordUnlockFailedAttempts();
+ setPasswordFailedAttempts(0);
// Navigate to reinitialize which will call syncVault to sync with server
navigate('/reinitialize', { replace: true });
@@ -671,7 +713,7 @@ const Unlock: React.FC = () => {
</div>
{/* Error Message */}
- {error && <AlertMessage type="error" message={error} className="mb-4 text-center" />}
+ {error && <AlertMessage type="error" message={error} className="mb-4" />}
<div className="mb-4">
<label className="block text-gray-700 dark:text-gray-200 font-medium mb-2" htmlFor="password">
diff --git a/apps/browser-extension/src/i18n/locales/en.json b/apps/browser-extension/src/i18n/locales/en.json
index 3a5c88441..3eebdf65d 100644
--- a/apps/browser-extension/src/i18n/locales/en.json
+++ b/apps/browser-extension/src/i18n/locales/en.json
@@ -30,7 +30,9 @@
"switchAccounts": "Switch accounts?",
"loginWithMobile": "Log in using Mobile App",
"unlockWithMobile": "Unlock using Mobile App",
- "scanQrCode": "Scan this QR code with your AliasVault mobile app to log in and unlock your vault."
+ "scanQrCode": "Scan this QR code with your AliasVault mobile app to log in and unlock your vault.",
+ "passwordAttemptsWarning": "Incorrect password. You will be logged out if you enter the wrong password {{remainingAttempts}} more times.",
+ "maxAttemptsReached": "Too many failed unlock attempts. You have been logged out for security reasons."
},
"menu": {
"vault": "Vault",
diff --git a/apps/browser-extension/src/utils/LocalPreferencesService.ts b/apps/browser-extension/src/utils/LocalPreferencesService.ts
index c80b51431..2152cd5d7 100644
--- a/apps/browser-extension/src/utils/LocalPreferencesService.ts
+++ b/apps/browser-extension/src/utils/LocalPreferencesService.ts
@@ -39,6 +39,9 @@ const KEYS = {
// Session/Navigation state
PENDING_REDIRECT_URL: 'session:pendingRedirectUrl',
SKIP_FORM_RESTORE: 'local:aliasvault_skip_form_restore',
+
+ // Brute force protection
+ PASSWORD_UNLOCK_FAILED_ATTEMPTS: 'local:password_unlock_failed_attempts',
} as const;
/**
@@ -407,4 +410,39 @@ export const LocalPreferencesService = {
async setLoginSaveBlockedDomains(domains: string[]): Promise<void> {
await storage.setItem(KEYS.LOGIN_SAVE_BLOCKED_DOMAINS, domains);
},
+
+ /*
+ * ============================================
+ * Brute Force Protection
+ * ============================================
+ */
+
+ /**
+ * Get the password unlock failed attempts count.
+ * @returns The number of failed password unlock attempts. Defaults to 0.
+ */
+ async getPasswordUnlockFailedAttempts(): Promise<number> {
+ const value = await storage.getItem(KEYS.PASSWORD_UNLOCK_FAILED_ATTEMPTS) as number | string | null;
+ if (typeof value === 'number') {
+ return value;
+ }
+ if (typeof value === 'string') {
+ return parseInt(value, 10) || 0;
+ }
+ return 0;
+ },
+
+ /**
+ * Set the password unlock failed attempts count.
+ */
+ async setPasswordUnlockFailedAttempts(attempts: number): Promise<void> {
+ await storage.setItem(KEYS.PASSWORD_UNLOCK_FAILED_ATTEMPTS, attempts);
+ },
+
+ /**
+ * Reset the password unlock failed attempts counter.
+ */
+ async resetPasswordUnlockFailedAttempts(): Promise<void> {
+ await storage.removeItem(KEYS.PASSWORD_UNLOCK_FAILED_ATTEMPTS);
+ },
};