fix(install): strip submodule URL userinfo before query and fragment

A ? or # inside the userinfo used to cut away the @ before it was found, leaving the credential in place.
This commit is contained in:
Willie
2026-10-07 02:40:48 +08:00
parent b1b5bcc375
commit 79bb10658a
2 changed files with 11 additions and 8 deletions
+9 -8
View File
@@ -32,16 +32,17 @@ func (l gitlink) String() string {
// accepts any text there, so this fails closed on the raw string: everything
// before the last "@" goes, keeping only a well-formed "scheme://" prefix.
func displayURL(raw string) string {
// Userinfo first: a "?" or "#" inside it must not hide the "@".
if at := strings.LastIndex(raw, "@"); at >= 0 {
if scheme, _, ok := strings.Cut(raw[:at], "://"); ok {
raw = scheme + "://" + raw[at+1:]
} else {
raw = raw[at+1:]
}
}
raw, _, _ = strings.Cut(raw, "#")
raw, _, _ = strings.Cut(raw, "?")
at := strings.LastIndex(raw, "@")
if at < 0 {
return raw
}
if scheme, _, ok := strings.Cut(raw[:at], "://"); ok {
return scheme + "://" + raw[at+1:]
}
return raw[at+1:]
return raw
}
// repoGitlinks lists the submodules recorded in HEAD of repoPath. extraEnv
+2
View File
@@ -155,6 +155,8 @@ func TestGitlinkString_HidesURLCredentials(t *testing.T) {
"s3cret@example.com:org/up.git": "example.com:org/up.git",
"https:/user:s3cret@example.com/org/up.git": "example.com/org/up.git",
"//user:s3cret@example.com/org/up.git": "example.com/org/up.git",
"https://user:s3cret?x@example.com/org/up.git": "https://example.com/org/up.git",
"https://user:s3cret#x@example.com/org/up.git": "https://example.com/org/up.git",
} {
got := gitlink{Path: "vendor/up", Commit: "abc", URL: raw}.String()
if strings.Contains(got, "s3cret") || !strings.Contains(got, want) {