feat(git): redact passwords from remote URL (#1709)

This commit is contained in:
Richard Gomez authored and GitHub committed 2025-01-19 15:17:07 -06:00
1 parent 3c7f3f0e08
commit 7a6183dab0
1 file changed
+23 -18
+23 -18
View File
@@ -90,11 +90,8 @@ type RemoteInfo struct {
} }
func NewRemoteInfo(platform scm.Platform, source string) (*RemoteInfo, error) { func NewRemoteInfo(platform scm.Platform, source string) (*RemoteInfo, error) {
var ( remoteUrl, err := getRemoteUrl(source)
remoteUrl string if err != nil {
err error
)
if remoteUrl, err = getRemoteUrl(source); err != nil {
if strings.Contains(err.Error(), "No remote configured") { if strings.Contains(err.Error(), "No remote configured") {
logging.Debug().Msg("skipping finding links: repository has no configured remote.") logging.Debug().Msg("skipping finding links: repository has no configured remote.")
platform = scm.NoPlatform platform = scm.NoPlatform
@@ -102,35 +99,35 @@ func NewRemoteInfo(platform scm.Platform, source string) (*RemoteInfo, error) {
} }
return nil, fmt.Errorf("unable to get remote URL: %w", err) return nil, fmt.Errorf("unable to get remote URL: %w", err)
} }
if platform == scm.NoPlatform {
parsedUrl, err := url.Parse(remoteUrl)
if err != nil {
return nil, fmt.Errorf("unable to parse remote URL: %w", err)
}
platform = platformFromHost(parsedUrl) if platform == scm.NoPlatform {
platform = platformFromHost(remoteUrl)
if platform == scm.NoPlatform { if platform == scm.NoPlatform {
logging.Info(). logging.Info().
Str("host", parsedUrl.Hostname()). Str("host", remoteUrl.Hostname()).
Msg("Unknown SCM platform. Use --platform to include links in findings.") Msg("Unknown SCM platform. Use --platform to include links in findings.")
} else { } else {
logging.Debug(). logging.Debug().
Str("host", parsedUrl.Hostname()). Str("host", remoteUrl.Hostname()).
Str("platform", platform.String()). Str("platform", platform.String()).
Msg("SCM platform parsed from host") Msg("SCM platform parsed from host")
} }
} }
End: End:
var rUrl string
if remoteUrl != nil {
rUrl = remoteUrl.String()
}
return &RemoteInfo{ return &RemoteInfo{
Platform: platform, Platform: platform,
Url: remoteUrl, Url: rUrl,
}, nil }, nil
} }
var sshUrlpat = regexp.MustCompile(`^git@([a-zA-Z0-9.-]+):([\w/.-]+?)(?:\.git)?$`) var sshUrlpat = regexp.MustCompile(`^git@([a-zA-Z0-9.-]+):([\w/.-]+?)(?:\.git)?$`)
func getRemoteUrl(source string) (string, error) { func getRemoteUrl(source string) (*url.URL, error) {
// This will return the first remote — typically, "origin". // This will return the first remote — typically, "origin".
cmd := exec.Command("git", "ls-remote", "--quiet", "--get-url") cmd := exec.Command("git", "ls-remote", "--quiet", "--get-url")
if source != "." { if source != "." {
@@ -141,9 +138,9 @@ func getRemoteUrl(source string) (string, error) {
if err != nil { if err != nil {
var exitError *exec.ExitError var exitError *exec.ExitError
if errors.As(err, &exitError) { if errors.As(err, &exitError) {
return "", fmt.Errorf("command failed (%d): %w, stderr: %s", exitError.ExitCode(), err, string(bytes.TrimSpace(exitError.Stderr))) return nil, fmt.Errorf("command failed (%d): %w, stderr: %s", exitError.ExitCode(), err, string(bytes.TrimSpace(exitError.Stderr)))
} }
return "", err return nil, err
} }
remoteUrl := string(bytes.TrimSpace(stdout)) remoteUrl := string(bytes.TrimSpace(stdout))
@@ -152,7 +149,15 @@ func getRemoteUrl(source string) (string, error) {
repo := strings.TrimSuffix(matches[2], ".git") repo := strings.TrimSuffix(matches[2], ".git")
remoteUrl = fmt.Sprintf("https://%s/%s", host, repo) remoteUrl = fmt.Sprintf("https://%s/%s", host, repo)
} }
return remoteUrl, nil
parsedUrl, err := url.Parse(remoteUrl)
if err != nil {
return nil, fmt.Errorf("unable to parse remote URL: %w", err)
}
// Remove any user info.
parsedUrl.User = nil
return parsedUrl, nil
} }
func platformFromHost(u *url.URL) scm.Platform { func platformFromHost(u *url.URL) scm.Platform {