diff --git a/v3/@claude-flow/cli/__tests__/memory-backup-wasm-restore.test.ts b/v3/@claude-flow/cli/__tests__/memory-backup-wasm-restore.test.ts new file mode 100644 index 0000000000..b0c4b97570 --- /dev/null +++ b/v3/@claude-flow/cli/__tests__/memory-backup-wasm-restore.test.ts @@ -0,0 +1,38 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; +import { join } from 'node:path'; +import { tmpdir } from 'node:os'; +import initSqlJs from 'sql.js'; +vi.mock('better-sqlite3', () => { throw new Error('native backend unavailable'); }); +import { restoreMemoryDbFromBackup } from '../src/services/memory-backup.js'; + +let dir: string; +let dbPath: string; +beforeEach(() => { + dir = mkdtempSync(join(tmpdir(), 'memory-wasm-restore-')); + dbPath = join(dir, 'memory.db'); + mkdirSync(join(dir, 'backups')); + writeFileSync(dbPath, 'live store'); +}); +afterEach(() => { rmSync(dir, { recursive: true, force: true }); }); + +describe('backup recovery without native SQLite', () => { + it('validates and selects the older readable snapshot instead of trusting the newest bytes', async () => { + const SQL = await initSqlJs(); + const db = new SQL.Database(); + db.run("CREATE TABLE memory_entries (content TEXT); INSERT INTO memory_entries VALUES ('retained')"); + const valid = Buffer.from(db.export()); + db.close(); + const clean = join(dir, 'backups', 'memory-2023-11-14T22-13-20-000Z.db'); + writeFileSync(clean, valid); + writeFileSync(join(dir, 'backups', 'memory-2023-11-14T22-13-21-000Z.db'), 'invalid SQLite'); + expect(await restoreMemoryDbFromBackup(dbPath)).toMatchObject({ restored: true, rows: 1, from: clean }); + expect(readFileSync(dbPath)).toEqual(valid); + }); + + it('refuses to replace the live store with an unverified nonempty file', async () => { + writeFileSync(join(dir, 'backups', 'memory-2023-11-14T22-13-20-000Z.db'), 'invalid SQLite'); + expect(await restoreMemoryDbFromBackup(dbPath)).toMatchObject({ restored: false }); + expect(readFileSync(dbPath, 'utf8')).toBe('live store'); + }); +}); diff --git a/v3/@claude-flow/cli/__tests__/memory-encrypted-backup-restore.test.ts b/v3/@claude-flow/cli/__tests__/memory-encrypted-backup-restore.test.ts new file mode 100644 index 0000000000..dddcb7af6e --- /dev/null +++ b/v3/@claude-flow/cli/__tests__/memory-encrypted-backup-restore.test.ts @@ -0,0 +1,59 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; +import { join } from 'node:path'; +import { tmpdir } from 'node:os'; +import Database from 'better-sqlite3'; +import { backupMemoryDb, restoreMemoryDbFromBackup } from '../src/services/memory-backup.js'; +import { encryptBuffer } from '../src/encryption/vault.js'; + +let dir: string; +let dbPath: string; +let plain: Buffer; +const key = Buffer.alloc(32, 7); +const start = 1_700_000_000_000; +beforeEach(() => { + dir = mkdtempSync(join(tmpdir(), 'memory-encrypted-restore-')); + dbPath = join(dir, 'memory.db'); + const db = new Database(dbPath); + db.exec("CREATE TABLE memory_entries (content TEXT); INSERT INTO memory_entries VALUES ('retained memory')"); + db.close(); + plain = readFileSync(dbPath); + vi.stubEnv('CLAUDE_FLOW_ENCRYPTION_KEY', key.toString('hex')); +}); +afterEach(() => { vi.unstubAllEnvs(); rmSync(dir, { recursive: true, force: true }); }); + +describe('encrypted backup recovery with native SQLite present', () => { + it('restores a validated encrypted snapshot without stripping encryption', async () => { + const encrypted = encryptBuffer(plain, key); + writeFileSync(dbPath, encrypted); + const snapshot = await backupMemoryDb({ dbPath, timestamp: start }); + expect(snapshot.backedUp).toBe(true); + writeFileSync(dbPath, 'damaged live store'); + const restored = await restoreMemoryDbFromBackup(dbPath); + expect(restored).toMatchObject({ restored: true, rows: 1, from: snapshot.path }); + expect(readFileSync(dbPath)).toEqual(encrypted); + expect(readFileSync(restored.corruptBackupPath!, 'utf8')).toBe('damaged live store'); + }); + + it('skips a newer encrypted snapshot with a bad authentication tag', async () => { + const encrypted = encryptBuffer(plain, key); + writeFileSync(dbPath, encrypted); + const clean = await backupMemoryDb({ dbPath, timestamp: start }); + const damaged = Buffer.from(encrypted); + damaged[damaged.length - 1] ^= 1; + writeFileSync(dbPath, damaged); + await backupMemoryDb({ dbPath, timestamp: start + 1000 }); + writeFileSync(dbPath, 'damaged live store'); + expect(await restoreMemoryDbFromBackup(dbPath)).toMatchObject({ restored: true, from: clean.path }); + expect(readFileSync(dbPath)).toEqual(encrypted); + }); + + it('leaves the live store untouched when no snapshot can be authenticated', async () => { + writeFileSync(dbPath, encryptBuffer(plain, key)); + await backupMemoryDb({ dbPath, timestamp: start }); + writeFileSync(dbPath, 'damaged live store'); + vi.stubEnv('CLAUDE_FLOW_ENCRYPTION_KEY', Buffer.alloc(32, 8).toString('hex')); + expect(await restoreMemoryDbFromBackup(dbPath)).toMatchObject({ restored: false }); + expect(readFileSync(dbPath, 'utf8')).toBe('damaged live store'); + }); +}); diff --git a/v3/@claude-flow/cli/src/services/memory-backup.ts b/v3/@claude-flow/cli/src/services/memory-backup.ts index 8158ccb1c8..06e584ad72 100644 --- a/v3/@claude-flow/cli/src/services/memory-backup.ts +++ b/v3/@claude-flow/cli/src/services/memory-backup.ts @@ -14,6 +14,7 @@ */ import * as fs from 'fs'; import * as path from 'path'; +import { readFileMaybeEncrypted } from '../fs-secure.js'; export interface BackupOptions { /** Source DB (default: /.swarm/memory.db). */ @@ -166,13 +167,46 @@ export interface RestoreResult { restored: boolean; /** The backup file that was restored. */ from?: string; - /** memory_entries count in the restored DB (-1 if it couldn't be verified). */ + /** Verified memory_entries count in the restored DB. */ rows?: number; /** Where the corrupt live DB was parked before the swap. */ corruptBackupPath?: string; skipped?: string; } +/** Verify a snapshot without modifying it, including RFE1 images and WASM-only hosts. */ +async function verifiedBackupRows(file: string, Database: any): Promise { + if (Database) { + let db: any; + try { + db = new Database(file, { readonly: true }); + if (String(db.pragma('integrity_check', { simple: true })).toLowerCase() !== 'ok') return null; + const rows = Number(db.prepare('SELECT COUNT(*) AS c FROM memory_entries').get()?.c ?? 0); + return rows > 0 ? rows : null; + } catch { + // Native SQLite cannot open encrypted RFE1 images. Verify the decrypted + // bytes below; a failed native open is never evidence of a good backup. + } finally { + try { db?.close(); } catch { /* best effort */ } + } + } + + let db: any; + try { + const initSqlJs = (await import('sql.js')).default; + const SQL = await initSqlJs(); + db = new SQL.Database(readFileMaybeEncrypted(file, null)); + const integrity = db.exec('PRAGMA integrity_check')[0]?.values?.[0]?.[0]; + const rows = Number(db.exec('SELECT COUNT(*) FROM memory_entries')[0]?.values?.[0]?.[0] ?? 0); + return String(integrity).toLowerCase() === 'ok' && rows > 0 ? rows : null; + } catch { + // No verifier, wrong key, failed authentication, or invalid SQLite image. + return null; + } finally { + try { db?.close(); } catch { /* best effort */ } + } +} + /** * Restore the newest integrity-ok backup over a corrupt/malformed memory DB * (issue #2584). @@ -207,33 +241,20 @@ export async function restoreMemoryDbFromBackup( } if (!snaps.length) return { restored: false, skipped: 'no-backups' }; - // better-sqlite3 verifies a candidate's integrity. Absent (WASM-only host) → - // accept the newest non-empty snapshot, flagged rows=-1 (unverified). + // Prefer native verification for plaintext snapshots. sql.js verifies + // encrypted snapshots and provides the same checks on WASM-only hosts. let Database: any = null; try { const mod: string = 'better-sqlite3'; Database = (await import(mod)).default; } catch { - /* verifier unavailable — trust newest non-empty */ + /* use the sql.js verifier */ } let chosen: { file: string; rows: number } | null = null; for (const file of snaps) { - try { - if (Database) { - const db = new Database(file, { readonly: true }); - const integ = String(db.pragma('integrity_check', { simple: true }) ?? ''); - let rows = 0; - try { - rows = (db.prepare('SELECT COUNT(*) AS c FROM memory_entries').get() as { c: number })?.c ?? 0; - } catch { /* no entries table — not a usable memory DB */ } - db.close(); - if (integ.toLowerCase() === 'ok' && rows > 0) { chosen = { file, rows }; break; } - } else if (fs.statSync(file).size > 0) { - chosen = { file, rows: -1 }; - break; - } - } catch { /* unreadable snapshot — try the next-older one */ } + const rows = await verifiedBackupRows(file, Database); + if (rows !== null) { chosen = { file, rows }; break; } } if (!chosen) return { restored: false, skipped: 'no-integrity-ok-backup' };