mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-12-01 05:24:22 +01:00
audit all branches by default, memoize commits, smarter regexes
This commit is contained in:
1 parent
35a59746df
commit
7e3c740592
1 file changed
+39
-80
@@ -73,7 +73,6 @@ type Options struct {
|
|||||||
GitLabOrg string `long:"gitlab-org" description:"Organization url to audit"`
|
GitLabOrg string `long:"gitlab-org" description:"Organization url to audit"`
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Branch string `short:"b" long:"branch" description:"branch name to audit (defaults to HEAD)"`
|
|
||||||
Commit string `short:"c" long:"commit" description:"sha of commit to stop at"`
|
Commit string `short:"c" long:"commit" description:"sha of commit to stop at"`
|
||||||
Depth int `long:"depth" description:"maximum commit depth"`
|
Depth int `long:"depth" description:"maximum commit depth"`
|
||||||
|
|
||||||
@@ -84,7 +83,6 @@ type Options struct {
|
|||||||
// Process options
|
// Process options
|
||||||
Threads int `long:"threads" description:"Maximum number of threads gitleaks spawns"`
|
Threads int `long:"threads" description:"Maximum number of threads gitleaks spawns"`
|
||||||
Disk bool `long:"disk" description:"Clones repo(s) to disk"`
|
Disk bool `long:"disk" description:"Clones repo(s) to disk"`
|
||||||
AuditAllRefs bool `long:"all-refs" description:"run audit on all refs"`
|
|
||||||
SingleSearch string `long:"single-search" description:"single regular expression to search for"`
|
SingleSearch string `long:"single-search" description:"single regular expression to search for"`
|
||||||
ConfigPath string `long:"config" description:"path to gitleaks config"`
|
ConfigPath string `long:"config" description:"path to gitleaks config"`
|
||||||
SSHKey string `long:"ssh-key" description:"path to ssh key"`
|
SSHKey string `long:"ssh-key" description:"path to ssh key"`
|
||||||
@@ -108,11 +106,10 @@ type Config struct {
|
|||||||
Regex string
|
Regex string
|
||||||
}
|
}
|
||||||
Whitelist struct {
|
Whitelist struct {
|
||||||
Files []string
|
Files []string
|
||||||
Regexes []string
|
Regexes []string
|
||||||
Commits []string
|
Commits []string
|
||||||
Branches []string
|
Repos []string
|
||||||
Repos []string
|
|
||||||
}
|
}
|
||||||
Misc struct {
|
Misc struct {
|
||||||
Entropy []string
|
Entropy []string
|
||||||
@@ -123,7 +120,6 @@ type gitDiff struct {
|
|||||||
content string
|
content string
|
||||||
commit *object.Commit
|
commit *object.Commit
|
||||||
filePath string
|
filePath string
|
||||||
branchName string
|
|
||||||
repoName string
|
repoName string
|
||||||
githubCommit *github.RepositoryCommit
|
githubCommit *github.RepositoryCommit
|
||||||
sha string
|
sha string
|
||||||
@@ -166,39 +162,22 @@ description = "PGP"
|
|||||||
regex = '''-----BEGIN PGP PRIVATE KEY BLOCK-----'''
|
regex = '''-----BEGIN PGP PRIVATE KEY BLOCK-----'''
|
||||||
[[regexes]]
|
[[regexes]]
|
||||||
description = "Facebook"
|
description = "Facebook"
|
||||||
regex = '''(?i)facebook.*['\"][0-9a-f]{32}['\"]'''
|
regex = '''(?i)facebook(.{0,4})?['\"][0-9a-f]{32}['\"]'''
|
||||||
[[regexes]]
|
[[regexes]]
|
||||||
description = "Twitter"
|
description = "Twitter"
|
||||||
regex = '''(?i)twitter.*['\"][0-9a-zA-Z]{35,44}['\"]'''
|
regex = '''(?i)twitter(.{0,4})?['\"][0-9a-zA-Z]{35,44}['\"]'''
|
||||||
[[regexes]]
|
[[regexes]]
|
||||||
description = "Github"
|
description = "Github"
|
||||||
regex = '''(?i)github.*['\"][0-9a-zA-Z]{35,40}['\"]'''
|
regex = '''(?i)github(.{0,4})?['\"][0-9a-zA-Z]{35,40}['\"]'''
|
||||||
[[regexes]]
|
[[regexes]]
|
||||||
description = "Slack"
|
description = "Slack"
|
||||||
regex = '''xox[baprs]-.*'''
|
regex = '''xox[baprs]-([0-9a-zA-Z]{10,48})?'''
|
||||||
[[regexes]]
|
|
||||||
description = "Telegram"
|
|
||||||
regex = '''\d{5,}:A[a-zA-Z0-9_\-]{34,34}'''
|
|
||||||
|
|
||||||
[whitelist]
|
[whitelist]
|
||||||
regexes = [
|
|
||||||
'''(?i)github.*ref.*['\"][0-9a-fA-F]{35,40}['\"]''',
|
|
||||||
'''(?i)shasum.*['\"][0-9a-fA-F]{40}['\"]''',
|
|
||||||
'''(?i)githead.*['\"][0-9a-fA-F]{40}['\"]''',
|
|
||||||
'''(?i)email_hash.*['\"][0-9a-fA-F]{40}['\"]''',
|
|
||||||
'''(?i)email_hash.*['\"][0-9a-fA-F]{32}['\"]''',
|
|
||||||
'''(?i)rev.*:.*['\"][0-9a-fA-F]{40}['\"]''',
|
|
||||||
]
|
|
||||||
files = [
|
|
||||||
"(.*?)(jpg|gif|doc|pdf|bin)$"
|
|
||||||
]
|
|
||||||
#commits = [
|
#commits = [
|
||||||
# "BADHA5H1",
|
# "BADHA5H1",
|
||||||
# "BADHA5H2",
|
# "BADHA5H2",
|
||||||
#]
|
#]
|
||||||
#branches = [
|
|
||||||
# "dev/goodrepo"
|
|
||||||
#]
|
|
||||||
#repos = [
|
#repos = [
|
||||||
# "mygoodrepo"
|
# "mygoodrepo"
|
||||||
#]
|
#]
|
||||||
@@ -216,7 +195,6 @@ var (
|
|||||||
whiteListRegexes []*regexp.Regexp
|
whiteListRegexes []*regexp.Regexp
|
||||||
whiteListFiles []*regexp.Regexp
|
whiteListFiles []*regexp.Regexp
|
||||||
whiteListCommits map[string]bool
|
whiteListCommits map[string]bool
|
||||||
whiteListBranches []string
|
|
||||||
whiteListRepos []*regexp.Regexp
|
whiteListRepos []*regexp.Regexp
|
||||||
entropyRanges []entropyRange
|
entropyRanges []entropyRange
|
||||||
fileDiffRegex *regexp.Regexp
|
fileDiffRegex *regexp.Regexp
|
||||||
@@ -224,6 +202,8 @@ var (
|
|||||||
dir string
|
dir string
|
||||||
threads int
|
threads int
|
||||||
totalCommits int64
|
totalCommits int64
|
||||||
|
commitMap = make(map[string]bool)
|
||||||
|
cMutex = &sync.Mutex{}
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -429,51 +409,24 @@ func auditGitRepo(repo *RepoDescriptor) ([]Leak, error) {
|
|||||||
return leaks, fmt.Errorf("skipping %s, whitelisted", repo.name)
|
return leaks, fmt.Errorf("skipping %s, whitelisted", repo.name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ref, err := repo.repository.Head()
|
|
||||||
|
// clear commit cache
|
||||||
|
commitMap = make(map[string]bool)
|
||||||
|
|
||||||
|
refs, err := repo.repository.Storer.IterReferences()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return leaks, err
|
return leaks, err
|
||||||
}
|
}
|
||||||
|
err = refs.ForEach(func(ref *plumbing.Reference) error {
|
||||||
if opts.AuditAllRefs {
|
if ref.Name().IsTag() {
|
||||||
skipBranch := false
|
|
||||||
refs, err := repo.repository.Storer.IterReferences()
|
|
||||||
if err != nil {
|
|
||||||
return leaks, err
|
|
||||||
}
|
|
||||||
err = refs.ForEach(func(ref *plumbing.Reference) error {
|
|
||||||
for _, b := range whiteListBranches {
|
|
||||||
if strings.HasSuffix(string(ref.Name()), b) {
|
|
||||||
skipBranch = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if skipBranch {
|
|
||||||
skipBranch = false
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
branchLeaks := auditGitReference(repo, ref)
|
|
||||||
for _, leak := range branchLeaks {
|
|
||||||
leaks = append(leaks, leak)
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
})
|
|
||||||
} else {
|
|
||||||
if opts.Branch != "" {
|
|
||||||
foundBranch := false
|
|
||||||
refs, _ := repo.repository.Storer.IterReferences()
|
|
||||||
branch := strings.Split(opts.Branch, "/")[len(strings.Split(opts.Branch, "/"))-1]
|
|
||||||
err = refs.ForEach(func(refBranch *plumbing.Reference) error {
|
|
||||||
if strings.Split(refBranch.Name().String(), "/")[len(strings.Split(refBranch.Name().String(), "/"))-1] == branch {
|
|
||||||
foundBranch = true
|
|
||||||
ref = refBranch
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
if foundBranch == false {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
leaks = auditGitReference(repo, ref)
|
branchLeaks := auditGitReference(repo, ref)
|
||||||
}
|
for _, leak := range branchLeaks {
|
||||||
|
leaks = append(leaks, leak)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
return leaks, err
|
return leaks, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -503,6 +456,12 @@ func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
err = cIter.ForEach(func(c *object.Commit) error {
|
err = cIter.ForEach(func(c *object.Commit) error {
|
||||||
|
if commitMap[c.Hash.String()] {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
cMutex.Lock()
|
||||||
|
commitMap[c.Hash.String()] = true
|
||||||
|
cMutex.Unlock()
|
||||||
if c == nil || c.Hash.String() == opts.Commit || (opts.Depth != 0 && commitCount == opts.Depth) {
|
if c == nil || c.Hash.String() == opts.Commit || (opts.Depth != 0 && commitCount == opts.Depth) {
|
||||||
cIter.Close()
|
cIter.Close()
|
||||||
return errors.New("ErrStop")
|
return errors.New("ErrStop")
|
||||||
@@ -535,6 +494,9 @@ func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, f := range patch.FilePatches() {
|
for _, f := range patch.FilePatches() {
|
||||||
|
if f.IsBinary() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
skipFile = false
|
skipFile = false
|
||||||
from, to := f.Files()
|
from, to := f.Files()
|
||||||
filePath = "???"
|
filePath = "???"
|
||||||
@@ -556,14 +518,13 @@ func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak {
|
|||||||
for _, chunk := range chunks {
|
for _, chunk := range chunks {
|
||||||
if chunk.Type() == 1 || chunk.Type() == 2 {
|
if chunk.Type() == 1 || chunk.Type() == 2 {
|
||||||
diff := gitDiff{
|
diff := gitDiff{
|
||||||
branchName: string(ref.Name()),
|
repoName: repoName,
|
||||||
repoName: repoName,
|
filePath: filePath,
|
||||||
filePath: filePath,
|
content: chunk.Content(),
|
||||||
content: chunk.Content(),
|
sha: c.Hash.String(),
|
||||||
sha: c.Hash.String(),
|
author: c.Author.String(),
|
||||||
author: c.Author.String(),
|
message: c.Message,
|
||||||
message: c.Message,
|
date: c.Author.When,
|
||||||
date: c.Author.When,
|
|
||||||
}
|
}
|
||||||
chunkLeaks := inspect(diff)
|
chunkLeaks := inspect(diff)
|
||||||
for _, leak := range chunkLeaks {
|
for _, leak := range chunkLeaks {
|
||||||
@@ -651,7 +612,6 @@ func addLeak(leaks []Leak, line string, offender string, leakType string, diff g
|
|||||||
Type: leakType,
|
Type: leakType,
|
||||||
Author: diff.author,
|
Author: diff.author,
|
||||||
File: diff.filePath,
|
File: diff.filePath,
|
||||||
Branch: diff.branchName,
|
|
||||||
Repo: diff.repoName,
|
Repo: diff.repoName,
|
||||||
Message: diff.message,
|
Message: diff.message,
|
||||||
Date: diff.date,
|
Date: diff.date,
|
||||||
@@ -838,7 +798,6 @@ func loadToml() error {
|
|||||||
regexes[regex.Description] = regexp.MustCompile(regex.Regex)
|
regexes[regex.Description] = regexp.MustCompile(regex.Regex)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
whiteListBranches = config.Whitelist.Branches
|
|
||||||
whiteListCommits = make(map[string]bool)
|
whiteListCommits = make(map[string]bool)
|
||||||
for _, commit := range config.Whitelist.Commits {
|
for _, commit := range config.Whitelist.Commits {
|
||||||
whiteListCommits[commit] = true
|
whiteListCommits[commit] = true
|
||||||
|
|||||||
Reference in new issue
Block a user