Clears false positives on valid .mcp.json (gap matrix, Batch 1):
- ${CLAUDE_PROJECT_DIR} is auto-injected at runtime (CC 2.1.139) and never
needs an env block — now allowlisted.
- POSIX expansions like ${VAR%pattern} / ${VAR:-default} are resolved by
Claude Code (CC 2.1.142); the env-var regex now matches only bare
${IDENTIFIER}, so operator expressions are skipped.
Genuine bare unreferenced vars are still flagged (broken-project regression
intact). The MCP `trust` field is untouched — it is verify-first (point 4),
not part of Batch 1.
Tests: hermetic runtime temp-fixture; 23/23 MCP green, both directions covered.
Ref: docs/cc-2.1.x-gap-matrix.md
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Ter3E2JSi1Khgmuf2kady8
190 lines
6.2 KiB
JavaScript
190 lines
6.2 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 { resetCounter } from '../../scanners/lib/output.mjs';
|
|
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 () => {
|
|
resetCounter();
|
|
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 () => {
|
|
resetCounter();
|
|
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('detects missing trust level', () => {
|
|
assert.ok(result.findings.some(f => f.title.includes('Missing trust level')));
|
|
});
|
|
|
|
it('missing trust is medium severity', () => {
|
|
const trust = result.findings.find(f => f.title.includes('Missing trust level'));
|
|
assert.equal(trust.severity, 'medium');
|
|
});
|
|
|
|
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 — env-var false positives (CC 2.1.139/2.1.142, Batch 1)', () => {
|
|
let tmpRoot;
|
|
let envFindings;
|
|
|
|
beforeEach(async () => {
|
|
resetCounter();
|
|
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}'],
|
|
trust: 'workspace',
|
|
},
|
|
legacy: {
|
|
type: 'stdio',
|
|
command: 'node',
|
|
args: ['${REAL_MISSING}'],
|
|
trust: 'workspace',
|
|
},
|
|
},
|
|
};
|
|
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 () => {
|
|
resetCounter();
|
|
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 () => {
|
|
resetCounter();
|
|
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);
|
|
});
|
|
});
|