Files
ruflo/v3/@claude-flow/cli/__tests__/validate-input-env.test.ts
T
rUvandClaude 1e7a33a768 dream(security): #3384 add PATH/search-path family to validateEnv() denylist (evaluated, ACCEPT-scoped) (#3385)
* dream(security): #3384 add PATH/search-path family to validateEnv() denylist (evaluated, ACCEPT-scoped)

validateEnv()'s loader-hijack denylist blocked LD_PRELOAD/NODE_OPTIONS-class
vars but not PATH itself or the equivalent interpreter/VCS search-path
family (CWE-427) -- flagged grade-A by last night's security scan.

Adds PATH, PYTHONPATH, PERL5LIB, RUBYLIB, GIT_EXTERNAL_DIFF,
GIT_SSH_COMMAND, BASH_ENV, IFS to the denylist, closing the only
MCP-reachable exposure (terminal_create/terminal_execute). An independent
adversarial critic found the lookup was case-sensitive (path/Path bypass
it on case-insensitive Windows); fixed same session via .toUpperCase()
case-folding, which also hardens the original 9 names.

13 new discriminating tests (32/32 total), baseline-fails/candidate-passes
verified via git-stash isolation and independently reproduced twice by
the critic. Full suite failed-file set byte-identical to baseline (36
pre-existing environmental failures) -- zero new failures.

Co-Authored-By: RuFlo <ruv@ruv.net>
Claude-Session: https://claude.ai/code/session_01Sq3BMM7SqC5dzEaQiogNyp

* docs(dream-cycle): update LEDGER.md with 2026-09-21 security row

Records tonight's PATH/search-path denylist fix, the new 2026-09-13/14
no-run gap (confirmed via git ls-remote), the 09-15..09-20 backlog
(confirmed real, not transcribed per established convention), and a
correction that search_pull_requests's merged field is stale/unreliable
(pull_request_read is authoritative).

Co-Authored-By: RuFlo <ruv@ruv.net>
Claude-Session: https://claude.ai/code/session_01Sq3BMM7SqC5dzEaQiogNyp

* fix(ci): build+overlay local @claude-flow/cli-core before running the test ratchet

PR #3385's own new tests (validate-input-env.test.ts) passed locally but
failed in CI's Test Suite job: package-lock.json resolves
@claude-flow/cli-core from the published npm registry tarball, not an
npm workspace link (unlike @claude-flow/security, which the previous
step in this same job already handles for the same reason). Since
scripts/ci-test-ratchet.mjs runs vitest from repo root,
v3/@claude-flow/cli's tests resolve @claude-flow/cli-core by walking up
to root node_modules -- the stale published dist/, never the local
source. Building the local package and overlaying its dist/ onto the
installed one (same version, so not a version bump) makes CI test the
actual PR diff instead of a stale registry snapshot, without touching
package-lock.json.

Co-Authored-By: RuFlo <ruv@ruv.net>
Claude-Session: https://claude.ai/code/session_01Sq3BMM7SqC5dzEaQiogNyp

* dream(security): #3384 re-validate persisted session.env at execute time

Post-review fix on PR #3385: terminal_execute() trusted session.env from
the on-disk store unconditionally after terminal_create's one-time
validateEnv() call. A session persisted before a denylist update (or a
store.json edited/restored out of band) could carry a now-denylisted
env var (PATH, etc.) forever, silently bypassing tonight's fix -- and
the original 9-name denylist -- for every execute on that pre-existing
session.

terminal_execute now re-validates session.env via validateEnv() and
drops it to {} on failure rather than merging it unchecked, so a stale/
tampered session degrades to a clean environment instead of blocking.
2 new tests (stash-isolated: fails on the pre-fix handler, passes on
the fix); full package suite unaffected (same 36 pre-existing
environmental failures as before this commit).

Co-Authored-By: RuFlo <ruv@ruv.net>
Claude-Session: https://claude.ai/code/session_01Sq3BMM7SqC5dzEaQiogNyp

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-09-26 10:07:43 -05:00

131 lines
4.5 KiB
TypeScript

/**
* 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',
// dream-cycle 2026-09-21 (CWE-427, Uncontrolled Search Path Element):
// PATH itself and the equivalent interpreter/VCS search-path vars were
// missing — a caller could override PATH for every subsequent command
// in the terminal session.
'PATH',
'PYTHONPATH',
'PERL5LIB',
'RUBYLIB',
'GIT_EXTERNAL_DIFF',
'GIT_SSH_COMMAND',
'BASH_ENV',
'IFS',
];
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({});
});
}
// dream-cycle 2026-09-21 (adversarial critique caveat): Windows env vars
// are case-insensitive, so `path`/`Path` must be denylisted the same as
// `PATH` even though they're distinct object keys here.
describe('case-insensitive denylist match', () => {
const caseVariants = ['path', 'Path', 'PaTh', 'node_options', 'Node_Options'];
for (const name of caseVariants) {
it(`rejects ${name}`, () => {
const r = validateEnv({ [name]: '/tmp/whatever' });
expect(r.valid).toBe(false);
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');
});
});
});