Make Origin validation opt-in in the app factories (keep Host validation on for localhost)
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Anfängerfreundlichkeit
- 45/100
Rechercherichtung
The change is in TransportSecurityMiddleware, likely in mcp/server/transport_security.py or a similar module. Start by reading the middleware's current Host and Origin validation logic. The fix involves making Origin validation conditional on a non-empty allowed_origins list and removing default localhost allowed_origins in MCPServer and lowlevel.Server. Test with browser extension clients to verify Origin headers no longer cause 403 errors. Update docs/run/deploy.md to reflect the new behavior.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
Description
When enable_dns_rebinding_protection=True, TransportSecurityMiddleware checks Host and Origin together. It can't do one without the other, and an empty allowed_origins means any request that carries an Origin header is rejected. MCPServer and lowlevel.Server also turn this on by default for localhost binds, with a localhost-only allowed_origins.
This blocks browser-based MCP clients such as extensions. A Chrome extension sends Origin: chrome-extension://<id>. The same request with the same credentials gets 200 without an Origin header and 403 with one. The only fix is for every server operator to allowlist each client by hand.
The Origin check doesn't add rebinding protection. The DNS-rebinding advisory (GHSA-9h52-p55h-vw2f) is closed by the Host check. An Origin check is a CSRF control, and it only matters when the server accepts credentials the browser sends automatically, such as cookies. The full threat model is in modelcontextprotocol/modelcontextprotocol#3370.
The Go SDK made this change already. Host protection on loopback is on by default. Origin protection was on by default in v1.4.1 through v1.5.0 and has been off since v1.6.0. The setting that could restore it was removed in v1.8.0.
Proposal:
- Validate
Originonly whenallowed_originsis non-empty. An empty list means no Origin check, not reject-all. - Drop the default localhost
allowed_originsinMCPServerandlowlevel.Server. - Host validation and its defaults are unchanged. When the Origin check is on,
Origin: nullis still rejected. - Update
docs/run/deploy.mdto describeallowed_originsas the CSRF control andallowed_hostsas the DNS-rebinding control.
This doesn't reopen CSRF on unauthenticated localhost servers. The middleware already returns 400 for any POST whose Content-Type isn't application/json, and a cross-site JSON POST forces a CORS preflight. I'd add a test that pins this.
Compatibility: there are two behaviour changes, and I'd like a maintainer's view on whether a 2.x minor release is OK for them.
- Localhost servers using the defaults would no longer reject cross-origin requests that pass the Host and Content-Type checks.
- Anyone who sets
allowed_hostswithoutallowed_originswould no longer reject every request that carries anOriginheader.
In both cases, setting allowed_origins explicitly restores today's behaviour.
Further proposals
-
Scheme wildcards in
allowed_origins. Once Origin validation is opt-in, an operator who turns it on still has to list every browser-extension client by hand. That's impossible for Firefox, whosemoz-extension://<uuid>origin differs on every install. Proposal:- An entry of the form
"<scheme>://*"matches anyOriginwith that scheme, for example"chrome-extension://*"or"moz-extension://*". - Exact entries and the existing
:*port wildcard keep working as today. "http://*"and"https://*"raise aValueError, because they would allow every website. Leavingallowed_originsempty already does that.- This would build on the fix for #3463 (the
:*suffix bug), since it touches the same matcher.
- An entry of the form
-
A machine-readable reason in the rejection body. Today a Host rejection is
421 Invalid Host headerand an Origin rejection is403 Invalid Origin header, both as plain text. A client can't reliably tell these from an auth failure, so it can't tell the user that signing in again won't help. Proposal: keep the status codes, but return a JSON-RPC error body whosedata.reasoncarries a stable code:{ "jsonrpc": "2.0", "error": { "code": -32000, "message": "Invalid Origin header", "data": { "reason": "invalid_origin" } }, "id": null }The reason values would match the ones the TypeScript SDK already computes internally (
missing_host,invalid_host,invalid_origin_header,invalid_origin), so clients can handle both SDKs the same way.
Disclosure: drafted with Claude Code;
- Vorherrschende Sprache
- Python
- Sterne
- 24.3k
- Forks
- 4k
- Ø Merge
- 1 T. 16 Std.
- Gemergte PRs (30 T.)
- 25
Beitragsleitfaden
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus modelcontextprotocol/python-sdk
-
v1 v2
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 70/100
modelcontextprotocol/python-sdk#3578 · 1 Kommentar ·
-
v1 v2
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 65/100
modelcontextprotocol/python-sdk#3573 · 2 Kommentare ·
-
v2
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
modelcontextprotocol/python-sdk#3566 ·
-
v1 v2
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 85/100
modelcontextprotocol/python-sdk#3546 · 5 Kommentare ·
-
v1 v2
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
modelcontextprotocol/python-sdk#3545 · 2 Kommentare ·
Alle Issues in modelcontextprotocol/python-sdk
Ähnliche Issues
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
stephrobert/dsoxlab#238 ·
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
sublimehq/package_control#1780 ·
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 65/100
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 70/100
nwg-piotr/nwg-displays#145 ·