Compare commits

...
4 Commits
Author SHA1 Message Date
Zachary Rice 80d29764a0 add global regex check (#717) 2021-11-25 11:17:03 -06:00
Zachary Rice f498b1d308 Commit debug log (#716)
* adding debug log for number of commits scanned

* change debug text

* readd commitsha
2021-11-25 10:46:47 -06:00
Joost Voskuil 8eabfd614e Update sarif.go (#713)
* Update sarif.go

Provide correct version information for Sarif Reports

* Update test

Set version to correct Sarif version
2021-11-25 10:46:31 -06:00
Zachary Rice 4acd7a3c8d adding logic to ignore gitleaks config during scans (#710) 2021-11-24 20:27:19 -06:00
10 changed files with 46 additions and 7 deletions

No files matched your search

+5
View File
@@ -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 {
+5
View File
@@ -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)
+1
View File
@@ -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
+1 -1
View File
@@ -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)$''']
+3 -3
View File
@@ -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
}
@@ -72,7 +72,7 @@ func DetectFindings(cfg config.Config, b []byte, filePath string, commit string)
Tags: r.Tags,
}
if r.Allowlist.RegexAllowed(f.Secret) {
if r.Allowlist.RegexAllowed(f.Secret) || cfg.Allowlist.RegexAllowed(f.Secret) {
continue
}
+14
View File
@@ -2,6 +2,7 @@ package detect
import (
"fmt"
"path/filepath"
"testing"
"github.com/spf13/viper"
@@ -111,6 +112,18 @@ 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{},
},
{
cfgName: "allow_global_aws_re",
bytes: []byte(`awsToken := \"AKIALALEMEL33243OLIA\"`),
filePath: "tmp.go",
expectedFindings: []report.Finding{},
},
}
for _, tt := range tests {
@@ -126,6 +139,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")
+7 -1
View File
@@ -5,6 +5,7 @@ import (
"sync"
"github.com/gitleaks/go-gitdiff/gitdiff"
"github.com/rs/zerolog/log"
"github.com/zricethezav/gitleaks/v8/config"
"github.com/zricethezav/gitleaks/v8/report"
godocutil "golang.org/x/tools/godoc/util"
@@ -17,7 +18,12 @@ func FromGit(files <-chan *gitdiff.File, cfg config.Config, outputOptions Option
var findings []*report.Finding
mu := sync.Mutex{}
wg := sync.WaitGroup{}
commitMap := make(map[string]bool)
for f := range files {
// keep track of commits for logging
if f.PatchHeader != nil {
commitMap[f.PatchHeader.SHA] = true
}
wg.Add(1)
go func(f *gitdiff.File) {
@@ -35,7 +41,6 @@ func FromGit(files <-chan *gitdiff.File, cfg config.Config, outputOptions Option
// Check if commit is allowed
if f.PatchHeader != nil {
commitSHA = f.PatchHeader.SHA
if cfg.Allowlist.CommitAllowed(f.PatchHeader.SHA) {
return
}
@@ -85,5 +90,6 @@ func FromGit(files <-chan *gitdiff.File, cfg config.Config, outputOptions Option
}
wg.Wait()
log.Debug().Msgf("%d commits scanned. Note: this number might be smaller than expected due to commits with no additions", len(commitMap))
return findings
}
+1 -1
View File
@@ -11,7 +11,7 @@ import (
func writeSarif(cfg config.Config, findings []*Finding, w io.WriteCloser) error {
sarif := Sarif{
Schema: "https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0-rtm.5.json",
Version: version,
Version: "2.1.0",
Runs: getRuns(cfg, findings),
}
+8
View File
@@ -0,0 +1,8 @@
[[rules]]
description = "AWS Access Key"
id = "aws-access-key"
regex = '''(A3T[A-Z0-9]|AKIA|AGPA|AIDA|AROA|AIPA|ANPA|ANVA|ASIA)[A-Z0-9]{16}'''
tags = ["key", "AWS"]
[allowlist]
regexes = ['''AKIALALEMEL33243OLIA''']
+1 -1
View File
@@ -1,6 +1,6 @@
{
"$schema": "https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0-rtm.5.json",
"version": "v8.0.0",
"version": "2.1.0",
"runs": [
{
"tool": {