Skip to content

Commit

Permalink
strip user without password (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakecoffman authored Apr 5, 2023
1 parent 24ab49f commit af162fe
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
12 changes: 11 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ func Scrub(argument string) string {
u, err := url.ParseRequestURI(argument)
if err == nil && u.Host != "" && contains(allowedSchemes, u.Scheme) {
u.Scheme = "https"
// Clear the user if there is no password, since the URL is usually ssh://[email protected].
// The username is required to tell the server you're doing Git operations, but not needed for HTTPS.
if _, isSet := u.User.Password(); !isSet {
u.User = nil
}
return u.String()
}
if scpUrl.MatchString(argument) {
Expand All @@ -109,7 +114,12 @@ func Scrub(argument string) string {
// host changed, possible attack
return argument
}
return newUrl
// Clear the user if there is no password, since the URL is usually [email protected].
// The username is required to tell the server you're doing Git operations, but not needed for HTTPS.
if _, isSet := u.User.Password(); !isSet {
u.User = nil
}
return u.String()
}
return argument
}
Expand Down
8 changes: 6 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,15 @@ func TestScrub(t *testing.T) {
},
{
input: "[email protected]:dependabot/git-https-shim",
expected: "https://[email protected]/dependabot/git-https-shim",
expected: "https://github.com/dependabot/git-https-shim",
},
{
input: "ssh://user:[email protected]/dependabot/git-https-shim",
expected: "https://user:[email protected]/dependabot/git-https-shim",
},
{
input: "ssh://[email protected]/dependabot/git-https-shim",
expected: "https://git@github.com/dependabot/git-https-shim",
expected: "https://github.com/dependabot/git-https-shim",
},
{
input: "ssh://github.com/dependabot/git-https-shim",
Expand Down

0 comments on commit af162fe

Please sign in to comment.