llm-security/tests/scanners/entropy.test.mjs
Kjell Tore Guttormsen b3c47330e1
test(llm-security): store the malicious-skill demo encoded, materialize at run time
v8.1.0 S2. examples/malicious-skill-demo/evil-project-health/ (7 files,
30 Unicode Tag chars, a base64 exfil blob) is now one archive,
evil-project-health.archive.json: rot13 text, every codepoint above U+007E
stored as a number, sha256 of each retired file recorded. materialize.mjs
writes it to a temp dir (CLI prints the path); run-demo.sh materializes
and deletes it itself; the six scanner tests that scanned the tree use it.
payload-trees.test.mjs asserts byte identity (mutation-checked).

av-surface: b 8->6, c 1->0, d 2->1. Demo 13/13 before and after. All
scanners report identical findings except git-forensics: it used to scan
this repository's own history (21 findings, none about the demo) and now
reports skipped in a temp dir, which git.test.mjs already accepts.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-22 13:21:22 +02:00

98 lines
3.9 KiB
JavaScript

// entropy.test.mjs — Integration tests for the entropy-scanner
// Tests against the evil-project-health fixture which contains:
// - ENCODED_CONFIG: base64 blob in SKILL.fixture.md
// - auth_credential: high-entropy hardcoded credential in telemetry.mjs
import { describe, it, beforeEach, after } from 'node:test';
import assert from 'node:assert/strict';
import { resetCounter } from '../../scanners/lib/output.mjs';
import { discoverFiles } from '../../scanners/lib/file-discovery.mjs';
import { scan } from '../../scanners/entropy-scanner.mjs';
import { materializeArchive } from '../../examples/malicious-skill-demo/materialize.mjs';
// v8.1.0 (S2): the demo tree is stored encoded and materialized at test time.
const { dir: FIXTURE, cleanup } = materializeArchive();
after(cleanup);
describe('entropy-scanner integration', () => {
let discovery;
beforeEach(async () => {
resetCounter();
discovery = await discoverFiles(FIXTURE);
});
it('returns status ok', async () => {
const result = await scan(FIXTURE, discovery);
assert.equal(result.status, 'ok', `Expected status 'ok', got '${result.status}'`);
});
it('scans at least one file', async () => {
const result = await scan(FIXTURE, discovery);
assert.ok(result.files_scanned >= 1, `Expected files_scanned >= 1, got ${result.files_scanned}`);
});
it('detects at least 1 high-entropy string (base64 payload in telemetry.mjs)', async () => {
// The scanner suppresses fixture/ and test/ paths, so only telemetry.mjs is live-scanned.
// The base64 ENCODED_CONFIG (len=84, H≈5.18) triggers a HIGH finding.
// The auth_credential (len=32) is below the 40-char MEDIUM minimum length threshold.
const result = await scan(FIXTURE, discovery);
assert.ok(
result.findings.length >= 1,
`Expected >= 1 high-entropy finding, got ${result.findings.length}. ` +
`Findings: ${result.findings.map(f => `${f.file}:${f.line} ${f.evidence}`).join('; ')}`
);
});
it('reports findings with HIGH or CRITICAL severity', async () => {
const result = await scan(FIXTURE, discovery);
const highOrCritical = result.findings.filter(
f => f.severity === 'high' || f.severity === 'critical'
);
assert.ok(
highOrCritical.length >= 1,
`Expected at least 1 HIGH or CRITICAL entropy finding, got ${highOrCritical.length}`
);
});
it('assigns correct scanner prefix ENT to all findings', async () => {
const result = await scan(FIXTURE, discovery);
const wrongPrefix = result.findings.filter(f => !f.id.startsWith('DS-ENT-'));
assert.equal(
wrongPrefix.length, 0,
`All findings should have DS-ENT- prefix. Wrong: ${wrongPrefix.map(f => f.id).join(', ')}`
);
});
it('finding IDs are sequential starting from DS-ENT-001 after reset', async () => {
const result = await scan(FIXTURE, discovery);
if (result.findings.length === 0) return;
assert.equal(result.findings[0].id, 'DS-ENT-001');
});
it('all findings include entropy value in evidence', async () => {
const result = await scan(FIXTURE, discovery);
for (const f of result.findings) {
assert.ok(
f.evidence && f.evidence.includes('H='),
`Finding ${f.id} evidence should include H= entropy value, got: ${f.evidence}`
);
}
});
it('all findings map to LLM01 or LLM03 owasp category', async () => {
const result = await scan(FIXTURE, discovery);
for (const f of result.findings) {
assert.ok(
f.owasp === 'LLM01' || f.owasp === 'LLM03',
`Finding ${f.id} owasp should be LLM01 or LLM03, got: ${f.owasp}`
);
}
});
it('duration_ms is a non-negative number', async () => {
const result = await scan(FIXTURE, discovery);
assert.ok(typeof result.duration_ms === 'number', 'duration_ms should be a number');
assert.ok(result.duration_ms >= 0, 'duration_ms should be >= 0');
});
});