Go's http.Client follows up to 10 redirects to any host, https to http included, and on a hop to another domain drops only Authorization, Cookie and WWW-Authenticate. So a vendor (or anything answering at its URL) that redirected elsewhere got x-api-key, x-goog-api-key, api-key and a provider's own headers sent there; Authorization too when only the port differed, and all of them in the clear on an https to http hop on the same host. The class is fixed where the requests leave: - provider.KeepsCredentials is a CheckRedirect that follows a redirect only to the request's own origin (scheme, host, port; or http to https on the same host and default ports), at most 10, and gives any other 3xx back as the answer (http.ErrUseLastResponse). - provider.Keyed(c) is a copy of c with c's own CheckRedirect, then KeepsCredentials. Provider.do, the choke point of every p.Do, sends through it, and every http.DefaultClient.Do in internal/provider that carries a credential is Keyed(http.DefaultClient).Do. A copy per call, since netproxy.Install sets the default client's transport at run time. Left alone: Grok's public download, Antigravity's version URL and cline.go's unsigned read. - the gateway's client (s.client, every vendor call), the Codex relay's and the Codex websocket's take KeepsCredentials, and so does chatgpt_api's siwcClient. The native route needs nothing more: the vendor's 3xx comes back to the caller as it came, Location included, and the caller decides. Translated routes don't follow either, rather than following with credentials stripped: it is the rule search_api.go had already (#1580), no vendor magpie speaks to was found redirecting its API to another origin, a stripped request elsewhere would only fail with a confusing 401, and the vendor-URL downloads (video, images) are fetched unsigned through vendorClient, so they are not affected. Tests: TestRedirectKeepsTheKeyAtItsOrigin (307 and 302 elsewhere with Authorization, x-api-key, x-goog-api-key, api-key and a custom header; a same-origin redirect followed with every key; three clients), TestRedirectNeverDowngrades (an https vendor's redirect to http on its own host and port), TestSameOrigin, TestKeyedKeepsTheClientsRule, TestNativeRedirectKeepsTheKey (Anthropic and Gemini providers with an own header) and TestChatRedirectKeepsTheKey. Broken by hand: with Provider.do back on client.Do and s.client without CheckRedirect, the thief got every key header, /away answered 200, the https vendor's /plain was followed (400 from the TLS port spoken to in http), the native route's claudeish and gem answered the thief's {"stolen":true} with Authorization, X-Api-Key and X-Relay-Secret sent there, and the chat route sent Api-Key, Authorization and X-Relay-Secret to it. TestSameOrigin and TestKeyedKeepsTheClientsRule don't depend on the wiring and passed. Run on mote at the pushed head 883690a0b (this commit and the next, on origin/main9a0aab32c): gofmt -l clean, go vet, GOOS=windows go vet and GOOS=linux -tags nogui go vet all clean. go test ./... -timeout 45m -count=1: 84 packages ok, including internal/gateway. 3 red, each filed and none in this change's path: - TestMainWindowCorners/macOS_26_SDK (gui, macOS 27, #3949) - TestRenewLoginsKeepsClaudeReading (provider, #5481). It is red 3 of 3 at the base49c9491b7without either commit. - TestHostEndedEndsWhatItsPluginStarted/own_end (plugin, a 30s wait in a full run; passes 3 of 3 alone at this head, #5515) This commit alone, at its first base49c9491b7, ran the same way with TestUnroutedHeldReportSaved (gateway, #4300) red besides. Rebased then onto origin/main303a31dd6(which fixed #5481) and run again at that tree: gofmt and the three vets clean, and internal/gateway, internal/provider, internal/plugin and internal/netproxy all ok. Rebased on19c45d5d4, which changes gateway.go (kept_error), Codex's usage_limit_reached, Cursor's mover min, sessions and the GUI; ran gofmt, go vet ./internal/..., GOOS=windows go vet of gateway and provider, and go test ./internal/gateway ./internal/provider: both ok. Rebased on94d4866b5, which changes only internal/sessions; ran gofmt, go vet of gateway, provider and sessions, and this change's own tests (-run Redirect|DropsProxyIdentity|SameOrigin|Keyed): ok. Rebased oncd77c725d, which changes only internal/provider's check-in; ran gofmt, go vet of gateway and provider, and this change's own tests again: ok. Not tried: a real vendor's redirect (none found that does), and the plugin host's fetch (host.js; Bun follows redirects by default), which is filed as #5508.
89 lines
2.8 KiB
Go
89 lines
2.8 KiB
Go
package provider
|
|
|
|
import (
|
|
"errors"
|
|
"net"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
// A request that carries a vendor's credential is followed through a
|
|
// redirect only to its own origin (the scheme, host and port it was sent
|
|
// to), or from http to https on the same host. Go follows any redirect to
|
|
// any host, https to http included, and on a hop to another host drops
|
|
// only Authorization, Cookie and WWW-Authenticate: x-api-key,
|
|
// x-goog-api-key, api-key and a provider's own headers went to wherever
|
|
// the vendor's Location named. A redirect elsewhere is not followed, and
|
|
// its 3xx is the answer, as it came, so the caller sees where the vendor
|
|
// sent it and no credential goes there. No vendor magpie speaks to was
|
|
// found to redirect its API to another origin (a download that does is
|
|
// fetched by its own URL, unsigned), and the search APIs had this rule
|
|
// already (search_api.go, #1580).
|
|
|
|
// KeepsCredentials is the CheckRedirect of a client that sends a vendor's
|
|
// credential: same-origin redirects are followed, at most 10 as Go's
|
|
// default, and any other is given back as the answer
|
|
// (http.ErrUseLastResponse).
|
|
func KeepsCredentials(next *http.Request, via []*http.Request) error {
|
|
if len(via) >= 10 {
|
|
return errors.New("stopped after 10 redirects")
|
|
}
|
|
if !SameOrigin(via[0].URL.Scheme, via[0].URL.Host, next.URL.Scheme, next.URL.Host) {
|
|
return http.ErrUseLastResponse
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// SameOrigin says whether a redirect from scheme://host to
|
|
// toScheme://toHost may carry a credential: the same scheme, host and
|
|
// port (a port left out is the scheme's own), or http to https on the
|
|
// same host and default ports.
|
|
func SameOrigin(scheme, host, toScheme, toHost string) bool {
|
|
scheme, toScheme = strings.ToLower(scheme), strings.ToLower(toScheme)
|
|
h, port := splitOrigin(scheme, host)
|
|
th, tport := splitOrigin(toScheme, toHost)
|
|
if h == "" || h != th {
|
|
return false
|
|
}
|
|
if scheme == toScheme {
|
|
return port == tport
|
|
}
|
|
// an upgrade: the key went over http already, to this same host
|
|
return scheme == "http" && toScheme == "https" && port == "80" && tport == "443"
|
|
}
|
|
|
|
func splitOrigin(scheme, host string) (string, string) {
|
|
h, port, err := net.SplitHostPort(host)
|
|
if err != nil {
|
|
h, port = host, ""
|
|
}
|
|
if port == "" {
|
|
switch scheme {
|
|
case "http":
|
|
port = "80"
|
|
case "https":
|
|
port = "443"
|
|
}
|
|
}
|
|
return strings.ToLower(strings.Trim(h, "[]")), port
|
|
}
|
|
|
|
// Keyed is c with KeepsCredentials after c's own CheckRedirect, for a
|
|
// request that carries a vendor's credential. c is left as it is.
|
|
func Keyed(c *http.Client) *http.Client {
|
|
if c == nil {
|
|
c = http.DefaultClient
|
|
}
|
|
k := *c
|
|
own := c.CheckRedirect
|
|
k.CheckRedirect = func(next *http.Request, via []*http.Request) error {
|
|
if own != nil {
|
|
if err := own(next, via); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return KeepsCredentials(next, via)
|
|
}
|
|
return &k
|
|
}
|