From 38a673a73ea84e08cef8ee212c88a367496fe3ff Mon Sep 17 00:00:00 2001 From: Rudy Celekli Date: Sat, 26 Sep 2026 19:33:40 -0400 Subject: [PATCH 1/2] fix(memory): validate encrypted backup images before recovery --- .../memory-backup-wasm-restore.test.ts | 38 ++++++++++++ .../memory-encrypted-backup-restore.test.ts | 59 +++++++++++++++++++ .../cli/src/services/memory-backup.ts | 59 +++++++++++++------ 3 files changed, 137 insertions(+), 19 deletions(-) create mode 100644 v3/@claude-flow/cli/__tests__/memory-backup-wasm-restore.test.ts create mode 100644 v3/@claude-flow/cli/__tests__/memory-encrypted-backup-restore.test.ts 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 d7093c42ef..37a0e4e887 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). */ @@ -151,13 +152,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). @@ -192,33 +226,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' }; From 35eb223b154e505ca84ee1c30a878951c0ca4fa7 Mon Sep 17 00:00:00 2001 From: Rudy Celekli Date: Sat, 26 Sep 2026 21:27:40 -0400 Subject: [PATCH 2/2] test(doctor): keep native binding mock consistent across parallel probes --- .../doctor-native-binding-2968.test.ts | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/v3/@claude-flow/cli/__tests__/doctor-native-binding-2968.test.ts b/v3/@claude-flow/cli/__tests__/doctor-native-binding-2968.test.ts index add05f3538..a52e37ec27 100644 --- a/v3/@claude-flow/cli/__tests__/doctor-native-binding-2968.test.ts +++ b/v3/@claude-flow/cli/__tests__/doctor-native-binding-2968.test.ts @@ -8,13 +8,10 @@ import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; -vi.mock('better-sqlite3', () => ({ - default: class MissingNativeBinding { - constructor() { - throw new Error('Could not locate the bindings file. Tried:\n → better_sqlite3.node'); - } - }, -})); +// Automatic module mocks remain consistent across concurrent dynamic imports; +// a manual factory can be bypassed while doctor's parallel probes resolve it. +vi.mock('better-sqlite3'); +import Database from 'better-sqlite3'; import { doctorCommand } from '../src/commands/doctor.js'; import { _resetMemoryRootCache } from '../src/memory/memory-initializer.js'; @@ -40,6 +37,9 @@ function findCheck(results: HealthCheck[], name: string): HealthCheck | undefine describe('doctor #2968 — missing better-sqlite3 native binding', () => { beforeEach(() => { + vi.mocked(Database).mockImplementation(function MissingNativeBinding() { + throw new Error('Could not locate the bindings file. Tried:\n → better_sqlite3.node'); + }); workdir = mkdtempSync(join(tmpdir(), 'doctor-2968-')); process.chdir(workdir); mkdirSync(join(workdir, '.swarm'), { recursive: true }); @@ -57,6 +57,10 @@ describe('doctor #2968 — missing better-sqlite3 native binding', () => { const result = await runDoctor(); const check = findCheck((result.data as DoctorData).results, 'Memory Structural Integrity'); + // Every concurrent native probe must observe the same missing-binding fixture. + expect(findCheck((result.data as DoctorData).results, 'Memory Persistence Driver')?.message) + .toMatch(/native better-sqlite3 binding unavailable/i); + expect(check?.status).toBe('warn'); expect(check?.message).toMatch(/native binding is unavailable/i); expect(check?.message).toMatch(/durable WAL-backed persistence unavailable/i); @@ -68,6 +72,10 @@ describe('doctor #2968 — missing better-sqlite3 native binding', () => { const result = await runDoctor('memory'); const check = findCheck((result.data as DoctorData).results, 'Memory Integrity'); + // Every concurrent native probe must observe the same missing-binding fixture. + expect(findCheck((result.data as DoctorData).results, 'Memory Persistence Driver')?.message) + .toMatch(/native better-sqlite3 binding unavailable/i); + expect(check?.status).toBe('warn'); expect(check?.message).toMatch(/native binding is unavailable/i); expect(check?.message).not.toMatch(/malformed/i);