Skip to content

Commit f5cbbb4

Browse files
authored
Merge pull request #348 from macedogm/sshkey-redact
Redact SSH key from URL query parameter
2 parents 23702d0 + 17af21e commit f5cbbb4

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cmd/go-getter/go-getter

url.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,23 @@ package getter
33
import "net/url"
44

55
// RedactURL is a port of url.Redacted from the standard library,
6-
// which is like url.String but replaces any password with "xxxxx".
6+
// which is like url.String but replaces any password with "redacted".
77
// Only the password in u.URL is redacted. This allows the library
88
// to maintain compatibility with go1.14.
9+
// This port was also extended to redact SSH key from URL query parameter.
910
func RedactURL(u *url.URL) string {
1011
if u == nil {
1112
return ""
1213
}
1314

1415
ru := *u
1516
if _, has := ru.User.Password(); has {
16-
ru.User = url.UserPassword(ru.User.Username(), "xxxxx")
17+
ru.User = url.UserPassword(ru.User.Username(), "redacted")
18+
}
19+
q := ru.Query()
20+
if q.Get("sshkey") != "" {
21+
q.Set("sshkey", "redacted")
22+
ru.RawQuery = q.Encode()
1723
}
1824
return ru.String()
1925
}

url_test.go

+24-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func TestRedactURL(t *testing.T) {
1919
Path: "this:that",
2020
User: url.UserPassword("user", "password"),
2121
},
22-
want: "http://user:xxxxx@host.tld/this:that",
22+
want: "http://user:redacted@host.tld/this:that",
2323
},
2424
{
2525
name: "blank Password",
@@ -39,7 +39,7 @@ func TestRedactURL(t *testing.T) {
3939
Path: "this:that",
4040
User: url.UserPassword("", "password"),
4141
},
42-
want: "http://:xxxxx@host.tld/this:that",
42+
want: "http://:redacted@host.tld/this:that",
4343
},
4444
{
4545
name: "blank Username, blank Password",
@@ -60,6 +60,28 @@ func TestRedactURL(t *testing.T) {
6060
url: nil,
6161
want: "",
6262
},
63+
{
64+
name: "non-blank SSH key in URL query parameter",
65+
url: &url.URL{
66+
Scheme: "ssh",
67+
User: url.User("git"),
68+
Host: "github.com",
69+
Path: "hashicorp/go-getter-test-private.git",
70+
RawQuery: "sshkey=LS0tLS1CRUdJTiBPUE",
71+
},
72+
want: "ssh://git@github.com/hashicorp/go-getter-test-private.git?sshkey=redacted",
73+
},
74+
{
75+
name: "blank SSH key in URL query parameter",
76+
url: &url.URL{
77+
Scheme: "ssh",
78+
User: url.User("git"),
79+
Host: "github.com",
80+
Path: "hashicorp/go-getter-test-private.git",
81+
RawQuery: "sshkey=",
82+
},
83+
want: "ssh://git@github.com/hashicorp/go-getter-test-private.git?sshkey=",
84+
},
6385
}
6486

6587
for _, tt := range cases {

0 commit comments

Comments
 (0)