| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Add a RedirectPolicy enum to create_mcp_http_client() that validates redirect targets via httpx event hooks. The default policy (BLOCK_SCHEME_DOWNGRADE) blocks HTTPS-to-HTTP redirect downgrades. ENFORCE_HTTPS restricts all redirects to HTTPS-only destinations. ALLOW_ALL preserves the previous unrestricted behavior. Github-Issue: modelcontextprotocol#2106
response.next_request is not populated by httpx when follow_redirects=True, causing the redirect protection hook to silently bypass all checks. Parse the Location header directly (matching httpx's own _build_redirect_request flow) and add integration tests via MockTransport to exercise the event hook wiring end-to-end. Github-Issue: modelcontextprotocol#2106
The Protocol describes the caller-side contract. No caller passes redirect_policy to the factory (sse_client only passes headers, timeout, auth). Adding it would break downstream code implementing custom factories. Github-Issue: modelcontextprotocol#2106
Add docstring note on the http_client parameter of streamable_http_client() clarifying that user-provided clients do not receive SSRF redirect protection. Also emit a logger.debug when a user-provided client is used. Github-Issue: modelcontextprotocol#2106
…anch Add test for relative Location headers (exercises the is_relative_url branch). Mark unreachable 200-response fallback in blocking test with pragma: no cover. Github-Issue: modelcontextprotocol#2106
|
Thanks for the PR. Tracking this in #3075 instead. Closing as part of a general backlog cleanup following the v2 release. If this is still relevant against v2, feel free to reopen. |
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
What changed
Adds a RedirectPolicy enum and redirect validation event hook to create_mcp_http_client() in src/mcp/shared/_httpx_utils.py to protect against SSRF attacks via HTTP redirects.
Changes:
src/mcp/shared/_httpx_utils.py:
src/mcp/client/streamable_http.py:
tests/shared/test_httpx_utils.py:
Why
A malicious MCP server can respond with a 3xx redirect pointing to internal network addresses (http://169.254.169.254, http://localhost, etc.), enabling SSRF attacks. The SDK previously set follow_redirects=True with zero redirect validation. The default BLOCK_SCHEME_DOWNGRADE policy prevents the most common SSRF vector (HTTPS→HTTP downgrade to reach internal services) while remaining backward-compatible for legitimate use cases.
Bug discovered during review
The initial implementation used response.next_request to inspect redirect targets, but httpx never populates next_request when follow_redirects=True — causing the protection to silently bypass all checks. Fixed to use response.has_redirect_location + parse the Location header directly, matching httpx's own _build_redirect_request flow.
How verified
Tradeoffs / risks
Scope notes
Closes #2106