Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Bind session storage crypto at composition

Manav Rathi80c81facc447
diff --git a/web/apps/locker/src/services/account-keys.ts b/web/apps/locker/src/services/account-keys.ts
index a2da3b763f..67abcd12e3 100644
--- a/web/apps/locker/src/services/account-keys.ts
+++ b/web/apps/locker/src/services/account-keys.ts
@@ -1,4 +1,8 @@
-import { masterKeyFromSession as readMasterKeyFromSession } from "ente-accounts/services/session-storage";
-import { decryptBox } from "ente-locker-wasm";
+import { createSessionStorage } from "ente-accounts/services/session-storage";
+import { decryptBox, encryptBox, generateKey } from "ente-locker-wasm";
-export const masterKeyFromSession = () => readMasterKeyFromSession(decryptBox);
+export const { masterKeyFromSession } = createSessionStorage({
+ decryptBox,
+ encryptBox,
+ generateKey,
+});
diff --git a/web/apps/space/src/services/login.ts b/web/apps/space/src/services/login.ts
index 25c769e6ce..23b9b24524 100644
--- a/web/apps/space/src/services/login.ts
+++ b/web/apps/space/src/services/login.ts
@@ -20,7 +20,7 @@ import {
import {
stashKeyEncryptionKeyInSessionStore,
unstashKeyEncryptionKeyFromSession,
-} from "ente-accounts/services/session-storage";
+} from "ente-accounts/services/prelogin-session";
import { getSRPAttributes, verifySRP } from "ente-accounts/services/srp";
import {
type EmailOrSRPVerificationResponse,
diff --git a/web/packages/accounts/components/SignUpContents.tsx b/web/packages/accounts/components/SignUpContents.tsx
index 15e7f5443a..ea3365d414 100644
--- a/web/packages/accounts/components/SignUpContents.tsx
+++ b/web/packages/accounts/components/SignUpContents.tsx
@@ -20,7 +20,7 @@ import {
stashReferralSource,
stashSRPSetupAttributes,
} from "ente-accounts/services/accounts-db";
-import { saveMasterKeyInSessionAndSafeStore } from "ente-accounts/services/session-storage";
+import { saveMasterKeyInSessionAndSafeStore } from "ente-accounts/services/prelogin-session";
import { generateSRPSetupAttributes } from "ente-accounts/services/srp";
import {
generateAndSaveInteractiveKeyAttributes,
diff --git a/web/packages/accounts/pages/credentials.tsx b/web/packages/accounts/pages/credentials.tsx
index 6ced1f13fb..70acc50a5c 100644
--- a/web/packages/accounts/pages/credentials.tsx
+++ b/web/packages/accounts/pages/credentials.tsx
@@ -31,19 +31,19 @@ import {
openPasskeyVerificationURL,
passkeyVerificationRedirectURL,
} from "ente-accounts/services/passkey";
-import { masterKeyFromSession } from "ente-accounts/services/prelogin-session";
+import {
+ masterKeyFromSession,
+ saveMasterKeyInSessionAndSafeStore,
+ stashKeyEncryptionKeyInSessionStore,
+ unstashKeyEncryptionKeyFromSession,
+ updateSessionFromElectronSafeStorageIfNeeded,
+} from "ente-accounts/services/prelogin-session";
import {
appHomeRoute,
stashRedirect,
unstashRedirect,
} from "ente-accounts/services/redirect";
import { checkSessionValidity } from "ente-accounts/services/session";
-import {
- saveMasterKeyInSessionAndSafeStore,
- stashKeyEncryptionKeyInSessionStore,
- unstashKeyEncryptionKeyFromSession,
- updateSessionFromElectronSafeStorageIfNeeded,
-} from "ente-accounts/services/session-storage";
import type { SRPAttributes } from "ente-accounts/services/srp";
import {
generateSRPSetupAttributes,
diff --git a/web/packages/accounts/pages/generate.tsx b/web/packages/accounts/pages/generate.tsx
index b70f2bed07..e8c0be829b 100644
--- a/web/packages/accounts/pages/generate.tsx
+++ b/web/packages/accounts/pages/generate.tsx
@@ -15,11 +15,9 @@ import {
savedPartialLocalUser,
saveJustSignedUp,
} from "ente-accounts/services/accounts-db";
+import { saveMasterKeyInSessionAndSafeStore } from "ente-accounts/services/prelogin-session";
import { appHomeRoute } from "ente-accounts/services/redirect";
-import {
- haveMasterKeyInSession,
- saveMasterKeyInSessionAndSafeStore,
-} from "ente-accounts/services/session-storage";
+import { haveMasterKeyInSession } from "ente-accounts/services/session-storage";
import {
generateSRPSetupAttributes,
getAndSaveSRPAttributes,
diff --git a/web/packages/accounts/pages/recover.tsx b/web/packages/accounts/pages/recover.tsx
index 0e28973d9a..ad89096c5f 100644
--- a/web/packages/accounts/pages/recover.tsx
+++ b/web/packages/accounts/pages/recover.tsx
@@ -8,12 +8,10 @@ import {
savedPartialLocalUser,
} from "ente-accounts/services/accounts-db";
import { decryptBox } from "ente-accounts/services/crypto";
+import { saveMasterKeyInSessionAndSafeStore } from "ente-accounts/services/prelogin-session";
import { recoveryKeyFromMnemonic } from "ente-accounts/services/recovery-key";
import { appHomeRoute, stashRedirect } from "ente-accounts/services/redirect";
-import {
- haveMasterKeyInSession,
- saveMasterKeyInSessionAndSafeStore,
-} from "ente-accounts/services/session-storage";
+import { haveMasterKeyInSession } from "ente-accounts/services/session-storage";
import type { KeyAttributes } from "ente-accounts/services/user";
import {
decryptAndStoreTokenIfNeeded,
diff --git a/web/packages/accounts/services/prelogin-session.ts b/web/packages/accounts/services/prelogin-session.ts
index 3bcefe2723..7678237005 100644
--- a/web/packages/accounts/services/prelogin-session.ts
+++ b/web/packages/accounts/services/prelogin-session.ts
@@ -1,10 +1,11 @@
-import { decryptBox } from "ente-prelogin-wasm";
-import {
- masterKeyFromSession as readMasterKeyFromSession,
- ensureMasterKeyFromSession as readRequiredMasterKeyFromSession,
-} from "./session-storage";
+import { decryptBox, encryptBox, generateKey } from "ente-prelogin-wasm";
+import { createSessionStorage } from "./session-storage";
-export const masterKeyFromSession = () => readMasterKeyFromSession(decryptBox);
-
-export const ensureMasterKeyFromSession = () =>
- readRequiredMasterKeyFromSession(decryptBox);
+export const {
+ ensureMasterKeyFromSession,
+ masterKeyFromSession,
+ saveMasterKeyInSessionAndSafeStore,
+ stashKeyEncryptionKeyInSessionStore,
+ unstashKeyEncryptionKeyFromSession,
+ updateSessionFromElectronSafeStorageIfNeeded,
+} = createSessionStorage({ decryptBox, encryptBox, generateKey });
diff --git a/web/packages/accounts/services/session-storage.ts b/web/packages/accounts/services/session-storage.ts
index d4bfc182b5..43838416ba 100644
--- a/web/packages/accounts/services/session-storage.ts
+++ b/web/packages/accounts/services/session-storage.ts
@@ -1,90 +1,111 @@
import { z } from "zod";
-import { decryptBox, encryptBox, generateKey } from "./crypto";
export const clearSessionStorage = () => sessionStorage.clear();
-const SessionKeyData = z.object({
- encryptedData: z.string(),
- key: z.string(),
- nonce: z.string(),
-});
-
-type SessionKeyData = z.infer<typeof SessionKeyData>;
-
-export type DecryptBox = (
- box: { encryptedData: string; nonce: string },
- key: string,
-) => Promise<string>;
-
-const sessionKeyData = async (keyData: string): Promise<SessionKeyData> => {
- const key = await generateKey();
- const box = await encryptBox(keyData, key);
- return { key, ...box };
-};
-
-export const ensureMasterKeyFromSession = async (decrypt: DecryptBox) => {
- const key = await masterKeyFromSession(decrypt);
- if (!key) throw new Error("Master key not found in session");
- return key;
-};
-
export const haveMasterKeyInSession = () =>
!!sessionStorage.getItem("encryptionKey");
-export const masterKeyFromSession = async (decrypt: DecryptBox) => {
- const value = sessionStorage.getItem("encryptionKey");
- if (!value) return undefined;
-
- const { encryptedData, key, nonce } = SessionKeyData.parse(
- JSON.parse(value),
- );
- return decrypt({ encryptedData, nonce }, key);
-};
-
-export const saveMasterKeyInSessionAndSafeStore = async (masterKey: string) => {
- await saveKeyInSessionStore("encryptionKey", masterKey);
- try {
- await globalThis.electron?.saveMasterKeyInSafeStorage(masterKey);
- } catch {
- // Best effort, matching the current accounts package behaviour.
- }
-};
-
-const saveKeyInSessionStore = async (keyName: string, keyData: string) => {
- sessionStorage.setItem(
- keyName,
- JSON.stringify(await sessionKeyData(keyData)),
- );
-};
-
-export const updateSessionFromElectronSafeStorageIfNeeded = async () => {
- const electron = globalThis.electron;
- if (!electron || haveMasterKeyInSession()) return;
-
- let masterKey: string | undefined;
- try {
- masterKey = await electron.masterKeyFromSafeStorage();
- } catch {
- masterKey = undefined;
- }
-
- if (masterKey) await saveKeyInSessionStore("encryptionKey", masterKey);
-};
-
-export const stashKeyEncryptionKeyInSessionStore = (kek: string) =>
- saveKeyInSessionStore("keyEncryptionKey", kek);
-
export const clearStashedKeyEncryptionKeyFromSession = () =>
sessionStorage.removeItem("keyEncryptionKey");
-export const unstashKeyEncryptionKeyFromSession = async () => {
- const value = sessionStorage.getItem("keyEncryptionKey");
- if (!value) return undefined;
+const SessionKeyData = z.object({
+ encryptedData: z.string(),
+ key: z.string(),
+ nonce: z.string(),
+});
- clearStashedKeyEncryptionKeyFromSession();
+type SessionKeyData = z.infer<typeof SessionKeyData>;
- const { encryptedData, key, nonce } = SessionKeyData.parse(
- JSON.parse(value),
- );
- return decryptBox({ encryptedData, nonce }, key);
+interface SessionStorageCrypto {
+ decryptBox: (
+ box: { encryptedData: string; nonce: string },
+ key: string,
+ ) => Promise<string>;
+ encryptBox: (
+ data: string,
+ key: string,
+ ) => Promise<{ encryptedData: string; nonce: string }>;
+ generateKey: () => Promise<string>;
+}
+
+export const createSessionStorage = ({
+ decryptBox,
+ encryptBox,
+ generateKey,
+}: SessionStorageCrypto) => {
+ const sessionKeyData = async (keyData: string): Promise<SessionKeyData> => {
+ const key = await generateKey();
+ const box = await encryptBox(keyData, key);
+ return { key, ...box };
+ };
+
+ const saveKeyInSessionStore = async (keyName: string, keyData: string) => {
+ sessionStorage.setItem(
+ keyName,
+ JSON.stringify(await sessionKeyData(keyData)),
+ );
+ };
+
+ const masterKeyFromSession = async () => {
+ const value = sessionStorage.getItem("encryptionKey");
+ if (!value) return undefined;
+
+ const { encryptedData, key, nonce } = SessionKeyData.parse(
+ JSON.parse(value),
+ );
+ return decryptBox({ encryptedData, nonce }, key);
+ };
+
+ const ensureMasterKeyFromSession = async () => {
+ const key = await masterKeyFromSession();
+ if (!key) throw new Error("Master key not found in session");
+ return key;
+ };
+
+ const saveMasterKeyInSessionAndSafeStore = async (masterKey: string) => {
+ await saveKeyInSessionStore("encryptionKey", masterKey);
+ try {
+ await globalThis.electron?.saveMasterKeyInSafeStorage(masterKey);
+ } catch {
+ // Best effort, matching the current accounts package behaviour.
+ }
+ };
+
+ const updateSessionFromElectronSafeStorageIfNeeded = async () => {
+ const electron = globalThis.electron;
+ if (!electron || haveMasterKeyInSession()) return;
+
+ let masterKey: string | undefined;
+ try {
+ masterKey = await electron.masterKeyFromSafeStorage();
+ } catch {
+ masterKey = undefined;
+ }
+
+ if (masterKey) await saveKeyInSessionStore("encryptionKey", masterKey);
+ };
+
+ const stashKeyEncryptionKeyInSessionStore = (kek: string) =>
+ saveKeyInSessionStore("keyEncryptionKey", kek);
+
+ const unstashKeyEncryptionKeyFromSession = async () => {
+ const value = sessionStorage.getItem("keyEncryptionKey");
+ if (!value) return undefined;
+
+ clearStashedKeyEncryptionKeyFromSession();
+
+ const { encryptedData, key, nonce } = SessionKeyData.parse(
+ JSON.parse(value),
+ );
+ return decryptBox({ encryptedData, nonce }, key);
+ };
+
+ return {
+ ensureMasterKeyFromSession,
+ masterKeyFromSession,
+ saveMasterKeyInSessionAndSafeStore,
+ stashKeyEncryptionKeyInSessionStore,
+ unstashKeyEncryptionKeyFromSession,
+ updateSessionFromElectronSafeStorageIfNeeded,
+ };
};