fix(remote): scope configured headers to the origin scheme
An https remote redirected to http on the same host kept its Host string, so remote.headers were sent over the plaintext hop. This was reachable under --insecure, which allows the downgrade redirect.
This commit is contained in:
parent
ed31b5edf7
commit
b9cdde670b
2 changed files with 20 additions and 6 deletions
|
|
@ -5,6 +5,7 @@ import (
|
|||
"fmt"
|
||||
"maps"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"slices"
|
||||
|
||||
"golang.org/x/net/http/httpguts"
|
||||
|
|
@ -18,13 +19,16 @@ type HeadersByHost map[string]map[string]string
|
|||
|
||||
type headersTransport struct {
|
||||
base http.RoundTripper
|
||||
scheme string
|
||||
host string
|
||||
headers map[string]string
|
||||
}
|
||||
|
||||
func (t *headersTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
// Scope all configured headers to this host, including after redirects.
|
||||
if !hostMatches(t.host, req.URL.Host) {
|
||||
// Scope all configured headers to this origin, including after redirects:
|
||||
// an https URL redirected to http on the same host keeps its Host string,
|
||||
// so the scheme must match too or the headers would travel in plaintext.
|
||||
if req.URL.Scheme != t.scheme || !hostMatches(t.host, req.URL.Host) {
|
||||
return t.base.RoundTrip(req)
|
||||
}
|
||||
req = req.Clone(req.Context())
|
||||
|
|
@ -43,15 +47,16 @@ func (node *HTTPNode) clientWithHeaders() (*http.Client, error) {
|
|||
if len(headers) == 0 {
|
||||
return node.client, nil
|
||||
}
|
||||
return withHeaders(node.client, node.url.Host, headers), nil
|
||||
return withHeaders(node.client, node.url, headers), nil
|
||||
}
|
||||
|
||||
func withHeaders(client *http.Client, host string, headers map[string]string) *http.Client {
|
||||
func withHeaders(client *http.Client, origin *url.URL, headers map[string]string) *http.Client {
|
||||
// The client may be http.DefaultClient; leave it unchanged.
|
||||
configured := *client
|
||||
configured.Transport = &headersTransport{
|
||||
base: cmp.Or(client.Transport, http.DefaultTransport),
|
||||
host: host,
|
||||
scheme: origin.Scheme,
|
||||
host: origin.Host,
|
||||
headers: headers,
|
||||
}
|
||||
return &configured
|
||||
|
|
|
|||
|
|
@ -127,6 +127,7 @@ func TestHeadersTransport(t *testing.T) {
|
|||
|
||||
transport := &headersTransport{
|
||||
base: roundTripperFunc(func(req *http.Request) (*http.Response, error) { return newResponse(req), nil }),
|
||||
scheme: "https",
|
||||
host: "gitlab.com",
|
||||
headers: map[string]string{"PRIVATE-TOKEN": "token"},
|
||||
}
|
||||
|
|
@ -147,12 +148,20 @@ func TestHeadersTransport(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
assert.Empty(t, resp.Request.Header.Get("PRIVATE-TOKEN"))
|
||||
})
|
||||
|
||||
t.Run("leaves the same host over http alone", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
req := newRequest(t, "http://gitlab.com/api/v4/Taskfile.yml")
|
||||
resp, err := transport.RoundTrip(req)
|
||||
require.NoError(t, err)
|
||||
assert.Empty(t, resp.Request.Header.Get("PRIVATE-TOKEN"))
|
||||
})
|
||||
}
|
||||
|
||||
func TestWithHeadersDoesNotMutateTheDefaultClient(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
client := withHeaders(http.DefaultClient, "gitlab.com", map[string]string{"PRIVATE-TOKEN": "token"})
|
||||
client := withHeaders(http.DefaultClient, &url.URL{Scheme: "https", Host: "gitlab.com"}, map[string]string{"PRIVATE-TOKEN": "token"})
|
||||
|
||||
assert.NotSame(t, http.DefaultClient, client)
|
||||
assert.Nil(t, http.DefaultClient.Transport)
|
||||
|
|
|
|||
Loading…
Reference in a new issue