Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Refactor manifest revision local tracking (#2159)
Leendert de Borstb6114ac2b93e
diff --git a/apps/browser-extension/src/entrypoints/popup/pages/auth/Login.tsx b/apps/browser-extension/src/entrypoints/popup/pages/auth/Login.tsxindex 2e2a91bad..13f2465ba 100644--- a/apps/browser-extension/src/entrypoints/popup/pages/auth/Login.tsx+++ b/apps/browser-extension/src/entrypoints/popup/pages/auth/Login.tsx@@ -21,7 +21,7 @@ import { SrpAuthService } from '@/utils/auth/SrpAuthService';import { StorageKeys } from '@/utils/constants/storageKeys';import type { VaultResponse, LoginResponse } from '@/utils/dist/core/models/webapi';import { EncryptionUtility } from '@/utils/EncryptionUtility';-import { getManifestRevisions, getPersonalManifestId } from '@/utils/ManifestRevisions';+import { getManifestRevisions } from '@/utils/ManifestRevisions';import { sendMessage } from '@/utils/messaging/ExtensionMessaging';import { ApiAuthError } from '@/utils/types/errors/ApiAuthError';import { hasErrorCode, getErrorMessage } from '@/utils/types/errors/AppErrorCodes';@@ -41,6 +41,19 @@ let usernamePrefillAttempted = false;/** Track if 2FA state restoration has been attempted (only do it once on mount) */let twoFactorStateRestoreAttempted = false;+/**+ * Whether the local vault's last-known baselines cover everything the server holds: every server manifest is known+ * locally at the same (or a higher, i.e. server rolled back) revision, and the local vault tracks no manifest the+ * server has since removed (revoked share). Only then is preserving the local blob over the freshly pulled one safe.+ * @param localRevisions - the per-manifest baselines the local blob was built from (captured before the pull)+ * @param serverRevisions - the server's per-manifest revisions (what the pull just stored)+ */+const localVaultCoversServer = (localRevisions: Record<string, number>, serverRevisions: Record<string, number>): boolean => {+ const serverCovered = Object.entries(serverRevisions).every(([manifestId, revision]) => (localRevisions[manifestId] ?? -1) >= revision);+ const nothingRevoked = Object.keys(localRevisions).every(manifestId => manifestId in serverRevisions);+ return serverCovered && nothingRevoked;+};+/*** Login page*/@@ -71,19 +84,17 @@ const Login: React.FC = () => {/*** Helper to persist and load vault after successful authentication.- * Checks if local vault exists from forced logout and preserves it if more advanced.+ * Checks if local vault exists from forced logout and preserves it if it still covers everything the server holds.* Also checks if the vault belongs to the same user - if different user, uses server vault.+ * @param preLoginRevisions - the per-manifest baselines as they were BEFORE the pull: the pull replaces the stored+ * map with the server's, so this captured copy is the only record of what the local blob was built from.*/- const persistAndLoadVault = async (vaultResponse: VaultResponse, encryptionKey: string, loginUsername: string): Promise<void> => {+ const persistAndLoadVault = async (vaultResponse: VaultResponse, encryptionKey: string, loginUsername: string, preLoginRevisions: Record<string, number>): Promise<void> => {// Check if there's existing vault data (from forced logout)const existingVault = await storage.getItem(StorageKeys.ENCRYPTED_VAULT) as string | null;const storedUsername = await storage.getItem(StorageKeys.USERNAME) as string | null;- const personalManifestId = await getPersonalManifestId();- const existingRevision = personalManifestId ? (await getManifestRevisions())[personalManifestId] ?? null : null;- let vaultToLoad = vaultResponse.vault.blob;-- if (existingVault && existingRevision !== null) {+ if (existingVault && Object.keys(preLoginRevisions).length > 0) {// Check if the existing vault belongs to a different userconst normalizedLoginUsername = loginUsername.toLowerCase().trim();const normalizedStoredUsername = storedUsername?.toLowerCase().trim();@@ -98,15 +109,11 @@ const Login: React.FC = () => {try {const decryptedExisting = await EncryptionUtility.symmetricDecrypt(existingVault, encryptionKey);- // Check if existing vault is more advanced than server- if (existingRevision >= vaultResponse.vault.currentRevisionNumber) {- console.info(- `Existing vault is more advanced (rev ${existingRevision} >= ${vaultResponse.vault.currentRevisionNumber}), ` +- `preserving local vault and will upload to server`- );+ // The pull stored the server's per-manifest revisions; compare them against the pre-login baselines.+ if (localVaultCoversServer(preLoginRevisions, await getManifestRevisions())) {+ console.info('Existing local vault covers every server manifest, preserving it (pending local changes upload on the next sync)');// Update metadata and load existing vault- vaultToLoad = existingVault;await sendMessage('STORE_VAULT_METADATA', {publicEmailDomainList: vaultResponse.vault.publicEmailDomainList,privateEmailDomainList: vaultResponse.vault.privateEmailDomainList,@@ -118,10 +125,7 @@ const Login: React.FC = () => {}// Server is more advanced, fetch server vault- console.info(- `Server vault is more advanced (rev ${vaultResponse.vault.currentRevisionNumber} > ${existingRevision}), ` +- `using server vault`- );+ console.info('Server has manifests ahead of (or unknown to) the local baselines, using server vault');} catch {// Decryption failed, password changed or corrupt vaultconsole.info('Existing vault could not be decrypted (password changed), using server vault');@@ -129,11 +133,8 @@ const Login: React.FC = () => {}}- // Normal flow: persist server vault to local storage- await sendMessage('STORE_ENCRYPTED_VAULT', {- vaultBlob: vaultResponse.vault.blob,- personalManifestRevision: vaultResponse.vault.currentRevisionNumber,- });+ // Normal flow: persist server vault to local storage. The pull already recorded the per-manifest revisions.+ await sendMessage('STORE_ENCRYPTED_VAULT', { vaultBlob: vaultResponse.vault.blob });await sendMessage('STORE_VAULT_METADATA', {publicEmailDomainList: vaultResponse.vault.publicEmailDomainList,@@ -142,7 +143,7 @@ const Login: React.FC = () => {});// Decrypt and load the vault into memory- const decryptedVault = await EncryptionUtility.symmetricDecrypt(vaultToLoad, encryptionKey);+ const decryptedVault = await EncryptionUtility.symmetricDecrypt(vaultResponse.vault.blob, encryptionKey);await dbContext.loadDatabase(decryptedVault);};@@ -173,6 +174,9 @@ const Login: React.FC = () => {encryptionSettings: loginResponse.encryptionSettings});+ // Capture the pre-pull revision baselines: the pull below replaces the stored map with the server's.+ const preLoginRevisions = await getManifestRevisions();+// Fetch the latest vault.const vaultResponseJson = await vaultSyncService.pull(encryptionKey);@@ -180,11 +184,11 @@ const Login: React.FC = () => {* Persist and load the vault.* If there was a forced logout, persistAndLoadVault checks existing vault data:* - If different user > uses server vault- * - If local vault is more advanced > preserves it (will upload via sync in /reinitialize)+ * - If local vault still covers the server state > preserves it (will upload via sync in /reinitialize)* - If server is more advanced > uses server vault* - If password changed (can't decrypt) > uses server vault*/- await persistAndLoadVault(vaultResponseJson, encryptionKey, username);+ await persistAndLoadVault(vaultResponseJson, encryptionKey, username, preLoginRevisions);// Reset prefill flag so next logout will prefill againusernamePrefillAttempted = false;@@ -480,11 +484,14 @@ const Login: React.FC = () => {encryptionSettings: result.encryptionSettings,});+ // Capture the pre-pull revision baselines: the pull below replaces the stored map with the server's.+ const preLoginRevisions = await getManifestRevisions();+// Fetch the latest vault.const vaultResponse = await vaultSyncService.pull(mobileKey);// Persist and load the vault- await persistAndLoadVault(vaultResponse, mobileKey, result.username);+ await persistAndLoadVault(vaultResponse, mobileKey, result.username, preLoginRevisions);/** Navigate to reinitialize page which will:diff --git a/apps/browser-extension/src/utils/ManifestRevisions.ts b/apps/browser-extension/src/utils/ManifestRevisions.tsindex 00c177d0d..a33829323 100644--- a/apps/browser-extension/src/utils/ManifestRevisions.ts+++ b/apps/browser-extension/src/utils/ManifestRevisions.ts@@ -7,7 +7,6 @@import { storage } from 'wxt/utils/storage';import { StorageKeys } from '@/utils/constants/storageKeys';-import { devWarn } from '@/utils/devLogger/DevLogger';/*** Every manifest's last-known server revision, keyed by manifest id; empty when nothing has been pulled yet.@@ -38,23 +37,3 @@ export async function recordManifestRevisions(revisions: Record<string, number>)export async function getPersonalManifestId(): Promise<string | null> {return (await storage.getItem(StorageKeys.VAULT_PERSONAL_MANIFEST_ID)) as string | null;}--/** The personal manifest's last-known server revision. */-export async function getPersonalManifestRevision(): Promise<number> {- const personalManifestId = await getPersonalManifestId();- return personalManifestId ? (await getManifestRevisions())[personalManifestId] ?? 0 : 0;-}--/**- * Record the personal manifest's revision.- * @param revision - the revision the server reported for the personal manifest- */-export async function recordPersonalManifestRevision(revision: number): Promise<void> {- const personalManifestId = await getPersonalManifestId();- if (!personalManifestId) {- devWarn(`[VaultSync] No personal manifest id known, dropping personal revision ${revision}; the next sync pulls to rebuild the baseline.`);- return;- }-- await recordManifestRevisions({ [personalManifestId]: revision });-}diff --git a/apps/browser-extension/src/utils/VaultSyncService.ts b/apps/browser-extension/src/utils/VaultSyncService.tsindex 77ae2736f..5a2c52639 100644--- a/apps/browser-extension/src/utils/VaultSyncService.ts+++ b/apps/browser-extension/src/utils/VaultSyncService.ts@@ -13,7 +13,7 @@ import { VaultSqlGenerator } from '@/utils/dist/core/vault';import { buildEmailRouting } from '@/utils/EmailRouting';import { EncryptionUtility } from '@/utils/EncryptionUtility';import { completeLegacyAccountKeyMigration, isLegacySqliteBlobSnapshot, legacyUnstampedRowAdoption, openLegacySqliteBlobSnapshot, prepareLegacyAccountKeyMigration, withOutdatedServerGuard, type LegacyAccountKeyMigration, type LegacySqliteBlobSnapshot } from '@/utils/legacy/LegacyStorageModelMigration';-import { getManifestRevisions, getPersonalManifestId, getPersonalManifestRevision, recordManifestRevisions, replaceManifestRevisions } from '@/utils/ManifestRevisions';+import { getManifestRevisions, getPersonalManifestId, recordManifestRevisions, replaceManifestRevisions } from '@/utils/ManifestRevisions';import {vaultCodecComputeCiphertextHash, vaultCodecComputeContentFingerprint, vaultCodecCanonicalizeFromSqlite, vaultCodecExtractEncryptionKeyForPublicKey, vaultCodecGenerateManifestSalt, vaultCodecUnpackPayload, vaultCodecMaterializeAsSqlite, vaultCodecPackPayload, vaultCodecValidateManifest, vaultCodecValidateDataBucket, type CodecBlobEntry, type CodecCanonicalized, type CodecManifest, type CodecManifestSpec} from '@/utils/RustCore';import { SharingService, type SessionSharedManifest } from '@/utils/SharingService';import { SqliteClient } from '@/utils/SqliteClient';@@ -123,11 +123,11 @@ type PullResult = {};/**- * Result of a push.+ * Result of a push. The revisions of every manifest the write carried are recorded in the per-manifest revision+ * map ({@link recordManifestRevisions}) as part of the push itself; callers only act on the status.*/export type PushResult = {status: 'ok' | 'outdated' | 'missing-blobs' | 'rejected';- newManifestRevision: number | null;reasons?: string[];/** The new encryption key (set by legacy migration). */newEncryptionKey?: string;@@ -907,7 +907,6 @@ export class VaultSyncService {if (!bucketValidation.ok) {return {status: 'rejected',- newManifestRevision: null,reasons: [`${label} validation failed: ${bucketValidation.failedRules.join(', ')}. ${bucketValidation.message}`.trim()],};}@@ -946,7 +945,6 @@ export class VaultSyncService {if (candidate.isPersonal) {return {status: 'rejected',- newManifestRevision: null,reasons: [`Manifest validation failed: ${validation.failedRules.join(', ')}. ${validation.message}`.trim()],};}@@ -980,7 +978,7 @@ export class VaultSyncService {*/if (manifestWrites.length === 0 && bucketDtos.length === 0) {devLog('[V2Push] No content changes detected (every manifest and data bucket matches the server baselines); skipping upload.');- return { status: 'ok', newManifestRevision: currentManifestRevision };+ return { status: 'ok' };}/*@@ -1074,7 +1072,7 @@ export class VaultSyncService {*/const unsatisfiable = resp.missingBlobHashes.filter(h => !blobEntries.has(h));if (unsatisfiable.length > 0) {- return { status: 'missing-blobs', newManifestRevision: currentManifestRevision, reasons: unsatisfiable };+ return { status: 'missing-blobs', reasons: unsatisfiable };}devWarn(`[V2Sync] Server reported ${resp.missingBlobHashes.length} missing blob(s); uploading and retrying once.`);@@ -1086,16 +1084,13 @@ export class VaultSyncService {resp = await webApi.post<typeof payload, VaultWriteResponseDto>(VAULT_ENDPOINT, payload);if (resp.missingBlobHashes && resp.missingBlobHashes.length > 0) {- return { status: 'missing-blobs', newManifestRevision: currentManifestRevision, reasons: resp.missingBlobHashes };+ return { status: 'missing-blobs', reasons: resp.missingBlobHashes };}}- const personalResult = (resp.manifestRevisions ?? []).find(r => r.manifestId === personalRecord.manifestId);- const newPersonalRevision = personalResult?.revision ?? currentManifestRevision;-if (resp.status !== 0) {// All-or-nothing: a single stale manifest or bucket rejected the whole write; the orchestrator pulls/merges/retries.- return { status: 'outdated', newManifestRevision: newPersonalRevision };+ return { status: 'outdated' };}// 5) Update local persisted state on success.@@ -1107,9 +1102,10 @@ export class VaultSyncService {await storage.setItem(StorageKeys.VAULT_BUCKET_REVISIONS, bucketRevisions);}+ // Advance the baseline of the manifests this write included.const sessionShared = await SharingService.getSessionSharedManifests();- const writtenRevisions = new Map((resp.manifestRevisions ?? []).filter(r => r.manifestId !== personalRecord.manifestId).map(r => [r.manifestId, r.revision]));- await recordManifestRevisions({ ...Object.fromEntries(writtenRevisions), [await this.resolvePersonalManifestId()]: newPersonalRevision });+ const writtenRevisions = new Map((resp.manifestRevisions ?? []).map(r => [r.manifestId, r.revision]));+ await recordManifestRevisions(Object.fromEntries(writtenRevisions));for (const record of Object.values(sessionShared)) {const revision = writtenRevisions.get(record.manifestId);if (revision !== undefined) {@@ -1157,7 +1153,7 @@ export class VaultSyncService {const totalChars = manifestChars + bucketChars + uploadedBlobChars;devLog(`[V2Push] Total pushed (encrypted): ${manifestWrites.length} manifest(s) ${formatKb(manifestChars)} + ${bucketDtos.length} buckets ${formatKb(bucketChars)} + ${uploadedCiphertexts.size} blobs ${formatKb(uploadedBlobChars)} = ${formatKb(totalChars)}.`);- return { status: 'ok', newManifestRevision: newPersonalRevision, newEncryptionKey: migration ? contentKey : undefined };+ return { status: 'ok', newEncryptionKey: migration ? contentKey : undefined };}/**@@ -1339,13 +1335,14 @@ export class VaultSyncService {await storage.setItem(StorageKeys.VAULT_MANIFEST_SALT, manifestSalt);}+ const personalManifestId = await this.resolvePersonalManifestId();const records: ManifestRecord[] = [{- manifestId: await this.resolvePersonalManifestId(),+ manifestId: personalManifestId,isPersonal: true,salt: manifestSalt,folderId: null,vek: null,- revision: await getPersonalManifestRevision(),+ revision: (await getManifestRevisions())[personalManifestId] ?? 0,name: null,// The personal manifest's own delivery key is published as the account-level key, not per manifest.canAdminister: false,diff --git a/apps/browser-extension/src/utils/db/queries/SettingsQueries.ts b/apps/browser-extension/src/utils/db/queries/SettingsQueries.tsindex 5e1f749be..e08abba25 100644--- a/apps/browser-extension/src/utils/db/queries/SettingsQueries.ts+++ b/apps/browser-extension/src/utils/db/queries/SettingsQueries.ts@@ -12,15 +12,6 @@ export class SettingsQueries {FROM Settings sWHERE s.ManifestId = ? AND s.Key = ?`;- /**- * Get setting by key regardless of manifest (first setting found).- */- public static readonly GET_SETTING_ANY_MANIFEST = `- SELECT s.Value- FROM Settings s- WHERE s.Key = ?- LIMIT 1`;-/*** Check if a setting exists within its manifest.*/diff --git a/apps/browser-extension/src/utils/db/repositories/SettingsRepository.ts b/apps/browser-extension/src/utils/db/repositories/SettingsRepository.tsindex c7f863a8c..d58feefa4 100644--- a/apps/browser-extension/src/utils/db/repositories/SettingsRepository.ts+++ b/apps/browser-extension/src/utils/db/repositories/SettingsRepository.ts@@ -15,17 +15,18 @@ export type CredentialSortOrder = 'OldestFirst' | 'NewestFirst' | 'Alphabetical'*/export class SettingsRepository extends BaseRepository {/**- * Get setting from database for a given key.- * Returns default value (empty string by default) if setting is not found.+ * Get setting from database for a given key, from the manifest this client writes into.* @param key - The setting key* @param defaultValue - Default value if setting not found* @returns The setting value*/public getSetting(key: string, defaultValue: string = ''): string {const manifestId = this.client.getActiveManifestId() ?? this.personalManifestId();- const results = manifestId- ? this.client.executeQuery<{ Value: string }>(SettingsQueries.GET_SETTING, [manifestId, key])- : this.client.executeQuery<{ Value: string }>(SettingsQueries.GET_SETTING_ANY_MANIFEST, [key]);+ if (!manifestId) {+ return defaultValue;+ }++ const results = this.client.executeQuery<{ Value: string }>(SettingsQueries.GET_SETTING, [manifestId, key]);return results.length > 0 ? results[0].Value : defaultValue;}diff --git a/apps/browser-extension/src/utils/legacy/LegacyStorageModelMigration.ts b/apps/browser-extension/src/utils/legacy/LegacyStorageModelMigration.tsindex 6a0b8a202..15b3778dc 100644--- a/apps/browser-extension/src/utils/legacy/LegacyStorageModelMigration.ts+++ b/apps/browser-extension/src/utils/legacy/LegacyStorageModelMigration.ts@@ -9,6 +9,7 @@ import { storage } from 'wxt/utils/storage';import { StorageKeys } from '@/utils/constants/storageKeys';import { devLog } from '@/utils/devLogger/DevLogger';import { EncryptionUtility } from '@/utils/EncryptionUtility';+import { replaceManifestRevisions } from '@/utils/ManifestRevisions';import { ServerUpdateRequiredError } from '@/utils/types/errors/ServerUpdateRequiredError';import { VaultKeyService } from '@/utils/VaultKeyService';@@ -92,12 +93,17 @@ export async function openLegacySqliteBlobSnapshot(snapshot: LegacySqliteBlobSna// There is no manifest-v1 server state behind this snapshot, so any content fingerprint we hold is stale.await storage.removeItem(StorageKeys.VAULT_CONTENT_FINGERPRINTS);+ const revision = typeof snapshot.legacyRevision === 'number' ? snapshot.legacyRevision : 0;+/** The vault has never been materialized, so it carries no record of which manifest is ours. Persist the- * server's id for it so the first migration canonicalize can stamp rows with it.+ * server's id for it so the first migration canonicalize can stamp rows with it, and seed the revision map+ * with it: a legacy vault is one logical manifest, and every pull path (this one included) must leave the+ * per-manifest baselines matching what it fetched.*/if (snapshot.personalManifestId) {await storage.setItem(StorageKeys.VAULT_PERSONAL_MANIFEST_ID, snapshot.personalManifestId);+ await replaceManifestRevisions({ [snapshot.personalManifestId]: revision });}devLog('[V2Pull] Legacy sqlite-blob pass-through (user not yet migrated), returning the blob as-is.');@@ -105,7 +111,7 @@ export async function openLegacySqliteBlobSnapshot(snapshot: LegacySqliteBlobSnareturn {encryptedBlob: snapshot.legacyVaultBlob ?? '',version: snapshot.version ?? '',- revision: typeof snapshot.legacyRevision === 'number' ? snapshot.legacyRevision : 0,+ revision,};}diff --git a/apps/browser-extension/src/utils/messaging/ExtensionMessaging.ts b/apps/browser-extension/src/utils/messaging/ExtensionMessaging.tsindex 575e2ef47..a352314e7 100644--- a/apps/browser-extension/src/utils/messaging/ExtensionMessaging.ts+++ b/apps/browser-extension/src/utils/messaging/ExtensionMessaging.ts@@ -78,7 +78,7 @@ export interface IExtensionMessageProtocol {GET_WEBAUTHN_SETTINGS(data: any): WebAuthnSettingsResponse;IS_URL_LINKED_TO_CREDENTIAL(data: { itemId: string; url: string }): { linked: boolean };LOCK_VAULT(): BoolResponse;- MARK_VAULT_CLEAN(data: { mutationSeqAtStart: number; newPersonalManifestRevision: number }): { cleared: boolean; currentMutationSeq: number };+ MARK_VAULT_CLEAN(data: { mutationSeqAtStart: number }): { cleared: boolean; currentMutationSeq: number };MIGRATE_VAULT_MANIFEST(): VaultManifestMigrationResult;OPEN_AUTOFILL_POPUP(data: { elementIdentifier: string; popupType?: string }): BoolResponse;OPEN_POPUP(): BoolResponse;@@ -95,7 +95,7 @@ export interface IExtensionMessageProtocol {SET_CLIPBOARD_CLEAR_TIMEOUT(data: number): boolean;SET_LOGIN_SAVE_ENABLED(data: boolean): BoolResponse;SET_RECENTLY_SELECTED(data: { itemId: string; domain: string }): { success: boolean };- STORE_ENCRYPTED_VAULT(data: { vaultBlob: string; markDirty?: boolean; personalManifestRevision?: number; expectedMutationSeq?: number; scope?: VaultMutationScope }): { success: boolean; mutationSequence: number };+ STORE_ENCRYPTED_VAULT(data: { vaultBlob: string; markDirty?: boolean; expectedMutationSeq?: number; scope?: VaultMutationScope }): { success: boolean; mutationSequence: number };STORE_ENCRYPTION_KEY(data: string): BoolResponse;STORE_ENCRYPTION_KEY_DERIVATION_PARAMS(data: EncryptionKeyDerivationParams): BoolResponse;STORE_LAST_AUTOFILLED(data: LastAutofilledCredential): { success: boolean };diff --git a/apps/browser-extension/src/utils/types/messaging/VaultUploadResponse.ts b/apps/browser-extension/src/utils/types/messaging/VaultUploadResponse.tsindex 831d36426..19692f6a1 100644--- a/apps/browser-extension/src/utils/types/messaging/VaultUploadResponse.ts+++ b/apps/browser-extension/src/utils/types/messaging/VaultUploadResponse.ts@@ -2,7 +2,6 @@ export type VaultUploadResponse = {success: boolean,error?: string,status?: number,- newRevisionNumber?: number,/** Mutation sequence at the start of upload, for race detection */mutationSeqAtStart?: number,/** Whether expired trash items were pruned from the vault during upload */