Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Move shared manifest session storage to local storage with encrypted wrapping (#2159)
Leendert de Borst28c4a2c6a490
diff --git a/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts b/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.tsindex a3b2edb92..fa0dc1301 100644--- a/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts+++ b/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts@@ -1629,12 +1629,11 @@ async function vaultHoldsUnwritableManifests(): Promise<boolean> {}/**- * Whether the stored vault holds rows of shared manifests this session has no key record for, which a pull has to repair.- * A manifest still unrecorded after its pull (the server no longer serves it) is not pulled for again.+ * Whether the stored vault holds rows of shared manifests this account has no key record for, which a pull has to repair.*/async function vaultHoldsUnrecordedSharedManifests(): Promise<boolean> {try {- const recorded = new Set(Object.values(await SharingService.getSessionSharedManifests()).map(record => record.manifestId.toLowerCase()));+ const recorded = new Set(Object.values(await SharingService.getSharedManifestRecords()).map(record => record.manifestId.toLowerCase()));for (const manifestId of recorded) {manifestsPulledForSharedKeys.delete(manifestId);}@@ -1723,7 +1722,7 @@ async function announceSyncPhase(needsPull: boolean, isDirty: boolean): Promise<*/async function applyServerDirectedChanges(webApi: WebApiService, statusResponse: StatusResponseV2, syncState: VaultSyncState, encryptionKey: string, needsPull: boolean): Promise<boolean> {const pendingActions = PendingActionProcessor.pendingActions(statusResponse);- const sharedManifestCount = Object.keys(await SharingService.getSessionSharedManifests()).length;+ const sharedManifestCount = Object.keys(await SharingService.getSharedManifestRecords()).length;if (pendingActions.length === 0 && sharedManifestCount === 0) {return false;}@@ -2618,7 +2617,7 @@ export async function handleGroupCreateVault(message: { groupId: string; name: sselfPublicKey,}, crypto.randomUUID().toUpperCase());- await SharingService.addSessionSharedManifest({+ await SharingService.addSharedManifestRecord({manifestId: mapping.manifestId,encryptedVek: mapping.encryptedVek,encryptionPublicKey: mapping.encryptionPublicKey,@@ -2626,7 +2625,7 @@ export async function handleGroupCreateVault(message: { groupId: string; name: ssalt: mapping.salt,name,canAdminister: true,- });+ }, encryptionKey);await recordManifestRevisions({ [mapping.manifestId]: mapping.revision });await createAnchorFolder(sqliteClient, mapping.manifestId, name);@@ -2697,16 +2696,17 @@ export async function handleGroupInviteMember(message: { groupId: string; manifereturn { success: false, apiErrorCode: 'INVITE_RECIPIENT_NOT_READY' };}- // Find the session's own grant on the vault: a key that was never handed to this account cannot be passed on.- let record = await SharingService.getSessionSharedManifest(manifest.manifestId);+ // Find this account's own grant on the vault: a key that was never handed to this account cannot be passed on.+ let record = await SharingService.getSharedManifestRecord(manifest.manifestId);if (!record) {- devWarn(`[Sharing] No key record for vault ${manifest.manifestId} in this session; pulling to re-record it before inviting.`);+ // Backstop: the records persist next to the vault, so a missing one means a desync only a pull repairs.+ devWarn(`[Sharing] No key record stored for vault ${manifest.manifestId}; pulling to re-record it before inviting.`);await handleFullVaultSync({ forcePull: true });- record = await SharingService.getSessionSharedManifest(manifest.manifestId);+ record = await SharingService.getSharedManifestRecord(manifest.manifestId);}if (!record) {- return failed('this session holds no key for the vault');+ return failed('this account holds no key for the vault');}const sqliteClient = await createVaultSqliteClient();diff --git a/apps/browser-extension/src/entrypoints/popup/pages/settings/FamilySharingSettings.tsx b/apps/browser-extension/src/entrypoints/popup/pages/settings/FamilySharingSettings.tsxindex fc4990c6c..582dfbd9b 100644--- a/apps/browser-extension/src/entrypoints/popup/pages/settings/FamilySharingSettings.tsx+++ b/apps/browser-extension/src/entrypoints/popup/pages/settings/FamilySharingSettings.tsx@@ -60,7 +60,7 @@ const FamilySharingSettings: React.FC = () => {const loaded = await SharingService.getOverview(webApi);setOverview(loaded);setInvitationNames(sqliteClient ? await SharingService.openInvitationNames(sqliteClient, loaded.receivedInvitations) : {});- const records = await SharingService.getSessionSharedManifests();+ const records = await SharingService.getSharedManifestRecords();const names = Object.fromEntries(Object.values(records).filter(record => record.name).map(record => [record.manifestId.toLowerCase(), record.name as string]));setVaultNames({ ...names, ...(sqliteClient ? anchorFolderNames(sqliteClient) : {}) });setError(null);diff --git a/apps/browser-extension/src/utils/PendingActionProcessor.ts b/apps/browser-extension/src/utils/PendingActionProcessor.tsindex a83507b0b..71edfbaa9 100644--- a/apps/browser-extension/src/utils/PendingActionProcessor.ts+++ b/apps/browser-extension/src/utils/PendingActionProcessor.ts@@ -89,7 +89,7 @@ export class PendingActionProcessor {* @param sqliteClient - the open local vault.*/private static async rotateDeliveryKey(action: PendingClientAction, sqliteClient: SqliteClient): Promise<ActionOutcome> {- const record = Object.values(await SharingService.getSessionSharedManifests()).find(candidate => idsEqual(candidate.manifestId, action.manifestId));+ const record = Object.values(await SharingService.getSharedManifestRecords()).find(candidate => idsEqual(candidate.manifestId, action.manifestId));if (!record?.canAdminister) {devLog(`[PendingActions] Vault ${action.manifestId} is not open to this session as an administrator; leaving its delivery key rotation for another sync.`);diff --git a/apps/browser-extension/src/utils/SharingService.ts b/apps/browser-extension/src/utils/SharingService.tsindex 953fb2bf8..d92a5ab88 100644--- a/apps/browser-extension/src/utils/SharingService.ts+++ b/apps/browser-extension/src/utils/SharingService.ts@@ -5,6 +5,7 @@ import { StorageKeys } from '@/utils/constants/storageKeys';import { devWarn } from '@/utils/devLogger/DevLogger';import { VaultKeyAlgorithm, type CreateSharedManifestRequest, type CreateSharedManifestResponse, type DeleteSharedManifestInitiateResponse, type DeleteSharedManifestRequest, type GrantManifestAccessRequest, type GrantManifestAccessResponse, type GroupMemberInfo, type GroupOverviewResponse, type ManifestGrant, type ReceivedManifestInvitation, type VaultKeyAlgorithmValue } from '@/utils/dist/core/models/webapi';import { EncryptionUtility } from '@/utils/EncryptionUtility';+import { readLegacySessionEncryptionKey } from '@/utils/legacy/LegacyStorageKeyFallbacks';import { vaultCodecGenerateManifestSalt } from '@/utils/RustCore';import type { SqliteClient } from '@/utils/SqliteClient';import { VaultKeyService } from '@/utils/VaultKeyService';@@ -40,12 +41,12 @@ export type ManifestVekGrant = {export type SharedManifestMapping = ManifestVekGrant & { manifestId: string; salt: string; revision: number };/**- * Session record of a shared manifest resolved during the last pull. Rebuilt from the server grant on every pull.+ * Key record of a shared manifest, resolved during the last pull (or a share create) and rebuilt from the server+ * grant on every pull.*/-export type SessionSharedManifest = ManifestVekGrant & {+export type SharedManifestRecord = ManifestVekGrant & {manifestId: string;salt: string;- /** Read out of the encrypted manifest on pull; null until its first revision has been pushed. */name?: string | null;canAdminister?: boolean;};@@ -227,7 +228,7 @@ export class SharingService {public static async ensureAnchorFolders(sqliteClient: SqliteClient): Promise<boolean> {let mutated = false;- for (const record of Object.values(await this.getSessionSharedManifests())) {+ for (const record of Object.values(await this.getSharedManifestRecords())) {if (!record.canAdminister) {// A member waiting for the administrator's first push has nothing to anchor yet.continue;@@ -257,37 +258,56 @@ export class SharingService {}/**- * The shared manifests resolved during the last pull (see {@link SessionSharedManifest}), keyed by manifest id.- * Both manifests the user administers and manifests shared with them arrive embedded in GET /v2/Vault; the pull- * flow decrypts their VEKs with the private key and records them here for the push path.+ * The shared-manifest key records (see {@link SharedManifestRecord}), keyed by manifest id.*/- public static async getSessionSharedManifests(): Promise<Record<string, SessionSharedManifest>> {- return ((await storage.getItem(StorageKeys.SHARED_MANIFESTS)) as Record<string, SessionSharedManifest> | null) ?? {};+ public static async getSharedManifestRecords(): Promise<Record<string, SharedManifestRecord>> {+ const ciphertext = (await storage.getItem(StorageKeys.SHARED_MANIFESTS)) as string | null;+ const encryptionKey = ciphertext ? await this.sessionEncryptionKey() : null;+ if (!ciphertext || !encryptionKey) {+ return {};+ }++ try {+ return JSON.parse(await EncryptionUtility.symmetricDecrypt(ciphertext, encryptionKey)) as Record<string, SharedManifestRecord>;+ } catch (error) {+ devWarn('[Sharing] The stored shared-manifest key records did not decrypt (re-keyed vault?); treating them as absent.', error);+ return {};+ }}/**- * The session record of one shared manifest.+ * The key record of one shared manifest.* @param manifestId - the manifest to look up.*/- public static async getSessionSharedManifest(manifestId: string): Promise<SessionSharedManifest | null> {- return Object.values(await this.getSessionSharedManifests()).find(record => record.manifestId.toLowerCase() === manifestId.toLowerCase()) ?? null;+ public static async getSharedManifestRecord(manifestId: string): Promise<SharedManifestRecord | null> {+ return Object.values(await this.getSharedManifestRecords()).find(record => record.manifestId.toLowerCase() === manifestId.toLowerCase()) ?? null;}/**- * Persist the session shared-manifest records (written by the pull flow and share create, read by push and the UI).+ * Persist the shared-manifest key records.+ * @param manifests - the full record map; replaces whatever is stored.+ * @param encryptionKey - the vault encryption key the map is sealed with, passed explicitly because both writers already hold it.*/- public static async setSessionSharedManifests(manifests: Record<string, SessionSharedManifest>): Promise<void> {- await storage.setItem(StorageKeys.SHARED_MANIFESTS, manifests);+ public static async setSharedManifestRecords(manifests: Record<string, SharedManifestRecord>, encryptionKey: string): Promise<void> {+ await storage.setItem(StorageKeys.SHARED_MANIFESTS, await EncryptionUtility.symmetricEncrypt(JSON.stringify(manifests), encryptionKey));}/**- * Add (or replace) a session shared-manifest record, used after creating a share so the very next push already- * carries the rows stamped for it, before any pull has run.+ * Add (or replace) one shared-manifest key record.+ * @param record - the record to add.+ * @param encryptionKey - the vault encryption key the map is sealed with.*/- public static async addSessionSharedManifest(record: SessionSharedManifest): Promise<void> {- const manifests = await this.getSessionSharedManifests();+ public static async addSharedManifestRecord(record: SharedManifestRecord, encryptionKey: string): Promise<void> {+ const manifests = await this.getSharedManifestRecords();manifests[record.manifestId] = record;- await this.setSessionSharedManifests(manifests);+ await this.setSharedManifestRecords(manifests, encryptionKey);+ }++ /**+ * The session vault encryption key, or null while the vault is locked.+ */+ private static async sessionEncryptionKey(): Promise<string | null> {+ return ((await storage.getItem(StorageKeys.ENCRYPTION_KEY)) as string | null) ?? await readLegacySessionEncryptionKey();}/**@@ -303,7 +323,7 @@ export class SharingService {* @param sqliteClient - the open local vault, the durable home of the account's superseded private keys.* @param record - the manifest to open.*/- public static async openSharedManifestVek(sqliteClient: SqliteClient, record: SessionSharedManifest): Promise<string | null> {+ public static async openSharedManifestVek(sqliteClient: SqliteClient, record: SharedManifestRecord): Promise<string | null> {if (record.algorithm !== VaultKeyAlgorithm.RsaOaepSha256) {devWarn(`[Sharing] Vault ${record.manifestId} grants its key under an unsupported algorithm "${record.algorithm}" (newer server?); leaving it closed.`);return null;@@ -329,7 +349,7 @@ export class SharingService {*/public static async openSharedManifestVeks(sqliteClient: SqliteClient): Promise<Map<string, string>> {const veks = new Map<string, string>();- for (const record of Object.values(await this.getSessionSharedManifests())) {+ for (const record of Object.values(await this.getSharedManifestRecords())) {const vek = await this.openSharedManifestVek(sqliteClient, record);if (vek) {veks.set(record.manifestId, vek);diff --git a/apps/browser-extension/src/utils/VaultSyncService.ts b/apps/browser-extension/src/utils/VaultSyncService.tsindex bbc7029d4..3e24d546d 100644--- a/apps/browser-extension/src/utils/VaultSyncService.ts+++ b/apps/browser-extension/src/utils/VaultSyncService.ts@@ -16,7 +16,7 @@ import { EncryptionUtility } from '@/utils/EncryptionUtility';import { completeLegacyAccountKeyMigration, isLegacySqliteBlobSnapshot, legacyUnstampedRowAdoption, openLegacySqliteBlobSnapshot, prepareLegacyAccountKeyMigration, withOutdatedServerGuard, type LegacyAccountKeyMigration, type LegacySqliteBlobSnapshot } from '@/utils/legacy/LegacyStorageModelMigration';import { getManifestRevisions, getPersonalManifestId, recordManifestRevisions, replaceManifestRevisions, toManifestRevisionMap } 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 { anchorFolderNames, SharingService, type ManifestVekGrant, type SessionSharedManifest } from '@/utils/SharingService';+import { anchorFolderNames, SharingService, type ManifestVekGrant, type SharedManifestRecord } from '@/utils/SharingService';import { SqliteClient } from '@/utils/SqliteClient';import { getStorageItem } from '@/utils/StorageUtility';import { VaultProcessingError } from '@/utils/types/errors/VaultProcessingError';@@ -417,7 +417,7 @@ export class VaultSyncService {*/const accountKeyVeks = new Map<string, string>([[personalDto.manifestId, vek]]);const resolved: ResolvedManifest[] = [];- const sessionSharedManifests: Record<string, SessionSharedManifest> = {};+ const sharedManifestRecords: Record<string, SharedManifestRecord> = {};const contentlessRevisions: Record<string, number> = {};for (const dto of [personalDto, ...(snapshot.manifests ?? []).filter(m => m.manifestId !== personalDto.manifestId)]) {const isPersonal = dto.manifestId === personalDto.manifestId;@@ -428,7 +428,7 @@ export class VaultSyncService {}const contentlessGrant = manifestKey ? grantOf(dto) : null;if (contentlessGrant) {- sessionSharedManifests[dto.manifestId] = {+ sharedManifestRecords[dto.manifestId] = {manifestId: dto.manifestId,...contentlessGrant,salt: await vaultCodecGenerateManifestSalt(),@@ -472,7 +472,7 @@ export class VaultSyncService {continue;}- sessionSharedManifests[entry.manifestId] = {+ sharedManifestRecords[entry.manifestId] = {manifestId: entry.manifestId,...grant,salt: entry.manifest.manifestSalt,@@ -480,7 +480,7 @@ export class VaultSyncService {canAdminister: dto.canAdminister ?? false,};}- await SharingService.setSessionSharedManifests(sessionSharedManifests);+ await SharingService.setSharedManifestRecords(sharedManifestRecords, vek);const personal = resolved[0];@@ -1363,7 +1363,7 @@ export class VaultSyncService {const stampedManifestIds = VaultCodec.manifestIdsInVault(sqliteClient);const sharedVeks = await SharingService.openSharedManifestVeks(sqliteClient);const folderNames = anchorFolderNames(sqliteClient);- for (const record of Object.values(await SharingService.getSessionSharedManifests())) {+ for (const record of Object.values(await SharingService.getSharedManifestRecords())) {if (!stampedManifestIds.has(record.manifestId)) {devLog(`[V2Push] Shared manifest ${record.manifestId} has no rows in this vault; leaving it out of the write rather than emptying it server-side.`);continue;