Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Update sharing logic (#2159)
Leendert de Borst29d6791a8fad
diff --git a/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts b/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.tsindex 4d0d1dfc7..8eff2ad88 100644--- a/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts+++ b/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts@@ -739,12 +739,16 @@ async function uploadDirtyBucketsOnly(sqliteClient: SqliteClient, scopes: VaultMreturn (await uploadNewVaultToServer(sqliteClient, { forceFullWrite: false, createVaultKey: false })).response;}+ // Same rule as the full push: a vault holding manifests this session cannot write is re-synced, not written to.+ if (await vaultHoldsUnwritableManifests()) {+ return { status: 2 };+ }+/** Every manifest this vault can write. Each gets a bucket back, empty where it holds no rows of the- * category, so deleting a manifest's last row still reaches the server; a row stamped for anything else- * (a manifest whose key this session never opened) is dropped and left to the next full push.+ * category, so deleting a manifest's last row still reaches the server.*/- const writeKeys = await vaultSyncService.resolveBucketWriteKeys(encryptionKey);+ const writeKeys = await vaultSyncService.resolveBucketWriteKeys(sqliteClient, encryptionKey);const overflowTable = await vaultCodecOverflowTable();for (const category of new Set(scopes)) {@@ -1541,7 +1545,7 @@ async function runSyncPreflight(webApi: WebApiService): Promise<SyncPreflightRes// Get current sync stateconst syncState = await handleGetSyncState();- const needsPull = serverManifestsNeedPull(statusResponse.manifestRevisions, await getManifestRevisions());+ let needsPull = serverManifestsNeedPull(statusResponse.manifestRevisions, await getManifestRevisions());devLog(`[VaultSync] Status received (needsPull ${needsPull}, isDirty ${syncState.isDirty})`);@@ -1581,9 +1585,39 @@ async function runSyncPreflight(webApi: WebApiService): Promise<SyncPreflightRessyncState.isDirty = false;}+ /*+ * The shared-vault keys this session holds are session state; the stored vault is not, so the two can drift+ * apart on their own — a browser restart clears the keys while the vault keeps its shared rows. Pushing in that+ * state would drop those rows from the write instead of failing it, so pull first: the pull is what re-records+ * the keys. Only checked when there is something to push, since that is the only case it protects.+ */+ if (syncState.isDirty && !needsPull && await vaultHoldsUnwritableManifests()) {+ needsPull = true;+ }+return { proceed: true, statusResponse, syncState, needsPull };}+/**+ * Whether the stored vault holds rows for manifests this session cannot write, which a pull has to repair before+ * anything may be pushed (see {@link VaultSyncService.findUnwritableManifests}). A vault that cannot even be+ * opened answers false: it has its own failure path, and forcing a pull here would only mask it.+ */+async function vaultHoldsUnwritableManifests(): Promise<boolean> {+ try {+ const unwritable = await vaultSyncService.findUnwritableManifests(await createVaultSqliteClient());+ if (unwritable.length === 0) {+ return false;+ }++ devWarn(`[VaultSync] Vault holds rows for manifest(s) this session cannot write (${unwritable.join(', ')}); pulling before the push.`);+ return true;+ } catch (error) {+ devWarn('[VaultSync] Could not check which manifests this session can write; leaving the pull decision to the revisions.', error);+ return false;+ }+}+/*** Fall back to offline mode when the server cannot be reached. Without a local vault there is nothing to fall* back on, so that case reports a failure instead.@@ -2545,7 +2579,9 @@ export async function handleGroupCreateVault(message: { groupId: string }): Promawait SharingService.addSessionSharedManifest({manifestId: mapping.manifestId,- vek: mapping.vek,+ encryptedVek: mapping.encryptedVek,+ encryptionPublicKey: mapping.encryptionPublicKey,+ algorithm: mapping.algorithm,salt: mapping.salt,name: group.name,canAdminister: true,@@ -2606,8 +2642,13 @@ export async function handleGroupInviteMember(message: { groupId: string; usernareturn { success: false, error: await t('sharing.family.errors.inviteFailed') };}+ const manifestVek = await SharingService.openSharedManifestVek(await createVaultSqliteClient(), record);+ if (!manifestVek) {+ return { success: false, error: await t('sharing.family.errors.inviteFailed') };+ }+const recipient = await SharingService.resolveInvitationRecipient(webApi, group.groupId, message.username);- const grant = await SharingService.encryptVekFor(record.vek, recipient);+ const grant = await SharingService.encryptVekFor(manifestVek, recipient);await SharingService.invite(webApi, group.groupId, recipient.userId, grant, VaultKeyAlgorithm.RsaOaepSha256);devLog(`[Sharing] Invited ${recipient.userId} to group ${group.groupId} with the key to vault ${record.manifestId} encrypted for them.`);diff --git a/apps/browser-extension/src/utils/SharingService.ts b/apps/browser-extension/src/utils/SharingService.tsindex b8908737c..c9a58eb3b 100644--- a/apps/browser-extension/src/utils/SharingService.ts+++ b/apps/browser-extension/src/utils/SharingService.ts@@ -27,7 +27,11 @@ export type ShareTarget = {/*** The client-side mapping of a shared manifest: the manifest id plus its VEK, blob-hashing salt and current revision.*/-export type SharedManifestMapping = { manifestId: string; vek: string; salt: string; revision: number };+export type ManifestVekGrant = {+ encryptedVek: string;+ encryptionPublicKey: string;+ algorithm: string;+};/*** Session record of a shared manifest resolved during the last pull. Kept in session storage (cleared on lock)diff --git a/apps/browser-extension/src/utils/VaultCodec.ts b/apps/browser-extension/src/utils/VaultCodec.tsindex e8ceb2125..f6d5b1620 100644--- a/apps/browser-extension/src/utils/VaultCodec.ts+++ b/apps/browser-extension/src/utils/VaultCodec.ts@@ -242,6 +242,27 @@ export class VaultCodec {}/**+ * Every manifest id this vault holds a row for.+ * @param sqliteClient - client wrapping the open SQLite DB+ */+ public static manifestIdsInVault(sqliteClient: SqliteClient): Set<string> {+ const stampedTables = this.listUserTables(sqliteClient).filter(+ name => sqliteClient.executeQuery<{ name: string }>(`SELECT name FROM pragma_table_info("${name}") WHERE name = 'ManifestId'`).length > 0+ );++ const ids = new Set<string>();+ for (const name of stampedTables) {+ for (const row of sqliteClient.executeQuery<{ ManifestId: string | null }>(`SELECT DISTINCT ManifestId FROM "${name}" WHERE ManifestId IS NOT NULL AND ManifestId != ''`)) {+ if (row.ManifestId) {+ ids.add(row.ManifestId);+ }+ }+ }+ return ids;+ }++ /**+ * Normalize a SQLite-returned row into a JSON-safe shape /*** Normalize a SQLite-returned row into a JSON-safe shape — Uint8Array bytes become `{ __b64: ... }`.* @param row - raw row from sql.js*/diff --git a/apps/browser-extension/src/utils/VaultSyncService.ts b/apps/browser-extension/src/utils/VaultSyncService.tsindex 92701f616..3a56d0ea8 100644--- a/apps/browser-extension/src/utils/VaultSyncService.ts+++ b/apps/browser-extension/src/utils/VaultSyncService.ts@@ -16,11 +16,11 @@ 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 { SharingService, type SessionSharedManifest } from '@/utils/SharingService';+import { SharingService, type ManifestVekGrant, type SessionSharedManifest } from '@/utils/SharingService';import { SqliteClient } from '@/utils/SqliteClient';import { getStorageItem } from '@/utils/StorageUtility';import { VaultProcessingError } from '@/utils/types/errors/VaultProcessingError';-import { type VaultManifest, type VaultDataBucket, type TableData, VaultCodec } from '@/utils/VaultCodec';+import { type VaultManifest, type VaultDataBucket, VaultCodec } from '@/utils/VaultCodec';import { VaultKeyService } from '@/utils/VaultKeyService';import { WebApiService } from '@/utils/WebApiService';@@ -87,23 +87,15 @@ async function verifyDecryptUnpack(base64Ciphertext: string, vek: string, expect}/**- * Every manifest id the vault holds a row for. Read off the whole table set the codec is about to be handed rather- * than off a chosen few tables: any stamped row of any table is a row that has to travel with its manifest, so a- * narrower check would silently drop the ones it did not look at. Ids are compared exactly, the way the codec- * routes them, so "present here" means the same thing as "rows will land there".- * @param tables - the vault's tables as read for this canonicalize+ * The grant a shared manifest is remembered by. Null when the snapshot carries no grant.+ * @param dto - the snapshot manifest*/-function manifestIdsPresentIn(tables: TableData[]): Set<string> {- const present = new Set<string>();- for (const table of tables) {- for (const record of table.records) {- const manifestId = record.ManifestId;- if (typeof manifestId === 'string' && manifestId.length > 0) {- present.add(manifestId);- }- }+function grantOf(dto: ManifestDto): ManifestVekGrant | null {+ if (!dto.encryptedVek || !dto.encryptionPublicKey) {+ return null;}- return present;++ return { encryptedVek: dto.encryptedVek, encryptionPublicKey: dto.encryptionPublicKey, algorithm: dto.algorithm ?? VaultKeyAlgorithm.RsaOaepSha256 };}/**@@ -436,10 +428,11 @@ export class VaultSyncService {if (dto.manifestId === personalDto.manifestId) {throw new Error(`VaultSyncService: no key resolved for the home manifest ${dto.manifestId} (key type "${dto.keyType ?? 'unspecified'}"), refusing to assemble.`);}- if (manifestKey) {+ const contentlessGrant = manifestKey ? grantOf(dto) : null;+ if (contentlessGrant) {sessionSharedManifests[dto.manifestId] = {manifestId: dto.manifestId,- vek: manifestKey,+ ...contentlessGrant,salt: await vaultCodecGenerateManifestSalt(),name: dto.name ?? null,canAdminister: dto.canAdminister ?? false,@@ -475,9 +468,15 @@ export class VaultSyncService {continue;}+ const grant = grantOf(dto);+ if (!grant) {+ devWarn(`[V2Pull] Shared manifest ${entry.manifestId} carries no grant this account can re-open; leaving it out of this session's writable set.`);+ continue;+ }+sessionSharedManifests[entry.manifestId] = {manifestId: entry.manifestId,- vek: manifestKey,+ ...grant,salt: entry.manifest.manifestSalt,name: entry.manifest.name ?? dto.name ?? null,canAdminister: dto.canAdminister ?? false,@@ -822,6 +821,17 @@ export class VaultSyncService {username: string,options?: { createVaultKey?: boolean; forceFullWrite?: boolean }): Promise<PushResult> {+ /*+ * Nothing is written while the vault holds manifests this session cannot write: those rows would be dropped+ * from the write rather than fail it. Reported as outdated, which is the status that sends the caller back+ * through a sync — and the sync pulls, which is what puts the grants and the vault back in step.+ */+ const unwritable = await this.findUnwritableManifests(sqliteClient);+ if (unwritable.length > 0) {+ devWarn(`[V2Push] Vault holds rows for manifest(s) this session cannot write (${unwritable.join(', ')}); refusing the write until a pull restores them.`);+ return { status: 'outdated', reasons: [`Manifest(s) ${unwritable.join(', ')} are not open to this session`] };+ }+const migration: LegacyAccountKeyMigration | null = options?.createVaultKey === true ? await prepareLegacyAccountKeyMigration(vek) : null;const contentKey = migration?.contentKey ?? vek;if (migration) {@@ -1171,7 +1181,7 @@ export class VaultSyncService {// Read tables from the SQLite database and apply the manifest-v1 format rules.const tables = VaultCodec.readTables(sqliteClient);- const manifestRecords = await this.resolveManifestRecords(tables);+ const manifestRecords = await this.resolveManifestRecords(sqliteClient);/** Membership is the ManifestId stamp on each row (written by the repositories at insert, move and share time),* so a spec only has to name the manifest and supply its blob salt. The personal manifest goes first: the codec reads the@@ -1302,25 +1312,41 @@ export class VaultSyncService {/*** The key each manifest's data buckets are encrypted with: the personal VEK for the personal manifest,- * and its own folder VEK for every shared manifest this session can open.+ * and its own VEK for every shared manifest whose grant this session can unwrap.+ * @param sqliteClient - the open local vault, which holds the private keys the grants are unwrapped with* @param personalVek - the personal manifest's symmetric key* @returns manifest id → the key its buckets are written under*/- public async resolveBucketWriteKeys(personalVek: string): Promise<Map<string, string>> {+ public async resolveBucketWriteKeys(sqliteClient: SqliteClient, personalVek: string): Promise<Map<string, string>> {const keys = new Map<string, string>([[await this.resolvePersonalManifestId(), personalVek]]);- for (const record of Object.values(await SharingService.getSessionSharedManifests())) {- if (record.vek) {- keys.set(record.manifestId, record.vek);- }+ for (const [manifestId, vek] of await SharingService.openSharedManifestVeks(sqliteClient)) {+ keys.set(manifestId, vek);}return keys;}+ /**+ * The manifests the local vault holds rows for that this session cannot write: no grant recorded for them, or+ * one that will not unwrap.+ * @param sqliteClient - the open local vault+ * @returns The manifest ids the vault holds but this session cannot write, empty when the two agree+ */+ public async findUnwritableManifests(sqliteClient: SqliteClient): Promise<string[]> {+ const writable = new Set<string>((await SharingService.openSharedManifestVeks(sqliteClient)).keys());++ const personalManifestId = await getPersonalManifestId();+ if (personalManifestId) {+ writable.add(personalManifestId);+ }++ return [...VaultCodec.manifestIdsInVault(sqliteClient)].filter(manifestId => !writable.has(manifestId));+ }+/*** Resolve every manifest this vault can write, personal manifest first, as one uniform list.- * @param tables - the vault's tables as read for this canonicalize+ * @param sqliteClient - the open local vault*/- private async resolveManifestRecords(tables: TableData[]): Promise<ManifestRecord[]> {+ private async resolveManifestRecords(sqliteClient: SqliteClient): Promise<ManifestRecord[]> {let manifestSalt = (await storage.getItem(StorageKeys.VAULT_MANIFEST_SALT)) as string | null;if (!manifestSalt) {manifestSalt = await vaultCodecGenerateManifestSalt();@@ -1336,17 +1362,26 @@ export class VaultSyncService {canAdminister: false,}];- const stampedManifestIds = manifestIdsPresentIn(tables);+ const stampedManifestIds = VaultCodec.manifestIdsInVault(sqliteClient);+ const sharedVeks = await SharingService.openSharedManifestVeks(sqliteClient);for (const record of Object.values(await SharingService.getSessionSharedManifests())) {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;}++ // No key, no write.+ const vek = sharedVeks.get(record.manifestId);+ if (!vek) {+ devWarn(`[V2Push] Shared manifest ${record.manifestId} did not open; leaving it out of the write.`);+ continue;+ }+records.push({manifestId: record.manifestId,isPersonal: false,salt: record.salt,- vek: record.vek,+ vek,name: record.name ?? null,canAdminister: record.canAdminister === true,});diff --git a/apps/browser-extension/src/utils/__tests__/ManifestIdsInVault.test.ts b/apps/browser-extension/src/utils/__tests__/ManifestIdsInVault.test.tsnew file mode 100644index 000000000..7356a0d92--- /dev/null+++ b/apps/browser-extension/src/utils/__tests__/ManifestIdsInVault.test.ts@@ -0,0 +1,89 @@+import initSqlJs from 'sql.js';+import { describe, expect, it } from 'vitest';++import { VaultCodec } from '../VaultCodec';++import type SqliteClient from '../SqliteClient';+import type { Database } from 'sql.js';++const PERSONAL = 'PERSONAL-MANIFEST';+const SHARED = 'SHARED-MANIFEST';++/**+ * Wrap an in-memory database as the sliver of {@link SqliteClient} the codec reads through.+ * @param db - the database to read+ * @returns A client that can answer `executeQuery`+ */+function asClient(db: Database): SqliteClient {+ return {+ /**+ * Run a statement and return its rows as plain objects.+ * @param query - the statement+ * @param params - bound parameters+ * @returns The rows+ */+ executeQuery: <T>(query: string, params: unknown[] = []): T[] => {+ const stmt = db.prepare(query);+ stmt.bind(params as never);+ const out: T[] = [];+ while (stmt.step()) {+ out.push(stmt.getAsObject() as T);+ }+ stmt.free();+ return out;+ },+ } as unknown as SqliteClient;+}++/**+ * Build a vault holding a stamped table, an unstamped one, and a table this check must discover on its own.+ * @returns The prepared database+ */+async function makeDb(): Promise<Database> {+ const SQL = await initSqlJs();+ const db = new SQL.Database();+ db.run(`+ CREATE TABLE Folders (ManifestId TEXT NOT NULL, Id TEXT NOT NULL, Name TEXT, PRIMARY KEY (ManifestId, Id));+ CREATE TABLE Items (ManifestId TEXT NOT NULL, Id TEXT NOT NULL, PRIMARY KEY (ManifestId, Id));+ CREATE TABLE __EFMigrationsHistory (MigrationId TEXT NOT NULL PRIMARY KEY, ProductVersion TEXT NOT NULL);+ INSERT INTO Folders VALUES ('${PERSONAL}', 'FOLDER-MINE', 'Mine'), ('${SHARED}', 'FOLDER-SHARED', 'Team');+ INSERT INTO Items VALUES ('${PERSONAL}', 'ITEM-MINE');+ INSERT INTO __EFMigrationsHistory VALUES ('20260101_Init', '9.0.0');+ `);+ return db;+}++describe('VaultCodec.manifestIdsInVault', () => {+ it('reports every manifest the vault holds a row for, from every stamped table', async () => {+ const ids = VaultCodec.manifestIdsInVault(asClient(await makeDb()));+ expect([...ids].sort()).toEqual([PERSONAL, SHARED]);+ });++ it('finds a table nothing lists, because it reads the stamp column off the schema', async () => {+ const db = await makeDb();+ db.run(`CREATE TABLE SomethingNewer (ManifestId TEXT NOT NULL, Id TEXT NOT NULL); INSERT INTO SomethingNewer VALUES ('THIRD-MANIFEST', 'X');`);+ expect(VaultCodec.manifestIdsInVault(asClient(db)).has('THIRD-MANIFEST')).toBe(true);+ });++ it('keeps the stamp exactly as written, since that is how the codec routes it', async () => {+ const db = await makeDb();+ db.run(`INSERT INTO Items VALUES ('${SHARED.toLowerCase()}', 'ITEM-OTHERCASE');`);+ const ids = VaultCodec.manifestIdsInVault(asClient(db));+ expect(ids.has(SHARED)).toBe(true);+ expect(ids.has(SHARED.toLowerCase())).toBe(true);+ });++ it('ignores unstamped rows and unstamped tables', async () => {+ const db = await makeDb();+ db.run(`CREATE TABLE Unstamped (Id TEXT NOT NULL); INSERT INTO Unstamped VALUES ('X');`);+ db.run(`CREATE TABLE Loose (ManifestId TEXT, Id TEXT NOT NULL); INSERT INTO Loose VALUES (NULL, 'A'), ('', 'B');`);+ expect([...VaultCodec.manifestIdsInVault(asClient(db))].sort()).toEqual([PERSONAL, SHARED]);+ });++ it('is empty for a vault predating the stamps, so a legacy vault never looks unwritable', async () => {+ const SQL = await initSqlJs();+ const db = new SQL.Database();+ db.run(`CREATE TABLE Folders (Id TEXT NOT NULL PRIMARY KEY, Name TEXT); INSERT INTO Folders VALUES ('FOLDER-LEGACY', 'Mine');`);+ expect(VaultCodec.manifestIdsInVault(asClient(db)).size).toBe(0);+ });+});diff --git a/apps/browser-extension/src/utils/db/queries/EncryptionKeyQueries.ts b/apps/browser-extension/src/utils/db/queries/EncryptionKeyQueries.tsindex 66378f816..d8e1922e5 100644--- a/apps/browser-extension/src/utils/db/queries/EncryptionKeyQueries.ts+++ b/apps/browser-extension/src/utils/db/queries/EncryptionKeyQueries.ts@@ -29,6 +29,18 @@ export class EncryptionKeyQueries {WHERE x.ManifestId = ? AND x.IsPrimary = 1 AND x.IsDeleted = 0LIMIT 1`;+ /**+ * Get the account keypair matching the given public half.+ */+ public static readonly GET_ACCOUNT_KEY_BY_PUBLIC_KEY = `+ SELECT+ x.PublicKey,+ x.PrivateKey,+ x.IsPrimary+ FROM EncryptionKeys x+ WHERE x.ManifestId = ? AND x.PublicKey = ? AND x.IsDeleted = 0+ LIMIT 1`;+/*** Demote a manifest's current keypair. Rotation demotes rather than deletes, so mail received before the* rotation stays decryptable by the members who still hold the folder.diff --git a/apps/browser-extension/src/utils/db/repositories/EncryptionKeyRepository.ts b/apps/browser-extension/src/utils/db/repositories/EncryptionKeyRepository.tsindex 0e4b1b813..c10c5e0c9 100644--- a/apps/browser-extension/src/utils/db/repositories/EncryptionKeyRepository.ts+++ b/apps/browser-extension/src/utils/db/repositories/EncryptionKeyRepository.ts@@ -39,6 +39,20 @@ export class EncryptionKeyRepository extends BaseRepository {return results.length > 0 ? results[0] : null;}+ /**+ * Get the account keypair matching the given public half.+ * @param publicKey - The public half the grant was encrypted for+ * @returns The keypair, or null when this vault holds no account key with that public half+ */+ public getAccountKeypair(publicKey: string): EncryptionKey | null {+ const personalManifestId = this.personalManifestId();+ if (!personalManifestId) {+ return null;+ }+ const results = this.client.executeQuery<EncryptionKey>(EncryptionKeyQueries.GET_ACCOUNT_KEY_BY_PUBLIC_KEY, [personalManifestId, publicKey]);+ return results.length > 0 ? results[0] : null;+ }+/*** Make the given keypair the manifest's active one, demoting (never deleting) whatever it supersedes so* mail received before the rotation stays decryptable.