Day 6, 30 minutes · advanced
Why it matters
An MCP server is a door into real systems: a stock list, a wish list, later maybe a till or a customer file. Every door gets tried. The MCP project keeps a Security Best Practices page that names the attacks seen so far and what stops each one. Knowing them is the difference between a demo and a server you can put on the internet.
In this lesson you walk through those attacks with Romy's servers in mind, and you test one defence that is already on: the guard against DNS rebinding. At the end you have seen Romy's server refuse a forged request, and you have a security review that says, for each attack, what protects her and what she still needs before going live.
The words you need today
- Attack vector: the route an attacker uses to get in.
- Mitigation: what you do to block or limit an attack.
- Host header and Origin header: two HTTP headers that say which site a request is for and which web page sent it.
- Proxy server: an MCP server that passes work on to another service's API.
The attacks and their mitigations
From the Security Best Practices page, in short:
| Attack | What happens | Mitigation |
|---|---|---|
| Confused deputy | An MCP proxy that uses one fixed client ID with another service can be tricked into skipping the user's consent | Ask for consent per client, check redirect addresses exactly, check the OAuth state value |
| Token passthrough | A server accepts a token that was not issued for it and forwards it | Never accept tokens that were not issued for this server |
| Server-side request forgery (SSRF) | A malicious server puts internal addresses in its OAuth metadata, and the client fetches them | Clients use HTTPS and block private address ranges |
| State handle hijacking | Someone guesses or steals an ID such as a cart or workflow ID and uses it as if it were theirs | Never treat an ID as proof of who calls; use random IDs bound to the user from the token |
| Local server compromise | A config contains a harmful start command, or a local server is reached through DNS rebinding | Show the exact command, ask for consent, sandbox, keep local servers locked to localhost, require a token or use stdio |
| Too-broad scopes | A stolen token with every permission can do everything | Start with few scopes, ask for more only when needed |
The page has more for client builders, such as checking authorization URLs and mix-up attacks. For a server builder, the rows above are the core.
DNS rebinding, tested
The Streamable HTTP page says servers must check the Origin header and answer an invalid one with 403, and should bind to 127.0.0.1 when running locally. The Python SDK's deploy page explains what it does by default. With no settings, it only accepts requests whose Host header is 127.0.0.1, localhost or [::1] with the port, and an Origin, when there is one, with the same address. A wrong Host gets 421 Misdirected Request, a wrong Origin gets 403 Forbidden. Your test script sends exactly those two forged requests.
Before a server leaves localhost
The same deploy page warns about a trap. Setting a host other than localhost does not add that name to the allowed list. It only switches the default protection off, so every Host and Origin is accepted. The right way is to list the allowed hosts and origins with transport_security. Put that on Romy's list together with these points from the authorization tutorial:
- Serve over HTTPS, for example behind a proxy that handles the certificate.
- Use an existing authorization server, and a well-tested library to check tokens, not your own code.
- Always check the audience and accept one issuer only.
- Never write
Authorizationheaders, tokens or codes to your logs. - Return a 5xx error, not a 401, when the token check itself is down, so users are not sent to sign in for nothing.
What this means for Romy
Romy's wish list stores whatever customer name the client sends. The elicitation page warns that a server must not rely on a user name the client provides, because it can be forged: it should identify the user from the verified token. In quillfeather_secure.py the token already says who calls, so that is where a real wish list would take the user from.
The steps
- Make sure Romy's HTTP server is running on 127.0.0.1.
- Create the test script, which sends a forged Host and a forged Origin.
- Run it and read the two refusals.
- Find the warnings in the server log.
- Write the security review for Romy's servers.
Follow the steps below.
Today's actions
Ticks are not saved without an accountDo it in Claude API
Claude API
- Make sure
quillfeather_remote.pyruns in its own terminal, on host 127.0.0.1 as set in lesson 1.uv run quillfeather_remote.py - In a second terminal, create
check_hosts.pyin the quillfeather folder. It sends two requests the way a hostile web page could, with a strange Host header and a strange Origin header."""Send two requests a web page could forge and print how Romy's server answers.""" import json import urllib.error import urllib.request URL = "http://127.0.0.1:8000/mcp" BODY = json.dumps({"jsonrpc": "2.0", "id": 1, "method": "tools/list", "params": {}}).encode() TESTS = { "foreign Host header": {"Host": "shop.example.com"}, "foreign Origin header": {"Origin": "https://evil.example.com"}, } for name, extra in TESTS.items(): headers = {"Content-Type": "application/json", "Accept": "application/json, text/event-stream"} headers.update(extra) request = urllib.request.Request(URL, data=BODY, headers=headers, method="POST") try: with urllib.request.urlopen(request) as response: print(f"{name}: {response.status} (accepted)") except urllib.error.HTTPError as error: print(f"{name}: {error.code} {error.read().decode()}") - Run it. You should see
foreign Host header: 421 Invalid Host headerandforeign Origin header: 403 Invalid Origin header. Nothing reached your tools.uv run check_hosts.py - Look in the server terminal. Each refusal shows up as a WARNING line with Invalid Host header or Invalid Origin header and the value that was refused.
- Create
security-review.txtin the quillfeather folder with this table and fill in the last column for Romy's servers, using the body of this lesson.Security review: Romy's MCP servers Attack Risk for Romy Protection DNS rebinding a web page drives quillfeather_remote.py Token passthrough her server forwards a token it got Confused deputy a proxy skips the user's consent State handle hijacking someone uses another person's wish list ID Over-broad scopes one stolen token can do everything Local server compromise a bad command in an MCP config SSRF a client is sent to an internal address Before the server leaves localhost: - Add one line at the bottom: what Romy must set before her server leaves localhost (the allowed hosts and origins, HTTPS and an authorization server). Stop the server with Ctrl+C.
Note: Do not change host to 0.0.0.0 to test from another device. The SDK docs say a non-localhost host switches the Host check off unless you also set allowed hosts, and the server would then be open to your whole network.
Create a free account to save your progress
Free, and no payment details. Your ticks and finished lessons are kept, on every device.
Create a free account I already have an accountCreate a free account to save your progress. Sign in or sign up