mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-12-01 05:24:22 +01:00
1 parent
65a91ae976
commit
65f42020af
11 files changed
+327
-261
No files matched your search
+14
-23
@@ -20,7 +20,6 @@ type CommitScanner struct {
|
||||
repo *git.Repository
|
||||
repoName string
|
||||
commit *object.Commit
|
||||
patch *object.Patch
|
||||
}
|
||||
|
||||
// NewCommitScanner creates and returns a commit scanner
|
||||
@@ -40,12 +39,6 @@ func (cs *CommitScanner) SetRepoName(repoName string) {
|
||||
cs.repoName = repoName
|
||||
}
|
||||
|
||||
// SetPatch sets the patch to be inspected by the commit scanner. This is used to avoid
|
||||
// a race condition when running a threaded repo scan
|
||||
func (cs *CommitScanner) SetPatch(patch *object.Patch) {
|
||||
cs.patch = patch
|
||||
}
|
||||
|
||||
// Scan kicks off a CommitScanner Scan
|
||||
func (cs *CommitScanner) Scan() (Report, error) {
|
||||
var scannerReport Report
|
||||
@@ -54,25 +47,23 @@ func (cs *CommitScanner) Scan() (Report, error) {
|
||||
return facScanner.Scan()
|
||||
}
|
||||
|
||||
if cs.patch == nil {
|
||||
parent, err := cs.commit.Parent(0)
|
||||
if err != nil {
|
||||
return scannerReport, err
|
||||
}
|
||||
|
||||
if parent == nil {
|
||||
return scannerReport, nil
|
||||
}
|
||||
|
||||
cs.patch, err = parent.Patch(cs.commit)
|
||||
if err != nil {
|
||||
return scannerReport, fmt.Errorf("could not generate Patch")
|
||||
}
|
||||
parent, err := cs.commit.Parent(0)
|
||||
if err != nil {
|
||||
return scannerReport, err
|
||||
}
|
||||
|
||||
patchContent := cs.patch.String()
|
||||
if parent == nil {
|
||||
return scannerReport, nil
|
||||
}
|
||||
|
||||
for _, f := range cs.patch.FilePatches() {
|
||||
patch, err := parent.Patch(cs.commit)
|
||||
if err != nil {
|
||||
return scannerReport, fmt.Errorf("could not generate Patch")
|
||||
}
|
||||
|
||||
patchContent := patch.String()
|
||||
|
||||
for _, f := range patch.FilePatches() {
|
||||
if f.IsBinary() {
|
||||
continue
|
||||
}
|
||||
|
||||
+60
-79
@@ -1,17 +1,17 @@
|
||||
package scan
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"context"
|
||||
|
||||
"golang.org/x/sync/errgroup"
|
||||
|
||||
"github.com/zricethezav/gitleaks/v7/config"
|
||||
"github.com/zricethezav/gitleaks/v7/options"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/go-git/go-git/v5"
|
||||
"github.com/go-git/go-git/v5/plumbing/object"
|
||||
"github.com/go-git/go-git/v5/plumbing/storer"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// RepoScanner is a repo scanner
|
||||
@@ -19,35 +19,30 @@ type RepoScanner struct {
|
||||
opts options.Options
|
||||
cfg config.Config
|
||||
repo *git.Repository
|
||||
throttle *Throttle
|
||||
repoName string
|
||||
|
||||
leakChan chan Leak
|
||||
leakWG *sync.WaitGroup
|
||||
leakCache map[string]bool
|
||||
leaks []Leak
|
||||
}
|
||||
|
||||
// NewRepoScanner returns a new repo scanner (go figure). This function also
|
||||
// sets up the leak listener for multi-threaded awesomeness.
|
||||
func NewRepoScanner(opts options.Options, cfg config.Config, repo *git.Repository) *RepoScanner {
|
||||
rs := &RepoScanner{
|
||||
opts: opts,
|
||||
cfg: cfg,
|
||||
repo: repo,
|
||||
leakChan: make(chan Leak),
|
||||
leakWG: &sync.WaitGroup{},
|
||||
leakCache: make(map[string]bool),
|
||||
repoName: getRepoName(opts),
|
||||
opts: opts,
|
||||
cfg: cfg,
|
||||
repo: repo,
|
||||
throttle: NewThrottle(opts),
|
||||
repoName: getRepoName(opts),
|
||||
}
|
||||
|
||||
go rs.receiveLeaks()
|
||||
|
||||
return rs
|
||||
}
|
||||
|
||||
// Scan kicks of a repo scan
|
||||
func (rs *RepoScanner) Scan() (Report, error) {
|
||||
var scannerReport Report
|
||||
var (
|
||||
scannerReport Report
|
||||
commits chan *object.Commit
|
||||
)
|
||||
logOpts, err := logOptions(rs.repo, rs.opts)
|
||||
if err != nil {
|
||||
return scannerReport, err
|
||||
@@ -56,80 +51,66 @@ func (rs *RepoScanner) Scan() (Report, error) {
|
||||
if err != nil {
|
||||
return scannerReport, err
|
||||
}
|
||||
semaphore := make(chan bool, howManyThreads(rs.opts.Threads))
|
||||
wg := sync.WaitGroup{}
|
||||
|
||||
err = cIter.ForEach(func(c *object.Commit) error {
|
||||
if c == nil || depthReached(scannerReport.Commits, rs.opts) {
|
||||
return storer.ErrStop
|
||||
}
|
||||
g, _ := errgroup.WithContext(context.Background())
|
||||
commits = make(chan *object.Commit)
|
||||
leaks := make(chan Leak)
|
||||
|
||||
if rs.cfg.Allowlist.CommitAllowed(c.Hash.String()) {
|
||||
return nil
|
||||
}
|
||||
commitNum := 0
|
||||
g.Go(func() error {
|
||||
defer close(commits)
|
||||
err = cIter.ForEach(func(c *object.Commit) error {
|
||||
if c == nil || depthReached(commitNum, rs.opts) {
|
||||
return storer.ErrStop
|
||||
}
|
||||
|
||||
// Check if at root
|
||||
if len(c.ParentHashes) == 0 {
|
||||
scannerReport.Commits++
|
||||
facScanner := NewFilesAtCommitScanner(rs.opts, rs.cfg, rs.repo, c)
|
||||
facScanner.repoName = rs.repoName
|
||||
facReport, err := facScanner.Scan()
|
||||
if err != nil {
|
||||
return err
|
||||
if rs.cfg.Allowlist.CommitAllowed(c.Hash.String()) {
|
||||
return nil
|
||||
}
|
||||
commitNum++
|
||||
commits <- c
|
||||
if c.Hash.String() == rs.opts.CommitTo {
|
||||
return storer.ErrStop
|
||||
}
|
||||
scannerReport.Leaks = append(scannerReport.Leaks, facReport.Leaks...)
|
||||
return nil
|
||||
}
|
||||
|
||||
// inspect first parent only as all other parents will be eventually reached
|
||||
// (they exist as the tip of other branches, etc)
|
||||
// See https://github.com/zricethezav/gitleaks/issues/413 for details
|
||||
parent, err := c.Parent(0)
|
||||
if err != nil || parent == nil {
|
||||
return err
|
||||
}
|
||||
patch, err := parent.Patch(c)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not generate Patch")
|
||||
}
|
||||
|
||||
scannerReport.Commits++
|
||||
wg.Add(1)
|
||||
semaphore <- true
|
||||
go func(c *object.Commit, patch *object.Patch) {
|
||||
defer func() {
|
||||
<-semaphore
|
||||
wg.Done()
|
||||
}()
|
||||
})
|
||||
cIter.Close()
|
||||
return nil
|
||||
})
|
||||
|
||||
for commit := range commits {
|
||||
c := commit
|
||||
rs.throttle.Limit()
|
||||
g.Go(func() error {
|
||||
commitScanner := NewCommitScanner(rs.opts, rs.cfg, rs.repo, c)
|
||||
commitScanner.SetRepoName(rs.repoName)
|
||||
commitScanner.SetPatch(patch)
|
||||
report, err := commitScanner.Scan()
|
||||
rs.throttle.Release()
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
for _, leak := range report.Leaks {
|
||||
rs.leakWG.Add(1)
|
||||
rs.leakChan <- leak
|
||||
leaks <- leak
|
||||
}
|
||||
}(c, patch)
|
||||
|
||||
if c.Hash.String() == rs.opts.CommitTo {
|
||||
return storer.ErrStop
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
wg.Wait()
|
||||
rs.leakWG.Wait()
|
||||
scannerReport.Leaks = append(scannerReport.Leaks, rs.leaks...)
|
||||
return scannerReport, nil
|
||||
}
|
||||
|
||||
func (rs *RepoScanner) receiveLeaks() {
|
||||
for leak := range rs.leakChan {
|
||||
rs.leaks = append(rs.leaks, leak)
|
||||
rs.leakWG.Done()
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
go func() {
|
||||
g.Wait()
|
||||
close(leaks)
|
||||
}()
|
||||
|
||||
for leak := range leaks {
|
||||
scannerReport.Leaks = append(scannerReport.Leaks, leak)
|
||||
}
|
||||
|
||||
scannerReport.Commits = commitNum
|
||||
return scannerReport, g.Wait()
|
||||
}
|
||||
|
||||
// SetRepoName sets the repo name
|
||||
func (rs *RepoScanner) SetRepoName(repoName string) {
|
||||
rs.repoName = repoName
|
||||
}
|
||||
+56
-15
@@ -5,15 +5,11 @@ import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"github.com/zricethezav/gitleaks/v7/config"
|
||||
"github.com/zricethezav/gitleaks/v7/options"
|
||||
|
||||
"github.com/sergi/go-diff/diffmatchpatch"
|
||||
)
|
||||
|
||||
const testRepoBase = "../test_data/test_repos/"
|
||||
@@ -202,6 +198,7 @@ func TestScan(t *testing.T) {
|
||||
Path: "../test_data/test_repos/",
|
||||
Report: "../test_data/test_local_owner_aws_leak.json.got",
|
||||
ReportFormat: "json",
|
||||
Threads: runtime.GOMAXPROCS(0),
|
||||
},
|
||||
wantPath: "../test_data/test_local_owner_aws_leak.json",
|
||||
},
|
||||
@@ -643,24 +640,68 @@ func fileCheck(wantPath, gotPath string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
sort.Slice(gotLeaks, func(i, j int) bool {
|
||||
return (gotLeaks)[i].Offender+(gotLeaks)[i].File < (gotLeaks)[j].Offender+(gotLeaks)[j].File
|
||||
})
|
||||
sort.Slice(wantLeaks, func(i, j int) bool {
|
||||
return (wantLeaks)[i].Offender+(wantLeaks)[i].File < (wantLeaks)[j].Offender+(wantLeaks)[j].File
|
||||
})
|
||||
|
||||
if !reflect.DeepEqual(gotLeaks, wantLeaks) {
|
||||
dmp := diffmatchpatch.New()
|
||||
diffs := dmp.DiffMain(string(want), string(got), false)
|
||||
return fmt.Errorf("%s does not equal %s: %s", wantPath, gotPath, dmp.DiffPrettyText(diffs))
|
||||
if len(wantLeaks) != len(gotLeaks) {
|
||||
return fmt.Errorf("got %d leaks, want %d leaks", len(gotLeaks), len(wantLeaks))
|
||||
}
|
||||
|
||||
for _, wantLeak := range wantLeaks {
|
||||
found := false
|
||||
for _, gotLeak := range gotLeaks {
|
||||
if same(gotLeak, wantLeak) {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
return fmt.Errorf("unable to find %+v in got leaks", wantLeak)
|
||||
}
|
||||
}
|
||||
|
||||
if err := os.Remove(gotPath); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func same(l1, l2 Leak) bool {
|
||||
if l1.Commit != l2.Commit {
|
||||
return false
|
||||
}
|
||||
|
||||
if l1.Offender != l2.Offender {
|
||||
return false
|
||||
}
|
||||
|
||||
if l1.Line != l2.Line {
|
||||
return false
|
||||
}
|
||||
|
||||
if l1.Tags != l2.Tags {
|
||||
return false
|
||||
}
|
||||
|
||||
if l1.LineNumber != l2.LineNumber {
|
||||
return false
|
||||
}
|
||||
|
||||
if l1.Author != l2.Author {
|
||||
return false
|
||||
}
|
||||
|
||||
if l1.LeakURL != l2.LeakURL {
|
||||
return false
|
||||
}
|
||||
|
||||
if l1.RepoURL != l2.RepoURL {
|
||||
return false
|
||||
}
|
||||
|
||||
if l1.Repo != l2.Repo {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
|
||||
}
|
||||
|
||||
func moveDotGit(from, to string) error {
|
||||
repoDirs, err := ioutil.ReadDir("../test_data/test_repos")
|
||||
if err != nil {
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package scan
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
|
||||
"github.com/zricethezav/gitleaks/v7/options"
|
||||
)
|
||||
|
||||
const (
|
||||
singleThreadCommitBuffer = 1
|
||||
multiThreadCommitBufferMultiplier = 10
|
||||
)
|
||||
|
||||
// Throttle is a struct that limits the number of concurrent goroutines and sets the
|
||||
// number of threads available for gitleaks to use via GOMAXPROCS.
|
||||
type Throttle struct {
|
||||
throttle chan bool
|
||||
}
|
||||
|
||||
// NewThrottle accepts some options and returns a throttle for scanners to use
|
||||
func NewThrottle(opts options.Options) *Throttle {
|
||||
t := Throttle{}
|
||||
if opts.Threads <= 1 {
|
||||
runtime.GOMAXPROCS(1)
|
||||
t.throttle = make(chan bool, singleThreadCommitBuffer)
|
||||
return &t
|
||||
}
|
||||
|
||||
runtime.GOMAXPROCS(opts.Threads)
|
||||
t.throttle = make(chan bool, multiThreadCommitBufferMultiplier*opts.Threads)
|
||||
return &t
|
||||
|
||||
}
|
||||
|
||||
// Limit blocks new goroutines from spinning up if throttle is at capacity
|
||||
func (t *Throttle) Limit() {
|
||||
t.throttle <- true
|
||||
}
|
||||
|
||||
// Release releases the hold on the throttle, allowing more goroutines to be spun up
|
||||
func (t *Throttle) Release() {
|
||||
<-t.throttle
|
||||
}
|
||||
Reference in new issue
Block a user