|
| 1 | +--- |
| 2 | +description: MCP (Model Context Protocol) Security based on CoSAI MCP Security guidelines |
| 3 | +languages: |
| 4 | +- python |
| 5 | +- javascript |
| 6 | +- typescript |
| 7 | +- go |
| 8 | +- rust |
| 9 | +alwaysApply: false |
| 10 | +--- |
| 11 | + |
| 12 | +rule_id: codeguard-0-mcp-security |
| 13 | + |
| 14 | +# MCP (Model Context Protocol) Security Guidelines |
| 15 | + |
| 16 | +NEVER deploy MCP servers or clients without implementing proper security controls. |
| 17 | + |
| 18 | + |
| 19 | +### Workload Identity and Authentication |
| 20 | +- Use SPIFFE/SPIRE for cryptographic workload identities |
| 21 | + - SPIFFE (Secure Production Identity Framework For Everyone) provides a standard for service identity |
| 22 | + - SPIRE (SPIFFE Runtime Environment) issues and rotates short-lived cryptographic identities (SVIDs) |
| 23 | + |
| 24 | + |
| 25 | +### Input and Data Sanitization |
| 26 | +- Validate ALL inputs using allowlists at every trust boundary |
| 27 | +- Sanitize file paths through canonicalization |
| 28 | +- Use parameterized queries for database operations |
| 29 | +- Apply context-aware output encoding (SQL, shell, HTML) |
| 30 | +- Treat ALL AI-generated content as untrusted input |
| 31 | +- Deploy prompt injection detection systems |
| 32 | +- Use strict JSON schemas to maintain boundaries between instructions and data |
| 33 | + |
| 34 | +### Sandboxing and Isolation |
| 35 | +- Design MCP servers to execute with least privilege |
| 36 | +- MCP servers interacting with host environment (files, commands, network) MUST implement sandboxing controls |
| 37 | +- LLM-generated code MUST NOT run with full user privileges |
| 38 | +- Implement additional sandboxing layers: gVisor, Kata Containers, SELinux sandboxes |
| 39 | + |
| 40 | +### Cryptographic Verification of Resources |
| 41 | +- Provide cryptographic signatures and SBOMs for all server code |
| 42 | +- Implement signature verification in your MCP client before loading servers |
| 43 | +- Use TLS for ALL data in transit |
| 44 | +- Implement remote attestation capabilities to verify servers are running expected code |
| 45 | + |
| 46 | +### Transport Layer Security |
| 47 | + |
| 48 | +#### stdio Transport (Local Servers) |
| 49 | +- STRONGLY RECOMMENDED for local MCP to eliminate DNS rebinding risks |
| 50 | +- Direct pipe-based stream communication |
| 51 | +- Implement sandbox to prevent privilege escalation |
| 52 | + |
| 53 | +#### HTTP Streaming Transport (Remote Servers) |
| 54 | +Required security controls to implement: |
| 55 | +- Payload Limits (prevent large payload and recursive payload DoS) |
| 56 | +- Client-Server Authentication/Authorization |
| 57 | +- Mutual TLS Authentication |
| 58 | +- TLS Encryption |
| 59 | +- CORS Protection |
| 60 | +- CSRF Protection |
| 61 | +- Integrity Checks (prevent replay, spoofing, poisoned responses) |
| 62 | + |
| 63 | +### Human-in-the-Loop |
| 64 | +- Implement confirmation prompts for risky operations in your MCP server |
| 65 | +- Use elicitation on MCP server side to request user confirmation of risky actions |
| 66 | +- Security-relevant messages MUST clearly indicate implications |
| 67 | +- Do NOT rely solely on human approval (users can become fatigued) |
| 68 | + |
| 69 | +### Logging and Observability |
| 70 | +- Implement logging in your MCP servers and clients |
| 71 | +- Log: tools decided to use, parameters, originating prompt |
| 72 | +- Use OpenTelemetry for end-to-end linkability of actions |
| 73 | +- Maintain immutable records of actions and authorizations |
| 74 | + |
| 75 | +--- |
| 76 | + |
| 77 | +## Deployment Pattern Security |
| 78 | + |
| 79 | +### All-Local (stdio or http) |
| 80 | +- Security depends entirely on host system posture |
| 81 | +- Use `stdio` transport to avoid DNS rebinding risks |
| 82 | +- Use sandboxing to limit privilege escalation attacks |
| 83 | +- Appropriate for development and personal use |
| 84 | + |
| 85 | +### Single-Tenant Remote (http) |
| 86 | +- Authentication between client and server is REQUIRED |
| 87 | +- Use secure credential storage (OS keychains, secret managers) |
| 88 | +- Communication MUST be authenticated and encrypted |
| 89 | +- Enterprise clients should enforce authenticated server discovery with explicit allowlists |
| 90 | + |
| 91 | +### Multi-Tenant Remote (http) |
| 92 | +- Require robust tenant isolation, identity, and access control |
| 93 | +- Implement strong multi-tenancy controls (per-tenant encryption, role-based access control) |
| 94 | +- Prefer MCP servers hosted directly by service provider |
| 95 | +- Provide remote attestation when possible |
| 96 | + |
| 97 | +You must always explain how this rule was applied and why it was applied. |
0 commit comments