π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Acquire sessions at sync and contact operation boundaries
Manav Rathieab889dbc6cb
diff --git a/web/apps/locker/src/components/locker/use-locker-data.ts b/web/apps/locker/src/components/locker/use-locker-data.tsindex 50300618b2..3fd29f2853 100644--- a/web/apps/locker/src/components/locker/use-locker-data.ts+++ b/web/apps/locker/src/components/locker/use-locker-data.ts@@ -1,5 +1,8 @@import { masterKeyFromSession } from "@/services/account-keys";-import { openAuthenticatedSession } from "@/services/authenticated-session";+import {+ ensureAuthenticatedSession,+ openAuthenticatedSession,+} from "@/services/authenticated-session";import type { LockerUploadLimitState } from "@/services/locker-limits";import {loadPersistedLockerState,@@ -82,21 +85,9 @@ export const useLockerData = ({}, [userDetails]);const warmContacts = useCallback(async () => {- const [authToken, masterKey] = await Promise.all([- savedAuthToken(),- masterKeyFromSession(),- ]);- if (!authToken || !masterKey) return;-- const userID = ensureLocalUser().id;- const session = await openAuthenticatedSession(- userID,- authToken,- masterKey,- );await initContacts(- userID,- session,+ ensureLocalUser().id,+ ensureAuthenticatedSession,contactsGetDiff,contactsGetProfilePicture,);diff --git a/web/apps/locker/src/services/sync/decrypt.ts b/web/apps/locker/src/services/sync/decrypt.tsindex 0fcaf4b696..fd690ae540 100644--- a/web/apps/locker/src/services/sync/decrypt.ts+++ b/web/apps/locker/src/services/sync/decrypt.ts@@ -10,16 +10,17 @@ import {decryptMetadataJSON,encryptBoxBytes,openCollectionKey,+ type Session,} from "ente-locker-wasm";import type { z } from "zod";import { ensureAuthenticatedSession } from "../authenticated-session";import { fromInfoTypeWireValue } from "../info-type-wire";import {+ getLockerCacheSnapshot,type EncryptedCollectionRecord,type EncryptedFileRecord,type LockerCollectionPayload,type LockerEncryptedCache,- getLockerCacheSnapshot,} from "../locker-cache";import type { StoredTrashFileRecord } from "../locker-db";import { toLockerCollectionParticipant } from "../remote-types";@@ -223,6 +224,7 @@ const encryptCollectionPayload = async (};export const toEncryptedCollectionRecord = (+ session: Session,collection: RemoteCollection,): Promise<EncryptedCollectionRecord> => {const record: EncryptedCollectionRecord = {@@ -238,7 +240,7 @@ export const toEncryptedCollectionRecord = (};const buildEncryptedRecord = async () => {- const collectionKey = await decryptCollectionKey(record);+ const collectionKey = await openCollectionKeyForRecord(session, record);const payload: LockerCollectionPayload = {owner: {...toLockerCollectionParticipant(collection.owner),@@ -293,6 +295,7 @@ const decryptCollectionDetails = async (};export const decryptStoredTrash = async (+ session: Session,cache: LockerEncryptedCache,trashFiles: StoredTrashFileRecord[],lastUpdatedAt: number,@@ -308,7 +311,10 @@ export const decryptStoredTrash = async (}try {- const collectionKey = await decryptCollectionKey(collectionRecord);+ const collectionKey = await openCollectionKeyForRecord(+ session,+ collectionRecord,+ );const item = await decryptFileToLockerItem(record,collectionKey,@@ -341,8 +347,14 @@ export const buildStoredTrashFileRecord = (export const decryptCollectionKey = async (record: EncryptedCollectionRecord,): Promise<string> =>+ openCollectionKeyForRecord(await ensureAuthenticatedSession(), record);++const openCollectionKeyForRecord = (+ session: Session,+ record: EncryptedCollectionRecord,+) =>openCollectionKey(- await ensureAuthenticatedSession(),+ session,record.ownerID,record.encryptedKey,record.keyDecryptionNonce,@@ -466,6 +478,7 @@ const decryptFileToLockerItem = async (};export const decryptAllData = async (+ session: Session,cache: LockerEncryptedCache,): Promise<DecryptAllDataResult> => {const activeCollectionRecords = [...cache.collections.values()].filter(@@ -489,7 +502,10 @@ export const decryptAllData = async (for (const collectionRecord of activeCollectionRecords) {try {- const collectionKey = await decryptCollectionKey(collectionRecord);+ const collectionKey = await openCollectionKeyForRecord(+ session,+ collectionRecord,+ );const collectionDetails = await decryptCollectionDetails(collectionRecord,collectionKey,diff --git a/web/apps/locker/src/services/sync/sync.ts b/web/apps/locker/src/services/sync/sync.tsindex c15e487c64..9bf5d99911 100644--- a/web/apps/locker/src/services/sync/sync.ts+++ b/web/apps/locker/src/services/sync/sync.ts@@ -2,6 +2,8 @@ import type { LockerCollection, LockerItem } from "@/types";import { authenticatedRequestHeaders, ensureOk } from "ente-base/http";import log from "ente-base/log";import { apiURL } from "ente-base/origins";+import type { Session } from "ente-locker-wasm";+import { ensureAuthenticatedSession } from "../authenticated-session";import {type EncryptedCollectionRecord,type EncryptedFileRecord,@@ -150,6 +152,7 @@ const withoutFailedCollections = (};const hydrateLockerState = async (+ session: Session,collections: Map<number, EncryptedCollectionRecord>,files: EncryptedFileRecord[],trashFiles: StoredTrashFileRecord[],@@ -157,7 +160,7 @@ const hydrateLockerState = async (): Promise<LockerHydratedState> => {const activeCache = buildLockerCache(collections, files, []);- const decrypted = await decryptAllData(activeCache);+ const decrypted = await decryptAllData(session, activeCache);if (decrypted.totalCollectionCount > 0 &&decrypted.collections.length === 0@@ -180,6 +183,7 @@ const hydrateLockerState = async (}const trash = await decryptStoredTrash(+ session,hydratedCache,trashFiles,trashLastUpdatedAt,@@ -196,8 +200,10 @@ const hydrateLockerState = async (export const loadPersistedLockerState =async (): Promise<LockerPersistedState> => {+ const session = await ensureAuthenticatedSession();const snapshot = await loadLockerSnapshotFromDB();const hydrated = await hydrateLockerState(+ session,snapshot.collections,snapshot.files,snapshot.trashFiles,@@ -213,6 +219,7 @@ export const loadPersistedLockerState =};export const syncLockerState = async (): Promise<LockerHydratedState> => {+ const session = await ensureAuthenticatedSession();const snapshot = await loadLockerSnapshotFromDB();const collectionChanges = await fetchEncryptedCollections(snapshot.collectionsSinceTime,@@ -227,7 +234,7 @@ export const syncLockerState = async (): Promise<LockerHydratedState> => {latestCollectionsSinceTime,change.updationTime,);- const record = await toEncryptedCollectionRecord(change);+ const record = await toEncryptedCollectionRecord(session, change);changedCollections.push(record);if (record.isDeleted) {deletedCollectionIDs.push(record.id);@@ -308,6 +315,7 @@ export const syncLockerState = async (): Promise<LockerHydratedState> => {const nextSnapshot = await loadLockerSnapshotFromDB();const hydrated = await hydrateLockerState(+ session,nextSnapshot.collections,nextSnapshot.files,nextSnapshot.trashFiles,diff --git a/web/apps/locker/tests/sync.test.ts b/web/apps/locker/tests/sync.test.tsindex 25c2e78273..3992e5e9bf 100644--- a/web/apps/locker/tests/sync.test.ts+++ b/web/apps/locker/tests/sync.test.ts@@ -24,12 +24,14 @@ vi.mock("../src/services/locker-db", () => ({deleteTrashFileRecords: vi.fn(),saveTrashSinceTime: vi.fn(),}));-const { openCollectionKey, decryptMetadataJSON } = vi.hoisted(() => ({- openCollectionKey: vi.fn(),- decryptMetadataJSON: vi.fn(),-}));+const { ensureAuthenticatedSession, openCollectionKey, decryptMetadataJSON } =+ vi.hoisted(() => ({+ ensureAuthenticatedSession: vi.fn(),+ openCollectionKey: vi.fn(),+ decryptMetadataJSON: vi.fn(),+ }));vi.mock("../src/services/authenticated-session", () => ({- ensureAuthenticatedSession: () => "session",+ ensureAuthenticatedSession,}));vi.mock("ente-locker-wasm", () => ({openCollectionKey,@@ -103,6 +105,7 @@ beforeEach(() => {vi.stubGlobal("fetch", fetchMock);replaceLockerCache({ collections: new Map(), files: new Map() });vi.mocked(db.loadLockerSnapshotFromDB).mockResolvedValue(snapshot());+ ensureAuthenticatedSession.mockResolvedValue("session");openCollectionKey.mockResolvedValue("collection-key");decryptMetadataJSON.mockImplementation((value: { encryptedData: string }) =>@@ -136,6 +139,7 @@ test("persisted hydration preserves memberships, metadata, trash and cursors witexpect(getLockerCacheSnapshot().files.get(10)?.size).toBe(2);expect(getLockerCacheSnapshot().files.has(11)).toBe(true);expect(fetchMock).not.toHaveBeenCalled();+ expect(ensureAuthenticatedSession).toHaveBeenCalledOnce();});test("partial decryption failure excludes only the failed collection and its cache records", async () => {@@ -222,6 +226,7 @@ test("incremental sync paginates changed collections and trash, skipping up-to-d}),);await syncLockerState();+ expect(ensureAuthenticatedSession).toHaveBeenCalledOnce();expect(fetchMock.mock.calls.map(([url]) => url)).toEqual(["/collections/v2?sinceTime=5","/collections/v2/diff?collectionID=1&sinceTime=10",diff --git a/web/apps/photos/src/pages/gallery.tsx b/web/apps/photos/src/pages/gallery.tsxindex 5fa02d0d32..b6d1114000 100644--- a/web/apps/photos/src/pages/gallery.tsx+++ b/web/apps/photos/src/pages/gallery.tsx@@ -501,9 +501,8 @@ const Page: React.FC = () => {return;}- let session;try {- session = await ensureAuthenticatedSession();+ await ensureAuthenticatedSession();} catch (e) {if (isNamedError(e, "missing_recovery_key")) {showMiniDialog(sessionExpiredDialogAttributes(logout));@@ -524,7 +523,7 @@ const Page: React.FC = () => {const user = ensureLocalUser();void initContacts(user.id,- session,+ ensureAuthenticatedSession,contactsGetDiff,contactsGetProfilePicture,)diff --git a/web/packages/contacts/index.test.ts b/web/packages/contacts/index.test.tsindex f5bdd006db..414cf1ac06 100644--- a/web/packages/contacts/index.test.ts+++ b/web/packages/contacts/index.test.ts@@ -1,11 +1,12 @@import { beforeEach, describe, expect, test, vi } from "vitest";-const session = {};+const getSession = vi.fn(() => Promise.resolve({}));beforeEach(() => {vi.resetModules();vi.restoreAllMocks();vi.clearAllMocks();+ getSession.mockReset();vi.useRealTimers();});@@ -92,10 +93,17 @@ describe("contacts synchronization", () => {const { contacts, getDiff, getProfilePicture, setKV } =await setupContactsModule();- await contacts.initContacts(101, session, getDiff, getProfilePicture);+ await contacts.initContacts(+ 101,+ getSession,+ getDiff,+ getProfilePicture,+ );+ expect(getSession).not.toHaveBeenCalled();expect(getDiff).not.toHaveBeenCalled();expect(contacts.contactsDisplaySnapshot().isHydrated).toBe(true);await contacts.pullContacts();+ expect(getSession).toHaveBeenCalledOnce();const persisted = setKV.mock.calls.map(([key, value]) => `${key}:${JSON.stringify(value)}`)@@ -113,7 +121,12 @@ describe("contacts synchronization", () => {const { contacts, getDiff, getProfilePicture, setKV } =await setupContactsModule({ rootKeyResolved: false, diff: [] });- await contacts.initContacts(101, session, getDiff, getProfilePicture);+ await contacts.initContacts(+ 101,+ getSession,+ getDiff,+ getProfilePicture,+ );await contacts.pullContacts();const persisted = setKV.mock.calls@@ -127,7 +140,12 @@ describe("contacts synchronization", () => {const { contacts, getDiff, getProfilePicture, setKV } =await setupContactsModule({ rootKeyResolved: false });- await contacts.initContacts(101, session, getDiff, getProfilePicture);+ await contacts.initContacts(+ 101,+ getSession,+ getDiff,+ getProfilePicture,+ );await contacts.pullContacts();const persisted = setKV.mock.calls@@ -138,6 +156,50 @@ describe("contacts synchronization", () => {});});+test("each pull and avatar load acquires the current session", async () => {+ const first = { token: "first" };+ const second = { token: "second" };+ const third = { token: "third" };+ getSession+ .mockResolvedValueOnce(first)+ .mockResolvedValueOnce(second)+ .mockResolvedValueOnce(third);+ const { contacts, getDiff, getProfilePicture } =+ await setupContactsModule();++ await contacts.initContacts(101, getSession, getDiff, getProfilePicture);+ await contacts.pullContacts();+ expect(getDiff.mock.calls[0]?.[0]).toBe(first);+ expect(getDiff.mock.calls[1]?.[0]).toBe(first);++ getDiff.mockResolvedValue({ records: [] });+ await contacts.pullContacts();+ expect(getDiff.mock.lastCall?.[0]).toBe(second);++ await contacts.__testing.preloadResolvedContactAvatar({ userID: 101 });+ expect(getProfilePicture).toHaveBeenCalledWith(+ third,+ expect.anything(),+ "ct_1",+ );+ expect(getSession).toHaveBeenCalledTimes(3);+});++test("logout while acquiring a session prevents the pull", async () => {+ const pending = Promise.withResolvers<object>();+ getSession.mockReturnValueOnce(pending.promise);+ const { contacts, getDiff, getProfilePicture } =+ await setupContactsModule();+ await contacts.initContacts(101, getSession, getDiff, getProfilePicture);++ const pulling = contacts.pullContacts();+ expect(getSession).toHaveBeenCalledOnce();+ contacts.logoutContacts();+ pending.resolve({});+ await pulling;+ expect(getDiff).not.toHaveBeenCalled();+});+describe("profile picture loading", () => {test("negative-caches failed profile picture fetches and logs at info", async () => {const { contacts, getDiff, getProfilePicture, info } =@@ -145,7 +207,12 @@ describe("profile picture loading", () => {getProfilePictureError: new Error("network failure"),});- await contacts.initContacts(101, session, getDiff, getProfilePicture);+ await contacts.initContacts(+ 101,+ getSession,+ getDiff,+ getProfilePicture,+ );await contacts.pullContacts();await contacts.__testing.preloadResolvedContactAvatar({@@ -175,7 +242,12 @@ describe("profile picture loading", () => {const { contacts, getDiff, getProfilePicture } =await setupContactsModule({ getProfilePictureBytes: pngBytes });- await contacts.initContacts(101, session, getDiff, getProfilePicture);+ await contacts.initContacts(+ 101,+ getSession,+ getDiff,+ getProfilePicture,+ );await contacts.pullContacts();await contacts.__testing.preloadResolvedContactAvatar({ userID: 101 });@@ -193,7 +265,7 @@ describe("profile picture loading", () => {await setupContactsModule();await contacts.initContacts(101,- session,+ getSession,getDiff,getProfilePicture,);@@ -224,7 +296,7 @@ describe("profile picture loading", () => {}await contacts.initContacts(change === "account change" ? 202 : 101,- session,+ getSession,getDiff,getProfilePicture,);@@ -268,7 +340,12 @@ describe("retry after warm-up failure", () => {}).mockResolvedValueOnce({ records: [] });- await contacts.initContacts(101, session, getDiff, getProfilePicture);+ await contacts.initContacts(+ 101,+ getSession,+ getDiff,+ getProfilePicture,+ );const ready = contacts.pullContacts();await vi.advanceTimersByTimeAsync(10_001);await expect(ready).resolves.toBeUndefined();@@ -283,7 +360,12 @@ describe("retry after warm-up failure", () => {getDiff.mockReset();getDiff.mockRejectedValue(new Error("down"));- await contacts.initContacts(101, session, getDiff, getProfilePicture);+ await contacts.initContacts(+ 101,+ getSession,+ getDiff,+ getProfilePicture,+ );const ready = expect(contacts.pullContacts()).rejects.toThrow("down");await vi.advanceTimersByTimeAsync(10_001);@@ -305,7 +387,12 @@ describe("retry after warm-up failure", () => {.mockRejectedValueOnce(new Error("transient")).mockResolvedValue({ records: [] });- await contacts.initContacts(101, session, getDiff, getProfilePicture);+ await contacts.initContacts(+ 101,+ getSession,+ getDiff,+ getProfilePicture,+ );const stalePull = contacts.pullContacts();await vi.advanceTimersByTimeAsync(0);expect(getDiff).toHaveBeenCalledTimes(1);@@ -314,7 +401,12 @@ describe("retry after warm-up failure", () => {await contacts.pullContacts();expect(getDiff).toHaveBeenCalledTimes(1);- await contacts.initContacts(101, session, getDiff, getProfilePicture);+ await contacts.initContacts(+ 101,+ getSession,+ getDiff,+ getProfilePicture,+ );await contacts.pullContacts();expect(getDiff).toHaveBeenCalledTimes(2);@@ -334,7 +426,12 @@ describe("logout", () => {await setupContactsModule({getProfilePictureBytes: new Uint8Array([1, 2, 3]),});- await contacts.initContacts(101, session, getDiff, getProfilePicture);+ await contacts.initContacts(+ 101,+ getSession,+ getDiff,+ getProfilePicture,+ );await contacts.pullContacts();await contacts.__testing.preloadResolvedContactAvatar({ userID: 101 });const changed = vi.fn();diff --git a/web/packages/contacts/index.ts b/web/packages/contacts/index.tsindex d28d6fa2bf..8635252b7a 100644--- a/web/packages/contacts/index.ts+++ b/web/packages/contacts/index.ts@@ -57,7 +57,8 @@ interface ProfilePictureOutput {wrappedRootContactKey?: WrappedRootContactKey;}-type GetDiff = (+type GetDiff<Session> = (+ session: Session,wrappedRootContactKey: WrappedRootContactKey | undefined,sinceTime: number,limit: number,@@ -73,7 +74,7 @@ interface ContactsState {listeners: Set<() => void>;currentSessionKey: string | undefined;sessionGeneration: number;- getDiff: GetDiff | undefined;+ sync: (() => Promise<void>) | undefined;getProfilePicture: LoadProfilePicture | undefined;wrappedRootContactKey: WrappedRootContactKey | undefined;pullPromise: Promise<void> | undefined;@@ -98,7 +99,7 @@ const state: ContactsState = {listeners: new Set(),currentSessionKey: undefined,sessionGeneration: 0,- getDiff: undefined,+ sync: undefined,getProfilePicture: undefined,wrappedRootContactKey: undefined,pullPromise: undefined,@@ -145,7 +146,7 @@ const clearInMemoryState = () => {for (const avatarURL of state.avatarURLByContactID.values()) {URL.revokeObjectURL(avatarURL);}- state.getDiff = undefined;+ state.sync = undefined;state.getProfilePicture = undefined;state.wrappedRootContactKey = undefined;state.pullPromise = undefined;@@ -309,14 +310,16 @@ const ensureSessionLoaded = async (sessionKey: string) =>? state.sessionGeneration: loadLocalSessionState(sessionKey);-const syncContacts = async (+const syncContacts = async <Session>(sessionKey: string,generation: number,- getDiff: GetDiff,+ getSession: () => Promise<Session>,+ getDiff: GetDiff<Session>,) => {if (!isCurrentSession(sessionKey, generation)) {return;}+ const session = await getSession();const cachedWrappedRootContactKey =state.wrappedRootContactKey ??(await savedWrappedRootContactKey(sessionKey));@@ -330,6 +333,7 @@ const syncContacts = async (while (true) {const output = await getDiff(+ session,state.wrappedRootContactKey,sinceTime,CONTACT_DIFF_LIMIT,@@ -377,13 +381,8 @@ const syncContacts = async (export const initContacts = async <Session>(userID: number,- session: Session,- getDiff: (- session: Session,- wrappedRootContactKey: WrappedRootContactKey | undefined,- sinceTime: number,- limit: number,- ) => Promise<ContactsDiffOutput>,+ getSession: () => Promise<Session>,+ getDiff: GetDiff<Session>,getProfilePicture: (session: Session,wrappedRootContactKey: WrappedRootContactKey | undefined,@@ -398,23 +397,20 @@ export const initContacts = async <Session>(return;}- state.getDiff = (key, sinceTime, limit) =>- getDiff(session, key, sinceTime, limit);- state.getProfilePicture = (key, contactID) =>- getProfilePicture(session, key, contactID);+ state.sync = () =>+ syncContacts(sessionKey, generation, getSession, getDiff);+ state.getProfilePicture = async (key, contactID) =>+ getProfilePicture(await getSession(), key, contactID);};export const pullContacts = async () => {- const sessionKey = state.currentSessionKey;- const generation = state.sessionGeneration;- const getDiff = state.getDiff;- if (!sessionKey || !getDiff) return;+ const sync = state.sync;+ if (!sync) return;if (state.pullPromise) return state.pullPromise;- const pullPromise = retryAsyncOperation(- () => syncContacts(sessionKey, generation, getDiff),- { retryProfile: "background" },- ).finally(() => {+ const pullPromise = retryAsyncOperation(sync, {+ retryProfile: "background",+ }).finally(() => {if (state.pullPromise === pullPromise) {state.pullPromise = undefined;}