Skip to content

Commit c513d34

Browse files
authored
Merge pull request #1664 from crazy-max/v0.10_backport_stripcreds
[v0.10 backport] build: strip credentials from remote url on collecting Git provenance info
2 parents 5ac3b4c + d455c07 commit c513d34

2 files changed

Lines changed: 58 additions & 2 deletions

File tree

util/gitutil/gitutil.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package gitutil
33
import (
44
"bytes"
55
"context"
6+
"net/url"
67
"os"
78
"os/exec"
89
"strings"
@@ -70,11 +71,11 @@ func (c *Git) RootDir() (string, error) {
7071
func (c *Git) RemoteURL() (string, error) {
7172
// Try to get the remote URL from the origin remote first
7273
if ru, err := c.clean(c.run("remote", "get-url", "origin")); err == nil && ru != "" {
73-
return ru, nil
74+
return stripCredentials(ru), nil
7475
}
7576
// If that fails, try to get the remote URL from the upstream remote
7677
if ru, err := c.clean(c.run("remote", "get-url", "upstream")); err == nil && ru != "" {
77-
return ru, nil
78+
return stripCredentials(ru), nil
7879
}
7980
return "", errors.New("no remote URL found for either origin or upstream")
8081
}
@@ -147,3 +148,16 @@ func IsUnknownRevision(err error) bool {
147148
errMsg := strings.ToLower(err.Error())
148149
return strings.Contains(errMsg, "unknown revision or path not in the working tree") || strings.Contains(errMsg, "bad revision")
149150
}
151+
152+
// stripCredentials takes a URL and strips username and password from it.
153+
// e.g. "https://user:[email protected]/path.git" will be changed to
154+
// "https://host.tld/path.git".
155+
// TODO: remove this function once fix from BuildKit is vendored here
156+
func stripCredentials(s string) string {
157+
ru, err := url.Parse(s)
158+
if err != nil {
159+
return s // string is not a URL, just return it
160+
}
161+
ru.User = nil
162+
return ru.String()
163+
}

util/gitutil/gitutil_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,45 @@ func TestGitRemoteURL(t *testing.T) {
189189
})
190190
}
191191
}
192+
193+
func TestStripCredentials(t *testing.T) {
194+
cases := []struct {
195+
name string
196+
url string
197+
want string
198+
}{
199+
{
200+
name: "non-blank Password",
201+
url: "https://user:[email protected]/this:that",
202+
want: "https://host.tld/this:that",
203+
},
204+
{
205+
name: "blank Password",
206+
url: "https://[email protected]/this:that",
207+
want: "https://host.tld/this:that",
208+
},
209+
{
210+
name: "blank Username",
211+
url: "https://:[email protected]/this:that",
212+
want: "https://host.tld/this:that",
213+
},
214+
{
215+
name: "blank Username, blank Password",
216+
url: "https://host.tld/this:that",
217+
want: "https://host.tld/this:that",
218+
},
219+
{
220+
name: "invalid URL",
221+
url: "1https://foo.com",
222+
want: "1https://foo.com",
223+
},
224+
}
225+
for _, tt := range cases {
226+
tt := tt
227+
t.Run(tt.name, func(t *testing.T) {
228+
if g, w := stripCredentials(tt.url), tt.want; g != w {
229+
t.Fatalf("got: %q\nwant: %q", g, w)
230+
}
231+
})
232+
}
233+
}

0 commit comments

Comments
 (0)