From 9d7f5e48df5035dd954085fa0bff9c4e03a7e986 Mon Sep 17 00:00:00 2001 From: sam Date: Tue, 21 Oct 2025 22:06:29 +0700 Subject: [PATCH] fix caching messages insecurely if signer nip44 failed --- src/contexts/DMContext.tsx | 9 ++++++++- src/lib/dmMessageStore.ts | 38 +++++++++++++++++++++----------------- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/contexts/DMContext.tsx b/src/contexts/DMContext.tsx index 69deb8d..1936726 100644 --- a/src/contexts/DMContext.tsx +++ b/src/contexts/DMContext.tsx @@ -1222,8 +1222,9 @@ export function DMProvider({ children, config }: DMProviderProps) { if (!userPubkey) return {}; try { - const dbReadStart = performance.now(); const { readMessagesFromDB } = await import('@/lib/dmMessageStore'); + + const dbReadStart = performance.now(); const cachedStore = await readMessagesFromDB(userPubkey, user?.signer); console.log(`[DM] ⏱️ IndexedDB read + decrypt took ${(performance.now() - dbReadStart).toFixed(0)}ms`); @@ -1521,6 +1522,12 @@ export function DMProvider({ children, config }: DMProviderProps) { // Write to store const writeAllMessagesToStore = useCallback(async () => { if (!userPubkey) return; + + // Don't try to save if user/signer is not available + if (!user?.signer) { + console.log('[DM] Skipping cache write - no signer available'); + return; + } try { const { writeMessagesToDB } = await import('@/lib/dmMessageStore'); diff --git a/src/lib/dmMessageStore.ts b/src/lib/dmMessageStore.ts index d477dae..e22039b 100644 --- a/src/lib/dmMessageStore.ts +++ b/src/lib/dmMessageStore.ts @@ -82,25 +82,29 @@ export async function writeMessagesToDB( try { const db = await openDatabase(); - // If signer is available, encrypt the entire store - if (signer?.nip44) { - const plaintext = JSON.stringify(messageStore); - const encrypted = await signer.nip44.encrypt(userPubkey, plaintext); - - const encryptedStore: EncryptedStore = { - encrypted: true, - data: encrypted, - }; - - await db.put(STORE_NAME, encryptedStore, userPubkey); - console.log('[MessageStore] ✅ Encrypted cache saved'); - } else { - // Fallback: save unencrypted (for backward compatibility) - await db.put(STORE_NAME, messageStore, userPubkey); - console.log('[MessageStore] ⚠️ Unencrypted cache saved (no signer)'); + // Always require encryption - if signer is not available, don't store + if (!signer?.nip44) { + console.warn('[MessageStore] ⚠️ Cannot save: NIP-44 signer not available'); + return; } + + const plaintext = JSON.stringify(messageStore); + const encrypted = await signer.nip44.encrypt(userPubkey, plaintext); + + // Validate that encryption returned a string + if (typeof encrypted !== 'string' || !encrypted) { + throw new Error('NIP-44 encryption failed: expected string result, got ' + typeof encrypted); + } + + const encryptedStore: EncryptedStore = { + encrypted: true, + data: encrypted, + }; + + await db.put(STORE_NAME, encryptedStore, userPubkey); + console.log('[MessageStore] ✅ Encrypted cache saved'); } catch (error) { - console.error('[MessageStore] Error writing to IndexedDB:', error); + console.error('[MessageStore] ❌ Error writing to IndexedDB:', error); throw error; } }