Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ Refer to [GitHub `../DEVELOPMENT.md`](https://github.com/supabase/supabase-go/bl

<!-- cSpell:ignore freebsd -->

## Unreleased

### Fixed

`Configuration.HTTPClient`:
A redirect whose `Location` header does not parse as a URL now comes back as the redirect response itself, without that header, and a domain client reports it as it reports any other non-2xx response.
Before this fix the request failed with an error that quoted the header whole, so a token or other secret in it reached any log that recorded the error.

## `v0.1.0-alpha.5` (2026-11-27)

### Fixed
Expand Down
10 changes: 6 additions & 4 deletions core/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,14 @@ func (c *Configuration) Retry() bool {
// [WithHTTPClient] decides, or up to ten in a row when there is none
// - anywhere else, it never follows the redirect, whatever that
// CheckRedirect decides, and Do returns the redirect response itself
// - nowhere, when its Location header does not parse as a URL, so Do
// returns the redirect response itself, without that header
//
// When Do fails, it returns a *url.Error, as [http.Client.Do] does, whose URL
// holds only the scheme, host and path of the request's URL, so the error's
// text leaves out the query string and any user information. Its Op and Err
// are those that [http.Client.Do] returned, so [errors.Is], [errors.As] and
// its Timeout method answer as they would for that error.
// holds only the scheme, host and path of the request's URL, without the
// query string or any user information. Its Op and Err are those that
// [http.Client.Do] returned, so [errors.Is], [errors.As] and its Timeout
// method answer as they would for that error.
func (c *Configuration) HTTPClient() HTTPClient {
return redactingClient{c.httpClient}
}
Expand Down
50 changes: 50 additions & 0 deletions core/configuration/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"slices"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -174,6 +175,55 @@ func TestTransportFailureError(t *testing.T) {
}
}

// TestUnparsableLocation proves that a redirect whose Location header does not
// parse as a URL comes back from Do as the redirect response itself, without
// that header. The request does not fail, so no error quotes the header. A
// Location can hold a secret, such as the access token in the redirect that
// ends a sign-in. A response of any other status comes back with the header
// as the server sent it.
func TestUnparsableLocation(t *testing.T) {
const location = "/next%zz?access_token=USER_ACCESS_TOKEN"
testCases := []struct {
name string
status int
wantLocation []string
}{
{name: "redirect", status: http.StatusFound},
{name: "other status", status: http.StatusCreated, wantLocation: []string{location}},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, _ *http.Request) {
writer.Header().Set("Location", location)
writer.WriteHeader(testCase.status)
}))
defer server.Close()
projectConfiguration, err := configuration.New(core.ModulePathRoot, server.URL, "API_KEY")
if err != nil {
t.Fatalf("configuration.New: %v", err)
}
request, err := http.NewRequest(http.MethodGet, server.URL, nil)
if err != nil {
t.Fatalf("new request: %v", err)
}

response, err := projectConfiguration.HTTPClient().Do(request)
if err != nil {
t.Fatalf("Do: %v", err)
}
if response.StatusCode != testCase.status {
t.Errorf("status = %d, want %d", response.StatusCode, testCase.status)
}
if got := response.Header.Values("Location"); !slices.Equal(got, testCase.wantLocation) {
t.Errorf("Location = %q, want %q", got, testCase.wantLocation)
}
if closeErr := response.Body.Close(); closeErr != nil {
t.Errorf("closing the response body: %v", closeErr)
}
})
}
}

func recordingServer(t *testing.T) (*httptest.Server, *http.Header) {
t.Helper()
var received http.Header
Expand Down
50 changes: 47 additions & 3 deletions core/internal/transport/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"log/slog"
"net/http"
"net/url"
"time"

"github.com/supabase/supabase-go/core"
Expand All @@ -32,8 +33,9 @@ type transport struct {
// one, the X-Client-Info header that identifies entryModulePath, and writes
// one debug log emission per round trip through logger. Its redirect policy is
// client's own, confined by sameOriginRedirects to the origin of each
// request. The input client is never mutated, so sharing [http.DefaultClient]
// remains safe.
// request. It returns a redirect whose Location header does not parse as a
// URL as the redirect response itself, without that header. The input client
// is never mutated, so sharing [http.DefaultClient] remains safe.
//
// entryModulePath must be one of the following, with its module linked into
// the build, otherwise this function panics:
Expand Down Expand Up @@ -117,7 +119,8 @@ func sameOriginRedirects(checkRedirect func(*http.Request, []*http.Request) erro
// to case. The transport never sets Authorization: that header carries an
// end-user's JWT, supplied by the application when acting for a signed-in
// user. Each round trip writes one debug log emission through the configured
// logger.
// logger. A redirect response whose Location header does not parse as a URL
// is returned without that header.
func (tr *transport) RoundTrip(request *http.Request) (*http.Response, error) {
clone := request.Clone(request.Context())

Expand All @@ -138,9 +141,50 @@ func (tr *transport) RoundTrip(request *http.Request) (*http.Response, error) {
start := time.Now()
response, err := tr.base.RoundTrip(clone)
tr.logRoundTrip(clone, response, err, time.Since(start))

if response != nil {
// Without this, the client would quote a Location it cannot parse, and any
// secret the header carries, in the error it fails the request with.
removeUnparsableLocation(response)
}

return response, err
}

// removeUnparsableLocation removes the Location header from response when
// response has a 3xx status and the header does not parse as a URL.
//
// A redirect's Location can carry a secret, such as the access token in the
// redirect that ends a sign-in. [http.Client] parses the header before its
// redirect policy runs, and where the header ends up depends on the outcome:
// - a Location that parses is followed, or returned in the response, as the
// redirect policy decides
// - a Location that parses but that the redirect policy fails on goes into
// the URL of the error, a field that can be rewritten
// - a Location that does not parse is quoted whole in the cause of the error
// the request fails with
// - a redirect without a Location is returned as the response itself
//
// So a Location that does not parse is the only one that reaches the cause of
// an error, which makes it the cue. Removing it turns that failure into the
// last case above. The cause could not be cleaned afterwards instead. It has
// no type of its own and wraps nothing, so only its wording identifies it. Go
// does not promise to keep that wording.
//
// The check is [url.Parse], the parser [http.Client] reads a Location with, so
// the two agree on which headers do not parse. Such a header is no use as a
// URL, so removing it loses nothing a caller could follow. Any 3xx status
// counts, not only those [http.Client] follows, so the removal holds whichever
// statuses a later Go follows.
func removeUnparsableLocation(response *http.Response) {
if response.StatusCode < 300 || response.StatusCode >= 400 {
return
}
if _, err := url.Parse(response.Header.Get("Location")); err != nil {
response.Header.Del("Location")
}
}

// logRoundTrip writes one debug log emission for the round trip, carrying the
// request method, host, path, outcome and duration. Bodies, query strings and
// header values never appear in the emission. It writes nothing when the
Expand Down
Loading