llm-security/tests/lib/policy-loader.test.mjs
Kjell Tore Guttormsen b6af9b46df feat(llm-security)!: v8 Phase 3 step 1 - remove the deprecated mode env-vars
BREAKING CHANGE: the four LLM_SECURITY_* configuration env-vars deprecated in
v7.3.0 are removed. .llm-security/policy.json is now the only source:

  LLM_SECURITY_INJECTION_MODE     -> injection.mode
  LLM_SECURITY_TRIFECTA_MODE      -> trifecta.mode
  LLM_SECURITY_ESCALATION_WINDOW  -> trifecta.escalation_window
  LLM_SECURITY_AUDIT_LOG          -> audit.log_path
  LLM_SECURITY_DEPRECATION_QUIET  -> dies with the mechanism it silenced

Setting a removed var is now inert - it does not warn, and it does not
configure. Env-vars with no policy equivalent (PRECOMPACT_MODE,
PRECOMPACT_MAX_BYTES, UPDATE_CHECK, MCP_CACHE_FILE, IDE_ROOTS) are unaffected.

getPolicyValueWithEnvWarn and its one-shot stderr warning are deleted from
policy-loader.mjs, along with the module-scoped warned-var Set. The four call
sites collapse to getPolicyValue. getPolicyValue's JSDoc claimed "environment
variables ALWAYS take precedence" - it never read env itself, so that line
described the shim, and it is corrected rather than deleted.

User-facing hook strings that advertised a removed var as the escape hatch now
name the policy key instead: the inject-scan block reason, its warn-mode note,
both escalation-window advisories, and the trifecta block message. A blocked
user following the old text would have set a var that does nothing.

Tests. tests/lib/v8-env-removal.test.mjs is the regression gate and was written
failing first (8 of 12 red before the change). It pins the NEGATIVE - setting a
removed var does not alter the outcome - because that is the half that rots
silently: a re-introduced process.env read would leave every migrated positive
test green, since those configure through policy.json and never set the var at
all. One assertion walks hooks/scripts and scanners for `process.env.<removed>`
so the re-introduction is caught structurally, not only behaviourally.

The 44 env-driven test occurrences (18 inject-scan, 13+4 session-guard, 9
audit-trail) migrate to a throwaway .llm-security/policy.json via a new
runHookWithPolicy helper in hook-helper.mjs; audit-trail runs in-process, so it
supplies the same policy through CLAUDE_PROJECT_ROOT. The D3 mechanism tests in
policy-loader.test.mjs are deleted with the mechanism.

Suite 2039 tests, 2037 pass (+12 gate, -7 D3 mechanism). The 2 failures are the
known parallel-load timing flakes (pre-compact-scan size-cap,
pre-install-supply-chain F-3); both green when run isolated.

Remaining in Phase 3: posture-scanner TRIFECTA_MODE heuristic, riskScoreV1
removal, ghost-var cleanup, docs + migration note.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BB4vXvwvtW4dxbPRd6vsez
2026-08-09 10:17:47 +02:00

140 lines
5.2 KiB
JavaScript

// policy-loader.test.mjs — Tests for policy-as-code loader
import { describe, it, beforeEach, afterEach } from 'node:test';
import assert from 'node:assert/strict';
import { writeFileSync, mkdirSync, rmSync, existsSync } from 'node:fs';
import { join } from 'node:path';
import { tmpdir } from 'node:os';
import { loadPolicy, getPolicyValue, getDefaultPolicy, _resetCacheForTest } from '../../scanners/lib/policy-loader.mjs';
const TEST_ROOT = join(tmpdir(), `llm-security-policy-test-${Date.now()}`);
const POLICY_DIR = join(TEST_ROOT, '.llm-security');
const POLICY_FILE = join(POLICY_DIR, 'policy.json');
describe('policy-loader', () => {
beforeEach(() => {
_resetCacheForTest();
mkdirSync(POLICY_DIR, { recursive: true });
});
afterEach(() => {
_resetCacheForTest();
try { rmSync(TEST_ROOT, { recursive: true }); } catch {}
});
it('returns defaults when no policy file exists', () => {
rmSync(POLICY_FILE, { force: true });
const policy = loadPolicy(TEST_ROOT);
assert.equal(policy.version, '1.0');
assert.equal(policy.injection.mode, 'block');
assert.equal(policy.trifecta.mode, 'warn');
assert.equal(policy.trifecta.window_size, 20);
});
it('reads and merges valid policy file', () => {
writeFileSync(POLICY_FILE, JSON.stringify({
version: '1.0',
trifecta: { mode: 'off' },
}));
const policy = loadPolicy(TEST_ROOT);
assert.equal(policy.trifecta.mode, 'off');
// Other defaults preserved
assert.equal(policy.trifecta.window_size, 20);
assert.equal(policy.injection.mode, 'block');
});
it('handles partial policy (deep merge preserves defaults)', () => {
writeFileSync(POLICY_FILE, JSON.stringify({
secrets: { additional_patterns: ['CUSTOM_SECRET=\\w+'] },
}));
const policy = loadPolicy(TEST_ROOT);
assert.deepEqual(policy.secrets.additional_patterns, ['CUSTOM_SECRET=\\w+']);
assert.deepEqual(policy.secrets.allowed_paths, []); // default preserved
});
it('caches policy per root', () => {
writeFileSync(POLICY_FILE, JSON.stringify({ trifecta: { mode: 'block' } }));
const p1 = loadPolicy(TEST_ROOT);
// Modify file — should still return cached
writeFileSync(POLICY_FILE, JSON.stringify({ trifecta: { mode: 'off' } }));
const p2 = loadPolicy(TEST_ROOT);
assert.equal(p1, p2); // same reference (cached)
assert.equal(p2.trifecta.mode, 'block'); // original value
});
it('getPolicyValue returns correct values', () => {
writeFileSync(POLICY_FILE, JSON.stringify({
mcp: { volume_threshold_bytes: 500_000 },
}));
const val = getPolicyValue('mcp', 'volume_threshold_bytes', 100_000, TEST_ROOT);
assert.equal(val, 500_000);
});
it('getPolicyValue returns default when key not in policy', () => {
writeFileSync(POLICY_FILE, JSON.stringify({ version: '1.0' }));
const val = getPolicyValue('mcp', 'nonexistent_key', 42, TEST_ROOT);
assert.equal(val, 42);
});
it('handles invalid JSON gracefully', () => {
writeFileSync(POLICY_FILE, 'not valid json!!!');
const policy = loadPolicy(TEST_ROOT);
// Should return defaults without crashing
assert.equal(policy.version, '1.0');
assert.equal(policy.injection.mode, 'block');
});
it('getDefaultPolicy returns a copy', () => {
const d1 = getDefaultPolicy();
const d2 = getDefaultPolicy();
assert.deepEqual(d1, d2);
assert.notEqual(d1, d2); // different references
});
it('default policy matches existing hardcoded values', () => {
const defaults = getDefaultPolicy();
// These must match the hardcoded values in hooks
assert.equal(defaults.injection.mode, 'block');
assert.equal(defaults.trifecta.mode, 'warn');
assert.equal(defaults.trifecta.window_size, 20);
assert.equal(defaults.trifecta.long_horizon_window, 100);
assert.equal(defaults.mcp.volume_threshold_bytes, 100_000);
});
it('default policy includes ci section with null/false defaults', () => {
const defaults = getDefaultPolicy();
assert.equal(defaults.ci.failOn, null);
assert.equal(defaults.ci.compact, false);
});
it('ci section merges correctly from policy file', () => {
writeFileSync(POLICY_FILE, JSON.stringify({
ci: { failOn: 'high' },
}));
const policy = loadPolicy(TEST_ROOT);
assert.equal(policy.ci.failOn, 'high');
assert.equal(policy.ci.compact, false); // default preserved
});
it('ci section allows compact override', () => {
writeFileSync(POLICY_FILE, JSON.stringify({
ci: { failOn: 'critical', compact: true },
}));
const policy = loadPolicy(TEST_ROOT);
assert.equal(policy.ci.failOn, 'critical');
assert.equal(policy.ci.compact, true);
});
it('default policy includes trifecta.escalation_window=5 (D3)', () => {
const defaults = getDefaultPolicy();
assert.equal(defaults.trifecta.escalation_window, 5);
});
it('getPolicyValue survives a scalar section override without throwing (#26)', () => {
// A user writing {"injection": "block"} (scalar instead of object) must
// not crash the "key in sectionObj" lookup with a TypeError.
writeFileSync(POLICY_FILE, JSON.stringify({ injection: 'block' }));
const val = getPolicyValue('injection', 'mode', 'warn', TEST_ROOT);
assert.equal(val, 'warn');
});
});