Skip to content

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

Fix sqlite client race condition issue during unlock (#1404)

Leendert de Borst75b47d7d8e43
diff --git a/apps/browser-extension/src/entrypoints/popup/context/DbContext.tsx b/apps/browser-extension/src/entrypoints/popup/context/DbContext.tsx
index cac109bdd..35289df21 100644
--- a/apps/browser-extension/src/entrypoints/popup/context/DbContext.tsx
+++ b/apps/browser-extension/src/entrypoints/popup/context/DbContext.tsx
@@ -21,9 +21,9 @@ type DbContextType = {
loadDatabase: (decryptedVaultBase64: string) => Promise<SqliteClient>;
/**
* Load the stored (encrypted) vault from background storage into memory.
- * Returns true if vault was loaded successfully, false otherwise.
+ * Returns the SqliteClient if vault was loaded successfully, null otherwise.
*/
- loadStoredDatabase: () => Promise<boolean>;
+ loadStoredDatabase: () => Promise<SqliteClient | null>;
storeEncryptionKey: (derivedKey: string) => Promise<void>;
storeEncryptionKeyDerivationParams: (params: EncryptionKeyDerivationParams) => Promise<void>;
clearDatabase: () => void;
@@ -112,9 +112,9 @@ export const DbProvider: React.FC<{ children: React.ReactNode }> = ({ children }
/**
* Load the stored (encrypted) vault from background storage into memory.
- * Returns true if vault was loaded successfully, false otherwise.
+ * Returns the SqliteClient if vault was loaded successfully, null otherwise.
*/
- const loadStoredDatabase = useCallback(async (): Promise<boolean> => {
+ const loadStoredDatabase = useCallback(async (): Promise<SqliteClient | null> => {
try {
const response = await sendMessage('GET_VAULT', {}, 'background') as messageVaultResponse;
if (response?.vault) {
@@ -124,17 +124,17 @@ export const DbProvider: React.FC<{ children: React.ReactNode }> = ({ children }
setSqliteClient(client);
setDbInitialized(true);
setDbAvailable(true);
- return true;
+ return client;
} else {
setDbInitialized(true);
setDbAvailable(false);
- return false;
+ return null;
}
} catch (error) {
console.error('Error retrieving vault from background:', error);
setDbInitialized(true);
setDbAvailable(false);
- return false;
+ return 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 cb8a27090..6a3407d2e 100644
--- a/apps/browser-extension/src/entrypoints/popup/pages/auth/Unlock.tsx
+++ b/apps/browser-extension/src/entrypoints/popup/pages/auth/Unlock.tsx
@@ -262,16 +262,14 @@ const Unlock: React.FC = () => {
await dbContext.storeEncryptionKey(passwordHashBase64);
// Load the stored vault from background (decrypts using stored encryption key)
- const loaded = await dbContext.loadStoredDatabase();
- if (!loaded) {
+ const sqliteClient = await dbContext.loadStoredDatabase();
+ if (!sqliteClient) {
// Decryption failed - likely wrong password
setError(t('auth.errors.wrongPassword'));
hideLoading();
return;
}
- const sqliteClient = dbContext.sqliteClient!;
-
// Check if there are pending migrations
if (await sqliteClient.hasPendingMigrations()) {
navigate('/upgrade', { replace: true });
@@ -364,14 +362,12 @@ const Unlock: React.FC = () => {
* - Merging local changes with server if hasPendingSync is true
* - Overwriting local with server if no local changes
*/
- const loaded = await dbContext.loadStoredDatabase();
- if (!loaded) {
+ const sqliteClient = await dbContext.loadStoredDatabase();
+ if (!sqliteClient) {
// Decryption failed - likely wrong PIN
throw new IncorrectPinError(3);
}
- const sqliteClient = dbContext.sqliteClient!;
-
// Check if there are pending migrations
if (await sqliteClient.hasPendingMigrations()) {
navigate('/upgrade', { replace: true });
@@ -445,16 +441,14 @@ const Unlock: React.FC = () => {
* - Merging local changes with server if hasPendingSync is true
* - Overwriting local with server if no local changes
*/
- const loaded = await dbContext.loadStoredDatabase();
- if (!loaded) {
+ const sqliteClient = await dbContext.loadStoredDatabase();
+ if (!sqliteClient) {
// Decryption failed
setError(t('common.errors.unknownErrorTryAgain'));
hideLoading();
return;
}
- const sqliteClient = dbContext.sqliteClient!;
-
// Check if there are pending migrations
if (await sqliteClient.hasPendingMigrations()) {
navigate('/upgrade', { replace: true });