feat(llm-security): wire TRG scanner into orchestrator and policy
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01V3s6WnubSSrFjAQTLQdVbG
This commit is contained in:
parent
fea943bdfb
commit
75aeeeee68
4 changed files with 83 additions and 1 deletions
|
|
@ -6,8 +6,10 @@
|
|||
|
||||
import { describe, it, beforeEach } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { resolve } from 'node:path';
|
||||
import { resolve, join } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { mkdtempSync, mkdirSync, writeFileSync, rmSync, readFileSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { resetCounter } from '../../scanners/lib/output.mjs';
|
||||
import { discoverFiles } from '../../scanners/lib/file-discovery.mjs';
|
||||
import { scan } from '../../scanners/trigger-scanner.mjs';
|
||||
|
|
@ -130,3 +132,59 @@ describe('trigger-scanner: poisoned', () => {
|
|||
assert.equal(total, result.findings.length);
|
||||
});
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Wiring — OWASP map + orchestrator registration (Step 2)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
describe('trigger-scanner: OWASP map registration', () => {
|
||||
it('TRG is present in all four OWASP maps with the right codes', async () => {
|
||||
const { OWASP_MAP, OWASP_AGENTIC_MAP, OWASP_SKILLS_MAP, OWASP_MCP_MAP } =
|
||||
await import('../../scanners/lib/severity.mjs');
|
||||
assert.deepEqual(OWASP_MAP.TRG, ['LLM06']);
|
||||
assert.deepEqual(OWASP_AGENTIC_MAP.TRG, []);
|
||||
assert.deepEqual(OWASP_SKILLS_MAP.TRG, ['AST04']);
|
||||
assert.deepEqual(OWASP_MCP_MAP.TRG, []);
|
||||
});
|
||||
});
|
||||
|
||||
describe('trigger-scanner: orchestrator registration', () => {
|
||||
it('scan-orchestrator imports and lists the TRG scanner', () => {
|
||||
const orchPath = resolve(__dirname, '../../scanners/scan-orchestrator.mjs');
|
||||
const text = readFileSync(orchPath, 'utf8');
|
||||
assert.match(text, /import\s+\{\s*scan as trgScan\s*\}\s+from\s+['"]\.\/trigger-scanner\.mjs['"]/);
|
||||
assert.match(text, /\{\s*name:\s*'trg',\s*fn:\s*trgScan\s*\}/);
|
||||
});
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Policy — overriding baiting_phrases changes detection
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
describe('trigger-scanner: policy override', () => {
|
||||
it('baiting_phrases from .llm-security/policy.json replaces the defaults', async () => {
|
||||
const dir = mkdtempSync(join(tmpdir(), 'trg-policy-'));
|
||||
try {
|
||||
mkdirSync(join(dir, '.llm-security'), { recursive: true });
|
||||
writeFileSync(
|
||||
join(dir, '.llm-security', 'policy.json'),
|
||||
JSON.stringify({ trg: { baiting_phrases: ['frobnicate'] } }),
|
||||
);
|
||||
mkdirSync(join(dir, 'skills', 'x'), { recursive: true });
|
||||
writeFileSync(
|
||||
join(dir, 'skills', 'x', 'SKILL.md'),
|
||||
'---\nname: x-tool\ndescription: This will frobnicate the payload, and also handle anything else.\n---\n\n# x-tool\n',
|
||||
);
|
||||
resetCounter();
|
||||
const discovery = await discoverFiles(dir);
|
||||
const result = await scan(dir, discovery);
|
||||
const baiting = result.findings.filter(f => /baiting/i.test(f.title));
|
||||
assert.ok(baiting.length >= 1, 'the policy-provided phrase should trigger baiting');
|
||||
assert.ok(baiting.some(f => /frobnicate/i.test(f.evidence)), 'should match the overridden phrase');
|
||||
// The default phrase list (which includes "anything") was replaced, so it must not match.
|
||||
assert.ok(!baiting.some(f => /anything/i.test(f.evidence)), 'default phrases must be replaced, not merged');
|
||||
} finally {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue