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

test(security): regression tests for validateEnv + fs-secure (audit_1776853149979)

Reuvenbbe53a21cde9
diff --git a/v3/@claude-flow/cli/__tests__/fs-secure.test.ts b/v3/@claude-flow/cli/__tests__/fs-secure.test.ts
new file mode 100644
index 0000000000..70b4a931be
--- /dev/null
+++ b/v3/@claude-flow/cli/__tests__/fs-secure.test.ts
@@ -0,0 +1,93 @@
+/**
+ * Regression test for audit_1776853149979: session/memory/terminal stores
+ * were written with the process umask — typically 0644, world-readable —
+ * even though they contain conversation snapshots, agent prompts, embeddings,
+ * and pasted shell command history that may include credentials. fs-secure's
+ * helpers force mode 0600 (files) / 0700 (dirs). These tests pin that
+ * contract.
+ *
+ * The mode-bit assertions are skipped on Windows, where chmod is a no-op
+ * and POSIX permission bits don't apply.
+ */
+
+import { describe, it, expect, beforeEach, afterEach } from 'vitest';
+import { mkdtempSync, rmSync, statSync, readFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+
+import { mkdirRestricted, writeFileRestricted } from '../src/fs-secure.js';
+
+const isPosix = process.platform !== 'win32';
+const itPosix = isPosix ? it : it.skip;
+
+describe('fs-secure (audit_1776853149979)', () => {
+ let workdir: string;
+
+ beforeEach(() => {
+ workdir = mkdtempSync(join(tmpdir(), 'fs-secure-test-'));
+ });
+
+ afterEach(() => {
+ rmSync(workdir, { recursive: true, force: true });
+ });
+
+ describe('writeFileRestricted', () => {
+ it('writes string content correctly', () => {
+ const path = join(workdir, 'session.json');
+ writeFileRestricted(path, '{"hello":"world"}');
+ expect(readFileSync(path, 'utf-8')).toBe('{"hello":"world"}');
+ });
+
+ it('writes Buffer content correctly (sql.js DB shape)', () => {
+ const path = join(workdir, 'memory.db');
+ const buf = Buffer.from([0xde, 0xad, 0xbe, 0xef]);
+ writeFileRestricted(path, buf);
+ expect(readFileSync(path)).toEqual(buf);
+ });
+
+ itPosix('sets mode 0600 (owner read/write only)', () => {
+ const path = join(workdir, 'session.json');
+ writeFileRestricted(path, '{}');
+ // Mask the type bits (S_IFMT) and check just the permission bits
+ const mode = statSync(path).mode & 0o777;
+ expect(mode).toBe(0o600);
+ });
+
+ itPosix('overwrite preserves restricted mode', () => {
+ const path = join(workdir, 'session.json');
+ writeFileRestricted(path, '{"v":1}');
+ writeFileRestricted(path, '{"v":2}');
+ const mode = statSync(path).mode & 0o777;
+ expect(mode).toBe(0o600);
+ expect(readFileSync(path, 'utf-8')).toBe('{"v":2}');
+ });
+ });
+
+ describe('mkdirRestricted', () => {
+ it('creates a missing directory', () => {
+ const dir = join(workdir, 'sessions');
+ mkdirRestricted(dir);
+ expect(statSync(dir).isDirectory()).toBe(true);
+ });
+
+ it('creates nested missing directories (recursive)', () => {
+ const dir = join(workdir, 'a', 'b', 'c');
+ mkdirRestricted(dir);
+ expect(statSync(dir).isDirectory()).toBe(true);
+ });
+
+ it('is idempotent on an existing directory', () => {
+ const dir = join(workdir, 'sessions');
+ mkdirRestricted(dir);
+ // Second call must not throw
+ expect(() => mkdirRestricted(dir)).not.toThrow();
+ });
+
+ itPosix('sets mode 0700 (owner only)', () => {
+ const dir = join(workdir, 'sessions');
+ mkdirRestricted(dir);
+ const mode = statSync(dir).mode & 0o777;
+ expect(mode).toBe(0o700);
+ });
+ });
+});
diff --git a/v3/@claude-flow/cli/__tests__/validate-input-env.test.ts b/v3/@claude-flow/cli/__tests__/validate-input-env.test.ts
new file mode 100644
index 0000000000..0e097c0b4e
--- /dev/null
+++ b/v3/@claude-flow/cli/__tests__/validate-input-env.test.ts
@@ -0,0 +1,102 @@
+/**
+ * Regression test for audit_1776853149979: terminal_create previously merged
+ * caller-supplied env straight into execSync's environment for every command
+ * in the session. Setting LD_PRELOAD or NODE_OPTIONS via that path was
+ * functionally RCE on the host. validateEnv enforces a denylist + POSIX
+ * shape — these tests pin the contract so the denylist can't silently
+ * regress.
+ */
+
+import { describe, it, expect } from 'vitest';
+import { validateEnv } from '../src/mcp-tools/validate-input.js';
+
+describe('validateEnv (audit_1776853149979)', () => {
+ describe('happy path', () => {
+ it('accepts undefined / null as empty env', () => {
+ expect(validateEnv(undefined)).toEqual({ valid: true, sanitized: {} });
+ expect(validateEnv(null)).toEqual({ valid: true, sanitized: {} });
+ });
+
+ it('accepts an empty object', () => {
+ expect(validateEnv({})).toEqual({ valid: true, sanitized: {} });
+ });
+
+ it('passes through a valid env unchanged', () => {
+ const r = validateEnv({ FOO: 'bar', MY_VAR: '1', _UNDERSCORE: 'ok' });
+ expect(r.valid).toBe(true);
+ expect(r.sanitized).toEqual({ FOO: 'bar', MY_VAR: '1', _UNDERSCORE: 'ok' });
+ });
+ });
+
+ describe('loader-hijack denylist', () => {
+ // Names that let an attacker hijack a child process before any user code
+ // runs. Each MUST be rejected — adding an exemption is a real CVE.
+ const denylisted = [
+ 'LD_PRELOAD',
+ 'LD_LIBRARY_PATH',
+ 'LD_AUDIT',
+ 'DYLD_INSERT_LIBRARIES',
+ 'DYLD_LIBRARY_PATH',
+ 'DYLD_FALLBACK_LIBRARY_PATH',
+ 'DYLD_FORCE_FLAT_NAMESPACE',
+ 'NODE_OPTIONS',
+ 'NODE_PATH',
+ ];
+
+ for (const name of denylisted) {
+ it(`rejects ${name}`, () => {
+ const r = validateEnv({ [name]: '/tmp/whatever' });
+ expect(r.valid).toBe(false);
+ expect(r.error).toContain(name);
+ expect(r.error).toContain('denylisted');
+ expect(r.sanitized).toEqual({});
+ });
+ }
+ });
+
+ describe('shape validation', () => {
+ it('rejects a name containing =', () => {
+ const r = validateEnv({ 'A=B': 'x' });
+ expect(r.valid).toBe(false);
+ expect(r.error).toContain('not a valid POSIX env name');
+ });
+
+ it('rejects a digit-first name', () => {
+ const r = validateEnv({ '1FOO': 'x' });
+ expect(r.valid).toBe(false);
+ expect(r.error).toContain('not a valid POSIX env name');
+ });
+
+ it('rejects a name with whitespace', () => {
+ const r = validateEnv({ 'BAD NAME': 'x' });
+ expect(r.valid).toBe(false);
+ expect(r.error).toContain('not a valid POSIX env name');
+ });
+
+ it('rejects an array (not an object)', () => {
+ const r = validateEnv(['FOO', 'BAR'] as unknown);
+ expect(r.valid).toBe(false);
+ expect(r.error).toContain('object of string→string');
+ });
+
+ it('rejects a non-string value', () => {
+ const r = validateEnv({ FOO: 42 });
+ expect(r.valid).toBe(false);
+ expect(r.error).toContain('FOO');
+ expect(r.error).toContain('must be a string');
+ });
+
+ it('rejects a value with a null byte', () => {
+ const value = ['a', String.fromCharCode(0), 'b'].join('');
+ const r = validateEnv({ FOO: value });
+ expect(r.valid).toBe(false);
+ expect(r.error).toContain('null byte');
+ });
+
+ it('rejects a value over 32K', () => {
+ const r = validateEnv({ FOO: 'x'.repeat(40_000) });
+ expect(r.valid).toBe(false);
+ expect(r.error).toContain('exceeds 32768');
+ });
+ });
+});