Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Tweak browser extension to let popup initialize its vault directly instead of going through background (#2413)
Leendert de Borstedec8c958a1b
diff --git a/apps/browser-extension/src/entrypoints/background.ts b/apps/browser-extension/src/entrypoints/background.tsindex 95f44161f..4977b07cb 100644--- a/apps/browser-extension/src/entrypoints/background.ts+++ b/apps/browser-extension/src/entrypoints/background.ts@@ -11,7 +11,7 @@ import { handleGetWebAuthnSettings, handleWebAuthnCreate, handleWebAuthnGet, hanimport { handleOpenPopup, handlePopupWithItem, handleOpenPopupCreateCredential, handleToggleContextMenu } from '@/entrypoints/background/PopupMessageHandler';import { handleStoreSavePromptState, handleGetSavePromptState, handleClearSavePromptState, handleStoreLastAutofilled, handleGetLastAutofilled, handleClearLastAutofilled } from '@/entrypoints/background/SavePromptStateHandler';import { handleStoreTwoFactorState, handleGetTwoFactorState, handleClearTwoFactorState } from '@/entrypoints/background/TwoFactorStateHandler';-import { handleCheckAuthStatus, handleClearPersistedFormValues, handleClearSession, handleClearVaultData, handleLockVault, handleGetFilteredItems, handleGetSearchItems, handleGetEncryptionKey, handleGetUnlockKeyDerivationParams, handleGetPersistedFormValues, handleGetVault, handleGetVaultMigrationStatus, handlePersistFormValues, handleStoreUnlockKey, handleStoreUnlockKeyDerivationParams, handleStoreEncryptedVault, handleGetSyncState, handleMigrateVaultManifest, handleFullVaultSync, handleGroupCreateVault, handleGroupInviteMember, handleGroupUpdateVault, handleGroupRevokeAccess, handleCheckLoginDuplicate, handleSaveLoginCredential, handleAddUrlToCredential, handleIsUrlLinkedToCredential, handleGetLoginSaveSettings, handleGetItemsWithTotp, handleSearchItemsWithTotp, handleGetTotpSecrets, handleGenerateTotpCode, handleSetRecentlySelected, handleRecordItemUsage } from '@/entrypoints/background/VaultMessageHandler';+import { handleCheckAuthStatus, handleClearPersistedFormValues, handleClearSession, handleClearVaultData, handleLockVault, handleGetFilteredItems, handleGetSearchItems, handleGetEncryptionKey, handleGetUnlockKeyDerivationParams, handleGetPersistedFormValues, handleGetVaultMigrationStatus, handlePersistFormValues, handleStoreUnlockKey, handleStoreUnlockKeyDerivationParams, handleStoreEncryptedVaultChunk, handleGetSyncState, handleMigrateVaultManifest, handleFullVaultSync, handleGroupCreateVault, handleGroupInviteMember, handleGroupUpdateVault, handleGroupRevokeAccess, handleCheckLoginDuplicate, handleSaveLoginCredential, handleAddUrlToCredential, handleIsUrlLinkedToCredential, handleGetLoginSaveSettings, handleGetItemsWithTotp, handleSearchItemsWithTotp, handleGetTotpSecrets, handleGenerateTotpCode, handleSetRecentlySelected, handleRecordItemUsage } from '@/entrypoints/background/VaultMessageHandler';import { logFailure } from '@/utils/Diagnostics';import { LocalPreferencesService } from '@/utils/LocalPreferencesService';@@ -253,12 +253,10 @@ export default defineBackground({/** Listen for messages via @webext-core/messaging.*/- onExtensionPageMessage('PING', () => true);onMessage('CHECK_AUTH_STATUS', () => handleCheckAuthStatus());onExtensionPageMessage('GET_ENCRYPTION_KEY', () => handleGetEncryptionKey());onExtensionPageMessage('GET_UNLOCK_KEY_DERIVATION_PARAMS', () => handleGetUnlockKeyDerivationParams());- onExtensionPageMessage('GET_VAULT', () => handleGetVault());onMessage('GET_FILTERED_ITEMS', ({ data }) => handleGetFilteredItems(data));onMessage('GET_SEARCH_ITEMS', ({ data }) => handleGetSearchItems(data));@@ -275,7 +273,7 @@ export default defineBackground({});onExtensionPageMessage('STORE_UNLOCK_KEY_DERIVATION_PARAMS', ({ data }) => handleStoreUnlockKeyDerivationParams(data));- onExtensionPageMessage('STORE_ENCRYPTED_VAULT', ({ data }) => handleStoreEncryptedVault(data));+ onExtensionPageMessage('STORE_ENCRYPTED_VAULT', ({ data }) => handleStoreEncryptedVaultChunk(data));onExtensionPageMessage('GET_SYNC_STATE', () => handleGetSyncState());onExtensionPageMessage('FULL_VAULT_SYNC', ({ data }) => handleFullVaultSync(data));diff --git a/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts b/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.tsindex 850610d6e..4c1ea88a2 100644--- a/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts+++ b/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts@@ -1,7 +1,7 @@/* eslint-disable @typescript-eslint/no-explicit-any */import { ApiRequestError } from '@aliasvault/client/api/errors/ApiRequestError';-import { AppErrorCode, formatErrorWithCode, getErrorMessage, hasErrorCode } from '@aliasvault/client/api/errors/AppErrorCodes';+import { AppErrorCode, formatErrorWithCode } from '@aliasvault/client/api/errors/AppErrorCodes';import { VaultVersionIncompatibleError } from '@aliasvault/client/api/errors/VaultVersionIncompatibleError';import { WebApiService } from '@aliasvault/client/api/WebApiService';import { MasterPasswordService } from '@aliasvault/client/auth/MasterPasswordService';@@ -21,7 +21,6 @@ import { type VaultMutationScope, DEFAULT_VAULT_MUTATION_SCOPE, hasUserVisibleScimport { hasSyncError, syncResult, VaultSync, type FullVaultSyncResult, type SharedManifestDetails, type SharingOperationResult, type VaultManifestMigrationResult } from '@aliasvault/client/sync/VaultSync';import { type IVaultSyncEngineHost, type VaultSyncOptions, type VaultSyncPhase as EngineSyncPhase, type VaultSyncStoreOutcome, type VaultSyncStoreRequest } from '@aliasvault/client/sync/VaultSyncEngine';import { getVaultSyncHoldReason } from '@aliasvault/client/sync/VaultSyncHold';-import { bytesToBase64 } from '@aliasvault/client/utilities/Base64';import { FieldKey, ItemTypes, createSystemField, type Item } from '@aliasvault/models/vault';import { clearAllSavePromptState } from '@/entrypoints/background/SavePromptStateHandler';@@ -29,9 +28,9 @@ import { handleClearTwoFactorState } from '@/entrypoints/background/TwoFactorStaimport { AUTH_STORAGE_KEYS, dirtyScopeStorageKey, SESSION_STORAGE_KEYS, StorageKeys, vaultDataStorageKeys, VAULT_LOCK_STORAGE_KEYS } from '@/utils/constants/storageKeys';import { devLog } from '@/utils/devLogger/DevLogger';-import { logExpected, logFailure } from '@/utils/Diagnostics';+import { logFailure } from '@/utils/Diagnostics';import { LocalPreferencesService } from '@/utils/LocalPreferencesService';-import { sendMessage, type TotpSecret } from '@/utils/messaging/ExtensionMessaging';+import { sendMessage, type TotpSecret, type VaultBlobStoreOptions } from '@/utils/messaging/ExtensionMessaging';import { RecentlySelectedItemService } from '@/utils/RecentlySelectedItemService';import { ServiceDetectionUtility } from '@/utils/serviceDetection/ServiceDetectionUtility';import type { BoolResponse as messageBoolResponse } from '@/utils/types/messaging/BoolResponse';@@ -40,7 +39,6 @@ import type { FullVaultSyncRequest } from '@/utils/types/messaging/FullVaultSyncimport type { ItemsResponse as messageItemsResponse } from '@/utils/types/messaging/ItemsResponse';import type { PasswordSettingsResponse as messagePasswordSettingsResponse } from '@/utils/types/messaging/PasswordSettingsResponse';import type { SaveLoginResponse } from '@/utils/types/messaging/SaveLoginResponse';-import type { VaultResponse as messageVaultResponse } from '@/utils/types/messaging/VaultResponse';import type { VaultSyncPhase } from '@/utils/types/messaging/VaultSyncPhase';import type { VaultSyncState } from '@/utils/types/messaging/VaultSyncState';@@ -229,58 +227,6 @@ export async function handleStoreUnlockKeyDerivationParams(}}-/**- * Get the vault from browser storage (local: for persistence).- */-export async function handleGetVault(-) : Promise<messageVaultResponse> {- try {- const encryptionKey = await handleGetEncryptionKey();-- const encryptedVault = await storage.getItem(StorageKeys.ENCRYPTED_VAULT) as string;- const publicEmailDomains = await storage.getItem<string[]>(StorageKeys.PUBLIC_EMAIL_DOMAINS);- const privateEmailDomains = await storage.getItem<string[]>(StorageKeys.PRIVATE_EMAIL_DOMAINS);- const hiddenPrivateEmailDomains = await storage.getItem<string[]>(StorageKeys.HIDDEN_PRIVATE_EMAIL_DOMAINS) ?? [];-- if (!encryptedVault) {- logExpected('[Vault] No encrypted vault in storage');- // E-201: No encrypted vault in storage- return { success: false, error: formatErrorWithCode(await t('common.errors.vaultNotAvailable'), AppErrorCode.VAULT_NOT_FOUND) };- }-- if (!encryptionKey) {- // E-202: No encryption key available (vault is locked)- return { success: false, error: formatErrorWithCode(await t('common.errors.vaultIsLocked'), AppErrorCode.VAULT_LOCKED) };- }-- let decryptedBytes: Uint8Array;- try {- decryptedBytes = await decryptVaultBlob(encryptedVault, encryptionKey);- } catch (error) {- logFailure('Failed to decrypt the stored vault', error);- // E-203: the stored vault does not decrypt with the session key- return { success: false, error: formatErrorWithCode(await t('common.errors.unknownError'), AppErrorCode.VAULT_DECRYPT_FAILED) };- }-- return {- success: true,- // The popup receives the database as base64: messages carry strings, not bytes.- vault: bytesToBase64(decryptedBytes),- publicEmailDomains: publicEmailDomains ?? [],- privateEmailDomains: privateEmailDomains ?? [],- hiddenPrivateEmailDomains: hiddenPrivateEmailDomains ?? []- };- } catch (error) {- logFailure('Failed to get vault', error);- // Keep an already-coded error (the key chain reports its own failures) instead of masking it.- if (hasErrorCode(error)) {- return { success: false, error: getErrorMessage(error, '') };- }- // E-601: reading the vault or its key from storage failed- return { success: false, error: formatErrorWithCode(await t('common.errors.unknownError'), AppErrorCode.STORAGE_READ_FAILED) };- }-}-/*** Lock the vault by clearing only session data.* This preserves local vault data so user can unlock again without server.@@ -680,6 +626,49 @@ export async function handleGetEncryptedVault(): Promise<string | null> {return await storage.getItem(StorageKeys.ENCRYPTED_VAULT) as string | null;}+/**+ * Encrypted vault blobs an extension page is sending in chunks, by transfer id (see VaultBlobTransfer).+ */+const pendingVaultBlobTransfers = new Map<string, { chunks: string[]; startedAt: number }>();++/**+ * How long an unfinished transfer is kept, so the chunks of a popup that closed during a save do not stay in memory.+ */+const VAULT_BLOB_TRANSFER_EXPIRY_MS = 60_000;++/**+ * Receive one chunk of an encrypted vault blob an extension page is sending. The last chunk carries the store options:+ * the joined blob is then stored via {@link handleStoreEncryptedVault}, whose result is returned. Earlier chunks return null.+ */+export async function handleStoreEncryptedVaultChunk(request: {+ transferId: string;+ index: number;+ chunk: string;+ commit?: VaultBlobStoreOptions;+}): Promise<{ success: boolean; mutationSequence: number } | null> {+ const now = Date.now();+ for (const [transferId, pending] of pendingVaultBlobTransfers) {+ if (now - pending.startedAt > VAULT_BLOB_TRANSFER_EXPIRY_MS) {+ pendingVaultBlobTransfers.delete(transferId);+ }+ }++ const transfer = pendingVaultBlobTransfers.get(request.transferId) ?? { chunks: [], startedAt: now };+ if (transfer.chunks.length !== request.index) {+ // E-602: earlier chunks are gone, e.g. the service worker restarted or the transfer expired+ pendingVaultBlobTransfers.delete(request.transferId);+ throw new Error(formatErrorWithCode(await t('common.errors.unknownError'), AppErrorCode.STORAGE_WRITE_FAILED));+ }+ transfer.chunks.push(request.chunk);+ if (!request.commit) {+ pendingVaultBlobTransfers.set(request.transferId, transfer);+ return null;+ }++ pendingVaultBlobTransfers.delete(request.transferId);+ return handleStoreEncryptedVault({ ...request.commit, vaultBlob: transfer.chunks.join('') });+}+/*** Store the encrypted vault blob.*diff --git a/apps/browser-extension/src/entrypoints/popup/context/DbContext.tsx b/apps/browser-extension/src/entrypoints/popup/context/DbContext.tsxindex f9d2d5274..4fbd83bb7 100644--- a/apps/browser-extension/src/entrypoints/popup/context/DbContext.tsx+++ b/apps/browser-extension/src/entrypoints/popup/context/DbContext.tsx@@ -1,5 +1,6 @@import { AppErrorCode, formatErrorWithCode } from '@aliasvault/client/api/errors/AppErrorCodes';import EncryptionUtility from '@aliasvault/client/crypto/EncryptionUtility';+import { decryptVaultBlob } from '@aliasvault/client/crypto/VaultBlob';import SqliteClient from '@aliasvault/client/database/SqliteClient';import { syncErrorMessage, toSyncErrorDetail } from '@aliasvault/client/sync/SyncErrorMessage';import { hasUnsyncedUserChanges as hasUnsyncedUserChangesInStorage } from '@aliasvault/client/sync/VaultDirtyState';@@ -20,14 +21,9 @@ import type { UnlockKeyDerivationParams } from '@aliasvault/models/metadata';import { storage } from '#imports';/**- * Maximum time to wait for the background service worker to answer a PING before treating it as unresponsive.+ * Maximum time to wait for the background service worker to hand out the vault encryption key before treating it as unresponsive.*/-const BACKGROUND_PING_TIMEOUT_MS = 5000;--/**- * Maximum time to wait for GET_VAULT once the background is responsive (large vaults take a while to decrypt and transfer).- */-const GET_VAULT_TIMEOUT_MS = 30000;+const BACKGROUND_RESPONSE_TIMEOUT_MS = 5000;/*** Wrap a promise in a timeout that returns a rejected promise with a translated error carrying the given code.@@ -91,7 +87,7 @@ type DbContextType = {*/shouldSuppressEmailErrors: () => boolean;/**- * Load the stored (encrypted) vault from background storage into memory.+ * Load the stored (encrypted) vault into memory.* Returns the SqliteClient if vault was loaded successfully, null otherwise.*/loadStoredDatabase: () => Promise<SqliteClient | null>;@@ -256,50 +252,62 @@ export const DbProvider: React.FC<{ children: React.ReactNode }> = ({ children }}, []);/**- * Load the stored (encrypted) vault from background storage into memory.+ * Load the stored (encrypted) vault into memory.* Returns the SqliteClient if vault was loaded successfully.- * Throws an error if the background returns an error (all errors now have E-XXX codes).+ * Throws an error with an E-XXX code when the vault is missing, locked or cannot be decrypted.+ *+ * The popup reads and decrypts the stored vault itself instead of receiving it from the background: a runtime message+ * is capped at 64 MiB in Chrome, which a large vault exceeds. Only the key comes from the background, which stays the+ * single writer of the stored vault (see STORE_ENCRYPTED_VAULT).*/const loadStoredDatabase = useCallback(async (): Promise<SqliteClient | null> => {try {- // Ping service worker with a short timeout so an unresponsive service worker fails fast instead of keeping the popup open indefinitely.- await withTimeout(sendMessage('PING'), BACKGROUND_PING_TIMEOUT_MS, AppErrorCode.BACKGROUND_UNRESPONSIVE, 'common.errors.backgroundUnresponsive');+ // Short timeout so an unresponsive service worker fails fast instead of keeping the popup open indefinitely.+ const encryptionKey = await withTimeout(sendMessage('GET_ENCRYPTION_KEY'), BACKGROUND_RESPONSE_TIMEOUT_MS, AppErrorCode.BACKGROUND_UNRESPONSIVE, 'common.errors.backgroundUnresponsive');++ let encryptedVault: string | null;+ try {+ encryptedVault = await storage.getItem<string>(StorageKeys.ENCRYPTED_VAULT);+ } catch (error) {+ logFailure('Failed to read the stored vault', error);+ throw new Error(formatErrorWithCode(t('common.errors.unknownError'), AppErrorCode.STORAGE_READ_FAILED));+ }- // Get vault from background with a 30sec timeout as decrypting and transferring a large vault can take several seconds depending on the device hardware.- const response = await withTimeout(sendMessage('GET_VAULT'), GET_VAULT_TIMEOUT_MS, AppErrorCode.VAULT_LOAD_TIMEOUT, 'common.errors.vaultLoadTimeout');+ if (!encryptedVault) {+ throw new Error(formatErrorWithCode(t('common.errors.vaultNotAvailable'), AppErrorCode.VAULT_NOT_FOUND));+ }+ if (!encryptionKey) {+ throw new Error(formatErrorWithCode(t('common.errors.vaultIsLocked'), AppErrorCode.VAULT_LOCKED));+ }- // Check if response contains an error, if so, throw.- if (!response?.success && response?.error) {- throw new Error(response.error);+ let decryptedVault: Uint8Array;+ try {+ decryptedVault = await decryptVaultBlob(encryptedVault, encryptionKey);+ } catch (error) {+ logFailure('Failed to decrypt the stored vault', error);+ throw new Error(formatErrorWithCode(t('common.errors.unknownError'), AppErrorCode.VAULT_DECRYPT_FAILED));}- if (response?.vault) {- const client = new SqliteClient();- await client.initializeFromBase64(response.vault);+ const client = new SqliteClient();+ await client.initializeFromBytes(decryptedVault);- setSqliteClient(client);- setDbInitialized(true);- setDbAvailable(true);- return client;- } else {- // No vault and no error - this shouldn't happen but handle gracefully- setDbInitialized(true);- setDbAvailable(false);- return null;- }+ setSqliteClient(client);+ setDbInitialized(true);+ setDbAvailable(true);+ return client;} catch (error) {const message = error instanceof Error ? error.message : String(error);if (message.includes(AppErrorCode.VAULT_LOCKED)) {// Vault is locked which is expected when the popup is opened after auto-lock timeout or browser restart.} else {- logFailure('Error retrieving vault from background', error);+ logFailure('Error loading the stored vault', error);}setDbInitialized(true);setDbAvailable(false);// Re-throw all errors so callers can display them with proper codesthrow error;}- }, []);+ }, [t]);/*** Get the vault metadata from local storage (persistent).diff --git a/apps/browser-extension/src/entrypoints/popup/hooks/useVaultMutate.ts b/apps/browser-extension/src/entrypoints/popup/hooks/useVaultMutate.tsindex 564272366..cab24e8b7 100644--- a/apps/browser-extension/src/entrypoints/popup/hooks/useVaultMutate.ts+++ b/apps/browser-extension/src/entrypoints/popup/hooks/useVaultMutate.ts@@ -8,6 +8,7 @@ import { useDb } from '@/entrypoints/popup/context/DbContext';import { devLog } from '@/utils/devLogger/DevLogger';import { logFailure } from '@/utils/Diagnostics';import { sendMessage } from '@/utils/messaging/ExtensionMessaging';+import { storeEncryptedVault } from '@/utils/messaging/VaultBlobTransfer';/** How many unanswered sync-state polls in a row before the poll gives up on the background context. */const MAX_POLL_FAILURES = 10;@@ -57,11 +58,7 @@ export function useVaultMutate(): {const encryptedVaultBlob = await encryptVaultBlob(dbContext.sqliteClient!.exportToBytes(), encryptionKey);// Store the updated vault locally, mark dirty, increment mutation sequence.- await sendMessage('STORE_ENCRYPTED_VAULT', {- vaultBlob: encryptedVaultBlob,- markDirty: true,- scopes- });+ await storeEncryptedVault(encryptedVaultBlob, { markDirty: true, scopes });} catch (error) {/** Storing failed, but the write itself is still in the local database and the next mutation that doesdiff --git a/apps/browser-extension/src/entrypoints/popup/pages/items/ItemsList.tsx b/apps/browser-extension/src/entrypoints/popup/pages/items/ItemsList.tsxindex a98c18e36..992e959ce 100644--- a/apps/browser-extension/src/entrypoints/popup/pages/items/ItemsList.tsx+++ b/apps/browser-extension/src/entrypoints/popup/pages/items/ItemsList.tsx@@ -3,7 +3,7 @@ import { CredentialSortOrder } from '@aliasvault/client/database/repositories/Seimport { canHaveSubfolders, getDescendantFolderIds, getFolderPath, getRecursiveItemCount, isItemInFolder, isSharedFolder } from '@aliasvault/client/items/FolderUtils';import { ItemFilter, applySearchFilter, applyTypeFilter, isItemTypeFilter, parseItemFilterType, type ItemFilterType } from '@aliasvault/client/items/ItemFilters';import { multiManifestRendering } from '@aliasvault/client/sharing/MultiManifestRendering';-import React, { useState, useEffect, useCallback, useMemo, useRef } from 'react';+import React, { useState, useEffect, useCallback, useDeferredValue, useMemo, useRef } from 'react';import { useTranslation } from 'react-i18next';import { useLocation, useNavigate, useParams, useSearchParams } from 'react-router-dom';@@ -51,6 +51,11 @@ const FILTER_EXPIRY_MS = 5 * 60 * 1000; // 5 minutes*/const CURRENT_SITE_SUGGESTION_MIN_ITEMS = 5;+/**+ * How many items render per infinite scroll batch, so a large vault does not render thousands of item cards at once.+ */+const ITEM_BATCH_SIZE = 200;+/*** Sort order options with their translation keys*/@@ -144,8 +149,16 @@ const ItemsList: React.FC = () => {const [sortOrder, setSortOrder] = useState<CredentialSortOrder>(CredentialSortOrder.NewestFirst);const [showSortMenu, setShowSortMenu] = useState(false);const [showFolders, setShowFolders] = useState(true);+ const [visibleItemCount, setVisibleItemCount] = useState(ITEM_BATCH_SIZE);const { setIsInitialLoading } = useLoading();const searchInputRef = useRef<HTMLInputElement | null>(null);+ const sentinelRef = useRef<HTMLDivElement | null>(null);++ /*+ * The list filters on a deferred copy of the search term: the input updates on every keystroke right away, and+ * React filters and renders the list for the latest term when it has time, so typing never waits on the list.+ */+ const listSearchTerm = useDeferredValue(searchTerm);// Load showFolders preference from storage on mountuseEffect(() => {@@ -582,8 +595,11 @@ const ItemsList: React.FC = () => {* - Counts include items in the folder AND all subfolders recursively* - Counts respect the active type/feature filter so the badge matches what the user sees inside.*/- const getFoldersWithCounts = (): FolderWithCount[] => {- if (searchTerm) {+ const folders = useMemo((): FolderWithCount[] => {+ // folderRefreshKey is included in deps to force re-computation when a folder changes+ void folderRefreshKey;++ if (listSearchTerm) {return []; // Don't show folders when searching}@@ -647,16 +663,16 @@ const ItemsList: React.FC = () => {})).sort((a: { name: string }, b: { name: string }) => a.name.localeCompare(b.name));return result;- };+ }, [listSearchTerm, dbContext?.sqliteClient, currentFolderRef, items, filterType, folderRefreshKey]);/*** Filter items based on current view (folder, search, filter type)*/- const filteredItems = ((): Item[] => {+ const filteredItems = useMemo((): Item[] => {// Filter by current folder (if in folder view)let folderScoped: Item[];if (currentFolderRef !== null) {- if (searchTerm) {+ if (listSearchTerm) {// When searching inside a folder, include items in subfolders tooconst allFolders = dbContext?.sqliteClient?.folders.getAll() || [];const childFolderIds = getDescendantFolderIds(currentFolderRef, allFolders);@@ -668,7 +684,7 @@ const ItemsList: React.FC = () => {// When not searching, only show direct items (not items in subfolders)folderScoped = items.filter((item: Item) => isItemInFolder(item, currentFolderRef));}- } else if (!searchTerm && showFolders) {+ } else if (!listSearchTerm && showFolders) {/** When showing folders (checkbox ON): only show root items (exclude items in folders)* When not showing folders (checkbox OFF): show all items flat@@ -680,8 +696,8 @@ const ItemsList: React.FC = () => {const typeFiltered = applyTypeFilter(folderScoped, filterType);- return applySearchFilter(typeFiltered, searchTerm);- })();+ return applySearchFilter(typeFiltered, listSearchTerm);+ }, [items, currentFolderRef, listSearchTerm, showFolders, filterType, dbContext?.sqliteClient]);/*** Sort the filtered items based on the current sort order.@@ -705,7 +721,46 @@ const ItemsList: React.FC = () => {}}, [filteredItems, sortOrder]);- const folders = getFoldersWithCounts();+ /**+ * Start again at the first batch whenever the list itself changes (search, filter, folder or sort order).+ */+ useEffect(() => {+ setVisibleItemCount(ITEM_BATCH_SIZE);+ }, [listSearchTerm, filterType, currentFolderRef, sortOrder, showFolders]);++ /*+ * The rendered part of the list. It always reaches a freshly duplicated item, so the highlight can scroll to it.+ */+ const highlightedIndex = useMemo(() => (highlightedItem ? sortedItems.findIndex(item => isSameItem(item, highlightedItem)) : -1), [sortedItems, highlightedItem]);+ const renderedItemCount = Math.max(visibleItemCount, highlightedIndex + 1);+ const visibleItems = useMemo(() => sortedItems.slice(0, renderedItemCount), [sortedItems, renderedItemCount]);+ const hasMoreItems = renderedItemCount < sortedItems.length;++ /**+ * Keep a highlighted item rendered after the highlight clears, instead of dropping it from the list again.+ */+ useEffect(() => {+ if (highlightedIndex >= 0) {+ setVisibleItemCount(count => Math.max(count, highlightedIndex + 1));+ }+ }, [highlightedIndex]);++ /**+ * Infinite scroll: render the next batch when the sentinel below the list comes into view.+ */+ useEffect(() => {+ const sentinel = sentinelRef.current;+ if (!sentinel || !hasMoreItems || isLoading) {+ return;+ }+ const observer = new IntersectionObserver((entries) => {+ if (entries.some(entry => entry.isIntersecting)) {+ setVisibleItemCount(count => count + ITEM_BATCH_SIZE);+ }+ });+ observer.observe(sentinel);+ return (): void => observer.disconnect();+ }, [hasMoreItems, visibleItemCount, isLoading]);/*** Calculate total item count including items in current folder and all child folders.@@ -788,7 +843,7 @@ const ItemsList: React.FC = () => {const { activeKind, activeIndex, itemIdFor, folderIdFor, activeDescendantId } = useListKeyboardNav({folderCount: folders.length,- itemCount: sortedItems.length,+ itemCount: visibleItems.length,searchInputRef,resetKey: currentFolderRef ? scopedKey(currentFolderRef.ManifestId, currentFolderRef.Id) : null,onActivateFolder: handleActivateFolder,@@ -797,6 +852,15 @@ const ItemsList: React.FC = () => {onClearSearch: handleClearSearch,});+ /**+ * Render the next batch while keyboard selection is still a few items from the end, so a held arrow-down keeps going.+ */+ useEffect(() => {+ if (activeKind === 'item' && activeIndex >= visibleItems.length - 10 && hasMoreItems) {+ setVisibleItemCount(count => count + ITEM_BATCH_SIZE);+ }+ }, [activeKind, activeIndex, visibleItems.length, hasMoreItems]);+if (isLoading) {return (<div className="flex justify-center items-center p-8">@@ -1084,14 +1148,14 @@ const ItemsList: React.FC = () => {)}{/* Items */}- {sortedItems.length > 0 && (+ {visibleItems.length > 0 && (<ul id="items-list" role="listbox" className="space-y-2">- {sortedItems.map((item, index) => (+ {visibleItems.map((item, index) => (<ItemCardkey={scopedKey(item.ManifestId, item.Id)}item={item}- showFolderPath={!!searchTerm && !!item.FolderPath}- searchTerm={searchTerm}+ showFolderPath={!!listSearchTerm && !!item.FolderPath}+ searchTerm={listSearchTerm}currentFolderPath={currentFolderPath}isActive={activeKind === 'item' && activeIndex === index}optionId={itemIdFor(index)}@@ -1103,6 +1167,8 @@ const ItemsList: React.FC = () => {</ul>)}+ {hasMoreItems && <div ref={sentinelRef} className="h-10" />}+{/* Show help text when inside an empty folder with no items */}{currentFolderId && sortedItems.length === 0 && !searchTerm && (<div className="text-gray-500 dark:text-gray-400 text-sm mb-4">diff --git a/apps/browser-extension/src/i18n/locales/en.json b/apps/browser-extension/src/i18n/locales/en.jsonindex ff9ef6f15..9107d036f 100644--- a/apps/browser-extension/src/i18n/locales/en.json+++ b/apps/browser-extension/src/i18n/locales/en.json@@ -122,7 +122,6 @@"vaultTooLarge": "The vault is too large for the server to accept. Try to remove some items or attachments to reduce the size and try again.","vaultSyncTimeout": "Vault sync timed out. This can happen when the vault is large or the internet connection is slow. Please try again.","backgroundUnresponsive": "The extension background service is not responding. Please close and reopen the extension, or restart your browser if the problem persists.",- "vaultLoadTimeout": "Loading the vault took too long. Please close and reopen the extension and try again, or restart your browser if the problem persists.","invalidCode": "Please enter a valid 6-digit authentication code.","serverError": "Could not reach AliasVault server. Please try again later or contact support if the problem persists.","vaultLoadError": "Something went wrong while loading your vault. Please copy the error details below and contact support for help.",diff --git a/apps/browser-extension/src/utils/messaging/ExtensionMessaging.ts b/apps/browser-extension/src/utils/messaging/ExtensionMessaging.tsindex 0fa23c73a..d1b07bfcc 100644--- a/apps/browser-extension/src/utils/messaging/ExtensionMessaging.ts+++ b/apps/browser-extension/src/utils/messaging/ExtensionMessaging.ts@@ -26,7 +26,6 @@ import type { DuplicateCheckResponse } from '@/utils/types/messaging/DuplicateChimport type { FullVaultSyncRequest } from '@/utils/types/messaging/FullVaultSyncRequest';import type { ItemsResponse } from '@/utils/types/messaging/ItemsResponse';import type { SaveLoginResponse } from '@/utils/types/messaging/SaveLoginResponse';-import type { VaultResponse } from '@/utils/types/messaging/VaultResponse';import type { VaultSyncPhase } from '@/utils/types/messaging/VaultSyncPhase';import type { VaultSyncState } from '@/utils/types/messaging/VaultSyncState';@@ -38,6 +37,15 @@ import type { FullVaultSyncResult, SharedManifestDetails, VaultManifestMigrationimport type { UnlockKeyDerivationParams } from '@aliasvault/models/metadata';import type { LoginResponse } from '@aliasvault/models/webapi';+/**+ * How the background stores an encrypted vault blob, sent along with its last chunk (see VaultBlobTransfer).+ */+export type VaultBlobStoreOptions = {+ markDirty?: boolean;+ expectedMutationSeq?: number;+ scopes?: VaultMutationScope[];+};+/* eslint-disable @typescript-eslint/no-explicit-any *//*** Protocol map enumerating every message that flows through the extension's@@ -76,7 +84,6 @@ export interface IExtensionMessageProtocol {GET_SYNC_STATE(): VaultSyncState;GET_TOTP_SECRETS(data: { items: ItemRef[] }): { success: boolean; secrets?: Record<string, TotpSecret>; error?: string };GET_TWO_FACTOR_STATE(): TwoFactorState | null;- GET_VAULT(): VaultResponse;GET_VAULT_MIGRATION_STATUS(): VaultMigrationKind;GET_WEBAUTHN_SETTINGS(data: any): WebAuthnSettingsResponse;GROUP_CREATE_VAULT(data: { groupId: string; name: string }): { success: boolean; error?: string; apiErrorCode?: string };@@ -92,7 +99,6 @@ export interface IExtensionMessageProtocol {OPEN_POPUP_WITH_ITEM(data: { itemId: string; manifestId: string }): BoolResponse;PASSKEY_POPUP_RESPONSE(data: any): { success: boolean };PERSIST_FORM_VALUES(data: any): void;- PING(): boolean;POPUP_HEARTBEAT(): void;RECORD_ITEM_USAGE(data: { itemId: string; manifestId: string; action: ItemUsageAction }): { success: boolean };RESET_AUTO_LOCK_TIMER(): void;@@ -101,7 +107,7 @@ export interface IExtensionMessageProtocol {SET_AUTO_LOCK_TIMEOUT(data: number): boolean;SET_CLIPBOARD_CLEAR_TIMEOUT(data: number): boolean;SET_RECENTLY_SELECTED(data: { itemId: string; manifestId: string; domain: string }): { success: boolean };- STORE_ENCRYPTED_VAULT(data: { vaultBlob: string; markDirty?: boolean; expectedMutationSeq?: number; scopes?: VaultMutationScope[] }): { success: boolean; mutationSequence: number };+ STORE_ENCRYPTED_VAULT(data: { transferId: string; index: number; chunk: string; commit?: VaultBlobStoreOptions }): { success: boolean; mutationSequence: number } | null;STORE_UNLOCK_KEY(data: string): BoolResponse;STORE_UNLOCK_KEY_DERIVATION_PARAMS(data: UnlockKeyDerivationParams): BoolResponse;STORE_LAST_AUTOFILLED(data: LastAutofilledCredential): { success: boolean };diff --git a/apps/browser-extension/src/utils/messaging/VaultBlobTransfer.ts b/apps/browser-extension/src/utils/messaging/VaultBlobTransfer.tsnew file mode 100644index 000000000..c5293661e--- /dev/null+++ b/apps/browser-extension/src/utils/messaging/VaultBlobTransfer.ts@@ -0,0 +1,25 @@+import { sendMessage, type VaultBlobStoreOptions } from '@/utils/messaging/ExtensionMessaging';++/**+ * Characters of the encrypted vault blob per STORE_ENCRYPTED_VAULT message. A single runtime message is capped at+ * 64 MiB in Chrome, which a large vault could exceed, so communication happens in chunks by default.+ */+const VAULT_BLOB_CHUNK_CHARS = 8 * 1024 * 1024;++/**+ * Send an encrypted vault blob to the background in chunks and store it there. The last chunk carries the store options.+ * @param vaultBlob - the encrypted vault blob+ * @param options - how the background stores the blob+ * @returns The store result the background reported for the last chunk+ */+export async function storeEncryptedVault(vaultBlob: string, options: VaultBlobStoreOptions): Promise<{ success: boolean; mutationSequence: number }> {+ const transferId = crypto.randomUUID();+ const chunkCount = Math.max(1, Math.ceil(vaultBlob.length / VAULT_BLOB_CHUNK_CHARS));+ for (let index = 0; index < chunkCount - 1; index++) {+ await sendMessage('STORE_ENCRYPTED_VAULT', { transferId, index, chunk: vaultBlob.slice(index * VAULT_BLOB_CHUNK_CHARS, (index + 1) * VAULT_BLOB_CHUNK_CHARS) });+ }++ const lastIndex = chunkCount - 1;+ // A chunk that carries the store options always gets the store result back, never null.+ return (await sendMessage('STORE_ENCRYPTED_VAULT', { transferId, index: lastIndex, chunk: vaultBlob.slice(lastIndex * VAULT_BLOB_CHUNK_CHARS), commit: options }))!;+}diff --git a/apps/browser-extension/src/utils/types/messaging/VaultResponse.ts b/apps/browser-extension/src/utils/types/messaging/VaultResponse.tsdeleted file mode 100644index b9405a0d3..000000000--- a/apps/browser-extension/src/utils/types/messaging/VaultResponse.ts+++ /dev/null@@ -1,7 +0,0 @@-export type VaultResponse = {- success: boolean, error?: string,- vault?: string,- publicEmailDomains?: string[],- privateEmailDomains?: string[],- hiddenPrivateEmailDomains?: string[]-};diff --git a/core/client/src/api/WebApiService.ts b/core/client/src/api/WebApiService.tsindex b3fb734c3..754634e65 100644--- a/core/client/src/api/WebApiService.ts+++ b/core/client/src/api/WebApiService.ts@@ -337,19 +337,6 @@ export class WebApiService {}, parseJson);}- /**- * Issue PUT request to the API.- */- public async put<TRequest, TResponse>(endpoint: string, data: TRequest): Promise<TResponse> {- return this.authFetch<TResponse>(endpoint, {- method: 'PUT',- headers: {- 'Content-Type': 'application/json',- },- body: JSON.stringify(data),- });- }-/*** Issue DELETE request to the API.*/diff --git a/core/client/src/api/errors/AppErrorCodes.ts b/core/client/src/api/errors/AppErrorCodes.tsindex b07456de9..0ecd57fb6 100644--- a/core/client/src/api/errors/AppErrorCodes.ts+++ b/core/client/src/api/errors/AppErrorCodes.ts@@ -23,7 +23,7 @@export enum AppErrorCode {// Generic errors (E-0xx)UNKNOWN_ERROR = 'E-001',- BACKGROUND_UNRESPONSIVE = 'E-002', // Background service worker did not answer a PING in time+ BACKGROUND_UNRESPONSIVE = 'E-002', // Background service worker did not answer in time// Auth status check errors (E-1xx) - handleCheckAuthStatusAUTH_STATUS_CHECK_FAILED = 'E-101',@@ -35,7 +35,6 @@ export enum AppErrorCode {VAULT_LOCKED = 'E-202', // No encryption key availableVAULT_DECRYPT_FAILED = 'E-203', // The locally stored vault does not decrypt with the session keyVAULT_METADATA_READ_FAILED = 'E-204', // Failed to read vault metadata- VAULT_LOAD_TIMEOUT = 'E-205', // Background did not return the vault in timeUNLOCK_KEY_REJECTED = 'E-206', // The unlock key does not open the account key (wrong password)KEY_CHAIN_UNREADABLE = 'E-207', // The account key opened, the vault encryption key under it did notKEY_OUT_OF_SYNC = 'E-208', // The session key does not open the key chain the server holds (re-login needed)@@ -141,7 +140,6 @@ export function getErrorTranslationKey(code: AppErrorCode): string {[AppErrorCode.VAULT_LOCKED]: 'common.errors.vaultIsLocked',[AppErrorCode.VAULT_DECRYPT_FAILED]: 'common.errors.vaultDataUnreadable',[AppErrorCode.VAULT_METADATA_READ_FAILED]: 'common.errors.unexpectedErrorContactSupport',- [AppErrorCode.VAULT_LOAD_TIMEOUT]: 'common.errors.vaultLoadTimeout',[AppErrorCode.UNLOCK_KEY_REJECTED]: 'common.errors.wrongPassword',[AppErrorCode.KEY_CHAIN_UNREADABLE]: 'common.errors.keyChainUnreadable',[AppErrorCode.KEY_OUT_OF_SYNC]: 'common.errors.sessionExpired',diff --git a/core/client/src/database/SqliteClient.ts b/core/client/src/database/SqliteClient.tsindex d4ddfd17d..a60fbe014 100644--- a/core/client/src/database/SqliteClient.ts+++ b/core/client/src/database/SqliteClient.ts@@ -5,7 +5,7 @@ import { VaultVersionIncompatibleError } from '../api/errors/VaultVersionIncompaimport { StorageKeys } from '../constants/StorageKeys';import { getPlatform } from '../platform/ClientPlatform';import { TranslatableMessage } from '../platform/TranslatableMessage';-import { base64ToBytes, bytesToBase64 } from '../utilities/Base64';+import { bytesToBase64 } from '../utilities/Base64';import { logDefect } from '../utilities/Diagnostics';import { detectImageMimeType } from '../utilities/ImageType';@@ -183,14 +183,6 @@ export class SqliteClient implements ISyncDatabaseClient {return this.db;}- /**- * Initialize the SQLite database from a base64 string.- * @param base64String - Base64 encoded SQLite database- */- public async initializeFromBase64(base64String: string): Promise<void> {- return this.initializeFromBytes(base64ToBytes(base64String));- }-/*** Initialize the SQLite database from the raw database bytes.* @param bytes - the SQLite databasediff --git a/core/client/src/utilities/Base64.ts b/core/client/src/utilities/Base64.tsindex 314866f21..e5bcdbac5 100644--- a/core/client/src/utilities/Base64.ts+++ b/core/client/src/utilities/Base64.ts@@ -8,11 +8,23 @@const CHUNK_SIZE = 0x8000;/**- * Encode raw bytes as base64 via latin-1 character codes.+ * The built-in base64 methods on Uint8Array (Chrome/Edge 140+, Firefox 133+, Safari 18.2+), typed here since not every+ * host's TS lib has them yet. They run in native code, about 50x faster on a large vault than the latin-1 fallback.+ */+type NativeBase64Bytes = Uint8Array & { toBase64?: () => string };+const NativeUint8Array = Uint8Array as typeof Uint8Array & { fromBase64?: (base64: string) => Uint8Array<ArrayBuffer> };+const hasNativeEncode = typeof (Uint8Array.prototype as NativeBase64Bytes).toBase64 === 'function';+const hasNativeDecode = typeof NativeUint8Array.fromBase64 === 'function';++/**+ * Encode raw bytes as base64, natively where the runtime supports it and via latin-1 character codes otherwise.* @param bytes - the bytes to encode* @returns The base64 representation of the bytes*/export function bytesToBase64(bytes: Uint8Array): string {+ if (hasNativeEncode) {+ return (bytes as NativeBase64Bytes).toBase64!();+ }const parts: string[] = [];for (let i = 0; i < bytes.length; i += CHUNK_SIZE) {parts.push(String.fromCharCode(...bytes.subarray(i, i + CHUNK_SIZE)));@@ -26,6 +38,9 @@ export function bytesToBase64(bytes: Uint8Array): string {* @returns The decoded bytes*/export function base64ToBytes(base64: string): Uint8Array<ArrayBuffer> {+ if (hasNativeDecode) {+ return NativeUint8Array.fromBase64!(base64);+ }const binaryString = atob(base64);const bytes = new Uint8Array(binaryString.length);for (let i = 0; i < binaryString.length; i++) {