]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: fix GOAUTH parsing for trailing slash
authorSam Thanawalla <samthanawalla@google.com>
Wed, 2 Apr 2025 20:30:37 +0000 (20:30 +0000)
committerGopher Robot <gobot@golang.org>
Mon, 7 Apr 2025 14:33:33 +0000 (07:33 -0700)
We were treating a url with a trailing slash differently than one
without. This CL treats them the same.

Additionally this fixes a bug in the way we iteratively try different
prefixes. We were only trying the host url but this change now tries all
different prefixes.

Fixes: #71889
Change-Id: I5d5f43000ae0e18ea8682050037253aff75ec142
Reviewed-on: https://go-review.googlesource.com/c/go/+/662435
Reviewed-by: Michael Matloob <matloob@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Sam Thanawalla <samthanawalla@google.com>

src/cmd/go/alldocs.go
src/cmd/go/internal/auth/auth.go
src/cmd/go/internal/auth/auth_test.go
src/cmd/go/internal/help/helpdoc.go

index ace9899250b44361da9840fa332a5c4525327710..42076e45026ffa2fcc3bb1a1a82067d5039795f5 100644 (file)
 //             BlankLine     = '\n' .
 //
 //     Example:
-//             https://example.com/
+//             https://example.com
 //             https://example.net/api/
 //
 //             Authorization: Basic <token>
index 83c28d160c9d90ff148f5102b9cd22ced445e8e0..8f2bded32029d1689ae9de54591eb935b76cfab9 100644 (file)
@@ -142,14 +142,18 @@ func runGoAuth(client *http.Client, res *http.Response, url string) {
 // them to the request headers.
 func loadCredential(req *http.Request, url string) bool {
        currentPrefix := strings.TrimPrefix(url, "https://")
+       currentPrefix = strings.TrimSuffix(currentPrefix, "/")
+
        // Iteratively try prefixes, moving up the path hierarchy.
+       // E.g. example.com/foo/bar, example.com/foo, example.com
        for {
                headers, ok := credentialCache.Load(currentPrefix)
                if !ok {
-                       currentPrefix, _, ok = strings.Cut(currentPrefix, "/")
-                       if !ok {
+                       lastSlash := strings.LastIndexByte(currentPrefix, '/')
+                       if lastSlash == -1 {
                                return false
                        }
+                       currentPrefix = currentPrefix[:lastSlash]
                        continue
                }
                for key, values := range headers.(http.Header) {
@@ -166,6 +170,7 @@ func loadCredential(req *http.Request, url string) bool {
 func storeCredential(prefix string, header http.Header) {
        // Trim "https://" prefix to match the format used in .netrc files.
        prefix = strings.TrimPrefix(prefix, "https://")
+       prefix = strings.TrimSuffix(prefix, "/")
        if len(header) == 0 {
                credentialCache.Delete(prefix)
        } else {
index c1bbf4b1a91e6f4e0c7a06d6ae3dd778dd150af0..599030fd13bafc623e8596cdc78b5aef8a580f26 100644 (file)
@@ -71,3 +71,20 @@ func TestCredentialCacheDelete(t *testing.T) {
                t.Errorf("loadCredential:\nhave %q\nwant %q", got.Header, want.Header)
        }
 }
+
+func TestCredentialCacheTrailingSlash(t *testing.T) {
+       // Store a credential for api.github.com/foo/bar
+       want := http.Request{Header: make(http.Header)}
+       want.SetBasicAuth("user", "pwd")
+       storeCredential("api.github.com/foo", want.Header)
+       got := &http.Request{Header: make(http.Header)}
+       ok := loadCredential(got, "api.github.com/foo/bar")
+       if !ok || !reflect.DeepEqual(got.Header, want.Header) {
+               t.Errorf("parseNetrc:\nhave %q\nwant %q", got.Header, want.Header)
+       }
+       got2 := &http.Request{Header: make(http.Header)}
+       ok = loadCredential(got2, "https://api.github.com/foo/bar/")
+       if !ok || !reflect.DeepEqual(got2.Header, want.Header) {
+               t.Errorf("parseNetrc:\nhave %q\nwant %q", got2.Header, want.Header)
+       }
+}
index 6101a45829931b6ff074371376ffa8aa0c3bcd0f..47e5d73dd2a769d362fa0202d0d3796860bb28fe 100644 (file)
@@ -1027,7 +1027,7 @@ command
                BlankLine     = '\n' .
 
        Example:
-               https://example.com/
+               https://example.com
                https://example.net/api/
 
                Authorization: Basic <token>