Global allowlist regex (#422)

* work in progress

* Update test configs

* adding test

* golint and change to allowlist

* fix race condition, this mistakenly got committed

* Updating tests and examples

* bump to v6 as this is a breaking change to the config

* More consistent variables
This commit is contained in:
Zachary Rice authored and GitHub committed 2020-08-12 17:18:25 -04:00
1 parent 6129b1a15d
commit fae366a404
69 files changed
+449 -204

No files matched your search

+5 -5
View File
@@ -11,8 +11,8 @@ import (
"runtime"
"time"
"github.com/zricethezav/gitleaks/v5/config"
"github.com/zricethezav/gitleaks/v5/manager"
"github.com/zricethezav/gitleaks/v6/config"
"github.com/zricethezav/gitleaks/v6/manager"
"github.com/BurntSushi/toml"
"github.com/go-git/go-billy/v5"
@@ -77,11 +77,11 @@ func Run(m *manager.Manager) error {
func runHelper(r *Repo) error {
// Ignore allowlisted repos
for _, wlRepo := range r.Manager.Config.Allowlist.Repos {
if RegexMatched(r.Manager.Opts.RepoPath, wlRepo) {
for _, allowListedRepo := range r.Manager.Config.Allowlist.Repos {
if RegexMatched(r.Manager.Opts.RepoPath, allowListedRepo) {
return nil
}
if RegexMatched(r.Manager.Opts.Repo, wlRepo) {
if RegexMatched(r.Manager.Opts.Repo, allowListedRepo) {
return nil
}
}
+17 -39
View File
@@ -11,8 +11,8 @@ import (
"strings"
"time"
"github.com/zricethezav/gitleaks/v5/config"
"github.com/zricethezav/gitleaks/v5/manager"
"github.com/zricethezav/gitleaks/v6/config"
"github.com/zricethezav/gitleaks/v6/manager"
fdiff "github.com/go-git/go-git/v5/plumbing/format/diff"
"github.com/go-git/go-git/v5/plumbing/object"
@@ -58,17 +58,17 @@ func (repo *Repo) CheckRules(bundle *Bundle) {
start := time.Now()
// For each rule we want to check filename allowlists
if isFileNameWhiteListed(filename, rule.Allowlist) || isFilePathWhiteListed(path, rule.Allowlist) {
if isAllowListed(filename, rule.Allowlist.Files) || isAllowListed(path, rule.Allowlist.Paths) {
continue
}
// If it has fileNameRegex and it doesnt match we continue to next rule
if ruleContainFileNameRegex(rule) && !RegexMatched(filename, rule.FileNameRegex) {
if ruleContainFileRegex(rule) && !RegexMatched(filename, rule.File) {
continue
}
// If it has filePathRegex and it doesnt match we continue to next rule
if ruleContainFilePathRegex(rule) && !RegexMatched(path, rule.FilePathRegex) {
if ruleContainPathRegex(rule) && !RegexMatched(path, rule.Path) {
continue
}
@@ -112,7 +112,7 @@ func (repo *Repo) CheckRules(bundle *Bundle) {
offender := bundle.Content[loc[0]:loc[1]]
groups := rule.Regex.FindStringSubmatch(offender)
if isOffenderWhiteListed(offender, rule.Allowlist) {
if isAllowListed(line, append(rule.Allowlist.Regexes, repo.config.Allowlist.Regexes...)) {
continue
}
@@ -146,7 +146,6 @@ func (repo *Repo) CheckRules(bundle *Bundle) {
}
}
// TODO should return filenameRegex if only file rule
repo.Manager.RecordTime(manager.RegexTime{
Time: howLong(start),
Regex: rule.Regex.String(),
@@ -339,28 +338,28 @@ func ruleContainRegex(rule config.Rule) bool {
}
// Checks if the given rule has a file name regex
func ruleContainFileNameRegex(rule config.Rule) bool {
if rule.FileNameRegex == nil {
func ruleContainFileRegex(rule config.Rule) bool {
if rule.File == nil {
return false
}
if rule.FileNameRegex.String() == "" {
if rule.File.String() == "" {
return false
}
return true
}
// Checks if the given rule has a file path regex
func ruleContainFilePathRegex(rule config.Rule) bool {
if rule.FilePathRegex == nil {
func ruleContainPathRegex(rule config.Rule) bool {
if rule.Path == nil {
return false
}
if rule.FilePathRegex.String() == "" {
if rule.Path.String() == "" {
return false
}
return true
}
func isCommitWhiteListed(commitHash string, allowlistedCommits []string) bool {
func isCommitAllowListed(commitHash string, allowlistedCommits []string) bool {
for _, hash := range allowlistedCommits {
if commitHash == hash {
return true
@@ -369,35 +368,14 @@ func isCommitWhiteListed(commitHash string, allowlistedCommits []string) bool {
return false
}
func isOffenderWhiteListed(offender string, allowlist []config.Allowlist) bool {
if len(allowlist) != 0 {
for _, wl := range allowlist {
if wl.Regex.FindString(offender) != "" {
func isAllowListed(target string, allowList []*regexp.Regexp) bool {
if len(allowList) != 0 {
for _, re := range allowList {
if re.FindString(target) != "" {
return true
}
}
}
return false
}
func isFileNameWhiteListed(filename string, allowlist []config.Allowlist) bool {
if len(allowlist) != 0 {
for _, wl := range allowlist {
if RegexMatched(filename, wl.File) {
return true
}
}
}
return false
}
func isFilePathWhiteListed(filepath string, allowlist []config.Allowlist) bool {
if len(allowlist) != 0 {
for _, wl := range allowlist {
if RegexMatched(filepath, wl.Path) {
return true
}
}
}
return false
}
+4 -3
View File
@@ -7,7 +7,7 @@ import (
"sync"
"time"
"github.com/zricethezav/gitleaks/v5/manager"
"github.com/zricethezav/gitleaks/v6/manager"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
@@ -96,7 +96,7 @@ func (repo *Repo) Scan() error {
}
// Check if Commit is allowlisted
if isCommitWhiteListed(c.Hash.String(), repo.config.Allowlist.Commits) {
if isCommitAllowListed(c.Hash.String(), repo.config.Allowlist.Commits) {
return nil
}
@@ -140,9 +140,10 @@ func (repo *Repo) Scan() error {
start := time.Now()
patch, err := parent.Patch(c)
if err != nil {
return fmt.Errorf("could not generate Patch")
log.Errorf("could not generate Patch")
}
repo.Manager.RecordTime(manager.PatchTime(howLong(start)))
wg.Add(1)
semaphore <- true
go func(c *object.Commit, patch *object.Patch) {
+13 -3
View File
@@ -10,9 +10,9 @@ import (
"sort"
"testing"
"github.com/zricethezav/gitleaks/v5/config"
"github.com/zricethezav/gitleaks/v5/manager"
"github.com/zricethezav/gitleaks/v5/options"
"github.com/zricethezav/gitleaks/v6/config"
"github.com/zricethezav/gitleaks/v6/manager"
"github.com/zricethezav/gitleaks/v6/options"
"github.com/sergi/go-diff/diffmatchpatch"
)
@@ -385,6 +385,16 @@ func TestScan(t *testing.T) {
},
wantPath: "../test_data/test_local_repo_eight.json",
},
{
description: "test local repo nine",
opts: options.Options{
RepoPath: "../test_data/test_repos/test_repo_9",
Report: "../test_data/test_local_repo_nine_aws_leak.json.got",
Config: "../test_data/test_configs/large_with_global_allowlist_regex.toml",
ReportFormat: "json",
},
wantPath: "../test_data/test_local_repo_nine_aws_leak.json",
},
}
for _, test := range tests {