Skip to content

armgit / ente

Public Imported from GitHub
0

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

fix(ensu): reencrypt migrated attachments

Prateek Sunal42e849b768fe
diff --git a/rust/apps/ensu/src-tauri/src/commands.rs b/rust/apps/ensu/src-tauri/src/commands.rs
index 27a915beca..9095acdae1 100644
--- a/rust/apps/ensu/src-tauri/src/commands.rs
+++ b/rust/apps/ensu/src-tauri/src/commands.rs
@@ -1833,6 +1833,31 @@ 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,
@@ -1857,10 +1882,11 @@ 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)
+ 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())
.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 =
@@ -2003,12 +2029,15 @@ 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() && !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() {
+ migrate_attachment_bytes(
+ &source_path,
+ &target_path,
+ &legacy_key,
+ &key,
+ message.session_uuid,
+ )?;
+ migrated_attachments += 1;
expected_attachment_ids.push(attachment.id.clone());
}
}
diff --git a/web/apps/ensu/src/services/chat/store.ts b/web/apps/ensu/src/services/chat/store.ts
index 4b5f78045e..71bb91853f 100644
--- a/web/apps/ensu/src/services/chat/store.ts
+++ b/web/apps/ensu/src/services/chat/store.ts
@@ -2,11 +2,7 @@ import { getKV, removeKV, setKV } from "ente-base/kv";
import log from "ente-base/log";
import { deleteDB, openDB, type DBSchema, type IDBPDatabase } from "idb";
import { base64ToBytes } from "services/base64";
-import {
- decryptAttachmentBytes,
- deriveAttachmentKeyB64,
- encryptAttachmentBytes,
-} from "./attachments";
+import { decryptAttachmentBytes, encryptAttachmentBytes } from "./attachments";
import {
allLegacyKeyCandidates,
cachedLocalChatKey,
@@ -19,20 +15,6 @@ import {
encryptChatPayload,
} from "./crypto";
-/** SHA-256 fingerprint (first 4 bytes, hex) for safe logging. */
-const shortHash = async (value: string) =>
- Array.from(
- new Uint8Array(
- await crypto.subtle.digest(
- "SHA-256",
- new TextEncoder().encode(value),
- ),
- ),
- )
- .slice(0, 4)
- .map((b) => b.toString(16).padStart(2, "0"))
- .join("");
-
const STORAGE_KEY = "ensu.chat.store.v1";
const NATIVE_MIGRATION_KEY = "ensu.chat.nativeMigration.v2";
const CHAT_DB_NAME = "ensu-chat";
@@ -863,12 +845,6 @@ const migrateLegacyNativeChatStoreToV2 = async (chatKey: string) => {
candidateKeys.push(chatKey);
}
- log.info("Migration keys", {
- chatKeyHash: await shortHash(chatKey),
- candidateCount: candidateKeys.length,
- sameKey: candidateKeys.length === 1 && candidateKeys[0] === chatKey,
- });
-
for (const candidateKey of candidateKeys) {
try {
const result = await invokeChat<NativeLegacyMigrationResult>(
@@ -876,12 +852,7 @@ const migrateLegacyNativeChatStoreToV2 = async (chatKey: string) => {
{ input: { keyB64: chatKey, legacyKeyB64: candidateKey } },
);
if (result.didMigrate) {
- log.info("Migrated legacy native chat store to v2 DB", {
- ...result,
- chatKeyHash: await shortHash(chatKey),
- legacyKeyHash: await shortHash(candidateKey),
- keyMatch: chatKey === candidateKey,
- });
+ log.info("Migrated legacy native chat store to v2 DB", result);
}
return;
} catch (error) {
@@ -1504,14 +1475,6 @@ export const storeEncryptedAttachmentBytes = async (
chatKey: string,
sessionUuid: string,
) => {
- const dk = await deriveAttachmentKeyB64(chatKey, sessionUuid);
- const dkHash = await shortHash(dk);
- log.info("Encrypting attachment", {
- id: id.slice(0, 8),
- session: sessionUuid.slice(0, 8),
- plainBytes: data.length,
- dkHash,
- });
const encrypted = await encryptAttachmentBytes(data, chatKey, sessionUuid);
await writeAttachmentBytes(id, encrypted);
};
@@ -1539,22 +1502,9 @@ export const readDecryptedAttachmentBytes = async (
sessionUuid: string,
): Promise<Uint8Array> => {
const encrypted = await readAttachmentBytes(id);
- const dk = await deriveAttachmentKeyB64(chatKey, sessionUuid);
- const dkHash = await shortHash(dk);
- log.info("Decrypting attachment", {
- id: id.slice(0, 8),
- session: sessionUuid.slice(0, 8),
- bytes: encrypted.length,
- dkHash,
- });
try {
return await decryptAttachmentBytes(encrypted, chatKey, sessionUuid);
} catch (error) {
- log.warn("Attachment decryption failed", {
- id: id.slice(0, 8),
- dkHash,
- error: String(error),
- });
const localKey = cachedLocalChatKey() ?? legacyLocalChatKey();
if (!localKey || localKey === chatKey) {
throw error;