Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
fix(git): config normalization for git sources (#3278)
Valentin B.075f8c67a56f
diff --git a/pkg/sources/git/git.go b/pkg/sources/git/git.goindex aefe9f6ef..b98cadbee 100644--- a/pkg/sources/git/git.go+++ b/pkg/sources/git/git.go@@ -969,17 +969,17 @@ func (s *Git) ScanRepo(ctx context.Context, repo *git.Repository, repoPath strin// If either commit cannot be resolved, it returns early.// If both are resolved, it finds and sets the merge base in scanOptions.func normalizeConfig(scanOptions *ScanOptions, repo *git.Repository) error {- baseCommit, baseSet, err := resolveAndSetCommit(repo, &scanOptions.BaseHash)+ baseCommit, err := resolveAndSetCommit(repo, &scanOptions.BaseHash)if err != nil {return err}- headCommit, headSet, err := resolveAndSetCommit(repo, &scanOptions.HeadHash)+ headCommit, err := resolveAndSetCommit(repo, &scanOptions.HeadHash)if err != nil {return err}- if !(baseSet && headSet) {+ if baseCommit == nil || headCommit == nil {return nil}@@ -998,32 +998,31 @@ func normalizeConfig(scanOptions *ScanOptions, repo *git.Repository) error {}// resolveAndSetCommit resolves a Git reference to a commit object and updates the reference if it was not a direct hash.-// Returns the commit object, a boolean indicating if the commit was successfully set, and any error encountered.-func resolveAndSetCommit(repo *git.Repository, ref *string) (*object.Commit, bool, error) {+// Returns the commit object and any error encountered.+func resolveAndSetCommit(repo *git.Repository, ref *string) (*object.Commit, error) {if repo == nil || ref == nil {- return nil, false, fmt.Errorf("repo and ref must be non-nil")+ return nil, fmt.Errorf("repo and ref must be non-nil")}if len(*ref) == 0 {- return nil, false, nil+ return nil, nil}originalRef := *refresolvedRef, err := resolveHash(repo, originalRef)if err != nil {- return nil, false, fmt.Errorf("unable to resolve ref: %w", err)+ return nil, fmt.Errorf("unable to resolve ref: %w", err)}commit, err := repo.CommitObject(plumbing.NewHash(resolvedRef))if err != nil {- return nil, false, fmt.Errorf("unable to resolve commit: %w", err)+ return nil, fmt.Errorf("unable to resolve commit: %w", err)}- wasSet := originalRef != resolvedRef- if wasSet {+ if originalRef != resolvedRef {*ref = resolvedRef}- return commit, wasSet, nil+ return commit, nil}func resolveHash(repo *git.Repository, ref string) (string, error) {