fixing everything

This commit is contained in:
zach rice committed 2018-10-24 12:28:54 -05:00
1 parent 64e7afea98
commit 98aeee9b1d
1 file changed
+74 -53
+74 -53
View File
@@ -18,6 +18,7 @@ import (
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
"gopkg.in/src-d/go-git.v4/plumbing" "gopkg.in/src-d/go-git.v4/plumbing"
@@ -81,13 +82,14 @@ type Options struct {
OwnerPath string `long:"owner-path" description:"Path to owner directory (repos discovered)"` OwnerPath string `long:"owner-path" description:"Path to owner directory (repos discovered)"`
// Process options // Process options
Disk bool `long:"disk" description:"Clones repo(s) to disk"` MaxGoRoutines int `long:"max-go" description:"Maximum number of concurrent go-routines gitleaks spawns"`
AuditAllRefs bool `long:"all-refs" description:"run audit on all refs"` Disk bool `long:"disk" description:"Clones repo(s) to disk"`
SingleSearch string `long:"single-search" description:"single regular expression to search for"` AuditAllRefs bool `long:"all-refs" description:"run audit on all refs"`
ConfigPath string `long:"config" description:"path to gitleaks config"` SingleSearch string `long:"single-search" description:"single regular expression to search for"`
SSHKey string `long:"ssh-key" description:"path to ssh key"` ConfigPath string `long:"config" description:"path to gitleaks config"`
ExcludeForks bool `long:"exclude-forks" description:"exclude forks for organization/user audits"` SSHKey string `long:"ssh-key" description:"path to ssh key"`
Entropy float64 `long:"entropy" short:"e" description:"Include entropy checks during audit. Entropy scale: 0.0(no entropy) - 8.0(max entropy)"` ExcludeForks bool `long:"exclude-forks" description:"exclude forks for organization/user audits"`
Entropy float64 `long:"entropy" short:"e" description:"Include entropy checks during audit. Entropy scale: 0.0(no entropy) - 8.0(max entropy)"`
// TODO: IncludeMessages string `long:"messages" description:"include commit messages in audit"` // TODO: IncludeMessages string `long:"messages" description:"include commit messages in audit"`
// Output options // Output options
@@ -383,7 +385,7 @@ func cloneRepo() (*RepoDescriptor, error) {
}) })
} }
} else if opts.RepoPath != "" { } else if opts.RepoPath != "" {
log.Infof("opening %s", opts.Repo) log.Infof("opening %s", opts.RepoPath)
repo, err = git.PlainOpen(opts.RepoPath) repo, err = git.PlainOpen(opts.RepoPath)
} else { } else {
log.Infof("cloning %s", opts.Repo) log.Infof("cloning %s", opts.Repo)
@@ -478,19 +480,24 @@ func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak {
repoName string repoName string
leaks []Leak leaks []Leak
commitCount int commitCount int
commitWg sync.WaitGroup
mutex = &sync.Mutex{}
semaphore chan bool
) )
repoName = repo.name repoName = repo.name
if opts.MaxGoRoutines != 0 {
maxGo = opts.MaxGoRoutines
}
if opts.RepoPath != "" {
maxGo = 1
}
semaphore = make(chan bool, maxGo)
cIter, err := repo.repository.Log(&git.LogOptions{From: ref.Hash()}) cIter, err := repo.repository.Log(&git.LogOptions{From: ref.Hash()})
if err != nil { if err != nil {
return nil return nil
} }
err = cIter.ForEach(func(c *object.Commit) error { err = cIter.ForEach(func(c *object.Commit) error {
defer func() {
if r := recover(); r != nil {
log.Warnf("recoverying from panic on commit %s, likely large diff causing panic", c.Hash.String())
}
}()
if c == nil || c.Hash.String() == opts.Commit || (opts.Depth != 0 && commitCount == opts.Depth) { if c == nil || c.Hash.String() == opts.Commit || (opts.Depth != 0 && commitCount == opts.Depth) {
cIter.Close() cIter.Close()
return errors.New("ErrStop") return errors.New("ErrStop")
@@ -502,57 +509,71 @@ func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak {
return nil return nil
} }
var (
filePath string
skipFile bool
)
err = c.Parents().ForEach(func(parent *object.Commit) error { err = c.Parents().ForEach(func(parent *object.Commit) error {
patch, err := c.Patch(parent) commitWg.Add(1)
if err != nil { semaphore <- true
log.Warnf("problem generating patch for commit: %s\n", c.Hash.String()) go func(c *object.Commit, parent *object.Commit) {
return nil var (
} filePath string
for _, f := range patch.FilePatches() { skipFile bool
skipFile = false )
from, to := f.Files() defer func() {
filePath = "???" commitWg.Done()
if from != nil { <-semaphore
filePath = from.Path() if r := recover(); r != nil {
} else if to != nil { log.Warnf("recoverying from panic on commit %s, likely large diff causing panic", c.Hash.String())
filePath = to.Path()
}
for _, re := range whiteListFiles {
if re.FindString(filePath) != "" {
skipFile = true
break
} }
}()
patch, err := c.Patch(parent)
if err != nil {
log.Warnf("problem generating patch for commit: %s\n", c.Hash.String())
return
} }
if skipFile { for _, f := range patch.FilePatches() {
continue skipFile = false
} from, to := f.Files()
chunks := f.Chunks() filePath = "???"
for _, chunk := range chunks { if from != nil {
if chunk.Type() == 1 || chunk.Type() == 2 { filePath = from.Path()
diff := gitDiff{ } else if to != nil {
branchName: string(ref.Name()), filePath = to.Path()
repoName: repoName, }
filePath: filePath, for _, re := range whiteListFiles {
content: chunk.Content(), if re.FindString(filePath) != "" {
sha: c.Hash.String(), skipFile = true
author: c.Author.String(), break
message: c.Message,
} }
chunkLeaks := inspect(diff) }
for _, leak := range chunkLeaks { if skipFile {
leaks = append(leaks, leak) continue
}
chunks := f.Chunks()
for _, chunk := range chunks {
if chunk.Type() == 1 || chunk.Type() == 2 {
diff := gitDiff{
branchName: string(ref.Name()),
repoName: repoName,
filePath: filePath,
content: chunk.Content(),
sha: c.Hash.String(),
author: c.Author.String(),
message: c.Message,
}
chunkLeaks := inspect(diff)
for _, leak := range chunkLeaks {
mutex.Lock()
leaks = append(leaks, leak)
mutex.Unlock()
}
} }
} }
} }
} }(c, parent)
return nil return nil
}) })
return nil return nil
}) })
commitWg.Wait()
return leaks return leaks
} }