Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redact SSH key from URL query parameter #348

Merged
merged 2 commits into from
Jan 3, 2022
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
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
10 changes: 8 additions & 2 deletions url.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@ package getter
import "net/url"

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

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://git@github.com/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://git@github.com/hashicorp/go-getter-test-private.git?sshkey=",
},
}

for _, tt := range cases {
Expand Down