mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-12-01 05:24:22 +01:00
Fix premature exit for nogit scans, limit goroutines (#619)
* fix premature exit on nogit scan, actually limit concurreny for nogit * removing files scanned log
This commit is contained in:
1 parent
aadd9387b2
commit
6ddf27c5c3
2 files changed
+31
-32
No files matched your search
+22
-20
@@ -6,6 +6,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
@@ -17,15 +18,19 @@ import (
|
|||||||
|
|
||||||
// NoGitScanner is a scanner that absolutely despises git
|
// NoGitScanner is a scanner that absolutely despises git
|
||||||
type NoGitScanner struct {
|
type NoGitScanner struct {
|
||||||
opts options.Options
|
opts options.Options
|
||||||
cfg config.Config
|
cfg config.Config
|
||||||
|
throttle *Throttle
|
||||||
|
mtx *sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewNoGitScanner creates and returns a nogit scanner. This is used for scanning files and directories
|
// NewNoGitScanner creates and returns a nogit scanner. This is used for scanning files and directories
|
||||||
func NewNoGitScanner(opts options.Options, cfg config.Config) *NoGitScanner {
|
func NewNoGitScanner(opts options.Options, cfg config.Config) *NoGitScanner {
|
||||||
ngs := &NoGitScanner{
|
ngs := &NoGitScanner{
|
||||||
opts: opts,
|
opts: opts,
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
|
throttle: NewThrottle(opts),
|
||||||
|
mtx: &sync.Mutex{},
|
||||||
}
|
}
|
||||||
|
|
||||||
// no-git scans should ignore .git folders by default
|
// no-git scans should ignore .git folders by default
|
||||||
@@ -44,7 +49,8 @@ func (ngs *NoGitScanner) Scan() (Report, error) {
|
|||||||
var scannerReport Report
|
var scannerReport Report
|
||||||
|
|
||||||
g, _ := errgroup.WithContext(context.Background())
|
g, _ := errgroup.WithContext(context.Background())
|
||||||
paths := make(chan string, 100)
|
|
||||||
|
paths := make(chan string)
|
||||||
|
|
||||||
g.Go(func() error {
|
g.Go(func() error {
|
||||||
defer close(paths)
|
defer close(paths)
|
||||||
@@ -60,11 +66,11 @@ func (ngs *NoGitScanner) Scan() (Report, error) {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
leaks := make(chan Leak, 100)
|
|
||||||
|
|
||||||
for path := range paths {
|
for path := range paths {
|
||||||
p := path
|
p := path
|
||||||
|
ngs.throttle.Limit()
|
||||||
g.Go(func() error {
|
g.Go(func() error {
|
||||||
|
defer ngs.throttle.Release()
|
||||||
if ngs.cfg.Allowlist.FileAllowed(filepath.Base(p)) ||
|
if ngs.cfg.Allowlist.FileAllowed(filepath.Base(p)) ||
|
||||||
ngs.cfg.Allowlist.PathAllowed(p) {
|
ngs.cfg.Allowlist.PathAllowed(p) {
|
||||||
return nil
|
return nil
|
||||||
@@ -84,7 +90,9 @@ func (ngs *NoGitScanner) Scan() (Report, error) {
|
|||||||
|
|
||||||
leak.Log(ngs.opts)
|
leak.Log(ngs.opts)
|
||||||
|
|
||||||
leaks <- leak
|
ngs.mtx.Lock()
|
||||||
|
scannerReport.Leaks = append(scannerReport.Leaks, leak)
|
||||||
|
ngs.mtx.Unlock()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -129,26 +137,20 @@ func (ngs *NoGitScanner) Scan() (Report, error) {
|
|||||||
leak.LineNumber = lineNumber
|
leak.LineNumber = lineNumber
|
||||||
leak.Rule = rule.Description
|
leak.Rule = rule.Description
|
||||||
leak.Tags = strings.Join(rule.Tags, ", ")
|
leak.Tags = strings.Join(rule.Tags, ", ")
|
||||||
|
|
||||||
leak.Log(ngs.opts)
|
leak.Log(ngs.opts)
|
||||||
|
|
||||||
leaks <- leak
|
ngs.mtx.Lock()
|
||||||
|
scannerReport.Leaks = append(scannerReport.Leaks, leak)
|
||||||
|
ngs.mtx.Unlock()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return f.Close()
|
return f.Close()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
if err := g.Wait(); err != nil {
|
||||||
if err := g.Wait(); err != nil {
|
log.Error(err)
|
||||||
log.Error(err)
|
|
||||||
}
|
|
||||||
close(leaks)
|
|
||||||
}()
|
|
||||||
|
|
||||||
for leak := range leaks {
|
|
||||||
scannerReport.Leaks = append(scannerReport.Leaks, leak)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return scannerReport, g.Wait()
|
return scannerReport, nil
|
||||||
}
|
}
|
||||||
+9
-12
@@ -2,6 +2,7 @@ package scan
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"golang.org/x/sync/errgroup"
|
"golang.org/x/sync/errgroup"
|
||||||
|
|
||||||
@@ -21,6 +22,7 @@ type RepoScanner struct {
|
|||||||
repo *git.Repository
|
repo *git.Repository
|
||||||
throttle *Throttle
|
throttle *Throttle
|
||||||
repoName string
|
repoName string
|
||||||
|
mtx *sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewRepoScanner returns a new repo scanner (go figure). This function also
|
// NewRepoScanner returns a new repo scanner (go figure). This function also
|
||||||
@@ -32,6 +34,7 @@ func NewRepoScanner(opts options.Options, cfg config.Config, repo *git.Repositor
|
|||||||
repo: repo,
|
repo: repo,
|
||||||
throttle: NewThrottle(opts),
|
throttle: NewThrottle(opts),
|
||||||
repoName: getRepoName(opts),
|
repoName: getRepoName(opts),
|
||||||
|
mtx: &sync.Mutex{},
|
||||||
}
|
}
|
||||||
|
|
||||||
return rs
|
return rs
|
||||||
@@ -54,7 +57,6 @@ func (rs *RepoScanner) Scan() (Report, error) {
|
|||||||
|
|
||||||
g, _ := errgroup.WithContext(context.Background())
|
g, _ := errgroup.WithContext(context.Background())
|
||||||
commits = make(chan *object.Commit)
|
commits = make(chan *object.Commit)
|
||||||
leaks := make(chan Leak)
|
|
||||||
|
|
||||||
commitNum := 0
|
commitNum := 0
|
||||||
g.Go(func() error {
|
g.Go(func() error {
|
||||||
@@ -91,25 +93,20 @@ func (rs *RepoScanner) Scan() (Report, error) {
|
|||||||
log.Error(err)
|
log.Error(err)
|
||||||
}
|
}
|
||||||
for _, leak := range report.Leaks {
|
for _, leak := range report.Leaks {
|
||||||
leaks <- leak
|
rs.mtx.Lock()
|
||||||
|
scannerReport.Leaks = append(scannerReport.Leaks, leak)
|
||||||
|
rs.mtx.Unlock()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
if err := g.Wait(); err != nil {
|
||||||
if err := g.Wait(); err != nil {
|
log.Error(err)
|
||||||
log.Error(err)
|
|
||||||
}
|
|
||||||
close(leaks)
|
|
||||||
}()
|
|
||||||
|
|
||||||
for leak := range leaks {
|
|
||||||
scannerReport.Leaks = append(scannerReport.Leaks, leak)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
scannerReport.Commits = commitNum
|
scannerReport.Commits = commitNum
|
||||||
return scannerReport, g.Wait()
|
return scannerReport, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetRepoName sets the repo name
|
// SetRepoName sets the repo name
|
||||||
|
|||||||
Reference in new issue
Block a user