🌊 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
TypeScript agentic-aiagentic-frameworkagentic-workflowagentsai-agentsai-assistantai-skillsautonomous-agentsclaudecodexdsh-pluginharnessmcp-servermulti-agentmulti-agent-systemsnpmskillsswarmswarm-intelligencetypescript
fix(memory): validate namespace on memory store and memory import (#3570)
drakeo33834b5c0ff5171
diff --git a/v3/@claude-flow/cli/__tests__/memory-store-import-namespace-3570.test.ts b/v3/@claude-flow/cli/__tests__/memory-store-import-namespace-3570.test.tsnew file mode 100644index 0000000000..2ba7944b8e--- /dev/null+++ b/v3/@claude-flow/cli/__tests__/memory-store-import-namespace-3570.test.ts@@ -0,0 +1,95 @@+/**+ * Regression for #3570 — `memory export` and `memory purge` reject a namespace+ * containing `../`, but `memory store` and `memory import` accepted and wrote+ * it, leaving a namespace the read-out and cleanup commands refuse to touch.+ * The write paths must reject it with the same message the export path uses.+ */+import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest';+import { mkdtempSync, rmSync, writeFileSync } from 'fs';+import { tmpdir } from 'os';+import { join } from 'path';++const mocks = vi.hoisted(() => ({+ checkMemoryInitialization: vi.fn(),+ initializeMemoryDatabase: vi.fn(),+ storeEntry: vi.fn(),+}));++vi.mock('../src/memory/memory-initializer.js', () => ({+ resolveDbPath: (path?: string) => path || '/project/.swarm/memory.db',+ checkMemoryInitialization: mocks.checkMemoryInitialization,+ initializeMemoryDatabase: mocks.initializeMemoryDatabase,+ storeEntry: mocks.storeEntry,+ searchEntries: vi.fn(),+ listEntries: vi.fn(),+ getEntry: vi.fn(),+ deleteEntry: vi.fn(),+}));++import { memoryCommand } from '../src/commands/memory.js';+import { memoryTools } from '../src/mcp-tools/memory-tools.js';++const storeCommand = memoryCommand.subcommands!.find(c => c.name === 'store')!;+const importTool = memoryTools.find(t => t.name === 'memory_import')!;+const exportTool = memoryTools.find(t => t.name === 'memory_export')!;++const tempDir = mkdtempSync(join(tmpdir(), 'ruflo-ns-3570-'));+const writeImport = (name: string, entries: unknown[]) => {+ const p = join(tempDir, name);+ writeFileSync(p, JSON.stringify({ entries }));+ return p;+};++beforeEach(() => {+ vi.clearAllMocks();+ mocks.checkMemoryInitialization.mockResolvedValue({ initialized: true });+ mocks.initializeMemoryDatabase.mockResolvedValue({ success: true });+ mocks.storeEntry.mockResolvedValue({ success: true, id: 'row-1' });+});++afterAll(() => rmSync(tempDir, { recursive: true, force: true }));++describe('namespace path-traversal validation on write paths (#3570)', () => {+ it('memory export already rejects the traversal namespace (the reference behaviour)', async () => {+ await expect(+ exportTool.handler({ outputPath: join(tempDir, 'x.json'), namespace: '../../..' }),+ ).rejects.toThrow(/namespace contains path traversal/);+ });++ it('memory store rejects a namespace containing ../ and writes nothing', async () => {+ const result = await storeCommand.action!({+ args: [],+ flags: { key: 'probe/x', value: 'v', namespace: '../../..' },+ } as any);+ expect(result?.success).toBe(false);+ expect(mocks.storeEntry).not.toHaveBeenCalled();+ });++ it('memory store still accepts an ordinary namespace', async () => {+ const result = await storeCommand.action!({+ args: [],+ flags: { key: 'probe/x', value: 'v', namespace: 'project-a' },+ } as any);+ expect(result?.success).toBe(true);+ expect(mocks.storeEntry).toHaveBeenCalledWith(expect.objectContaining({ namespace: 'project-a' }));+ });++ it('memory import rejects an entry carrying a ../ namespace with the export message and writes nothing', async () => {+ const inputPath = writeImport('bad.json', [+ { key: 'ok', namespace: 'project', value: 'fine' },+ { key: 'K', namespace: '../../..', value: 'v' },+ ]);+ await expect(importTool.handler({ inputPath })).rejects.toThrow(/namespace contains path traversal/);+ expect(mocks.storeEntry).not.toHaveBeenCalled();+ });++ it('memory import still imports entries with ordinary namespaces', async () => {+ const inputPath = writeImport('good.json', [+ { key: 'a', namespace: 'project', value: 'one' },+ { key: 'b', value: 'two' },+ ]);+ const result: any = await importTool.handler({ inputPath });+ expect(result.imported.entries).toBe(2);+ expect(mocks.storeEntry).toHaveBeenCalledTimes(2);+ });+});diff --git a/v3/@claude-flow/cli/src/commands/memory.ts b/v3/@claude-flow/cli/src/commands/memory.tsindex 2590045192..c889548df1 100644--- a/v3/@claude-flow/cli/src/commands/memory.ts+++ b/v3/@claude-flow/cli/src/commands/memory.ts@@ -13,6 +13,7 @@ import { countSiblingStoreRows } from '../memory/sibling-store.js';import { resolveDbPath } from '../memory/memory-initializer.js';import { existsSync } from 'node:fs';import { siblingAgentDbPath } from '../memory/memory-bridge.js';+import { validateIdentifier } from '../mcp-tools/validate-input.js';/*** #3228: a miss in one store is not a miss in the memory.@@ -194,6 +195,13 @@ const storeCommand: Command = {return { success: false, exitCode: 1 };}+ // #3570: reject a traversal namespace before persisting, as export/purge do.+ const vNs = validateIdentifier(namespace, 'namespace');+ if (!vNs.valid) {+ output.printError(vNs.error!);+ return { success: false, exitCode: 1 };+ }+// #2752 MemPoison gate — scan before persist when opted in.// CLI flag ctx.flags.scanContent takes precedence over RUFLO_MEMORY_SCAN_ON_WRITE env var// (ADR-125 §"CLI flag wins" / ADR-130 §env-var-config-precedence — fix for #2794).diff --git a/v3/@claude-flow/cli/src/mcp-tools/memory-tools.ts b/v3/@claude-flow/cli/src/mcp-tools/memory-tools.tsindex 5e4e34bffd..f7aa32506c 100644--- a/v3/@claude-flow/cli/src/mcp-tools/memory-tools.ts+++ b/v3/@claude-flow/cli/src/mcp-tools/memory-tools.ts@@ -1611,6 +1611,15 @@ export const memoryTools: MCPTool[] = [const entries = Array.isArray(doc.entries) ? doc.entries : [];const nsOverride = input.namespace ? String(input.namespace) : undefined;if (nsOverride) { const v = validateIdentifier(nsOverride, 'namespace'); if (!v.valid) throw new Error(v.error); }+ // #3570: validate every entry's namespace up front so a bad file writes nothing.+ if (!nsOverride) {+ for (const e of entries) {+ if (e && typeof e.key === 'string' && e.namespace !== undefined) {+ const v = validateIdentifier(String(e.namespace), 'namespace');+ if (!v.valid) throw new Error(v.error);+ }+ }+ }let imported = 0; let skipped = 0;for (const e of entries) {if (!e || typeof e.key !== 'string') { skipped++; continue; }