Skip to content

Commit

Permalink
Redact SSH key from URL query parameter
Browse files Browse the repository at this point in the history
Signed-off-by: Guilherme Macedo <[email protected]>
  • Loading branch information
macedogm committed Jan 3, 2022
1 parent 23702d0 commit 36b68b2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cmd/go-getter/go-getter
7 changes: 6 additions & 1 deletion url.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ func RedactURL(u *url.URL) string {

ru := *u
if _, has := ru.User.Password(); has {
ru.User = url.UserPassword(ru.User.Username(), "xxxxx")
ru.User = url.UserPassword(ru.User.Username(), "redacted")
}
q := ru.Query()
if q.Get("sshkey") != "" {
q.Set("sshkey", "redacted")
ru.RawQuery = q.Encode()
}
return ru.String()
}
26 changes: 24 additions & 2 deletions url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestRedactURL(t *testing.T) {
Path: "this:that",
User: url.UserPassword("user", "password"),
},
want: "http://user:xxxxx@host.tld/this:that",
want: "http://user:redacted@host.tld/this:that",
},
{
name: "blank Password",
Expand All @@ -39,7 +39,7 @@ func TestRedactURL(t *testing.T) {
Path: "this:that",
User: url.UserPassword("", "password"),
},
want: "http://:xxxxx@host.tld/this:that",
want: "http://:redacted@host.tld/this:that",
},
{
name: "blank Username, blank Password",
Expand All @@ -60,6 +60,28 @@ func TestRedactURL(t *testing.T) {
url: nil,
want: "",
},
{
name: "non-blank SSH key in URL query parameter",
url: &url.URL{
Scheme: "ssh",
User: url.User("git"),
Host: "github.com",
Path: "hashicorp/go-getter-test-private.git",
RawQuery: "sshkey=LS0tLS1CRUdJTiBPUE",
},
want: "ssh://[email protected]/hashicorp/go-getter-test-private.git?sshkey=redacted",
},
{
name: "blank SSH key in URL query parameter",
url: &url.URL{
Scheme: "ssh",
User: url.User("git"),
Host: "github.com",
Path: "hashicorp/go-getter-test-private.git",
RawQuery: "sshkey=",
},
want: "ssh://[email protected]/hashicorp/go-getter-test-private.git?sshkey=",
},
}

for _, tt := range cases {
Expand Down

0 comments on commit 36b68b2

Please sign in to comment.