Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
Redact secret in git command output (#2539)
Cody Roseb7f08db1eff7
diff --git a/pkg/sources/git/git.go b/pkg/sources/git/git.goindex c60dc4032..f3b4cb146 100644--- a/pkg/sources/git/git.go+++ b/pkg/sources/git/git.go@@ -421,7 +421,7 @@ func executeClone(ctx context.Context, params cloneParams) (*git.Repository, errgitArgs = append(gitArgs, params.args...)cloneCmd := exec.Command("git", gitArgs...)- safeURL, err := stripPassword(params.gitURL)+ safeURL, secretForRedaction, err := stripPassword(params.gitURL)if err != nil {ctx.Logger().V(1).Info("error stripping password from git url", "error", err)}@@ -433,17 +433,24 @@ func executeClone(ctx context.Context, params cloneParams) (*git.Repository, err)// Execute command and wait for the stdout / stderr.- output, err := cloneCmd.CombinedOutput()+ outputBytes, err := cloneCmd.CombinedOutput()+ var output string+ if secretForRedaction != "" {+ output = strings.ReplaceAll(string(outputBytes), secretForRedaction, "<secret>")+ } else {+ output = string(outputBytes)+ }+if err != nil {err = fmt.Errorf("error executing git clone: %w", err)}- logger.V(3).Info("git subcommand finished", "output", string(output))+ logger.V(3).Info("git subcommand finished", "output", output)if cloneCmd.ProcessState == nil {return nil, fmt.Errorf("clone command exited with no output")}if cloneCmd.ProcessState != nil && cloneCmd.ProcessState.ExitCode() != 0 {- logger.V(1).Info("git clone failed", "output", string(output), "error", err)+ logger.V(1).Info("git clone failed", "output", output, "error", err)return nil, fmt.Errorf("could not clone repo: %s, %w", safeURL, err)}@@ -979,19 +986,24 @@ func resolveHash(repo *git.Repository, ref string) (string, error) {return resolved.String(), nil}-func stripPassword(u string) (string, error) {+// stripPassword removes username:password contents from URLs. The first return value is the cleaned URL and the second+// is the password that was returned, if any. Callers can therefore use this function to identify secret material to+// redact elsewhere. If the argument begins with git@, it is returned unchanged, and the returned password is the empty+// string. If the argument is otherwise not parseable by url.Parse, an error is returned.+func stripPassword(u string) (string, string, error) {if strings.HasPrefix(u, "git@") {- return u, nil+ return u, "", nil}repoURL, err := url.Parse(u)if err != nil {- return "", fmt.Errorf("repo remote is not a URI: %w", err)+ return "", "", fmt.Errorf("repo remote is not a URI: %w", err)}+ password, _ := repoURL.User.Password()repoURL.User = nil- return repoURL.String(), nil+ return repoURL.String(), password, nil}// TryAdditionalBaseRefs looks for additional possible base refs for a repo and returns a hash if found.@@ -1153,7 +1165,7 @@ func getSafeRemoteURL(repo *git.Repository, preferred string) string {remote = remotes[0]}// URLs is guaranteed to be non-empty- safeURL, err := stripPassword(remote.Config().URLs[0])+ safeURL, _, err := stripPassword(remote.Config().URLs[0])if err != nil {return ""}