Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Refactor local password confirm logic (#2413)
Leendert de Borst62c7b6d594c8
diff --git a/apps/web/src/pages/settings/importexport/ImportExport.tsx b/apps/web/src/pages/settings/importexport/ImportExport.tsxindex af1203b70..a3672fa32 100644--- a/apps/web/src/pages/settings/importexport/ImportExport.tsx+++ b/apps/web/src/pages/settings/importexport/ImportExport.tsx@@ -1,4 +1,3 @@-import { MasterPasswordService, PasswordVerificationResult } from '@aliasvault/client/auth/MasterPasswordService';import { AliasVaultCsvExportService } from '@aliasvault/client/transfer/export/AliasVaultCsvExportService';import { AvexExportService } from '@aliasvault/client/transfer/export/AvexExportService';import { AvuxExportService } from '@aliasvault/client/transfer/export/AvuxExportService';@@ -20,6 +19,7 @@ import { useDb } from '@/context/DbContext';import { useLoading } from '@/context/LoadingContext';import { useNotifications } from '@/context/NotificationContext';import { usePageTitle } from '@/hooks/usePageTitle';+import { verifyMasterPassword } from '@/utils/MasterPasswordCheck';/*** The export formats.@@ -182,25 +182,16 @@ const ImportExport: React.FC = () => {showLoading(t(`${tk}.VerifyingPasswordMessage`));try {- const result = await MasterPasswordService.verifyPassword(password);+ const isValid = await verifyMasterPassword(password);hideLoading();- switch (result) {- case PasswordVerificationResult.Success:- if (currentExportType === ExportType.Avex) {- setShowExportPasswordModal(true);- } else {- await handleExportConfirmed();- }- break;- case PasswordVerificationResult.InvalidPassword:- setPasswordError(t(`${tk}.PasswordIncorrect`));- setShowPasswordConfirmation(true);- break;- default:- setPasswordError(t(`${tk}.PasswordVerificationFailed`));- setShowPasswordConfirmation(true);- break;+ if (!isValid) {+ setPasswordError(t(`${tk}.PasswordIncorrect`));+ setShowPasswordConfirmation(true);+ } else if (currentExportType === ExportType.Avex) {+ setShowExportPasswordModal(true);+ } else {+ await handleExportConfirmed();}} catch (error) {console.error('[Export] Error verifying password:', error);diff --git a/apps/web/src/pages/settings/importexport/ResetVault.tsx b/apps/web/src/pages/settings/importexport/ResetVault.tsxindex 10cc636c8..914805249 100644--- a/apps/web/src/pages/settings/importexport/ResetVault.tsx+++ b/apps/web/src/pages/settings/importexport/ResetVault.tsx@@ -1,4 +1,3 @@-import { MasterPasswordService, PasswordVerificationResult } from '@aliasvault/client/auth/MasterPasswordService';import React, { useState } from 'react';import { useTranslation } from 'react-i18next';import { useNavigate } from 'react-router-dom';@@ -16,6 +15,7 @@ import { useLoading } from '@/context/LoadingContext';import { useNotifications } from '@/context/NotificationContext';import { usePageTitle } from '@/hooks/usePageTitle';import { useVaultMutate } from '@/hooks/useVaultMutate';+import { verifyMasterPassword } from '@/utils/MasterPasswordCheck';/*** Empty the vault while keeping the account: confirm the username, then prompt to verify the master password.@@ -69,15 +69,10 @@ const ResetVault: React.FC = () => {notifications.clearMessages();try {- const verificationResult = await MasterPasswordService.verifyPassword(password);- if (verificationResult === PasswordVerificationResult.InvalidPassword) {+ if (!await verifyMasterPassword(password)) {notifications.addErrorMessage(t(`${tk}.ResetVaultPasswordIncorrect`), true);return;}- if (verificationResult !== PasswordVerificationResult.Success) {- notifications.addErrorMessage(t(`${tk}.ResetVaultErrorMessage`), true);- return;- }const sqliteClient = dbContext.sqliteClient;if (!sqliteClient) {diff --git a/apps/web/src/utils/MasterPasswordCheck.ts b/apps/web/src/utils/MasterPasswordCheck.tsnew file mode 100644index 000000000..7377c547d--- /dev/null+++ b/apps/web/src/utils/MasterPasswordCheck.ts@@ -0,0 +1,29 @@+import { extractErrorCode } from '@aliasvault/client/api/errors/AppErrorCodes';+import { MasterPasswordService } from '@aliasvault/client/auth/MasterPasswordService';+import { SrpAuthService } from '@aliasvault/client/auth/SrpAuthService';+import { VaultKeyService } from '@aliasvault/client/auth/VaultKeyService';++/**+ * Check the master password offline against the cached key chain, the same way an offline unlock does.+ * @param password - the master password to check+ * @returns True when the password is correct, false when it is wrong.+ * @throws Error when the check itself cannot run (no stored derivation parameters or key chain).+ */+export async function verifyMasterPassword(password: string): Promise<boolean> {+ const params = await MasterPasswordService.getStoredDerivationParams();+ if (!params || !await VaultKeyService.hasLocalVaultKey()) {+ throw new Error('No local key chain to verify the master password against');+ }++ const prepared = await SrpAuthService.prepareCredentials(password, params.salt, params.encryptionSettings);+ try {+ await VaultKeyService.verifyUnlockKey(prepared.passwordHashBase64);+ return true;+ } catch (error) {+ const code = error instanceof Error ? extractErrorCode(error.message) : null;+ if (await VaultKeyService.isWrongUnlockKey(code)) {+ return false;+ }+ throw error;+ }+}diff --git a/core/client/src/auth/MasterPasswordService.ts b/core/client/src/auth/MasterPasswordService.tsindex a0b7c9440..a16e55cc6 100644--- a/core/client/src/auth/MasterPasswordService.ts+++ b/core/client/src/auth/MasterPasswordService.ts@@ -1,9 +1,7 @@-import { AppErrorCode, extractErrorCode } from '../api/errors/AppErrorCodes';import { StorageKeys } from '../constants/StorageKeys';import { EncryptionUtility } from '../crypto/EncryptionUtility';import { getPlatform } from '../platform/ClientPlatform';import { VaultSyncHoldReason, withVaultSyncHold } from '../sync/VaultSyncHold';-import { logDefect } from '../utilities/Diagnostics';import { SrpAuthService, type SrpClientProof } from './SrpAuthService';import { VaultKeyService } from './VaultKeyService';@@ -36,15 +34,6 @@ export class PasswordChangedElsewhereError extends Error {}}-/**- * Outcome of a local master password check.- */-export enum PasswordVerificationResult {- Success = 'Success',- InvalidPassword = 'InvalidPassword',- VerificationError = 'VerificationError',-}-/** The SRP challenge a server endpoint issues before it accepts a password-confirmed action. */export type SrpChallenge = {salt: string;@@ -71,36 +60,6 @@ export class MasterPasswordService {return getPlatform().storage.get<UnlockKeyDerivationParams>(StorageKeys.UNLOCK_KEY_DERIVATION_PARAMS);}- /**- * Verify the master password locally.- * @param password - the master password to check- */- public static async verifyPassword(password: string): Promise<PasswordVerificationResult> {- try {- const parameters = await MasterPasswordService.getStoredDerivationParams();- if (!parameters) {- return PasswordVerificationResult.VerificationError;- }-- // Without a cached chain a derived key cannot be checked against anything.- if (!await VaultKeyService.hasLocalVaultKey()) {- return PasswordVerificationResult.VerificationError;- }-- const prepared = await SrpAuthService.prepareCredentials(password, parameters.salt, parameters.encryptionSettings);- try {- await VaultKeyService.verifyUnlockKey(prepared.passwordHashBase64);- return PasswordVerificationResult.Success;- } catch (error) {- const code = error instanceof Error ? extractErrorCode(error.message) : null;- return code === AppErrorCode.UNLOCK_KEY_REJECTED ? PasswordVerificationResult.InvalidPassword : PasswordVerificationResult.VerificationError;- }- } catch (error) {- logDefect('[MasterPassword] Password verification failed unexpectedly', error);- return PasswordVerificationResult.VerificationError;- }- }-/*** Sanity check that a salt the server reports is still the one this device derived its keys from. Unknown on* either side passes: there is nothing to compare yet.