π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Fix session lifecycle and Contacts warm-up
Manav Rathie989c4a99f74
diff --git a/mobile/apps/locker/lib/services/authenticated_session.dart b/mobile/apps/locker/lib/services/authenticated_session.dartindex 131235cc8b..6eacefa775 100644--- a/mobile/apps/locker/lib/services/authenticated_session.dart+++ b/mobile/apps/locker/lib/services/authenticated_session.dart@@ -11,8 +11,7 @@ Session authenticatedSession() {final config = Configuration.instance;final authToken = config.getToken();final userId = config.getUserID();- final masterKey = config.getKey();- if (authToken == null || userId == null || masterKey == null) {+ if (authToken == null || userId == null || !config.hasConfiguredAccount()) {throw StateError('Authenticated session is not available');}final baseUrl = config.getHttpEndpoint();@@ -27,7 +26,7 @@ Session authenticatedSession() {final opened = openSession(baseUrl: baseUrl,authToken: authToken,- masterKey: masterKey,+ masterKey: config.getKey()!,userAgent:services.enteDio.options.headers[HttpHeaders.userAgentHeader] as String,clientPackage: services.packageInfo.packageName,diff --git a/mobile/apps/photos/lib/services/authenticated_session.dart b/mobile/apps/photos/lib/services/authenticated_session.dartindex 8aa0e90114..6c5a35e84e 100644--- a/mobile/apps/photos/lib/services/authenticated_session.dart+++ b/mobile/apps/photos/lib/services/authenticated_session.dart@@ -11,8 +11,7 @@ Session authenticatedSession() {final config = Configuration.instance;final authToken = config.getToken();final userId = config.getUserID();- final masterKey = config.getKey();- if (authToken == null || userId == null || masterKey == null) {+ if (authToken == null || userId == null || !config.hasConfiguredAccount()) {throw StateError('Authenticated session is not available');}final baseUrl = endpointConfig.endpoint;@@ -27,7 +26,7 @@ Session authenticatedSession() {final opened = openSession(baseUrl: baseUrl,authToken: authToken,- masterKey: masterKey,+ masterKey: config.getKey()!,userAgent:services.enteDio.options.headers[HttpHeaders.userAgentHeader] as String,clientPackage: services.packageInfo.packageName,diff --git a/web/apps/locker/src/components/lockerPage/use-locker-data.ts b/web/apps/locker/src/components/lockerPage/use-locker-data.tsindex 4ddfc5b711..f2a4683c20 100644--- a/web/apps/locker/src/components/lockerPage/use-locker-data.ts+++ b/web/apps/locker/src/components/lockerPage/use-locker-data.ts@@ -1,7 +1,4 @@-import {- authenticatedSession,- openAuthenticatedSession,-} from "@/services/authenticated-session";+import { openAuthenticatedSession } from "@/services/authenticated-session";import {LOCKER_FILE_LIMIT_FREE,LOCKER_FILE_LIMIT_PAID,@@ -111,13 +108,20 @@ export const useLockerData = ({}, [userDetails]);const warmContacts = useCallback(async () => {- const authToken = await savedAuthToken();- if (!authToken) return;-- const session = authenticatedSession();- session.updateAuthToken(authToken);+ const [authToken, masterKey] = await Promise.all([+ savedAuthToken(),+ masterKeyFromSession(),+ ]);+ if (!authToken || !masterKey) return;++ const userID = ensureLocalUser().id;+ const session = await openAuthenticatedSession(+ userID,+ authToken,+ masterKey,+ );await ensureContactsReady(- ensureLocalUser().id,+ userID,session,contactsGetDiff,contactsGetProfilePicture,@@ -333,7 +337,6 @@ export const useLockerData = ({}setMasterKey(mk);- await openAuthenticatedSession(ensureLocalUser().id, token, mk);void warmContacts().catch((error: unknown) => {log.warn("[locker] Failed to warm contacts display cache",diff --git a/web/apps/locker/src/services/authenticated-session.ts b/web/apps/locker/src/services/authenticated-session.tsindex f36a6988f5..77c079fb56 100644--- a/web/apps/locker/src/services/authenticated-session.ts+++ b/web/apps/locker/src/services/authenticated-session.ts@@ -2,32 +2,53 @@ import { clientPackageName, desktopAppVersion, isDesktop } from "ente-base/app";import { apiOrigin } from "ente-base/origins";import { openSession, type Session } from "ente-locker-wasm";-let current: { key: string; session: Session } | undefined;+let current: { key: string; opening: Promise<Session> } | undefined;+let generation = 0;export const openAuthenticatedSession = async (userID: number,authToken: string,masterKeyB64: string,) => {+ const startedGeneration = generation;const baseUrl = await apiOrigin();+ if (startedGeneration !== generation) {+ throw new Error("Authenticated session was cleared");+ }const key = `${baseUrl}:${userID}`;- if (current?.key === key) {- current.session.updateAuthToken(authToken);- return current.session;+ if (current?.key !== key) {+ const opening = openSession({+ baseUrl,+ authToken,+ masterKeyB64,+ clientPackage: clientPackageName,+ clientVersion: isDesktop ? desktopAppVersion : undefined,+ })+ .then((session) => {+ if (current?.opening !== opening) {+ session.free();+ throw new Error("Authenticated session was cleared");+ }+ return session;+ })+ .catch((error: unknown) => {+ if (current?.opening === opening) current = undefined;+ throw error;+ });+ current = { key, opening };}- const session = await openSession({- baseUrl,- authToken,- masterKeyB64,- clientPackage: clientPackageName,- clientVersion: isDesktop ? desktopAppVersion : undefined,- });- current = { key, session };+ const entry = current;+ const session = await entry.opening;+ if (current !== entry) {+ throw new Error("Authenticated session was cleared");+ }+ session.updateAuthToken(authToken);return session;};-export const authenticatedSession = () => {- if (!current) throw new Error("Authenticated session is not open");- return current.session;+export const clearAuthenticatedSession = () => {+ generation++;+ // In-flight calls may still borrow the handle; wasm-bindgen finalizes it.+ current = undefined;};diff --git a/web/apps/locker/src/services/logout.ts b/web/apps/locker/src/services/logout.tsindex 2e3495d8c9..f8e2f5a998 100644--- a/web/apps/locker/src/services/logout.ts+++ b/web/apps/locker/src/services/logout.ts@@ -1,6 +1,7 @@import { savedLocalUser } from "ente-accounts/services/accounts-db";import { accountLogout } from "ente-accounts/services/logout";import log from "ente-base/log";+import { clearAuthenticatedSession } from "./authenticated-session";import { clearLockerDB } from "./locker-db";import { clearLockerCache } from "./remote-cache";@@ -12,6 +13,12 @@ export const lockerLogout = async () => {const userID = savedLocalUser()?.id;+ try {+ clearAuthenticatedSession();+ } catch (error) {+ ignoreError("Authenticated session", error);+ }+try {clearLockerCache();} catch (error) {diff --git a/web/apps/photos/src/pages/gallery.tsx b/web/apps/photos/src/pages/gallery.tsxindex adfcd89a96..bfc8133935 100644--- a/web/apps/photos/src/pages/gallery.tsx+++ b/web/apps/photos/src/pages/gallery.tsx@@ -512,22 +512,21 @@ const Page: React.FC = () => {const user = ensureLocalUser();const masterKey = await masterKeyFromSession();if (masterKey) {- const session = await openAuthenticatedSession(- user.id,- authToken,- masterKey,- );- void ensureContactsReady(- user.id,- session,- contactsGetDiff,- contactsGetProfilePicture,- ).catch((error: unknown) => {- log.warn(- "[gallery] Failed to warm contacts display cache",- error,- );- });+ void openAuthenticatedSession(user.id, authToken, masterKey)+ .then((session) =>+ ensureContactsReady(+ user.id,+ session,+ contactsGetDiff,+ contactsGetProfilePicture,+ ),+ )+ .catch((error: unknown) => {+ log.warn(+ "[gallery] Failed to warm contacts display cache",+ error,+ );+ });}const userDetails = await savedUserDetailsOrTriggerPull();dispatch({diff --git a/web/apps/photos/src/services/authenticated-session.ts b/web/apps/photos/src/services/authenticated-session.tsindex d7fde899b7..99a8dbbd2c 100644--- a/web/apps/photos/src/services/authenticated-session.ts+++ b/web/apps/photos/src/services/authenticated-session.ts@@ -2,32 +2,53 @@ import { clientPackageName, desktopAppVersion, isDesktop } from "ente-base/app";import { apiOrigin } from "ente-base/origins";import { openSession, type Session } from "ente-photos-wasm";-let current: { key: string; session: Session } | undefined;+let current: { key: string; opening: Promise<Session> } | undefined;+let generation = 0;export const openAuthenticatedSession = async (userID: number,authToken: string,masterKeyB64: string,) => {+ const startedGeneration = generation;const baseUrl = await apiOrigin();+ if (startedGeneration !== generation) {+ throw new Error("Authenticated session was cleared");+ }const key = `${baseUrl}:${userID}`;- if (current?.key === key) {- current.session.updateAuthToken(authToken);- return current.session;+ if (current?.key !== key) {+ const opening = openSession({+ baseUrl,+ authToken,+ masterKeyB64,+ clientPackage: clientPackageName,+ clientVersion: isDesktop ? desktopAppVersion : undefined,+ })+ .then((session) => {+ if (current?.opening !== opening) {+ session.free();+ throw new Error("Authenticated session was cleared");+ }+ return session;+ })+ .catch((error: unknown) => {+ if (current?.opening === opening) current = undefined;+ throw error;+ });+ current = { key, opening };}- const session = await openSession({- baseUrl,- authToken,- masterKeyB64,- clientPackage: clientPackageName,- clientVersion: isDesktop ? desktopAppVersion : undefined,- });- current = { key, session };+ const entry = current;+ const session = await entry.opening;+ if (current !== entry) {+ throw new Error("Authenticated session was cleared");+ }+ session.updateAuthToken(authToken);return session;};-export const authenticatedSession = () => {- if (!current) throw new Error("Authenticated session is not open");- return current.session;+export const clearAuthenticatedSession = () => {+ generation++;+ // In-flight calls may still borrow the handle; wasm-bindgen finalizes it.+ current = undefined;};diff --git a/web/apps/photos/src/services/logout.ts b/web/apps/photos/src/services/logout.tsindex 138f6ccdbd..378e0d37b8 100644--- a/web/apps/photos/src/services/logout.ts+++ b/web/apps/photos/src/services/logout.ts@@ -15,6 +15,7 @@ import { logoutML, terminateMLWorker } from "ente-new/photos/services/ml";import { logoutSearch } from "ente-new/photos/services/search";import { logoutSettings } from "ente-new/photos/services/settings";import { logoutUserDetails } from "ente-new/photos/services/user-details";+import { clearAuthenticatedSession } from "./authenticated-session";import { uploadManager } from "./upload-manager";// Individual cleanup failures must not abort logout.@@ -22,6 +23,12 @@ export const photosLogout = async () => {const ignoreError = (label: string, e: unknown) =>log.error(`Ignoring error during logout (${label})`, e);+ try {+ clearAuthenticatedSession();+ } catch (e) {+ ignoreError("Authenticated session", e);+ }+// Stop workers before clearing databases they may still access.try {await terminateMLWorker();diff --git a/web/apps/photos/tests/authenticated-session.test.ts b/web/apps/photos/tests/authenticated-session.test.tsnew file mode 100644index 0000000000..d6adb923d7--- /dev/null+++ b/web/apps/photos/tests/authenticated-session.test.ts@@ -0,0 +1,153 @@+import type { Session } from "ente-photos-wasm";+import { beforeEach, expect, test, vi } from "vitest";++const { apiOrigin, openSession } = vi.hoisted(() => ({+ apiOrigin: vi.fn<() => Promise<string>>(),+ openSession: vi.fn<typeof import("ente-photos-wasm").openSession>(),+}));++vi.mock("ente-base/app", () => ({+ clientPackageName: "io.ente.photos.web",+ desktopAppVersion: undefined,+ isDesktop: false,+}));+vi.mock("ente-base/origins", () => ({ apiOrigin }));+vi.mock("ente-photos-wasm", () => ({ openSession }));++let sessions: typeof import("../src/services/authenticated-session");++beforeEach(async () => {+ vi.resetModules();+ vi.resetAllMocks();+ apiOrigin.mockResolvedValue("http://localhost:8080");+ sessions = await import("../src/services/authenticated-session");+});++const mockSession = () =>+ ({+ free: vi.fn(),+ updateAuthToken: vi.fn(),+ [Symbol.dispose]: vi.fn(),+ }) satisfies Session;++test("concurrent opens share a session and retain the latest token", async () => {+ const ready = Promise.withResolvers<Session>();+ const session = mockSession();+ openSession.mockReturnValue(ready.promise);++ const first = sessions.openAuthenticatedSession(1, "old-token", "key");+ const second = sessions.openAuthenticatedSession(1, "new-token", "key");+ await Promise.resolve();+ expect(openSession).toHaveBeenCalledTimes(1);++ ready.resolve(session);+ expect(await first).toBe(session);+ expect(await second).toBe(session);+ expect(session.updateAuthToken).toHaveBeenLastCalledWith("new-token");+ expect(await sessions.openAuthenticatedSession(1, "new-token", "key")).toBe(+ session,+ );+ expect(openSession).toHaveBeenCalledTimes(1);++ sessions.clearAuthenticatedSession();+ sessions.clearAuthenticatedSession();+ expect(session.free).not.toHaveBeenCalled();+ const next = mockSession();+ openSession.mockResolvedValueOnce(next);+ expect(await sessions.openAuthenticatedSession(1, "new-token", "key")).toBe(+ next,+ );+ expect(openSession).toHaveBeenCalledTimes(2);+});++test("failed initialization can be retried", async () => {+ const error = new Error("WASM download failed");+ const session = mockSession();+ openSession.mockRejectedValueOnce(error).mockResolvedValueOnce(session);++ await expect(+ sessions.openAuthenticatedSession(1, "token", "key"),+ ).rejects.toBe(error);+ expect(await sessions.openAuthenticatedSession(1, "token", "key")).toBe(+ session,+ );+ expect(openSession).toHaveBeenCalledTimes(2);+});++test.each(["account", "origin"])(+ "changing the %s replaces the cached session without freeing active callers' handles",+ async (change) => {+ const previous = mockSession();+ const next = mockSession();+ openSession.mockResolvedValueOnce(previous).mockResolvedValueOnce(next);+ await sessions.openAuthenticatedSession(1, "token", "key");++ if (change === "origin") {+ apiOrigin.mockResolvedValue("http://localhost:8081");+ }+ const userID = change === "account" ? 2 : 1;+ expect(+ await sessions.openAuthenticatedSession(userID, "token", "key"),+ ).toBe(next);+ expect(previous.free).not.toHaveBeenCalled();+ expect(next.free).not.toHaveBeenCalled();+ },+);++test("clearing during origin lookup prevents initialization", async () => {+ const origin = Promise.withResolvers<string>();+ apiOrigin.mockReturnValue(origin.promise);+ const opening = sessions.openAuthenticatedSession(1, "token", "key");+ sessions.clearAuthenticatedSession();+ origin.resolve("http://localhost:8080");++ await expect(opening).rejects.toThrow("Authenticated session was cleared");+ expect(openSession).not.toHaveBeenCalled();+});++test("clearing during initialization disposes its eventual handle", async () => {+ const ready = Promise.withResolvers<Session>();+ const session = mockSession();+ openSession.mockReturnValue(ready.promise);+ const opening = sessions.openAuthenticatedSession(1, "token", "key");+ await Promise.resolve();++ sessions.clearAuthenticatedSession();+ ready.resolve(session);+ await expect(opening).rejects.toThrow("Authenticated session was cleared");+ expect(session.free).toHaveBeenCalledTimes(1);+});++test.each(["resolve", "reject"])(+ "a replaced initialization cannot affect the new session when it %ss",+ async (outcome) => {+ const ready = Promise.withResolvers<Session>();+ const previous = mockSession();+ const next = mockSession();+ openSession+ .mockReturnValueOnce(ready.promise)+ .mockResolvedValueOnce(next);+ const opening = sessions.openAuthenticatedSession(+ 1,+ "old-token",+ "key",+ );+ await Promise.resolve();+ await sessions.openAuthenticatedSession(2, "new-token", "key");++ if (outcome === "resolve") {+ ready.resolve(previous);+ } else {+ ready.reject(new Error("WASM download failed"));+ }+ await expect(opening).rejects.toThrow();+ expect(previous.free).toHaveBeenCalledTimes(+ outcome === "resolve" ? 1 : 0,+ );+ expect(next.free).not.toHaveBeenCalled();+ expect(+ await sessions.openAuthenticatedSession(2, "new-token", "key"),+ ).toBe(next);+ expect(openSession).toHaveBeenCalledTimes(2);+ },+);diff --git a/web/packages/contacts/index.test.ts b/web/packages/contacts/index.test.tsindex 04620b42e3..fd96c624a7 100644--- a/web/packages/contacts/index.test.ts+++ b/web/packages/contacts/index.test.ts@@ -123,10 +123,7 @@ describe("ensureContactsReady", () => {test("does not persist an unresolved wrapped root contact key", async () => {const { contacts, getDiff, getProfilePicture, setKV } =- await setupContactsModule({- rootKeyResolved: false,- diff: [],- });+ await setupContactsModule({ rootKeyResolved: false, diff: [] });await contacts.ensureContactsReady(101,@@ -213,6 +210,65 @@ describe("profile picture loading", () => {const blobArg = createObjectURL.mock.calls[0]?.[0] as Blob | undefined;expect(blobArg?.type).toBe("image/png");});++ test.each(["refresh", "account change", "avatar change"])(+ "handles an in-flight avatar during a contacts %s",+ async (change) => {+ const createObjectURL = vi+ .spyOn(URL, "createObjectURL")+ .mockReturnValue("blob:contact");+ const { contacts, getDiff, getProfilePicture } =+ await setupContactsModule();+ await contacts.ensureContactsReady(+ 101,+ session,+ getDiff,+ getProfilePicture,+ );++ const picture =+ Promise.withResolvers<+ Awaited<ReturnType<typeof getProfilePicture>>+ >();+ getProfilePicture.mockReturnValueOnce(picture.promise);+ const loading = contacts.__testing.preloadResolvedContactAvatar({+ userID: 101,+ });++ getDiff.mockResolvedValue({ records: [] });+ if (change === "avatar change") {+ getDiff.mockResolvedValueOnce({+ records: [+ {+ id: "ct_1",+ contactUserId: 101,+ profilePictureAttachmentID: "ua_2",+ isDeleted: false,+ updatedAt: 2,+ },+ ],+ });+ }+ await contacts.ensureContactsReady(+ change === "account change" ? 202 : 101,+ session,+ getDiff,+ getProfilePicture,+ );++ picture.resolve({+ bytes: new Uint8Array([1, 2, 3]),+ wrappedRootContactKey: {+ encryptedKey: "wrapped-root-key",+ header: "wrapped-header",+ },+ });+ await loading;+ expect(createObjectURL).toHaveBeenCalledTimes(+ change === "refresh" ? 1 : 0,+ );+ },+ );});describe("retry after warm-up failure", () => {@@ -236,9 +292,7 @@ describe("retry after warm-up failure", () => {},],})- .mockResolvedValueOnce({- records: [],- });+ .mockResolvedValueOnce({ records: [] });const ready = contacts.ensureContactsReady(101,diff --git a/web/packages/contacts/index.ts b/web/packages/contacts/index.tsindex a7619d9cb3..395e14728e 100644--- a/web/packages/contacts/index.ts+++ b/web/packages/contacts/index.ts@@ -510,7 +510,7 @@ const ensureProfilePictureLoaded = async (contactID: string) => {.then((output) => {if (!isCurrentSession(sessionKey, generation) ||- state.getProfilePicture !== getProfilePicture+ state.avatarLoadsByContactID.get(contactID) !== load) {return;}@@ -530,7 +530,7 @@ const ensureProfilePictureLoaded = async (contactID: string) => {.catch((error: unknown) => {if (!isCurrentSession(sessionKey, generation) ||- state.getProfilePicture !== getProfilePicture+ state.avatarLoadsByContactID.get(contactID) !== load) {return;}