mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2026-12-03 08:07:21 +00:00
fix caching messages insecurely if signer nip44 failed
This commit is contained in:
1 parent
dbacdbc8ac
commit
9d7f5e48df
2 files changed
+29
-18
No files matched your search
@@ -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');
|
||||
|
||||
+21
-17
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user