BREAKING CHANGE: the {NNN} in CA-{SCANNER}-{NNN} identifies the check that
produced the finding. It used to be the finding's position in that scanner's
output for that run, which made it unstable across CONFIGURATIONS, not just
across releases as STATE framed it. Measured on two fixtures: "No custom
subagents" was CA-GAP-007 on minimal-project and CA-GAP-004 on healthy-project.
A user who fixed an unrelated earlier gap silently renumbered every later one,
so a .config-audit-ignore pin retargeted to a neighbouring finding with no
version change at all.
Second measured arm: README already documented the opposite scheme. It and the
scanner headers describe ~20 numbers as check codes (CA-SKL-003 = oversized
body, CA-PLH-015 = folder shadowing, CA-TOK-006 = schema deferral), and the
counter could only produce those in the all-fire case -- source-order positions
are 4, 3 and 8. The documentation described the scheme; the implementation was
what was wrong. Every published number is preserved by construction and pinned
exhaustively in tests/lib/finding-codes.test.mjs.
scanners/lib/finding-codes.mjs is the single authority. Every finding() call
passes a `code`; an undeclared or missing one THROWS. No counter fallback --
that would reproduce D1's findGapId -> 'unknown' silent degradation and let a
half-converted scanner ship IDs that look valid. findingCounter/resetCounter
are deleted outright, not left as no-ops. Retirement is now a mechanism:
RETIRED_CODES tombstones a withdrawn key so its number is never reissued,
seeded with GAP t3_8 -- the D1 removal that opened this chunk.
IDs are consequently NOT unique per finding: one check failing in three files
emits three findings sharing an ID. That inverts which consumer is correct, so
every f.id/findingId site was classified before the change. diff-engine and
most of fix-engine already keyed on scanner+title+file (drift was never lying);
fix-engine's verification did not, and keyed on the ID alone -- fixing one of
two sibling instances marked both fixed, and the untouched one, still present
in the re-scan, was reported as a REGRESSION. Red test first, then keyed on
(findingId, file), which both planFixes and applyFixes already carry.
plugin-health's crossIds Set was measured and is a clean negative: cross
findings are allFindings.slice(crossPluginStart) and codes 18/19 are emitted
only in that tail, so the partition holds by construction.
unknownSuppressions() reports a pin that names no declared check, in the
--output-file payload (ux-rules rule 2 -- a stderr-only warning is invisible to
the commands) and only when one exists, so a clean config is byte-identical.
That is what makes the break safe: a stale pin goes loud instead of dying quiet.
Frozen tests/snapshots/v5.0.0/ untouched on disk. IDs are masked out of that
comparison (mask-finding-ids.mjs) rather than re-derived -- re-deriving
positional IDs would assert the retired scheme against itself, and #58's
isGapEntry off-by-one is the measured example of that misfiring. The dead
re-derivation is removed from strip-retired-gap.mjs. default-output snapshots
re-approved after confirming the diff is IDs and nothing else.
Guards, each seen red against its own defect: a missing code (scanner errors
out mid-sweep), an orphan declaration, a resurrected retired key, and a
documented ID naming no check. The sweep asserts the union across all 16
scanners, never per scanner -- a per-scanner assertion goes green on a partial
conversion.
Fasit written before implementation: docs/mbug28-id-semantics-fasit.local.md,
including one correction made before running (CML has 12 checks over 13 call
sites -- the anchored and calibrated char-budget arms are one check, which a
repeated-title sweep found and my call-site count had missed).
Suite 1535 -> 1573, 0 failing.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MyqCQKK2ornJ1jFWwqx17E
243 lines
8.5 KiB
JavaScript
243 lines
8.5 KiB
JavaScript
import { describe, it, beforeEach, afterEach } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { resolve, join } from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { mkdtemp, writeFile, rm } from 'node:fs/promises';
|
|
import { tmpdir } from 'node:os';
|
|
import { discoverConfigFiles } from '../../scanners/lib/file-discovery.mjs';
|
|
import { scan } from '../../scanners/mcp-config-validator.mjs';
|
|
|
|
const __dirname = fileURLToPath(new URL('.', import.meta.url));
|
|
const FIXTURES = resolve(__dirname, '../fixtures');
|
|
|
|
describe('MCP scanner — healthy project', () => {
|
|
let result;
|
|
beforeEach(async () => {
|
|
const discovery = await discoverConfigFiles(resolve(FIXTURES, 'healthy-project'));
|
|
result = await scan(resolve(FIXTURES, 'healthy-project'), discovery);
|
|
});
|
|
|
|
it('returns status ok', () => {
|
|
assert.equal(result.status, 'ok');
|
|
});
|
|
|
|
it('reports scanner prefix MCP', () => {
|
|
assert.equal(result.scanner, 'MCP');
|
|
});
|
|
|
|
it('scans at least 1 file', () => {
|
|
assert.ok(result.files_scanned >= 1);
|
|
});
|
|
|
|
it('has no critical or high findings', () => {
|
|
assert.equal(result.counts.critical, 0);
|
|
assert.equal(result.counts.high, 0);
|
|
});
|
|
|
|
it('finding IDs match CA-MCP-NNN pattern', () => {
|
|
for (const f of result.findings) {
|
|
assert.match(f.id, /^CA-MCP-\d{3}$/);
|
|
}
|
|
});
|
|
|
|
it('has counts object with all severity levels', () => {
|
|
assert.ok('critical' in result.counts);
|
|
assert.ok('high' in result.counts);
|
|
assert.ok('medium' in result.counts);
|
|
assert.ok('low' in result.counts);
|
|
assert.ok('info' in result.counts);
|
|
});
|
|
});
|
|
|
|
describe('MCP scanner — broken project', () => {
|
|
let result;
|
|
beforeEach(async () => {
|
|
const discovery = await discoverConfigFiles(resolve(FIXTURES, 'broken-project'));
|
|
result = await scan(resolve(FIXTURES, 'broken-project'), discovery);
|
|
});
|
|
|
|
it('returns status ok', () => {
|
|
assert.equal(result.status, 'ok');
|
|
});
|
|
|
|
it('detects SSE server type', () => {
|
|
assert.ok(result.findings.some(f => f.title.includes('SSE')));
|
|
});
|
|
|
|
it('SSE recommendation is info severity', () => {
|
|
const sse = result.findings.find(f => f.title.includes('SSE'));
|
|
assert.equal(sse.severity, 'info');
|
|
});
|
|
|
|
it('detects unknown server type', () => {
|
|
assert.ok(result.findings.some(f => f.title.includes('Unknown MCP server type')));
|
|
});
|
|
|
|
it('unknown server type is high severity', () => {
|
|
const unknown = result.findings.find(f => f.title.includes('Unknown MCP server type'));
|
|
assert.equal(unknown.severity, 'high');
|
|
});
|
|
|
|
it('does NOT flag any trust level — `trust` is not a real .mcp.json field (CC docs verified 2026-06-18)', () => {
|
|
assert.ok(!result.findings.some(f => /trust level/i.test(f.title)),
|
|
'no "Missing trust level"/"Invalid trust level" findings: the field does not exist in the official schema');
|
|
});
|
|
|
|
it('detects unreferenced env vars in args', () => {
|
|
assert.ok(result.findings.some(f => f.title.includes('Unreferenced env var')));
|
|
});
|
|
|
|
it('detects unknown server fields', () => {
|
|
assert.ok(result.findings.some(f => f.title.includes('Unknown MCP server field')));
|
|
});
|
|
|
|
it('has multiple findings', () => {
|
|
assert.ok(result.findings.length >= 5);
|
|
});
|
|
});
|
|
|
|
describe('MCP scanner — stray `trust` field is an unknown field (verify-first, 2026-06-18)', () => {
|
|
let result;
|
|
let tmpRoot;
|
|
|
|
beforeEach(async () => {
|
|
tmpRoot = await mkdtemp(join(tmpdir(), 'ca-mcp-trust-'));
|
|
// `trust` is NOT a field in the official .mcp.json schema (verified against
|
|
// code.claude.com/docs/en/mcp + /settings, 2026-06-18). Approval is
|
|
// dialog/settings-based (enableAllProjectMcpServers / enabledMcpjsonServers /
|
|
// disabledMcpjsonServers), never a per-server JSON field.
|
|
const mcp = {
|
|
mcpServers: {
|
|
legacy: { type: 'stdio', command: 'node', args: ['server.mjs'], trust: 'workspace' },
|
|
},
|
|
};
|
|
await writeFile(join(tmpRoot, '.mcp.json'), JSON.stringify(mcp, null, 2) + '\n', 'utf8');
|
|
const discovery = await discoverConfigFiles(tmpRoot);
|
|
result = await scan(tmpRoot, discovery);
|
|
});
|
|
|
|
afterEach(async () => {
|
|
if (tmpRoot) await rm(tmpRoot, { recursive: true, force: true });
|
|
});
|
|
|
|
it('flags `trust` as an unknown MCP server field', () => {
|
|
const f = result.findings.find(x => x.title.includes('Unknown MCP server field')
|
|
&& /trust/.test(x.description || ''));
|
|
assert.ok(f, 'a `trust` field must now be reported as an unknown field');
|
|
});
|
|
|
|
it('emits no "trust level" findings at all', () => {
|
|
assert.ok(!result.findings.some(x => /trust level/i.test(x.title)),
|
|
'the invented trust-level checks must be gone');
|
|
});
|
|
});
|
|
|
|
describe('MCP scanner — `alwaysLoad` is a valid field (v5.10 B4, verify-first 2026-06-23)', () => {
|
|
let result;
|
|
let tmpRoot;
|
|
|
|
beforeEach(async () => {
|
|
tmpRoot = await mkdtemp(join(tmpdir(), 'ca-mcp-alwaysload-'));
|
|
// alwaysLoad exempts a server from MCP tool-schema deferral (CC v2.1.121+).
|
|
// Verified against code.claude.com/docs/en/mcp.md#exempt-a-server-from-deferral.
|
|
const mcp = {
|
|
mcpServers: {
|
|
core: { type: 'http', url: 'https://mcp.example.com/mcp', alwaysLoad: true },
|
|
},
|
|
};
|
|
await writeFile(join(tmpRoot, '.mcp.json'), JSON.stringify(mcp, null, 2) + '\n', 'utf8');
|
|
const discovery = await discoverConfigFiles(tmpRoot);
|
|
result = await scan(tmpRoot, discovery);
|
|
});
|
|
|
|
afterEach(async () => {
|
|
if (tmpRoot) await rm(tmpRoot, { recursive: true, force: true });
|
|
});
|
|
|
|
it('does NOT flag `alwaysLoad` as an unknown MCP server field', () => {
|
|
const f = result.findings.find(x => x.title.includes('Unknown MCP server field')
|
|
&& /alwaysLoad/.test(x.description || ''));
|
|
assert.equal(f, undefined, 'alwaysLoad is a valid field and must not be flagged');
|
|
});
|
|
});
|
|
|
|
describe('MCP scanner — env-var false positives (CC 2.1.139/2.1.142, Batch 1)', () => {
|
|
let tmpRoot;
|
|
let envFindings;
|
|
|
|
beforeEach(async () => {
|
|
tmpRoot = await mkdtemp(join(tmpdir(), 'ca-mcp-env-'));
|
|
const mcp = {
|
|
mcpServers: {
|
|
proj: {
|
|
type: 'stdio',
|
|
command: 'node',
|
|
// CLAUDE_PROJECT_DIR is auto-injected (CC 2.1.139); ${VAR%…}/${VAR:-…}
|
|
// are POSIX expansions CC resolves (2.1.142) — none need an env block.
|
|
args: ['${CLAUDE_PROJECT_DIR}/server.mjs', '--root', '${HOME%/}', '--cfg', '${CONFIG_DIR:-/etc}'],
|
|
},
|
|
legacy: {
|
|
type: 'stdio',
|
|
command: 'node',
|
|
args: ['${REAL_MISSING}'],
|
|
},
|
|
},
|
|
};
|
|
await writeFile(join(tmpRoot, '.mcp.json'), JSON.stringify(mcp, null, 2) + '\n', 'utf8');
|
|
const discovery = await discoverConfigFiles(tmpRoot);
|
|
const result = await scan(tmpRoot, discovery);
|
|
envFindings = result.findings.filter(f => f.title === 'Unreferenced env var in args');
|
|
});
|
|
|
|
afterEach(async () => {
|
|
if (tmpRoot) await rm(tmpRoot, { recursive: true, force: true });
|
|
});
|
|
|
|
it('does NOT flag ${CLAUDE_PROJECT_DIR} (auto-injected runtime var)', () => {
|
|
const f = envFindings.find(x => /CLAUDE_PROJECT_DIR/.test(x.description || ''));
|
|
assert.equal(f, undefined, 'CLAUDE_PROJECT_DIR must be allowlisted');
|
|
});
|
|
|
|
it('does NOT flag POSIX expansions ${VAR%…} / ${VAR:-…}', () => {
|
|
const f = envFindings.find(x => /HOME|CONFIG_DIR/.test(x.description || ''));
|
|
assert.equal(f, undefined, 'POSIX operator expansions are not env-var references');
|
|
});
|
|
|
|
it('STILL flags a genuine bare unreferenced var', () => {
|
|
assert.equal(envFindings.length, 1,
|
|
`expected exactly REAL_MISSING; got: ${envFindings.map(f => f.description).join(' | ')}`);
|
|
assert.match(envFindings[0].description || '', /REAL_MISSING/);
|
|
});
|
|
});
|
|
|
|
describe('MCP scanner — empty project', () => {
|
|
let result;
|
|
beforeEach(async () => {
|
|
const discovery = await discoverConfigFiles(resolve(FIXTURES, 'empty-project'));
|
|
result = await scan(resolve(FIXTURES, 'empty-project'), discovery);
|
|
});
|
|
|
|
it('returns skipped status', () => {
|
|
assert.equal(result.status, 'skipped');
|
|
});
|
|
|
|
it('has 0 findings', () => {
|
|
assert.equal(result.findings.length, 0);
|
|
});
|
|
});
|
|
|
|
describe('MCP scanner — minimal project', () => {
|
|
let result;
|
|
beforeEach(async () => {
|
|
const discovery = await discoverConfigFiles(resolve(FIXTURES, 'minimal-project'));
|
|
result = await scan(resolve(FIXTURES, 'minimal-project'), discovery);
|
|
});
|
|
|
|
it('returns skipped when no .mcp.json', () => {
|
|
assert.equal(result.status, 'skipped');
|
|
});
|
|
|
|
it('has 0 findings', () => {
|
|
assert.equal(result.findings.length, 0);
|
|
});
|
|
});
|