ktg-plugin-marketplace/plugins/llm-security/tests/scanners/workflow-scanner.test.mjs
Kjell Tore Guttormsen ede37219a3 feat(workflow-scanner): E11 part 2 — re-interpolation + auth-bypass + WFL prefix + orchestrator
Closes E11. Three new pieces, plus integration:

1. Re-interpolation detector (Appsmith GHSL-2024-277 stealth pattern).
   The scanner now collects env: bindings (key -> source-expression
   text) by walking parsed events whose parentChain includes 'env',
   then for each `${{ env.<KEY> }}` inside run:, re-injects MEDIUM
   if the binding source matches the 23-field blacklist. This
   catches the pattern where developers apply env-indirection but
   then re-interpolate the env var in run:, which cancels the
   mitigation (template substitution happens before shell parsing).

2. Auth-bypass category (Synacktiv 2023 Dependabot spoofing).
   Detects `if: ${{ github.actor == 'dependabot[bot]' }}` and
   variants. MEDIUM, owasp: 'LLM06' (Excessive Agency). Distinct
   from injection — same expression syntax, different threat class.
   Recommendation steers users to `github.event.pull_request.user.login`.

3. severity.mjs OWASP map registration. WFL prefix added to all
   four maps:
   - OWASP_MAP['WFL'] = ['LLM02', 'LLM06']
   - OWASP_AGENTIC_MAP['WFL'] = ['ASI04']
   - OWASP_SKILLS_MAP['WFL'] = []
   - OWASP_MCP_MAP['WFL'] = []
   Empty arrays for skills/MCP are explicit, not omitted — keeps
   `Object.keys(OWASP_MAP)` symmetric across maps.

4. scan-orchestrator.mjs registration. workflowScan added between
   supply-chain and toxic-flow (toxic-flow correlates after primaries).
   Verified via integration: orchestrator emits 9 WFL findings on
   tests/fixtures/workflows/.

Bug fix: extractTriggers in workflow-yaml-state.mjs was collecting
sub-properties (`branches:`, `types:`) as triggers. Now tracks the
first nested indent level and ignores anything deeper.

Tests:
- 6 new cases in tests/scanners/workflow-scanner.test.mjs:
  re-interp TP, no-double-count, auth-bypass TP, auth-bypass FP
  (startsWith head_ref is not auth-bypass), OWASP map shape,
  orchestrator import + SCANNERS array entry.
- 2 new fixtures: tp-reinterpolation.yml, auth-bypass-dependabot.yml.
- Existing 14 scanner tests + 15 state-machine tests unchanged.

Test count: 1732 -> 1738 (+6). Wave B total: +53 over baseline 1685.
Pre-compact-scan flake unchanged (passes in isolation).
2026-04-30 15:57:10 +02:00

216 lines
8.3 KiB
JavaScript

// workflow-scanner.test.mjs — E11 integration tests against fixtures
// in tests/fixtures/workflows/.{github,forgejo}/workflows/.
import { describe, it, before } from 'node:test';
import assert from 'node:assert/strict';
import { resolve } from 'node:path';
import { resetCounter } from '../../scanners/lib/output.mjs';
const { scan, discoverWorkflows } = await import('../../scanners/workflow-scanner.mjs');
const FIXTURE_DIR = resolve(import.meta.dirname, '../fixtures/workflows');
function findingsByFile(findings, fileSubstr) {
return findings.filter(f => (f.file || '').includes(fileSubstr));
}
describe('workflow-scanner — discoverWorkflows', () => {
it('finds .yml files in .github/workflows/ and .forgejo/workflows/', async () => {
const files = await discoverWorkflows(FIXTURE_DIR);
const githubCount = files.filter(f => f.includes('/.github/workflows/')).length;
const forgejoCount = files.filter(f => f.includes('/.forgejo/workflows/')).length;
assert.ok(githubCount >= 5, `expected ≥5 GitHub fixtures, got ${githubCount}`);
assert.ok(forgejoCount >= 2, `expected ≥2 Forgejo fixtures, got ${forgejoCount}`);
});
it('returns empty array for path with no workflow dirs', async () => {
const files = await discoverWorkflows('/tmp');
assert.deepEqual(files, []);
});
});
describe('workflow-scanner — true-positive cases', () => {
let result;
before(async () => {
resetCounter();
result = await scan(FIXTURE_DIR);
});
it('flags github.head_ref under pull_request_target as HIGH', () => {
const fs = findingsByFile(result.findings, 'tp-prtarget-head-ref.yml');
assert.equal(fs.length, 1);
assert.equal(fs[0].severity, 'high');
assert.match(fs[0].evidence, /github\.head_ref/);
});
it('flags discussion.title under discussion as HIGH (gluestack CVE pattern)', () => {
const fs = findingsByFile(result.findings, 'tp-discussion-title.yml');
assert.equal(fs.length, 1);
assert.equal(fs[0].severity, 'high');
assert.match(fs[0].evidence, /discussion\.title/);
});
it('flags comment.body under pull_request as MEDIUM', () => {
const fs = findingsByFile(result.findings, 'tp-pull-request-comment.yml');
assert.equal(fs.length, 1);
assert.equal(fs[0].severity, 'medium');
assert.match(fs[0].evidence, /comment\.body/);
});
it('flags issue.body inside `run: |` block-scalar as HIGH', () => {
const fs = findingsByFile(result.findings, 'tp-block-scalar-run.yml');
assert.equal(fs.length, 1);
assert.equal(fs[0].severity, 'high');
assert.match(fs[0].evidence, /issue\.body/);
});
});
describe('workflow-scanner — false-positive suppression', () => {
let result;
before(async () => {
resetCounter();
result = await scan(FIXTURE_DIR);
});
it('does NOT flag head_ref inside `if:` (sink mismatch)', () => {
const fs = findingsByFile(result.findings, 'fp-if-context.yml');
assert.equal(fs.length, 0, `expected no findings, got: ${JSON.stringify(fs)}`);
});
it('does NOT flag pull_request.title inside top-level `env:` mapping', () => {
const fs = findingsByFile(result.findings, 'fp-env-block.yml');
assert.equal(fs.length, 0);
});
});
describe('workflow-scanner — INFO classification', () => {
let result;
before(async () => {
resetCounter();
result = await scan(FIXTURE_DIR);
});
it('reports github.event.pull_request.number as INFO (numeric/safe)', () => {
const fs = findingsByFile(result.findings, 'fp-numeric-field.yml');
assert.equal(fs.length, 1);
assert.equal(fs[0].severity, 'info');
});
});
describe('workflow-scanner — Forgejo platform', () => {
let result;
before(async () => {
resetCounter();
result = await scan(FIXTURE_DIR);
});
it('flags forgejo.head_ref under pull_request as MEDIUM', () => {
const fs = findingsByFile(result.findings, 'forgejo-tp.yml');
assert.equal(fs.length, 1);
assert.equal(fs[0].severity, 'medium');
assert.match(fs[0].file, /\.forgejo\/workflows\//);
assert.match(fs[0].recommendation, /Forgejo/);
});
it('treats workflow_run as privileged on Forgejo (HIGH severity preserved)', () => {
const fs = findingsByFile(result.findings, 'forgejo-workflow-run.yml');
assert.equal(fs.length, 1);
assert.equal(fs[0].severity, 'high');
});
});
describe('workflow-scanner — output envelope', () => {
it('returns scannerResult with status=ok and counts', async () => {
resetCounter();
const r = await scan(FIXTURE_DIR);
assert.equal(r.status, 'ok');
assert.equal(r.scanner, 'workflow');
assert.ok(r.files_scanned >= 7);
assert.ok(typeof r.duration_ms === 'number');
assert.ok(r.counts.high + r.counts.medium + r.counts.info >= 7);
});
it('emits findings with WFL scanner prefix in id', async () => {
resetCounter();
const r = await scan(FIXTURE_DIR);
for (const f of r.findings) {
assert.match(f.id, /^DS-WFL-\d{3}$/);
assert.equal(f.scanner, 'WFL');
}
});
it('returns ok with no findings on empty target', async () => {
resetCounter();
const r = await scan('/tmp');
assert.equal(r.status, 'ok');
assert.equal(r.findings.length, 0);
});
});
// ---------------------------------------------------------------------------
// B4 — re-interpolation + auth-bypass categories
// ---------------------------------------------------------------------------
describe('workflow-scanner — B4 re-interpolation', () => {
it('flags ${{ env.PR_TITLE }} in run: when PR_TITLE was set from blacklisted source', async () => {
resetCounter();
const r = await scan(FIXTURE_DIR);
const fs = findingsByFile(r.findings, 'tp-reinterpolation.yml');
const reinterp = fs.find(f => /Re-interpolation/i.test(f.title));
assert.ok(reinterp, `expected a Re-interpolation finding in ${JSON.stringify(fs)}`);
assert.equal(reinterp.severity, 'medium');
assert.match(reinterp.evidence, /env\.PR_TITLE/);
assert.match(reinterp.description, /Appsmith|GHSL-2024-277/);
});
it('does not double-count: re-interpolation is emitted instead of a regular run-context finding for the env.<KEY> expression', async () => {
resetCounter();
const r = await scan(FIXTURE_DIR);
const fs = findingsByFile(r.findings, 'tp-reinterpolation.yml');
const runForEnvVar = fs.filter(f => f.evidence === '${{ env.PR_TITLE }}' && !/Re-interpolation/i.test(f.title));
assert.equal(runForEnvVar.length, 0);
});
});
describe('workflow-scanner — B4 auth-bypass', () => {
it('flags github.actor == "dependabot[bot]" in if: as MEDIUM auth-bypass', async () => {
resetCounter();
const r = await scan(FIXTURE_DIR);
const fs = findingsByFile(r.findings, 'auth-bypass-dependabot.yml');
const auth = fs.find(f => /Actor auth-bypass/i.test(f.title));
assert.ok(auth, `expected an Actor auth-bypass finding in ${JSON.stringify(fs)}`);
assert.equal(auth.severity, 'medium');
assert.equal(auth.owasp, 'LLM06');
assert.match(auth.recommendation, /pull_request\.user\.login/);
});
it('does NOT flag plain `if: ${{ startsWith(github.head_ref, …) }}` as auth-bypass', async () => {
resetCounter();
const r = await scan(FIXTURE_DIR);
const fs = findingsByFile(r.findings, 'fp-if-context.yml');
const auth = fs.filter(f => /Actor auth-bypass/i.test(f.title));
assert.equal(auth.length, 0);
});
});
describe('workflow-scanner — severity.mjs OWASP map registration', () => {
it('OWASP_MAP includes WFL with LLM02 and LLM06', async () => {
const { OWASP_MAP, OWASP_AGENTIC_MAP, OWASP_SKILLS_MAP, OWASP_MCP_MAP } =
await import('../../scanners/lib/severity.mjs');
assert.deepEqual(OWASP_MAP.WFL, ['LLM02', 'LLM06']);
assert.deepEqual(OWASP_AGENTIC_MAP.WFL, ['ASI04']);
assert.deepEqual(OWASP_SKILLS_MAP.WFL, []);
assert.deepEqual(OWASP_MCP_MAP.WFL, []);
});
});
describe('workflow-scanner — orchestrator registration', () => {
it('scan-orchestrator imports and lists the workflow scanner', async () => {
const { readFileSync } = await import('node:fs');
const { resolve: resolvePath } = await import('node:path');
const orchPath = resolvePath(import.meta.dirname, '../../scanners/scan-orchestrator.mjs');
const text = readFileSync(orchPath, 'utf8');
assert.match(text, /import\s+\{\s*scan as workflowScan\s*\}\s+from\s+['"]\.\/workflow-scanner\.mjs['"]/);
assert.match(text, /\{\s*name:\s*'workflow',\s*fn:\s*workflowScan\s*\}/);
});
});