From b9cdde670ba90c7eaca2daaf3545bb1a49eccdae Mon Sep 17 00:00:00 2001 From: Valentin Maerten Date: Sun, 1 Nov 2026 21:32:26 +0100 Subject: [PATCH] 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. --- taskfile/http_headers.go | 15 ++++++++++----- taskfile/http_headers_test.go | 11 ++++++++++- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/taskfile/http_headers.go b/taskfile/http_headers.go index 4cdbfaab..d10cb2c1 100644 --- a/taskfile/http_headers.go +++ b/taskfile/http_headers.go @@ -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 diff --git a/taskfile/http_headers_test.go b/taskfile/http_headers_test.go index b6dde364..c44dcc26 100644 --- a/taskfile/http_headers_test.go +++ b/taskfile/http_headers_test.go @@ -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)