-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
nogit.go
152 lines (133 loc) Β· 3.4 KB
/
nogit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package scan
import (
"bufio"
"context"
"os"
"path/filepath"
"strings"
"sync"
log "github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
"github.com/zricethezav/gitleaks/v7/config"
"github.com/zricethezav/gitleaks/v7/options"
)
// NoGitScanner is a scanner that absolutely despises git
type NoGitScanner struct {
opts options.Options
cfg config.Config
throttle *Throttle
mtx *sync.Mutex
}
// NewNoGitScanner creates and returns a nogit scanner. This is used for scanning files and directories
func NewNoGitScanner(opts options.Options, cfg config.Config) *NoGitScanner {
ngs := &NoGitScanner{
opts: opts,
cfg: cfg,
throttle: NewThrottle(opts),
mtx: &sync.Mutex{},
}
// no-git scans should ignore .git folders by default
// issue: https://github.com/zricethezav/gitleaks/issues/474
// ngs.cfg.Allowlist
err := ngs.cfg.Allowlist.IgnoreDotGit()
if err != nil {
log.Error(err)
return nil
}
return ngs
}
// Scan kicks off a NoGitScanner Scan
func (ngs *NoGitScanner) Scan() (Report, error) {
var scannerReport Report
g, _ := errgroup.WithContext(context.Background())
paths := make(chan string)
g.Go(func() error {
defer close(paths)
return filepath.Walk(ngs.opts.Path,
func(path string, fInfo os.FileInfo, err error) error {
if err != nil {
return err
}
if fInfo.Mode().IsRegular() {
paths <- path
}
return nil
})
})
for path := range paths {
p := path
ngs.throttle.Limit()
g.Go(func() error {
defer ngs.throttle.Release()
if ngs.cfg.Allowlist.FileAllowed(filepath.Base(p)) ||
ngs.cfg.Allowlist.PathAllowed(p) {
return nil
}
for _, rule := range ngs.cfg.Rules {
if rule.HasFileOrPathLeakOnly(p) {
leak := NewLeak("", "Filename or path offender: "+p, defaultLineNumber)
relPath, err := filepath.Rel(ngs.opts.Path, p)
if err != nil {
leak.File = p
} else {
leak.File = relPath
}
leak.Rule = rule.Description
leak.Tags = strings.Join(rule.Tags, ", ")
leak.Log(ngs.opts)
ngs.mtx.Lock()
scannerReport.Leaks = append(scannerReport.Leaks, leak)
ngs.mtx.Unlock()
}
}
f, err := os.Open(p) // #nosec
if err != nil {
return err
}
scanner := bufio.NewScanner(f)
lineNumber := 0
for scanner.Scan() {
lineNumber++
for _, rule := range ngs.cfg.Rules {
line := scanner.Text()
if rule.AllowList.FileAllowed(filepath.Base(p)) ||
rule.AllowList.PathAllowed(p) {
continue
}
offender := rule.Inspect(line)
if offender.IsEmpty() {
continue
}
if ngs.cfg.Allowlist.RegexAllowed(line) {
continue
}
if rule.File.String() != "" && !rule.HasFileLeak(filepath.Base(p)) {
continue
}
if rule.Path.String() != "" && !rule.HasFilePathLeak(p) {
continue
}
leak := NewLeak(line, offender.ToString(), defaultLineNumber).WithEntropy(offender.EntropyLevel)
relPath, err := filepath.Rel(ngs.opts.Path, p)
if err != nil {
leak.File = p
} else {
leak.File = relPath
}
leak.LineNumber = lineNumber
leak.Rule = rule.Description
leak.Tags = strings.Join(rule.Tags, ", ")
leak.Log(ngs.opts)
ngs.mtx.Lock()
scannerReport.Leaks = append(scannerReport.Leaks, leak)
ngs.mtx.Unlock()
}
}
return f.Close()
})
}
if err := g.Wait(); err != nil {
log.Error(err)
}
return scannerReport, nil
}