modelcontextprotocol / modelcontextprotocol/python-sdk
DNS rebinding :* allowlist matches Host/Origin suffixes that are not ports
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 24.3k
- 分支
- 4k
- 平均合併
- 1 天 1 小時
- 30 天內合併 PR
- 31
描述
What happened
TransportSecurityMiddleware treats allowed_hosts / allowed_origins entries that end in :* as a prefix match: value.startswith(base + ":").
With allowed_hosts=["127.0.0.1:*"] or ["wild.example:*"], these Host values are accepted today:
127.0.0.1:8080.evilwild.example:9000.evil
The same pattern accepts Origin http://wild.example:9000.evil for http://wild.example:*.
Existing tests only cover a numeric port (wild.example:9000). They do not cover a suffix after the port.
What I expected
base:* should mean base plus a numeric port, not any string that starts with base:.
How to reproduce
On main @ 08a3bc8:
from mcp.server.transport_security import TransportSecurityMiddleware, TransportSecuritySettings
from starlette.requests import Request
settings = TransportSecuritySettings(
enable_dns_rebinding_protection=True,
allowed_hosts=["wild.example:*"],
allowed_origins=["http://wild.example:*"],
)
mw = TransportSecurityMiddleware(settings)
req = Request({"type": "http", "method": "GET", "headers": [(b"host", b"wild.example:9000.evil")]})
# validate_request returns None (accept). I expected 421.
I can send a PR that requires the suffix after base: to be digits, plus tests for the suffix cases. Happy to do that if you want it.
Written with AI assistance. I read the matcher next to tests/server/test_transport_security.py and reproduced it locally.
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
從 mcp/server/transport_security.py 開始,其中 TransportSecurityMiddleware 會比對 allowed_hosts 和 allowed_origins,接著閱讀 tests/server/test_transport_security.py。重現回報的後綴案例,並將其與現有的數字連接埠涵蓋範圍進行比較。完成的標準是數字連接埠仍然會被接受,而像 wild.example:9000.evil 這樣的值以及相符的 Origin 會被拒絕。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- security
- Issue 類型
- 缺陷
- 難度
- 2/5
- 預估耗時
- 1-3 小時
- 活躍度
- 活躍
- 描述清晰度
- 描述清楚
- 新手友好度
- 84/100