Compare commits

...
3 Commits
4 changed files with 14 additions and 18 deletions

No files matched your search

+2
View File
@@ -17,6 +17,8 @@ type Options struct {
Redact bool Redact bool
} }
const MAXGOROUTINES = 4
func DetectFindings(cfg config.Config, b []byte, filePath string, commit string) []report.Finding { func DetectFindings(cfg config.Config, b []byte, filePath string, commit string) []report.Finding {
var findings []report.Finding var findings []report.Finding
linePairs := regexp.MustCompile("\n").FindAllIndex(b, -1) linePairs := regexp.MustCompile("\n").FindAllIndex(b, -1)
+5 -10
View File
@@ -7,7 +7,6 @@ import (
"sync" "sync"
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
godocutil "golang.org/x/tools/godoc/util"
"github.com/zricethezav/gitleaks/v8/config" "github.com/zricethezav/gitleaks/v8/config"
"github.com/zricethezav/gitleaks/v8/report" "github.com/zricethezav/gitleaks/v8/report"
@@ -20,7 +19,7 @@ func FromFiles(source string, cfg config.Config, outputOptions Options) ([]repor
findings []report.Finding findings []report.Finding
mu sync.Mutex mu sync.Mutex
) )
concurrentGoroutines := make(chan struct{}, 4) concurrentGoroutines := make(chan struct{}, MAXGOROUTINES)
g, _ := errgroup.WithContext(context.Background()) g, _ := errgroup.WithContext(context.Background())
paths := make(chan string) paths := make(chan string)
g.Go(func() error { g.Go(func() error {
@@ -41,18 +40,15 @@ func FromFiles(source string, cfg config.Config, outputOptions Options) ([]repor
}) })
for pa := range paths { for pa := range paths {
p := pa p := pa
concurrentGoroutines <- struct{}{}
g.Go(func() error { g.Go(func() error {
concurrentGoroutines <- struct{}{} defer func() {
<-concurrentGoroutines
}()
b, err := os.ReadFile(p) b, err := os.ReadFile(p)
if err != nil { if err != nil {
<-concurrentGoroutines
return err return err
} }
if !godocutil.IsText(b) {
<-concurrentGoroutines
return nil
}
fis := DetectFindings(cfg, b, p, "") fis := DetectFindings(cfg, b, p, "")
for _, fi := range fis { for _, fi := range fis {
// need to add 1 since line counting starts at 1 // need to add 1 since line counting starts at 1
@@ -69,7 +65,6 @@ func FromFiles(source string, cfg config.Config, outputOptions Options) ([]repor
findings = append(findings, fi) findings = append(findings, fi)
mu.Unlock() mu.Unlock()
} }
<-concurrentGoroutines
return nil return nil
}) })
} }
+6 -7
View File
@@ -9,7 +9,6 @@ import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/zricethezav/gitleaks/v8/config" "github.com/zricethezav/gitleaks/v8/config"
"github.com/zricethezav/gitleaks/v8/report" "github.com/zricethezav/gitleaks/v8/report"
godocutil "golang.org/x/tools/godoc/util"
) )
// FromGit accepts a gitdiff.File channel (structure output from `git log -p`) and a configuration // FromGit accepts a gitdiff.File channel (structure output from `git log -p`) and a configuration
@@ -19,16 +18,20 @@ func FromGit(files <-chan *gitdiff.File, cfg config.Config, outputOptions Option
var findings []report.Finding var findings []report.Finding
mu := sync.Mutex{} mu := sync.Mutex{}
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
concurrentGoroutines := make(chan struct{}, MAXGOROUTINES)
commitMap := make(map[string]bool) commitMap := make(map[string]bool)
for f := range files { for f := range files {
// keep track of commits for logging // keep track of commits for logging
if f.PatchHeader != nil { if f.PatchHeader != nil {
commitMap[f.PatchHeader.SHA] = true commitMap[f.PatchHeader.SHA] = true
} }
wg.Add(1) wg.Add(1)
concurrentGoroutines <- struct{}{}
go func(f *gitdiff.File) { go func(f *gitdiff.File) {
defer wg.Done() defer func() {
wg.Done()
<-concurrentGoroutines
}()
if f.IsBinary { if f.IsBinary {
return return
} }
@@ -54,10 +57,6 @@ func FromGit(files <-chan *gitdiff.File, cfg config.Config, outputOptions Option
continue continue
} }
if !godocutil.IsText([]byte(tf.Raw(gitdiff.OpAdd))) {
continue
}
for _, fi := range DetectFindings(cfg, []byte(tf.Raw(gitdiff.OpAdd)), f.NewName, commitSHA) { for _, fi := range DetectFindings(cfg, []byte(tf.Raw(gitdiff.OpAdd)), f.NewName, commitSHA) {
// don't add to start/end lines if finding is from a file only rule // don't add to start/end lines if finding is from a file only rule
if !strings.HasPrefix(fi.Match, "file detected") { if !strings.HasPrefix(fi.Match, "file detected") {
+1 -1
View File
@@ -64,7 +64,7 @@ func messageText(f Finding) string {
} }
func getResults(findings []Finding) []Results { func getResults(findings []Finding) []Results {
var results []Results results := []Results{}
for _, f := range findings { for _, f := range findings {
r := Results{ r := Results{
Message: Message{ Message: Message{