From 4acd7a3c8d20b7116a32f9bbade6a3cf15f741e4 Mon Sep 17 00:00:00 2001 From: Zachary Rice Date: Wed, 24 Nov 2021 20:27:19 -0600 Subject: [PATCH] adding logic to ignore gitleaks config during scans (#710) --- cmd/detect.go | 5 +++++ cmd/protect.go | 5 +++++ config/config.go | 1 + config/gitleaks.toml | 2 +- detect/detect.go | 4 ++-- detect/detect_test.go | 8 ++++++++ 6 files changed, 22 insertions(+), 3 deletions(-) diff --git a/cmd/detect.go b/cmd/detect.go index 2807d369..d936dee1 100644 --- a/cmd/detect.go +++ b/cmd/detect.go @@ -2,6 +2,7 @@ package cmd import ( "os" + "path/filepath" "time" "github.com/rs/zerolog/log" @@ -40,12 +41,16 @@ func runDetect(cmd *cobra.Command, args []string) { log.Fatal().Err(err).Msg("Failed to load config") } + cfg.Path, _ = cmd.Flags().GetString("config") source, _ := cmd.Flags().GetString("source") logOpts, _ := cmd.Flags().GetString("log-opts") verbose, _ := cmd.Flags().GetBool("verbose") redact, _ := cmd.Flags().GetBool("redact") noGit, _ := cmd.Flags().GetBool("no-git") exitCode, _ := cmd.Flags().GetInt("exit-code") + if cfg.Path == "" { + cfg.Path = filepath.Join(source, ".gitleaks.toml") + } start := time.Now() if noGit { diff --git a/cmd/protect.go b/cmd/protect.go index e9646cbd..a8f51c80 100644 --- a/cmd/protect.go +++ b/cmd/protect.go @@ -2,6 +2,7 @@ package cmd import ( "os" + "path/filepath" "time" "github.com/rs/zerolog/log" @@ -35,11 +36,15 @@ func runProtect(cmd *cobra.Command, args []string) { log.Fatal().Err(err).Msg("Failed to load config") } + cfg.Path, _ = cmd.Flags().GetString("config") source, _ := cmd.Flags().GetString("source") verbose, _ := cmd.Flags().GetBool("verbose") redact, _ := cmd.Flags().GetBool("redact") exitCode, _ := cmd.Flags().GetInt("exit-code") staged, _ := cmd.Flags().GetBool("staged") + if cfg.Path == "" { + cfg.Path = filepath.Join(source, ".gitleaks.toml") + } start := time.Now() files, err := git.GitDiff(source, staged) diff --git a/config/config.go b/config/config.go index 4033cedc..72fae04f 100644 --- a/config/config.go +++ b/config/config.go @@ -38,6 +38,7 @@ type ViperConfig struct { // Config is a configuration struct that contains rules and an allowlist if present. type Config struct { + Path string Description string Rules []*Rule Allowlist Allowlist diff --git a/config/gitleaks.toml b/config/gitleaks.toml index f1f4b5d7..e291a49f 100644 --- a/config/gitleaks.toml +++ b/config/gitleaks.toml @@ -445,4 +445,4 @@ regex = '''(?i)(typeform[a-z0-9_ .\-,]{0,25})(=|>|:=|\|\|:|<=|=>|:).{0,5}(tfp_[a [allowlist] description = "global allow lists" regexes = ['''219-09-9999''', '''078-05-1120''', '''(9[0-9]{2}|666)-\d{2}-\d{4}'''] -files = ['''(.*?)(jpg|gif|doc|pdf|bin|svg|socket)$'''] +paths = ['''(.*?)(jpg|gif|doc|pdf|bin|svg|socket)$'''] diff --git a/detect/detect.go b/detect/detect.go index 5fcda741..e3322c3b 100644 --- a/detect/detect.go +++ b/detect/detect.go @@ -19,8 +19,8 @@ func DetectFindings(cfg config.Config, b []byte, filePath string, commit string) var findings []report.Finding linePairs := regexp.MustCompile("\n").FindAllIndex(b, -1) - // check if we should skip file based on the global allowlist - if cfg.Allowlist.PathAllowed(filePath) { + // check if we should skip file based on the global allowlist or if the file is the same as the gitleaks config + if cfg.Allowlist.PathAllowed(filePath) || filePath == cfg.Path { return findings } diff --git a/detect/detect_test.go b/detect/detect_test.go index 4a965a09..8b7abd02 100644 --- a/detect/detect_test.go +++ b/detect/detect_test.go @@ -2,6 +2,7 @@ package detect import ( "fmt" + "path/filepath" "testing" "github.com/spf13/viper" @@ -111,6 +112,12 @@ func TestDetectFindings(t *testing.T) { expectedFindings: []report.Finding{}, wantError: fmt.Errorf("Discord API key invalid regex entropy group 5, max regex entropy group 3"), }, + { + cfgName: "simple", + bytes: []byte(`awsToken := \"AKIALALEMEL33243OLIA\"`), + filePath: filepath.Join(configPath, "simple.toml"), + expectedFindings: []report.Finding{}, + }, } for _, tt := range tests { @@ -126,6 +133,7 @@ func TestDetectFindings(t *testing.T) { var vc config.ViperConfig viper.Unmarshal(&vc) cfg, err := vc.Translate() + cfg.Path = filepath.Join(configPath, tt.cfgName+".toml") if tt.wantError != nil { if err == nil { t.Errorf("expected error")