Title: fix: add SSRF redirect protection to httpx client factory by dgenio · Pull Request #2180 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: fix: add SSRF redirect protection to httpx client factory by dgenio · Pull Request #2180 · modelcontextprotocol/python-sdk
X Title: fix: add SSRF redirect protection to httpx client factory by dgenio · Pull Request #2180 · modelcontextprotocol/python-sdk
Description: 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: Added RedirectPolicy enum with three presets: BLOCK_SCHEME_DOWNGRADE (default) — blocks HTTPS→HTTP redirect downgrades ENFORCE_HTTPS — only allows HTTPS redirect destinations ALLOW_ALL — no restrictions (legacy behavior) Added _check_redirect() async response event hook that validates redirect targets against the configured policy using response.has_redirect_location and the Location header Added redirect_policy parameter to create_mcp_http_client() (default: BLOCK_SCHEME_DOWNGRADE) McpHttpClientFactory Protocol left unchanged — redirect_policy is an internal concern of create_mcp_http_client, not part of the caller-side contract src/mcp/client/streamable_http.py: Added docstring note on http_client parameter clarifying that user-provided clients do not receive SSRF redirect protection Added logger.debug when a user-provided client is used tests/shared/test_httpx_utils.py: 14 unit tests covering all three policies, edge cases (non-redirect responses, missing Location header), and all scheme combinations 2 integration tests using httpx.MockTransport that exercise the event hook wiring end-to-end through the real httpx client 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 ruff check . — all checks passed ruff format --check . — no formatting issues pyright — 0 errors, 0 warnings pytest tests/shared/test_httpx_utils.py -v — 16 passed pytest tests/client/test_client.py -v — 19 passed (no regressions) Tradeoffs / risks The new redirect_policy parameter on create_mcp_http_client has a default value, so all existing callers are unaffected — no breaking change McpHttpClientFactory Protocol is not modified, preserving compatibility for downstream code implementing custom factories Users who need the legacy behavior can pass redirect_policy=RedirectPolicy.ALLOW_ALL Private/link-local IP range blocking is not included in this PR — suggested as a follow-up Scope notes RedirectPolicy is not re-exported from mcp.__init__ — create_mcp_http_client itself is also not publicly exported, so both share the same visibility via mcp.shared._httpx_utils. Public API surface expansion is suggested as a follow-up HttpResource in resources/types.py uses a bare httpx.AsyncClient() (separate concern, not addressed here) Closes #2106
Open Graph Description: 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:...
X Description: 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:...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/pull/2180
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:fa6bc780-7180-75c7-3d2c-8ccadbc1dc96 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | 963E:26889C:F1E450:1438536:6A5BA34E |
| html-safe-nonce | 6b67630602018c17634cb0547b481d74594a2bf35e48f7914262b4349facc25a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NjNFOjI2ODg5QzpGMUU0NTA6MTQzODUzNjo2QTVCQTM0RSIsInZpc2l0b3JfaWQiOiI0NjM5NTc4MDUyMTA3NzIzMDIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | c065901e8d4c426f3015aba58e7927e931b8c26fce553e9dce806053b2d1b094 |
| hovercard-subject-tag | pull_request:3338803597 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/modelcontextprotocol/python-sdk/pull/2180/files |
| twitter:image | https://avatars.githubusercontent.com/u/12731907?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/12731907?s=400&v=4 |
| og:image:alt | 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:... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5290d7e14309ad1e76106a9c4237bd1041517e83ea182c8ab756752cb0c6940b |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/modelcontextprotocol/python-sdk git https://github.com/modelcontextprotocol/python-sdk.git |
| octolytics-dimension-user_id | 182288589 |
| octolytics-dimension-user_login | modelcontextprotocol |
| octolytics-dimension-repository_id | 862584018 |
| octolytics-dimension-repository_nwo | modelcontextprotocol/python-sdk |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 862584018 |
| octolytics-dimension-repository_network_root_nwo | modelcontextprotocol/python-sdk |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 9c975978430e9ad293956f2bbdaf153b1bd84a99 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width