mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2026-12-06 00:37:23 +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 {};
|
if (!userPubkey) return {};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const dbReadStart = performance.now();
|
|
||||||
const { readMessagesFromDB } = await import('@/lib/dmMessageStore');
|
const { readMessagesFromDB } = await import('@/lib/dmMessageStore');
|
||||||
|
|
||||||
|
const dbReadStart = performance.now();
|
||||||
const cachedStore = await readMessagesFromDB(userPubkey, user?.signer);
|
const cachedStore = await readMessagesFromDB(userPubkey, user?.signer);
|
||||||
console.log(`[DM] ⏱️ IndexedDB read + decrypt took ${(performance.now() - dbReadStart).toFixed(0)}ms`);
|
console.log(`[DM] ⏱️ IndexedDB read + decrypt took ${(performance.now() - dbReadStart).toFixed(0)}ms`);
|
||||||
|
|
||||||
@@ -1522,6 +1523,12 @@ export function DMProvider({ children, config }: DMProviderProps) {
|
|||||||
const writeAllMessagesToStore = useCallback(async () => {
|
const writeAllMessagesToStore = useCallback(async () => {
|
||||||
if (!userPubkey) return;
|
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 {
|
try {
|
||||||
const { writeMessagesToDB } = await import('@/lib/dmMessageStore');
|
const { writeMessagesToDB } = await import('@/lib/dmMessageStore');
|
||||||
|
|
||||||
|
|||||||
+21
-17
@@ -82,25 +82,29 @@ export async function writeMessagesToDB(
|
|||||||
try {
|
try {
|
||||||
const db = await openDatabase();
|
const db = await openDatabase();
|
||||||
|
|
||||||
// If signer is available, encrypt the entire store
|
// Always require encryption - if signer is not available, don't store
|
||||||
if (signer?.nip44) {
|
if (!signer?.nip44) {
|
||||||
const plaintext = JSON.stringify(messageStore);
|
console.warn('[MessageStore] ⚠️ Cannot save: NIP-44 signer not available');
|
||||||
const encrypted = await signer.nip44.encrypt(userPubkey, plaintext);
|
return;
|
||||||
|
|
||||||
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)');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
} catch (error) {
|
||||||
console.error('[MessageStore] Error writing to IndexedDB:', error);
|
console.error('[MessageStore] ❌ Error writing to IndexedDB:', error);
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user