This repository has been archived by the owner on May 18, 2022. It is now read-only.
forked from rzandonai/gitleaks
/
filesatcommit.go
175 lines (144 loc) 路 4.39 KB
/
filesatcommit.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package scan
import (
"path/filepath"
"strings"
"github.com/ed-wp/gitleaks/v7/config"
"github.com/ed-wp/gitleaks/v7/options"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
)
// FilesAtCommitScanner is a files at commit scanner. This differs from CommitScanner
// as CommitScanner generates patches that are scanned. FilesAtCommitScanner instead looks at
// files available at a commit's worktree and scans the entire content of said files.
// Apologies for the awful struct name...
type FilesAtCommitScanner struct {
opts options.Options
cfg config.Config
repo *git.Repository
commit *object.Commit
repoName string
}
// NewFilesAtCommitScanner creates and returns a files at commit scanner
func NewFilesAtCommitScanner(opts options.Options, cfg config.Config, repo *git.Repository, commit *object.Commit) *FilesAtCommitScanner {
fs := &FilesAtCommitScanner{
opts: opts,
cfg: cfg,
repo: repo,
commit: commit,
repoName: getRepoName(opts),
}
return fs
}
// Scan kicks off a FilesAtCommitScanner Scan
func (fs *FilesAtCommitScanner) Scan() (Report, error) {
var scannerReport Report
if fs.cfg.Allowlist.CommitAllowed(fs.commit.Hash.String()) {
return scannerReport, nil
}
fIter, err := fs.commit.Files()
if err != nil {
return scannerReport, err
}
err = fIter.ForEach(func(f *object.File) error {
bin, err := f.IsBinary()
if bin {
return nil
} else if err != nil {
return err
}
if fs.cfg.Allowlist.FileAllowed(filepath.Base(f.Name)) ||
fs.cfg.Allowlist.PathAllowed(f.Name) {
return nil
}
content, err := f.Contents()
if err != nil {
return err
}
// Check individual file path ONLY rules
for _, rule := range fs.cfg.Rules {
if rule.AllowList.FileAllowed(filepath.Base(f.Name)) ||
rule.AllowList.PathAllowed(f.Name) ||
rule.AllowList.CommitAllowed(fs.commit.Hash.String()) {
continue
}
if rule.Multiline {
offenders := rule.InspectFile(content)
if offenders != nil {
for _, offender := range offenders {
if offender.IsEmpty() {
continue
}
if fs.cfg.Allowlist.RegexAllowed(content) {
continue
}
if rule.File.String() != "" && !rule.HasFileLeak(filepath.Base(f.Name)) {
continue
}
if rule.Path.String() != "" && !rule.HasFilePathLeak(f.Name) {
continue
}
leak := NewLeak(offender.Match, offender.ToString(), offender.Line).WithCommit(fs.commit).WithEntropy(offender.EntropyLevel)
leak.File = f.Name
leak.RepoURL = fs.opts.RepoURL
leak.Repo = fs.repoName
leak.LeakURL = leak.URL()
leak.Rule = rule.Description
leak.Tags = strings.Join(rule.Tags, ", ")
leak.Log(fs.opts)
scannerReport.Leaks = append(scannerReport.Leaks, leak)
}
}
}
if rule.CommitAllowed(fs.commit.Hash.String()) {
continue
}
if rule.HasFileOrPathLeakOnly(f.Name) {
leak := NewLeak("", "Filename or path offender: "+f.Name, defaultLineNumber).WithCommit(fs.commit)
leak.Repo = fs.repoName
leak.File = f.Name
leak.RepoURL = fs.opts.RepoURL
leak.LeakURL = leak.URL()
leak.Rule = rule.Description
leak.Tags = strings.Join(rule.Tags, ", ")
leak.Log(fs.opts)
scannerReport.Leaks = append(scannerReport.Leaks, leak)
continue
}
}
for i, line := range strings.Split(content, "\n") {
for _, rule := range fs.cfg.Rules {
if rule.AllowList.FileAllowed(filepath.Base(f.Name)) ||
rule.AllowList.PathAllowed(f.Name) ||
rule.AllowList.CommitAllowed(fs.commit.Hash.String()) {
continue
}
offender := rule.Inspect(line)
if offender.IsEmpty() {
continue
}
if fs.cfg.Allowlist.RegexAllowed(line) {
continue
}
if rule.File.String() != "" && !rule.HasFileLeak(filepath.Base(f.Name)) {
continue
}
if rule.Path.String() != "" && !rule.HasFilePathLeak(f.Name) {
continue
}
leak := NewLeak(line, offender.ToString(), defaultLineNumber).WithCommit(fs.commit).WithEntropy(offender.EntropyLevel)
leak.File = f.Name
leak.LineNumber = i + 1
leak.RepoURL = fs.opts.RepoURL
leak.Repo = fs.repoName
leak.LeakURL = leak.URL()
leak.Rule = rule.Description
leak.Tags = strings.Join(rule.Tags, ", ")
leak.Log(fs.opts)
scannerReport.Leaks = append(scannerReport.Leaks, leak)
}
}
return nil
})
scannerReport.Commits = 1
return scannerReport, err
}