Skip to content

Latest commit

 

History

History
37 lines (29 loc) · 1.59 KB

File metadata and controls

37 lines (29 loc) · 1.59 KB

MCP Server Security in Go

Companion code for the MCP Server Security in Go — Hardening Your Server tutorial. A minimal Model Context Protocol server built with mark3labs/mcp-go that exposes a read_doc tool over stdio — and starts deliberately vulnerable to path traversal so the tutorial can break it, then harden it.

The hardening steps are marked STEP 1STEP 5 in main.go:

  1. safeJoin — confine paths to the docs directory (kills traversal)
  2. Input limits — length cap + .md extension allowlist
  3. Output hygiene — response size cap, and label served content as untrusted data
  4. Bound time and volume — context timeout + rate limiter, so a surge (malicious or otherwise) gets a tool error instead of service
  5. Tool annotations — declare read_doc read-only/idempotent; flag destructive tools so clients put a human in the loop

Run it

go build -o te-mcp-secure .
./te-mcp-secure   # speaks MCP over stdio

Reproduce the attack

printf '%s\n' \
'{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"smoke","version":"1.0"}}}' \
'{"jsonrpc":"2.0","method":"notifications/initialized"}' \
'{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"read_doc","arguments":{"path":"../../../../etc/passwd"}}}' \
| ./te-mcp-secure

Before the hardening steps this leaks /etc/passwd; after, it returns a tool error and the process stays up.