Files
ruvnet__ruflo/v3/@claude-flow/cli/__tests__/fs-secure.test.ts
Reuven bbe53a21cd test(security): regression tests for validateEnv + fs-secure (audit_1776853149979)
Pin the recent audit fixes so they can't silently regress.

__tests__/validate-input-env.test.ts (19 cases):
  - happy path: undefined/null/empty/valid env passes through
  - loader-hijack denylist: each of the 9 denylisted names (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) gets its own assertion — adding an exemption to that list
    becomes a real CVE that future me/anyone needs to consciously delete
    a test for
  - shape: name with =, digit-first name, whitespace, non-object value,
    non-string value, null byte in value, value over 32K — all rejected

__tests__/fs-secure.test.ts (8 cases):
  - writeFileRestricted: writes string + Buffer correctly, sets mode
    0600, preserves mode on overwrite
  - mkdirRestricted: creates new + nested dirs, idempotent on existing,
    sets mode 0700
  - mode-bit assertions skipped on Windows (process.platform === 'win32')
    because chmod is a no-op there and POSIX permission bits don't apply

All 27 new tests pass. Sibling validate-input-agent-spawn.test.ts also
re-run as a regression check on the shared source file — still 4/4 passing.
2026-05-03 20:30:08 -04:00

94 lines
3.1 KiB
TypeScript

/**
* 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);
});
});
});