Restrict MCP HTTP redirects to the configured origin (#39046)

## Why

MCP requests can contain sensitive headers and tool-call bodies. Following a
cross-origin redirect could disclose them to another server.

## What changed

- Follow redirects only when every hop remains on the configured server's
  origin, and require HTTPS for redirects on non-loopback hostnames.
- Preserve standard redirect method and body handling while sharing the
  original timeout and enforcing a 10-hop limit.
- Avoid replaying plaintext proxy credentials across redirects, while leaving
  OAuth requests that stop at redirects unchanged.

## Testing

Add coverage for same-origin and cross-origin redirects, redirect methods,
headers and bodies, proxy credentials, timeouts, hop limits, and legacy MCP
tool calls.

GitOrigin-RevId: 3e267911a1665ad8c2fc63825099a758770f4d68
This commit is contained in:
Eric Traut authored and copyberry committed 2026-08-17 18:46:12 +00:00
1 parent 06418909a0
commit ff770113ca
6 files changed
+721 -15

No files matched your search

+44 -2
View File
@@ -197,6 +197,23 @@ impl HttpHeadersClient {
}
Ok(params)
}
fn reject_proxy_authorization_redirect(
response: &HttpRequestResponse,
) -> Result<(), ExecServerError> {
if (300..400).contains(&response.status)
&& response
.headers
.iter()
.any(|header| header.name.eq_ignore_ascii_case("location"))
{
return Err(ExecServerError::HttpRequest(
"MCP HTTP redirect cannot safely replay Proxy-Authorization credentials"
.to_string(),
));
}
Ok(())
}
}
impl HttpClient for HttpHeadersClient {
@@ -205,8 +222,21 @@ impl HttpClient for HttpHeadersClient {
params: HttpRequestParams,
) -> BoxFuture<'_, Result<HttpRequestResponse, ExecServerError>> {
async move {
// OAuth validates redirect responses itself; only intercept MCP replay.
let mcp_redirect_was_stopped = params.redirect_policy == HttpRedirectPolicy::Stop
&& !params.request_id.starts_with("oauth-request-");
let params = self.prepare_request(params).await?;
self.inner.http_request(params).await
let prevent_proxy_authorization_redirect = mcp_redirect_was_stopped
&& Url::parse(&params.url).is_ok_and(|url| url.scheme() == "http")
&& params
.headers
.iter()
.any(|header| header.name.eq_ignore_ascii_case("proxy-authorization"));
let response = self.inner.http_request(params).await?;
if prevent_proxy_authorization_redirect {
Self::reject_proxy_authorization_redirect(&response)?;
}
Ok(response)
}
.boxed()
}
@@ -216,8 +246,20 @@ impl HttpClient for HttpHeadersClient {
params: HttpRequestParams,
) -> BoxFuture<'_, Result<(HttpRequestResponse, HttpResponseBodyStream), ExecServerError>> {
async move {
let mcp_redirect_was_stopped = params.redirect_policy == HttpRedirectPolicy::Stop
&& !params.request_id.starts_with("oauth-request-");
let params = self.prepare_request(params).await?;
self.inner.http_request_stream(params).await
let prevent_proxy_authorization_redirect = mcp_redirect_was_stopped
&& Url::parse(&params.url).is_ok_and(|url| url.scheme() == "http")
&& params
.headers
.iter()
.any(|header| header.name.eq_ignore_ascii_case("proxy-authorization"));
let (response, stream) = self.inner.http_request_stream(params).await?;
if prevent_proxy_authorization_redirect {
Self::reject_proxy_authorization_redirect(&response)?;
}
Ok((response, stream))
}
.boxed()
}