MCP hardening
Three MCP server bug classes published in November 2026, rebuilt as a local lab. Each one ships a deliberately vulnerable server, a fixed twin, and tests that show the bug on one and the block on the other. No model, no AWS, no network, no real DNS.
01. why it matters
AI agents now call tools through MCP servers: read a repo, call a cloud API, drive a local service. Operators trust three things: that a "read-only" tool only reads, that a deny list actually denies, and that a server on 127.0.0.1 is only reachable locally. Each lab breaks one of those assumptions. The lab shows how, and what a fix that holds looks like.
| advisory | affected | fixed in | |
|---|---|---|---|
| Lab 01 | GHSA-6hqx-gqrx-c5r2 ↗ | mcp-server-git before 2026.11.11 | 2026.11.11 |
| Lab 02 | GHSA-4wq9-grf3-qg2p ↗ | awslabs.aws-api-mcp-server ≥0.2.13, <1.5.1 | 1.5.1 |
| Lab 03 | GHSA-9x8x-crjq-g64h ↗ | jupyter-mcp-server ≤1.0.2 | 1.0.3 |
Same missing Host/Origin check, on the /mcp route itself: GHSA-833v-v58w-294h (@doist/todoist-mcp ≤10.3.2) and GHSA-mx6p-3fg7-v6pj (obsidian-sync-mcp ≤0.5.6).
02. lab 01: when git status runs code
The bug. The Git tool runs inside whatever repository the client points it at. That repository's own .git/config can name a program, and Git runs it on ordinary commands:
| config key | triggered by | what Git does |
|---|---|---|
| core.fsmonitor | git status | runs the named program to list changed files |
| diff.external | git diff | hands the diff to the named program |
| filter.<x>.clean | git diff on the work tree | pipes file content through the named program |
The scenario. An agent is asked to look at an unpacked archive or a copied folder. It calls git_status. The repo's config names a script, and the script runs with the MCP server's environment, including any tokens it was given. Nothing in the tool name said "execute".
# vulnerable.py subprocess.run(["git", "-C", repo_path, "-c", "core.quotepath=false", *args]) # fixed.py MIN_GIT = (2, 31) HARDENED_CONFIG = (("core.fsmonitor", "false"), ("core.hooksPath", os.devnull), ("core.pager", "cat"), ("submodule.recurse", "false"), ...) DIFF_FLAGS = ("--no-ext-diff", "--no-textconv") SUBMODULE_FLAG = "--ignore-submodules=dirty"
The fix.
- Every Git call gets
GIT_CONFIG_COUNToverrides, which outrank.git/config:core.fsmonitor=false, hooks path on the null device,core.pager=cat, signing and submodule recursion off. - Filter, merge and
branch.*.mergeOptionsdrivers defined by the repo (scopelocalorworktree) are blanked. Global and system drivers such as Git LFS stay. - Diffs run with
--no-ext-diff --no-textconv; status and diff with--ignore-submodules=dirty. - The tool refuses Git older than 2.31 (it ignores the overrides) and checks that the override really applied before running anything.
03. lab 02: a deny list that does not deny
The bug. The operator writes deny entries in CLI form, like iam create-saml-provider. The server turns the requested name into kebab case by putting a hyphen before every capital, so CreateSAMLProvider becomes create-s-a-m-l-provider. The strings never match, so the call is allowed. Names with consecutive capitals (SAML, MFA, DB, SSH) all fall through; TerminateInstances does not, which is why a test with only simple names stays green.
# vulnerable re.sub(r"([A-Z])", r"-\1", op).lower().lstrip("-") # fixed: same function for policy AND request def canonical_token(operation): return re.sub(r"[^a-z0-9]", "", operation.lower())
The fix. One resolve function strips separators and case, then looks the name up in a small service model. Policy entries and requests both go through it, so the three spellings are the same operation. It fails closed: an unknown name is denied, a policy entry that does not resolve is a startup error, elicit never runs without a human confirmation, and the decision is applied at the call itself.
| operation | vulnerable | fixed |
|---|---|---|
| iam:CreateSAMLProvider | allow, executed | deny |
| iam:DeactivateMFADevice | allow, executed | elicit (held) |
| rds:DeleteDBCluster | allow, executed | deny |
| rds:DeleteDBInstance | allow, executed | deny |
| iam:UploadSSHPublicKey | allow, executed | deny |
| ec2:TerminateInstances | deny | deny |
| ec2:DescribeInstances | allow | allow |
The last two rows are controls: the vulnerable build does read the policy, and the fix does not block legitimate calls.
04. lab 03: DNS rebinding on a side route
The bug. DNS rebinding keeps an attacker's hostname in the Host header while the connection lands on 127.0.0.1, so a browser page can reach a loopback server. The Python SDK can validate Host and Origin on streamable HTTP, but that check runs inside the /mcp handler only. A custom route on the same app, here PUT /api/connect, never goes through it: /mcp already answers 421 while the side route accepts the forged Host and swaps the backend.
The fix. One HostOriginMiddleware on the whole app calls the SDK's validator before every route, including routes added later. Host outside 127.0.0.1, localhost, [::1] → 421. Browser Origin outside the matching list → 403. A missing Origin (non-browser client) is allowed, as in the SDK. The connect handler only accepts a memory:// backend.
| request | vulnerable | fixed |
|---|---|---|
| PUT /api/connect, local Host and Origin | 200 | 200 |
| PUT /api/connect, Host: attacker.example | 200, backend swapped | 421 |
| PUT /api/connect, Origin: http://attacker.example | — | 403 |
| POST /mcp, Host: attacker.example | 421 | 421 |
| POST /mcp, local Host and Origin | 200 | 200 |
The /mcp rows are controls: the SDK check works where it applies; the gap is everything beside it.
05. demo
make demo runs each lab vulnerable, then fixed. Real output, captured with Git 2.43.0 (marker ids replaced by <id>):
$ make demo === Lab 01: repository Git config -> code execution === payload: marker.sh writes one file and nothing else vulnerable git_status fsmonitor marker WRITTEN /tmp/mcp-lab-pwned-<id> fixed git_status fsmonitor marker absent /tmp/mcp-lab-pwned-<id> vulnerable git_diff_unstaged diff.external marker WRITTEN /tmp/mcp-lab-pwned-<id> fixed git_diff_unstaged diff.external marker absent /tmp/mcp-lab-pwned-<id> vulnerable git_diff_unstaged filter.clean marker WRITTEN /tmp/mcp-lab-pwned-<id> fixed git_diff_unstaged filter.clean marker absent /tmp/mcp-lab-pwned-<id> === Lab 02: policy bypass via operation-name normalization === naive kebab of CreateSAMLProvider = create-s-a-m-l-provider policy deny entry = iam create-saml-provider vulnerable iam:CreateSAMLProvider decision=allow executed=True fixed iam:CreateSAMLProvider decision=deny executed=False vulnerable iam:DeactivateMFADevice decision=allow executed=True fixed iam:DeactivateMFADevice decision=elicit executed=False vulnerable rds:DeleteDBCluster decision=allow executed=True fixed rds:DeleteDBCluster decision=deny executed=False vulnerable rds:DeleteDBInstance decision=allow executed=True fixed rds:DeleteDBInstance decision=deny executed=False vulnerable iam:UploadSSHPublicKey decision=allow executed=True fixed iam:UploadSSHPublicKey decision=deny executed=False vulnerable ec2:TerminateInstances decision=deny executed=False fixed ec2:TerminateInstances decision=deny executed=False vulnerable ec2:DescribeInstances decision=allow executed=True fixed ec2:DescribeInstances decision=allow executed=True === Lab 03: DNS rebinding on a custom MCP route === payload: PUT /api/connect sets an in-memory marker and does not open a socket vulnerable PUT /api/connect local Host and Origin status=200 marker=local-ok vulnerable PUT /api/connect Host=attacker.example status=200 marker=rebound fixed PUT /api/connect local Host and Origin status=200 marker=local-ok fixed PUT /api/connect Host=attacker.example status=421 marker=local-ok fixed PUT /api/connect Origin=http://attacker.example status=403 marker=local-ok vulnerable POST /mcp Host=attacker.example status=421 marker=rebound fixed POST /mcp Host=attacker.example status=421 marker=local-ok vulnerable POST /mcp local Host and Origin status=200 marker=rebound fixed POST /mcp local Host and Origin status=200 marker=local-ok $ make test 56 passed
The demo deletes the marker and the temp repo after printing. make test: 56 passed on the same tree. CI runs pytest and the Docker build on every push.
06. operator checklist
Excerpt from CHECKLIST.md.
- Override command-bearing Git config on every call, and confirm the override applied before running.
- Refuse Git < 2.31: a warning is not a fix.
- A read-only tool name is not a sandbox. Do not hand secrets to a Git server that can be pointed at a repo you did not create.
- Confine the server to an allowlisted root, and still treat nested checkouts, archives and copied folders as untrusted.
- Normalize policy and request with the same function, and test with names like SAML, MFA, DB, SSH.
- Fail closed: unknown names denied, unresolvable policy entries stop startup, no policy data means no calls.
- Check Host and Origin with one middleware on every route, not only
/mcp. Binding to127.0.0.1is not a substitute. - Host/Origin checks are not authentication: add a credential on any state-changing route a browser can reach.
07. limits
- Simplified re-implementations that show the bug class and the fix, not drop-in copies of the upstream servers.
- Lab 01 does not jail
repo_pathto a root; that is an operator control listed in the checklist. - Lab 01 reads config one entry per line, so a driver value containing a newline is out of scope (upstream uses a NUL-delimited listing).
- Lab 02's fail-closed default is stricter than the patched upstream, which still allows a call when no list matches.
- Lab 03 does not replay real DNS: forged Host and Origin headers are set on an in-process client, and no port is bound by the demo. It does not add the bearer credential upstream 1.0.3 put on its management routes.
08. safe by design
The payload writes one marker file under /tmp and nothing else: no shell, no network, no credentials. Lab 03 only flips an in-memory marker and rejects any backend that is not memory://. The cloud is a dictionary in memory. Run it only on a machine you control, against repos you created for the lab.