Skip to content

armgit / ente

Public Imported from GitHub
0

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

Scope Ensu chat key cache by account

Prateek Sunalaa88f6167e8b
diff --git a/web/apps/ensu/src/pages/_app.tsx b/web/apps/ensu/src/pages/_app.tsx
index bf75ece95a..12f8bdbff9 100644
--- a/web/apps/ensu/src/pages/_app.tsx
+++ b/web/apps/ensu/src/pages/_app.tsx
@@ -1,7 +1,6 @@
import { CssBaseline } from "@mui/material";
import { ThemeProvider } from "@mui/material/styles";
import { savedLocalUser } from "ente-accounts/services/accounts-db";
-import { accountLogout } from "ente-accounts/services/logout";
import { staticAppTitle } from "ente-base/app";
import { CustomHead } from "ente-base/components/Head";
import {
@@ -21,6 +20,7 @@ import { logStartupBanner } from "ente-base/log-web";
import "katex/dist/katex.min.css";
import type { AppProps } from "next/app";
import React, { useCallback, useEffect, useMemo } from "react";
+import { ensuLogout } from "services/logout";
const App: React.FC<AppProps> = ({ Component, pageProps }) => {
useSetupLogs();
@@ -68,7 +68,7 @@ const App: React.FC<AppProps> = ({ Component, pageProps }) => {
}, []);
const logout = useCallback(() => {
- void accountLogout().then(() => window.location.replace("/"));
+ void ensuLogout();
}, []);
const baseContext = useMemo(
diff --git a/web/apps/ensu/src/services/chat/chatKey.ts b/web/apps/ensu/src/services/chat/chatKey.ts
index ed77eb1e60..e0c0e56229 100644
--- a/web/apps/ensu/src/services/chat/chatKey.ts
+++ b/web/apps/ensu/src/services/chat/chatKey.ts
@@ -1,4 +1,5 @@
import { HTTPError } from "ente-base/http";
+import { savedLocalUser } from "ente-accounts/services/accounts-db";
import {
isTauriAppRuntime,
secureStorageDelete,
@@ -18,6 +19,7 @@ type PersistedChatKeys = { remoteChatKey?: string; localChatKey?: string };
let _persistedChatKeys: PersistedChatKeys = {};
let _chatKeyStoreInitPromise: Promise<void> | undefined;
+let _chatKeyStoreInitUserID: number | undefined;
const readLocalStorageKey = (key: string) => {
if (typeof localStorage === "undefined") return undefined;
@@ -34,6 +36,29 @@ const removeLocalStorageKey = (key: string) => {
localStorage.removeItem(key);
};
+const scopedRemoteChatKeyLocalStorageKey = () => {
+ const userID = savedLocalUser()?.id;
+ return userID ? `${CHAT_KEY_LOCAL_STORAGE_KEY}.${userID}` : undefined;
+};
+
+const scopedRemoteChatKeySecureStorageKey = () => {
+ const userID = savedLocalUser()?.id;
+ return userID ? `${REMOTE_CHAT_KEY_SECURE_STORAGE_KEY}.${userID}` : undefined;
+};
+
+const clearLegacyRemoteChatKeyLocalStorage = () =>
+ removeLocalStorageKey(CHAT_KEY_LOCAL_STORAGE_KEY);
+
+const clearScopedRemoteChatKeyLocalStorage = () => {
+ if (typeof localStorage === "undefined") return;
+ const keys = [];
+ for (let i = 0; i < localStorage.length; i += 1) {
+ const key = localStorage.key(i);
+ if (key?.startsWith(`${CHAT_KEY_LOCAL_STORAGE_KEY}.`)) keys.push(key);
+ }
+ for (const key of keys) localStorage.removeItem(key);
+};
+
const nativeChatKeyPath = async () => {
const { appDataDir, join } = await import("@tauri-apps/api/path");
const root = await appDataDir();
@@ -71,18 +96,21 @@ const readNativeChatKeys = async (): Promise<PersistedChatKeys> => {
const persistNativeChatKeys = async () => {
if (!isTauriAppRuntime()) return;
- const operations: Promise<void>[] = [];
- if (_persistedChatKeys.remoteChatKey) {
- operations.push(
- secureStorageSet(
- REMOTE_CHAT_KEY_SECURE_STORAGE_KEY,
- _persistedChatKeys.remoteChatKey,
- ),
- );
- } else {
- operations.push(
- secureStorageDelete(REMOTE_CHAT_KEY_SECURE_STORAGE_KEY),
- );
+ const operations: Promise<void>[] = [
+ secureStorageDelete(REMOTE_CHAT_KEY_SECURE_STORAGE_KEY),
+ ];
+ const remoteStorageKey = scopedRemoteChatKeySecureStorageKey();
+ if (remoteStorageKey) {
+ if (_persistedChatKeys.remoteChatKey) {
+ operations.push(
+ secureStorageSet(
+ remoteStorageKey,
+ _persistedChatKeys.remoteChatKey,
+ ),
+ );
+ } else {
+ operations.push(secureStorageDelete(remoteStorageKey));
+ }
}
if (_persistedChatKeys.localChatKey) {
@@ -108,12 +136,20 @@ const persistNativeChatKeysSoon = () => {
export const initChatKeyStore = async () => {
if (!isTauriAppRuntime()) return;
- _chatKeyStoreInitPromise ??= (async () => {
+ const userID = savedLocalUser()?.id;
+ if (_chatKeyStoreInitPromise && _chatKeyStoreInitUserID === userID) {
+ return _chatKeyStoreInitPromise;
+ }
+
+ _chatKeyStoreInitUserID = userID;
+ _chatKeyStoreInitPromise = (async () => {
+ const remoteStorageKey = scopedRemoteChatKeySecureStorageKey();
+ const remoteLocalStorageKey = scopedRemoteChatKeyLocalStorageKey();
const [secureRemoteChatKey, secureLocalChatKey, nativeKeys] =
await Promise.all([
- secureStorageGet(REMOTE_CHAT_KEY_SECURE_STORAGE_KEY).catch(
- () => undefined,
- ),
+ remoteStorageKey
+ ? secureStorageGet(remoteStorageKey).catch(() => undefined)
+ : Promise.resolve(undefined),
secureStorageGet(LOCAL_CHAT_KEY_SECURE_STORAGE_KEY).catch(
() => undefined,
),
@@ -121,8 +157,9 @@ export const initChatKeyStore = async () => {
]);
const remoteChatKey =
secureRemoteChatKey ??
- nativeKeys.remoteChatKey ??
- readLocalStorageKey(CHAT_KEY_LOCAL_STORAGE_KEY);
+ (remoteLocalStorageKey
+ ? readLocalStorageKey(remoteLocalStorageKey)
+ : undefined);
const localChatKey =
secureLocalChatKey ??
nativeKeys.localChatKey ??
@@ -130,19 +167,28 @@ export const initChatKeyStore = async () => {
_persistedChatKeys = { remoteChatKey, localChatKey };
+ try {
+ await secureStorageDelete(REMOTE_CHAT_KEY_SECURE_STORAGE_KEY);
+ } catch {
+ // Ignore legacy secure storage cleanup failures.
+ }
+
if (
secureRemoteChatKey !== remoteChatKey ||
secureLocalChatKey !== localChatKey
) {
try {
await persistNativeChatKeys();
- removeLocalStorageKey(CHAT_KEY_LOCAL_STORAGE_KEY);
+ clearLegacyRemoteChatKeyLocalStorage();
+ clearScopedRemoteChatKeyLocalStorage();
removeLocalStorageKey(LOCAL_CHAT_KEY_LOCAL_STORAGE_KEY);
} catch {
- // Keep legacy localStorage keys when secure storage is unavailable.
+ clearLegacyRemoteChatKeyLocalStorage();
+ // Keep scoped localStorage keys when secure storage is unavailable.
}
} else {
- removeLocalStorageKey(CHAT_KEY_LOCAL_STORAGE_KEY);
+ clearLegacyRemoteChatKeyLocalStorage();
+ clearScopedRemoteChatKeyLocalStorage();
removeLocalStorageKey(LOCAL_CHAT_KEY_LOCAL_STORAGE_KEY);
}
@@ -168,7 +214,10 @@ const setCachedChatKey = (chatKey: string) => {
return;
}
- writeLocalStorageKey(CHAT_KEY_LOCAL_STORAGE_KEY, chatKey);
+ clearLegacyRemoteChatKeyLocalStorage();
+ const storageKey = scopedRemoteChatKeyLocalStorageKey();
+ if (!storageKey) return;
+ writeLocalStorageKey(storageKey, chatKey);
};
const setCachedLocalChatKey = (chatKey: string) => {
@@ -189,7 +238,9 @@ export const cachedChatKey = (): string | undefined => {
return _persistedChatKeys.remoteChatKey;
}
- return readLocalStorageKey(CHAT_KEY_LOCAL_STORAGE_KEY);
+ clearLegacyRemoteChatKeyLocalStorage();
+ const storageKey = scopedRemoteChatKeyLocalStorageKey();
+ return storageKey ? readLocalStorageKey(storageKey) : undefined;
};
/**
@@ -207,10 +258,10 @@ export const clearCachedChatKey = () => {
if (isTauriAppRuntime()) {
delete _persistedChatKeys.remoteChatKey;
persistNativeChatKeysSoon();
- return;
}
- removeLocalStorageKey(CHAT_KEY_LOCAL_STORAGE_KEY);
+ clearLegacyRemoteChatKeyLocalStorage();
+ clearScopedRemoteChatKeyLocalStorage();
};
export const clearLocalChatKey = () => {
diff --git a/web/apps/ensu/src/services/logout.ts b/web/apps/ensu/src/services/logout.ts
new file mode 100644
index 0000000000..b5c27d12c8
--- /dev/null
+++ b/web/apps/ensu/src/services/logout.ts
@@ -0,0 +1,29 @@
+import { invoke } from "@tauri-apps/api/core";
+import { accountLogout } from "ente-accounts/services/logout";
+import log from "ente-base/log";
+
+const isTauriRuntime = () =>
+ typeof window !== "undefined" &&
+ ("__TAURI__" in window || "__TAURI_IPC__" in window);
+
+/**
+ * Logout sequence for the Ensu app.
+ *
+ * This function is guaranteed not to throw any errors.
+ */
+export const ensuLogout = async () => {
+ const ignoreError = (label: string, e: unknown) =>
+ log.error(`Ignoring error during logout (${label})`, e);
+
+ await accountLogout();
+
+ if (isTauriRuntime()) {
+ try {
+ await invoke("chat_db_reset");
+ } catch (e) {
+ ignoreError("Tauri chat DB", e);
+ }
+ }
+
+ window.location.replace("/");
+};