Claude MCP skill code injection — dependency vulnerability scanner and patch application endpoints
SA-SKL-INJECTION27 documents four code injection vulnerabilities in MCP skill registry dependency vulnerability scanner and patch application endpoints. All four arise from the same root cause: constructing shell commands with user-controlled path arguments via exec(), which invokes a shell and interprets metacharacters. INJECTION27-001 passes a user-controlled packageJsonPath to snyk test, enabling semicolon injection that exfiltrates all environment variables including API keys; INJECTION27-002 passes a user-controlled workDir to npm audit fix --prefix, enabling space-injection of --scripts-prepend-node-path=auto that executes attacker-controlled lifecycle scripts during the fix pass; INJECTION27-003 passes a user-controlled outputFile to a yarn audit --json > redirection command, enabling injection of a second command that appends server file contents; and INJECTION27-004 passes a user-controlled workDir to npm install --package-lock-only --prefix, enabling injection of --global that installs packages system-wide and modifies the Node.js binary resolution path for all processes.
exec(`snyk test --file=${packageJsonPath}`); attacker packageJsonPath with ; env exfiltrates all environment variables including API keys and database credentials
exec(`npm audit fix --prefix ${workDir}`); attacker workDir with space-injected --scripts-prepend-node-path=auto executes attacker-controlled lifecycle scripts during the audit fix pass
exec(`yarn audit --json > ${outputFile}`); attacker outputFile with ; cat /etc/passwd appended to the shell command exfiltrates server files alongside the audit output
exec(`npm install --package-lock-only --prefix ${workDir}`); attacker workDir with --global flag injection installs packages globally, modifying system-wide npm resolution for all processes
INJECTION27-001 — Snyk path injection exfiltrates environment variables via semicolon
Dependency vulnerability scanner endpoints that run snyk test on uploaded skill packages pass the path to the package.json file as a command-line argument. When the path is interpolated into an exec() template string, shell metacharacters in the path are interpreted by the shell spawned by exec(). An attacker who can control the packageJsonPath value — for example by naming an uploaded file ; env > /tmp/keys; true — inserts a semicolon-separated command that runs env, writing all environment variables (including SNYK_TOKEN, DATABASE_URL, AWS_SECRET_ACCESS_KEY) to /tmp/keys. A second upload of the keys file as a “changelog” then exfiltrates the credentials.
// INJECTION27-001 — VULNERABLE
const { exec } = require('child_process');
const { promisify } = require('util');
const execAsync = promisify(exec);
async function runSnykScan(packageJsonPath) {
// exec() spawns a shell — semicolons in packageJsonPath inject additional commands
const { stdout } = await execAsync(
`snyk test --file=${packageJsonPath} --json` // VULNERABLE
);
return JSON.parse(stdout);
}
The fix for shell injection is always execFile() with an argument array: execFile('snyk', ['test', '--file', packageJsonPath, '--json']) never invokes a shell, so no metacharacters in packageJsonPath are interpreted. Additionally validate the path with a regex and realpathSync() containment check before passing it to any subprocess — path traversal (../../etc/passwd) and symlink attacks are distinct from shell injection but equally dangerous.
// INJECTION27-001 — FIXED
const { execFile } = require('child_process');
const { promisify } = require('util');
const path = require('path');
const fs = require('fs');
const execFileAsync = promisify(execFile);
const UPLOADS_BASE = '/srv/scanner/uploads';
async function runSnykScan(packageJsonPath) {
// Validate path — regex + realpathSync containment check
if (!/^[a-z0-9_\-\/]{1,300}$/.test(packageJsonPath)) throw new Error('Invalid path');
const resolved = fs.realpathSync(packageJsonPath);
if (!resolved.startsWith(UPLOADS_BASE + '/')) throw new Error('Path traversal detected');
// execFile — no shell, no metacharacter interpretation
const { stdout } = await execFileAsync('snyk', ['test', '--file', resolved, '--json'], {
env: { PATH: '/usr/local/bin:/usr/bin', HOME: '/tmp' }, // stripped env — no credential leakage
timeout: 60000,
});
return JSON.parse(stdout);
}
INJECTION27-002 — npm audit fix prefix injection enables attacker lifecycle script execution
Automatic patch application endpoints that run npm audit fix on behalf of the user accept a workDir parameter specifying which uploaded skill package directory to fix. When the directory path is interpolated into an exec() call, an attacker crafts a workDir value with a space followed by --scripts-prepend-node-path=auto: the shell splits the string on the space, treating --scripts-prepend-node-path=auto as an additional npm flag. This flag causes npm to prepend the Node.js binary directory to the PATH during lifecycle script execution, allowing the attacker’s malicious postinstall script in an uploaded dependency to execute with elevated path resolution.
// INJECTION27-002 — VULNERABLE
async function applyNpmAuditFix(workDir) {
// exec() with interpolated workDir — space-injection adds attacker npm flags
const { stdout, stderr } = await execAsync(
`npm audit fix --prefix ${workDir}` // VULNERABLE
);
return { stdout, stderr };
}
// INJECTION27-002 — FIXED
async function applyNpmAuditFix(workDir) {
if (!/^[a-z0-9_\-\/]{1,300}$/.test(workDir)) throw new Error('Invalid workDir');
const resolved = fs.realpathSync(workDir);
if (!resolved.startsWith(UPLOADS_BASE + '/')) throw new Error('Path traversal detected');
// execFile — no shell; --ignore-scripts prevents any lifecycle script execution during fix
const { stdout, stderr } = await execFileAsync('npm', [
'audit', 'fix',
'--prefix', resolved,
'--ignore-scripts', // critical: prevents postinstall/prepare scripts from running
'--no-fund',
'--json',
], {
env: { PATH: '/usr/local/bin:/usr/bin', NPM_CONFIG_CACHE: '/tmp/.npm' },
timeout: 120000,
});
return { stdout, stderr };
}
INJECTION27-003 — yarn audit output file injection appends server file contents
Some implementations pipe yarn audit --json output to a file for later processing. When the output file path is interpolated into an exec() call containing a shell redirection operator (>), an attacker injects a semicolon after the path to append an additional shell command: workdir/results.json; cat /etc/passwd >> workdir/results.json. The shell executes the yarn audit command normally (writing the JSON output to the file), then executes the injected cat command, appending the contents of /etc/passwd to the results file. The combined file is then returned to the user as the audit output.
// INJECTION27-003 — VULNERABLE
async function runYarnAudit(workDir, outputFile) {
// exec() with shell redirection and user-controlled outputFile — command injection
await execAsync(
`yarn audit --json > ${outputFile}` // VULNERABLE — attacker injects ; cat /etc/passwd >> ${outputFile}
);
return fs.readFileSync(outputFile, 'utf8');
}
// INJECTION27-003 — FIXED
// execFile — stdout captured directly; write to file via fs.writeFile with sanitized path.
async function runYarnAudit(workDir) {
const resolved = fs.realpathSync(workDir);
if (!resolved.startsWith(UPLOADS_BASE + '/')) throw new Error('Path traversal detected');
// Capture stdout directly — no shell redirection; no user-controlled output path
const { stdout } = await execFileAsync('yarn', ['audit', '--json'], {
cwd: resolved,
env: { PATH: '/usr/local/bin:/usr/bin', HOME: '/tmp' },
timeout: 60000,
maxBuffer: 10 * 1024 * 1024, // 10 MB limit
});
const outputPath = path.join(resolved, 'audit-results.json');
// Write via fs — no shell, no injection
await fs.promises.writeFile(outputPath, stdout, { mode: 0o600 });
return stdout;
}
INJECTION27-004 — npm install global flag injection hijacks system-wide package resolution
Lock-file generation endpoints that run npm install --package-lock-only --prefix <workDir> to generate an updated lock file without installing packages are vulnerable to flag injection via the workDir parameter. An attacker crafts a workDir containing --global: the shell tokenizes the string and passes --global as an npm flag, causing npm to operate in global installation mode instead of the intended local mode. Combined with a malicious package.json in the uploaded skill package, this installs the attacker’s chosen packages globally — modifying /usr/local/lib/node_modules and the global binary directory for all processes running on the same container or host.
// INJECTION27-004 — VULNERABLE
async function generateLockFile(workDir) {
// exec() with interpolated workDir — attacker injects --global to install system-wide
const { stdout } = await execAsync(
`npm install --package-lock-only --prefix ${workDir}` // VULNERABLE
);
return stdout;
}
// INJECTION27-004 — FIXED
async function generateLockFile(workDir) {
if (!/^[a-z0-9_\-\/]{1,300}$/.test(workDir)) throw new Error('Invalid workDir');
const resolved = fs.realpathSync(workDir);
if (!resolved.startsWith(UPLOADS_BASE + '/')) throw new Error('Path traversal detected');
// execFile — no shell; explicit arg array — --global cannot be injected via resolved path
const { stdout } = await execFileAsync('npm', [
'install',
'--package-lock-only',
'--prefix', resolved, // passed as a value, not shell-tokenized
'--no-scripts', // belt-and-suspenders: no lifecycle scripts
'--ignore-scripts',
'--no-fund',
], {
env: { PATH: '/usr/local/bin:/usr/bin', NPM_CONFIG_CACHE: '/tmp/.npm' },
timeout: 120000,
});
return stdout;
}
Related advisories: For code injection in npm package analysis and static analysis tool invocation endpoints, see SA-SKL-INJECTION25. For code injection in code coverage and test runner invocation endpoints, see SA-SKL-INJECTION26. For privilege escalation in organization role assignment and permission delegation endpoints, see SA-SKL-PRIVESC20.
SkillAudit checks for INJECTION27, INJECTION26, SSRF, CORS, prototype pollution, and 50+ more vulnerability patterns — in 60 seconds, with a shareable grade badge.
Get early access