Skip to content

Commit

Permalink
🛡 do not leak credentials on git clone
Browse files Browse the repository at this point in the history
Signed-off-by: Dominik Richter <[email protected]>
  • Loading branch information
arlimus authored and chris-rock committed Sep 29, 2023
1 parent c38e7e1 commit 02d86a8
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions providers/terraform/connection/hcl_manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func NewHclGitConnection(id uint32, asset *inventory.Asset) (*Connection, error)
return conn, nil
}

func gitClone(url string) (string, func(), error) {
func gitClone(gitUrl string) (string, func(), error) {
cloneDir, err := os.MkdirTemp(os.TempDir(), "gitClone")
if err != nil {
return "", nil, errors.Wrap(err, "failed to create temporary dir for git processing")
Expand All @@ -197,24 +197,34 @@ func gitClone(url string) (string, func(), error) {
}
}

log.Info().Str("url", url).Str("path", cloneDir).Msg("git clone")
// Note: DO NOT leak credentials into logs!!
var infoUrl string
if u, err := url.Parse(gitUrl); err == nil {
if u.User != nil {
u.User = url.User("_obfuscated_")
}
infoUrl = u.String()
}

log.Info().Str("url", infoUrl).Str("path", cloneDir).Msg("git clone")
repo, err := git.PlainClone(cloneDir, false, &git.CloneOptions{
URL: url,
URL: gitUrl,
Progress: os.Stderr,
Depth: 1,
RecurseSubmodules: git.DefaultSubmoduleRecursionDepth,
})
if err != nil {
closer()
return "", nil, errors.Wrap(err, "failed to clone git repo "+url)
return "", nil, errors.Wrap(err, "failed to clone git repo "+infoUrl)
}

ref, err := repo.Head()
if err != nil {
closer()
return "", nil, errors.Wrap(err, "failed to get head of git repo "+url)
return "", nil, errors.Wrap(err, "failed to get head of git repo "+infoUrl)
}
log.Info().Str("url", url).Str("path", cloneDir).Str("head", ref.Hash().String()).Msg("finshed git clone")

log.Info().Str("url", infoUrl).Str("path", cloneDir).Str("head", ref.Hash().String()).Msg("finished git clone")

return cloneDir, closer, nil
}

0 comments on commit 02d86a8

Please sign in to comment.