Skip to content

armgit / ente

Public Imported from GitHub
0

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

fix(ensu): preserve legacy attachment key

Prateek Sunal798d94f00dc1
diff --git a/rust/apps/ensu/src-tauri/src/commands.rs b/rust/apps/ensu/src-tauri/src/commands.rs
index d5719e44c8..27a915beca 100644
--- a/rust/apps/ensu/src-tauri/src/commands.rs
+++ b/rust/apps/ensu/src-tauri/src/commands.rs
@@ -1833,25 +1833,6 @@ fn verify_migrated_chat_db(
Ok(())
}
-fn migrate_attachment_bytes(
- source_path: &Path,
- target_path: &Path,
- legacy_key: &[u8],
- target_key: &[u8],
- session_uuid: Uuid,
-) -> Result<(), ApiError> {
- let legacy_ciphertext =
- fs::read(source_path).map_err(|err| ApiError::new("io", err.to_string()))?;
- let plaintext =
- chat_sync::crypto::decrypt_attachment_bytes(&legacy_ciphertext, legacy_key, session_uuid)
- .map_err(|err| ApiError::new("db_crypto", err.to_string()))?;
- let ciphertext =
- chat_sync::crypto::encrypt_attachment_bytes(&plaintext, target_key, session_uuid)
- .map_err(|err| ApiError::new("db_crypto", err.to_string()))?;
- fs::write(target_path, ciphertext).map_err(|err| ApiError::new("io", err.to_string()))?;
- Ok(())
-}
-
fn migrate_legacy_chat_db(
app: &AppHandle,
input: &ChatDbMigrateLegacyInput,
@@ -1876,11 +1857,10 @@ fn migrate_legacy_chat_db(
let key = core_crypto::decode_b64(&input.key_b64).map_err(ApiError::from)?;
let legacy_db =
- EnsuDb::open_sqlite_with_defaults(&legacy_db_path, &legacy_sync_path, legacy_key.clone())
- .map_err(ApiError::from)?;
- let target_db =
- EnsuDb::open_sqlite_with_defaults(&target_db_path, &target_sync_path, key.clone())
+ EnsuDb::open_sqlite_with_defaults(&legacy_db_path, &legacy_sync_path, legacy_key)
.map_err(ApiError::from)?;
+ let target_db = EnsuDb::open_sqlite_with_defaults(&target_db_path, &target_sync_path, key)
+ .map_err(ApiError::from)?;
let legacy_sync_state =
SyncStateDb::open_sqlite_with_defaults(&legacy_sync_path).map_err(ApiError::from)?;
let target_sync_state =
@@ -2023,15 +2003,12 @@ fn migrate_legacy_chat_db(
let source_path = legacy_attachments_dir.join(&attachment.id);
let target_path = target_attachments_dir.join(&attachment.id);
- if source_path.exists() {
- migrate_attachment_bytes(
- &source_path,
- &target_path,
- &legacy_key,
- &key,
- message.session_uuid,
- )?;
+ if source_path.exists() && !target_path.exists() {
+ fs::copy(&source_path, &target_path)
+ .map_err(|err| ApiError::new("io", err.to_string()))?;
migrated_attachments += 1;
+ }
+ if source_path.exists() {
expected_attachment_ids.push(attachment.id.clone());
}
}
diff --git a/web/apps/ensu/src/services/chat/chatKey.ts b/web/apps/ensu/src/services/chat/chatKey.ts
index 6257c761ff..437944b107 100644
--- a/web/apps/ensu/src/services/chat/chatKey.ts
+++ b/web/apps/ensu/src/services/chat/chatKey.ts
@@ -15,6 +15,7 @@ const LOCAL_CHAT_KEY_LOCAL_STORAGE_KEY = "ensu.chatKey.local";
const CHAT_KEY_FILE_NAME = "chat-keys.json";
const REMOTE_CHAT_KEY_SECURE_STORAGE_KEY = "remoteChatKey.v2";
const LOCAL_CHAT_KEY_SECURE_STORAGE_KEY = "localChatKey.v2";
+const LEGACY_ATTACHMENT_KEY_SECURE_STORAGE_KEY = "legacyAttachmentKey.v2";
const LEGACY_REMOTE_CHAT_KEY_SECURE_STORAGE_KEY = "remoteChatKey";
const LEGACY_LOCAL_CHAT_KEY_SECURE_STORAGE_KEY = "localChatKey";
@@ -22,6 +23,7 @@ type PersistedChatKeys = { remoteChatKey?: string; localChatKey?: string };
let _persistedChatKeys: PersistedChatKeys = {};
let _legacyPersistedChatKeys: PersistedChatKeys = {};
+let _legacyAttachmentChatKey: string | undefined;
/** All distinct keys discovered during init, before any cleanup. */
let _allDiscoveredLocalChatKeys: string[] = [];
let _chatKeyStoreInitPromise: Promise<void> | undefined;
@@ -177,6 +179,7 @@ export const initChatKeyStore = async () => {
const [
secureRemoteChatKey,
secureLocalChatKey,
+ secureLegacyAttachmentKey,
legacySecureRemoteChatKey,
legacySecureLocalChatKey,
nativeKeys,
@@ -187,6 +190,9 @@ export const initChatKeyStore = async () => {
secureStorageGet(LOCAL_CHAT_KEY_SECURE_STORAGE_KEY).catch(
() => undefined,
),
+ secureStorageGet(LEGACY_ATTACHMENT_KEY_SECURE_STORAGE_KEY).catch(
+ () => undefined,
+ ),
legacyRemoteStorageKey
? secureStorageGet(legacyRemoteStorageKey).catch(
() => undefined,
@@ -219,6 +225,7 @@ export const initChatKeyStore = async () => {
remoteChatKey: legacyRemoteChatKey,
localChatKey: legacyLocalChatKey,
};
+ _legacyAttachmentChatKey = secureLegacyAttachmentKey;
// Capture every distinct local chat key found across all sources
// *before* cleanup deletes legacy copies. The migration needs all
@@ -365,6 +372,22 @@ export const allLegacyKeyCandidates = (): string[] => {
return keys;
};
+export const legacyAttachmentChatKey = (): string | undefined =>
+ _legacyAttachmentChatKey;
+
+export const setLegacyAttachmentChatKey = async (chatKey?: string) => {
+ _legacyAttachmentChatKey = chatKey;
+ if (!isTauriAppRuntime()) return;
+ if (chatKey) {
+ await secureStorageSet(
+ LEGACY_ATTACHMENT_KEY_SECURE_STORAGE_KEY,
+ chatKey,
+ );
+ } else {
+ await secureStorageDelete(LEGACY_ATTACHMENT_KEY_SECURE_STORAGE_KEY);
+ }
+};
+
export const clearCachedChatKey = () => {
if (isTauriAppRuntime()) {
delete _persistedChatKeys.remoteChatKey;
diff --git a/web/apps/ensu/src/services/chat/store.ts b/web/apps/ensu/src/services/chat/store.ts
index 71bb91853f..d9f1079a39 100644
--- a/web/apps/ensu/src/services/chat/store.ts
+++ b/web/apps/ensu/src/services/chat/store.ts
@@ -6,7 +6,9 @@ import { decryptAttachmentBytes, encryptAttachmentBytes } from "./attachments";
import {
allLegacyKeyCandidates,
cachedLocalChatKey,
+ legacyAttachmentChatKey,
legacyLocalChatKey,
+ setLegacyAttachmentChatKey,
} from "./chatKey";
import {
decryptChatField,
@@ -852,6 +854,9 @@ const migrateLegacyNativeChatStoreToV2 = async (chatKey: string) => {
{ input: { keyB64: chatKey, legacyKeyB64: candidateKey } },
);
if (result.didMigrate) {
+ await setLegacyAttachmentChatKey(
+ candidateKey === chatKey ? undefined : candidateKey,
+ );
log.info("Migrated legacy native chat store to v2 DB", result);
}
return;
@@ -1505,11 +1510,23 @@ export const readDecryptedAttachmentBytes = async (
try {
return await decryptAttachmentBytes(encrypted, chatKey, sessionUuid);
} catch (error) {
- const localKey = cachedLocalChatKey() ?? legacyLocalChatKey();
- if (!localKey || localKey === chatKey) {
- throw error;
+ for (const fallbackKey of [
+ legacyAttachmentChatKey(),
+ cachedLocalChatKey(),
+ legacyLocalChatKey(),
+ ]) {
+ if (!fallbackKey || fallbackKey === chatKey) continue;
+ try {
+ return await decryptAttachmentBytes(
+ encrypted,
+ fallbackKey,
+ sessionUuid,
+ );
+ } catch {
+ // Try the next known legacy key.
+ }
}
- return decryptAttachmentBytes(encrypted, localKey, sessionUuid);
+ throw error;
}
};