DEV Community

Cover image for How to Secure MCP Servers in Claude Desktop & Cursor IDE (Stop Injection Attacks)
Denis
Denis

Posted on Originally published at pixeloffice.eu

How to Secure MCP Servers in Claude Desktop & Cursor IDE (Stop Injection Attacks)

The Model Context Protocol (MCP) has rapidly become the universal open standard for giving AI coding models and desktop assistants access to local tools, databases, and APIs. However, connecting unvalidated MCP servers into Claude Desktop, Cursor IDE, or Windsurf opens severe attack vectors.

The Core Problem: Unchecked Stdio & Shell Privileges

MCP tools run with the full execution privileges of your local user account. An unescaped parameter in a shell execution or file-reading tool allows a hijacked prompt or malicious web context to execute arbitrary code or exfiltrate private SSH keys.

// VULNERABLE PATTERN:
server.setRequestHandler(CallToolRequestSchema, async (request) => {
  if (request.params.name === "run_script") {
    // Dangerous: metacharacters like ; | & ` are unescaped!
    exec(`node ${request.params.arguments.scriptPath}`);
  }
});
Enter fullscreen mode Exit fullscreen mode

3 Critical Attack Vectors

  1. Arbitrary Shell Escape: Unsanitized arguments passed to child processes allow injected commands (; rm -rf / or curl attacker.com).
  2. Directory Traversal: Tools reading files without root boundary checks allow path escapes (../../../../.env or ~/.ssh/id_rsa).
  3. Missing Schema Restraints: Omitting additionalProperties: false enables silent payload key injection.

Sub-35ms Automated Mitigation with MCP-Shield

You can audit any local MCP configuration instantly:

npx @pixeloffice-eu/mcp-shield scan
Enter fullscreen mode Exit fullscreen mode

Or use the interactive web visualizer at MCP-Shield Studio.

Hardened Zero-Dependency Safe Wrapper (Node.js)

// safe-mcp-wrapper.cjs — Zero-Dependency Hardened Sentinel
const path = require('path');
const ALLOWED_ROOT = process.env.MCP_SANDBOX_DIR || process.cwd();

function sanitizePath(untrustedPath) {
  const resolved = path.resolve(ALLOWED_ROOT, untrustedPath);
  if (!resolved.startsWith(ALLOWED_ROOT)) {
    throw new Error(`[MCP-Shield] Security Violation: Path '${untrustedPath}' escapes sandbox directory.`);
  }
  return resolved;
}

function sanitizeCommandArg(arg) {
  if (typeof arg !== 'string' || /[;&|$`><!\\]/.test(arg)) {
    throw new Error(`[MCP-Shield] Injection Blocked: Command argument contains forbidden shell metacharacters.`);
  }
  return arg.trim();
}

module.exports = { sanitizePath, sanitizeCommandArg };
Enter fullscreen mode Exit fullscreen mode

Live Testing & Attestation

You can test your own tool schemas and export downloadable compliance certificates directly at:
👉 Pixel Office MCP-Shield

Top comments (0)