llm-security/tests/scanners/unicode.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

108 lines
4.2 KiB
JavaScript

// unicode.test.mjs — Integration tests for the unicode-scanner
// Tests against the evil-project-health fixture which contains:
// - Zero-width characters in SKILL.fixture.md
// - Unicode Tag block codepoints (steganographic hidden message) in SKILL.fixture.md
// - BIDI override characters in SKILL.fixture.md
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/unicode-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('unicode-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 zero-width characters (CRITICAL or HIGH)', async () => {
const result = await scan(FIXTURE, discovery);
const zeroWidthFindings = result.findings.filter(f =>
(f.severity === 'critical' || f.severity === 'high') &&
(
f.title.toLowerCase().includes('zero-width') ||
f.title.toLowerCase().includes('zero width') ||
(f.evidence && f.evidence.toLowerCase().includes('u+200'))
)
);
assert.ok(
zeroWidthFindings.length >= 1,
`Expected at least 1 zero-width finding, got ${zeroWidthFindings.length}. ` +
`All findings: ${result.findings.map(f => f.title).join('; ')}`
);
});
it('detects Unicode Tag block codepoints (CRITICAL) — steganographic hidden message', async () => {
const result = await scan(FIXTURE, discovery);
const tagFindings = result.findings.filter(f =>
f.severity === 'critical' &&
f.title.toLowerCase().includes('unicode tag')
);
assert.ok(
tagFindings.length >= 1,
`Expected at least 1 Unicode Tag finding (CRITICAL), got ${tagFindings.length}. ` +
`All findings: ${result.findings.map(f => f.title).join('; ')}`
);
});
it('reports at least 3 total findings across all categories', async () => {
const result = await scan(FIXTURE, discovery);
assert.ok(
result.findings.length >= 3,
`Expected >= 3 total unicode findings, got ${result.findings.length}`
);
});
it('assigns correct scanner prefix UNI to all findings', async () => {
const result = await scan(FIXTURE, discovery);
const wrongPrefix = result.findings.filter(f => !f.id.startsWith('DS-UNI-'));
assert.equal(
wrongPrefix.length, 0,
`All findings should have DS-UNI- prefix. Wrong: ${wrongPrefix.map(f => f.id).join(', ')}`
);
});
it('finding IDs are sequential starting from DS-UNI-001', async () => {
const result = await scan(FIXTURE, discovery);
if (result.findings.length === 0) return;
assert.equal(result.findings[0].id, 'DS-UNI-001');
});
it('all findings have required fields', async () => {
const result = await scan(FIXTURE, discovery);
for (const f of result.findings) {
assert.ok(f.id, `Finding missing id`);
assert.ok(f.scanner, `Finding ${f.id} missing scanner`);
assert.ok(f.severity, `Finding ${f.id} missing severity`);
assert.ok(f.title, `Finding ${f.id} missing title`);
assert.ok(f.owasp, `Finding ${f.id} missing owasp`);
}
});
it('counts object reflects actual findings array', async () => {
const result = await scan(FIXTURE, discovery);
const countTotal = Object.values(result.counts).reduce((s, n) => s + n, 0);
assert.equal(
countTotal, result.findings.length,
`counts total (${countTotal}) should equal findings.length (${result.findings.length})`
);
});
});