Skip to content

armgit / ruflo

Public Imported from GitHub
0

🌊 The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated

fix(memory): decrypt images before initialization checks

Rudy Celekli1c96e1705210
diff --git a/v3/@claude-flow/cli/__tests__/memory-init-encrypted-image.test.ts b/v3/@claude-flow/cli/__tests__/memory-init-encrypted-image.test.ts
new file mode 100644
index 0000000000..d9c227884e
--- /dev/null
+++ b/v3/@claude-flow/cli/__tests__/memory-init-encrypted-image.test.ts
@@ -0,0 +1,36 @@
+import { afterEach, describe, expect, it, vi } from 'vitest';
+import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import initSqlJs from 'sql.js';
+import { encryptBuffer } from '../src/encryption/vault.js';
+import { checkMemoryInitialization } from '../src/memory/memory-initializer.js';
+
+const dirs: string[] = [];
+afterEach(() => {
+ vi.unstubAllEnvs();
+ for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true });
+});
+
+describe('initialization status of a real SQLite image', () => {
+ it.each([false, true])('recognizes an existing store (encrypted=%s) without modifying it', async (encrypted) => {
+ const dir = mkdtempSync(join(tmpdir(), 'memory-init-image-'));
+ dirs.push(dir);
+ const dbPath = join(dir, 'memory.db');
+ const SQL = await initSqlJs();
+ const db = new SQL.Database();
+ db.run("CREATE TABLE metadata (key TEXT, value TEXT); INSERT INTO metadata VALUES ('schema_version', '3'), ('backend', 'sql.js'); CREATE TABLE memory_entries (id TEXT); CREATE TABLE patterns (id TEXT)");
+ const plain = Buffer.from(db.export());
+ db.close();
+ const key = Buffer.alloc(32, 7);
+ vi.stubEnv('CLAUDE_FLOW_ENCRYPTION_KEY', key.toString('hex'));
+ const image = encrypted ? encryptBuffer(plain, key) : plain;
+ writeFileSync(dbPath, image);
+
+ expect(await checkMemoryInitialization(dbPath)).toMatchObject({
+ initialized: true, version: '3', backend: 'sql.js',
+ features: { patternLearning: true },
+ });
+ expect(readFileSync(dbPath)).toEqual(image);
+ });
+});
diff --git a/v3/@claude-flow/cli/__tests__/memory-init-handle-leak-3249.test.ts b/v3/@claude-flow/cli/__tests__/memory-init-handle-leak-3249.test.ts
index ab976fe102..9b27a7df58 100644
--- a/v3/@claude-flow/cli/__tests__/memory-init-handle-leak-3249.test.ts
+++ b/v3/@claude-flow/cli/__tests__/memory-init-handle-leak-3249.test.ts
@@ -18,9 +18,10 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
+import { encryptBuffer } from '../src/encryption/vault.js';
const handle = vi.hoisted(() => ({
- instances: [] as Array<{ closed: boolean }>,
+ instances: [] as Array<{ closed: boolean; data?: unknown }>,
}));
// Set per test: what `db.exec()` does once the handle exists.
@@ -34,7 +35,7 @@ vi.mock('sql.js', () => {
class Database {
closed = false;
- constructor(_data?: unknown) {
+ constructor(public data?: unknown) {
handle.instances.push(this);
}
@@ -73,14 +74,15 @@ afterEach(() => {
if (originalMemoryPath === undefined) delete process.env.CLAUDE_FLOW_MEMORY_PATH;
else process.env.CLAUDE_FLOW_MEMORY_PATH = originalMemoryPath;
_resetMemoryRootCache();
+ vi.unstubAllEnvs();
rmSync(testDir, { recursive: true, force: true });
});
describe('checkMemoryInitialization handle lifecycle (#3249)', () => {
it('closes the handle when the schema query rejects the image', async () => {
const dbPath = join(testDir, 'memory.db');
- // RFE1 ciphertext: bytes sql.js will accept but cannot parse as SQLite.
- writeFileSync(dbPath, Buffer.from('RFE1\x00\x01\x02 not a sqlite image', 'utf8'));
+ // Invalid plaintext still creates a handle that the query rejects.
+ writeFileSync(dbPath, Buffer.from('not a sqlite image', 'utf8'));
const result = await checkMemoryInitialization(dbPath);
@@ -103,6 +105,31 @@ describe('checkMemoryInitialization handle lifecycle (#3249)', () => {
expect(handle.instances[0].closed).toBe(true);
});
+ it('decrypts an RFE1 image before passing it to sql.js', async () => {
+ const dbPath = join(testDir, 'memory.db');
+ const key = Buffer.alloc(32, 7);
+ const plaintext = Buffer.from('SQLite format 3\0 fixture');
+ vi.stubEnv('CLAUDE_FLOW_ENCRYPTION_KEY', key.toString('hex'));
+ writeFileSync(dbPath, encryptBuffer(plaintext, key));
+ behaviour.exec = () => [{ values: [['memory_entries'], ['metadata']] }];
+
+ const result = await checkMemoryInitialization(dbPath);
+
+ expect(result.initialized).toBe(true);
+ expect(handle.instances).toHaveLength(1);
+ expect(handle.instances[0].data).toEqual(plaintext);
+ expect(handle.instances[0].closed).toBe(true);
+ });
+
+ it('does not open a sql.js handle when RFE1 authentication fails', async () => {
+ const dbPath = join(testDir, 'memory.db');
+ vi.stubEnv('CLAUDE_FLOW_ENCRYPTION_KEY', Buffer.alloc(32, 8).toString('hex'));
+ writeFileSync(dbPath, encryptBuffer(Buffer.from('SQLite format 3\0'), Buffer.alloc(32, 7)));
+
+ expect(await checkMemoryInitialization(dbPath)).toEqual({ initialized: false });
+ expect(handle.instances).toHaveLength(0);
+ });
+
it('opens no handle at all when the file is absent', async () => {
const result = await checkMemoryInitialization(join(testDir, 'missing.db'));
diff --git a/v3/@claude-flow/cli/src/memory/memory-initializer.ts b/v3/@claude-flow/cli/src/memory/memory-initializer.ts
index 634c4df7ea..dfde92e1d9 100644
--- a/v3/@claude-flow/cli/src/memory/memory-initializer.ts
+++ b/v3/@claude-flow/cli/src/memory/memory-initializer.ts
@@ -2103,7 +2103,7 @@ export async function checkMemoryInitialization(dbPath?: string): Promise<{
const initSqlJs = (await import('sql.js')).default;
const SQL = await initSqlJs();
- const fileBuffer = fs.readFileSync(path_);
+ const fileBuffer = readFileMaybeEncrypted(path_, null);
db = new SQL.Database(fileBuffer);
// Check for metadata table