← back to projects
~/projects/mcp-hardening-lab

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.

CI status

View on GitHub ↗

56tests passing
3labs, each vulnerable + fixed
3Git exec sinks closed
5 / 5policy bypasses blocked
421/403forged Host / Origin rejected on every route

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.

advisoryaffectedfixed in
Lab 01GHSA-6hqx-gqrx-c5r2 ↗mcp-server-git before 2026.11.112026.11.11
Lab 02GHSA-4wq9-grf3-qg2p ↗awslabs.aws-api-mcp-server ≥0.2.13, <1.5.11.5.1
Lab 03GHSA-9x8x-crjq-g64h ↗jupyter-mcp-server ≤1.0.21.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 keytriggered bywhat Git does
core.fsmonitorgit statusruns the named program to list changed files
diff.externalgit diffhands the diff to the named program
filter.<x>.cleangit diff on the work treepipes 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.

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.

operationvulnerablefixed
iam:CreateSAMLProviderallow, executeddeny
iam:DeactivateMFADeviceallow, executedelicit (held)
rds:DeleteDBClusterallow, executeddeny
rds:DeleteDBInstanceallow, executeddeny
iam:UploadSSHPublicKeyallow, executeddeny
ec2:TerminateInstancesdenydeny
ec2:DescribeInstancesallowallow

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.

requestvulnerablefixed
PUT /api/connect, local Host and Origin200200
PUT /api/connect, Host: attacker.example200, backend swapped421
PUT /api/connect, Origin: http://attacker.example—403
POST /mcp, Host: attacker.example421421
POST /mcp, local Host and Origin200200

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 && make test
$ 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.

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_path to 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

localonly
nonetwork
noreal cloud

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.

09. stack

$ git clone → view repo ↗