Skip to content

Commit 9961bc4

Browse files
authored
Fix add-wizard auth failure when git remote uses HTTPS URL with embedded username (#25375)
1 parent a10e1e2 commit 9961bc4

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

pkg/cli/git.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cli
33
import (
44
"errors"
55
"fmt"
6+
"net/url"
67
"os"
78
"os/exec"
89
"path/filepath"
@@ -86,12 +87,26 @@ func parseGitHubRepoSlugFromURL(url string) string {
8687
// Supports HTTPS (https://host[:port]/path), HTTP (http://host[:port]/path), and SSH (git@host[:port]:path or ssh://git@host[:port]/path) formats.
8788
// Returns the host portion as "host[:port]" when parsed, or "github.com" as the default if the URL cannot be parsed.
8889
func extractHostFromRemoteURL(remoteURL string) string {
89-
// HTTPS / HTTP format: https://host/path or http://host/path
90+
// HTTPS / HTTP format: https://[userinfo@]host/path or http://[userinfo@]host/path
91+
// Use net/url.Parse to correctly handle all userinfo variants (user@, user:pass@,
92+
// and passwords containing '@') and to extract the bare host without credentials.
9093
for _, scheme := range []string{"https://", "http://"} {
91-
if after, ok := strings.CutPrefix(remoteURL, scheme); ok {
94+
if strings.HasPrefix(remoteURL, scheme) {
95+
if u, err := url.Parse(remoteURL); err == nil && u.Host != "" {
96+
return u.Host
97+
}
98+
// Fallback: strip scheme and any userinfo manually.
99+
after := remoteURL[len(scheme):]
92100
if host, _, found := strings.Cut(after, "/"); found {
101+
// Strip optional userinfo (everything up to and including the last '@').
102+
if idx := strings.LastIndex(host, "@"); idx >= 0 {
103+
host = host[idx+1:]
104+
}
93105
return host
94106
}
107+
if idx := strings.LastIndex(after, "@"); idx >= 0 {
108+
return after[idx+1:]
109+
}
95110
return after
96111
}
97112
}

pkg/cli/git_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,26 @@ func TestExtractHostFromRemoteURL(t *testing.T) {
460460
url string
461461
expected string
462462
}{
463+
{
464+
name: "HTTPS with embedded username (Windows-style)",
465+
url: "https://bryanknox@github.com/owner/repo.git",
466+
expected: "github.com",
467+
},
468+
{
469+
name: "HTTPS with embedded username on GHES",
470+
url: "https://user@ghes.example.com/org/repo.git",
471+
expected: "ghes.example.com",
472+
},
473+
{
474+
name: "HTTP with embedded username",
475+
url: "http://user@ghes.example.com/org/repo.git",
476+
expected: "ghes.example.com",
477+
},
478+
{
479+
name: "HTTPS with embedded username and password",
480+
url: "https://user:pass@github.com/owner/repo.git",
481+
expected: "github.com",
482+
},
463483
{
464484
name: "public GitHub HTTPS",
465485
url: "https://github.com/owner/repo.git",

0 commit comments

Comments
 (0)