Ignore all comits when .gitleaksignore fingerprint lacks SHA (#1156)

* fix: .gitleaksignore without sha in fingerprint

* chore: add prior commit to .gitleaksignore
This commit is contained in:
Richard Gomez authored and GitHub committed 2023-06-13 13:42:51 -05:00
1 parent 0dbdde837f
commit 91ff8f9b70
24 files changed
+148 -11

No files matched your search

+15 -6
View File
@@ -147,7 +147,7 @@ func NewDetectorDefaultConfig() (*Detector, error) {
}
func (d *Detector) AddGitleaksIgnore(gitleaksIgnorePath string) error {
log.Debug().Msg("found .gitleaksignore file")
log.Debug().Msgf("found .gitleaksignore file: %s", gitleaksIgnorePath)
file, err := os.Open(gitleaksIgnorePath)
if err != nil {
@@ -598,16 +598,25 @@ func (d *Detector) Detect(fragment Fragment) []report.Finding {
// addFinding synchronously adds a finding to the findings slice
func (d *Detector) addFinding(finding report.Finding) {
if finding.Commit == "" {
finding.Fingerprint = fmt.Sprintf("%s:%s:%d", finding.File, finding.RuleID, finding.StartLine)
} else {
globalFingerprint := fmt.Sprintf("%s:%s:%d", finding.File, finding.RuleID, finding.StartLine)
if finding.Commit != "" {
finding.Fingerprint = fmt.Sprintf("%s:%s:%s:%d", finding.Commit, finding.File, finding.RuleID, finding.StartLine)
} else {
finding.Fingerprint = globalFingerprint
}
// check if we should ignore this finding
if _, ok := d.gitleaksIgnore[finding.Fingerprint]; ok {
log.Debug().Msgf("ignoring finding with Fingerprint %s",
if _, ok := d.gitleaksIgnore[globalFingerprint]; ok {
log.Debug().Msgf("ignoring finding with global Fingerprint %s",
finding.Fingerprint)
return
} else if finding.Commit != "" {
// Awkward nested if because I'm not sure how to chain these two conditions.
if _, ok := d.gitleaksIgnore[finding.Fingerprint]; ok {
log.Debug().Msgf("ignoring finding with Fingerprint %s",
finding.Fingerprint)
return
}
}
if d.baseline != nil && !IsNew(finding, d.baseline) {
+40 -1
View File
@@ -6,6 +6,7 @@ import (
"path/filepath"
"testing"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
@@ -474,6 +475,22 @@ func TestFromGit(t *testing.T) {
t.Error(err)
}
detector := NewDetector(cfg)
var ignorePath string
info, err := os.Stat(tt.source)
if err != nil {
log.Fatal().Err(err).Msg("could not call AddGitleaksIgnore")
}
if info.IsDir() {
ignorePath = filepath.Join(tt.source, ".gitleaksignore")
} else {
ignorePath = filepath.Join(filepath.Dir(tt.source), ".gitleaksignore")
}
if err = detector.AddGitleaksIgnore(ignorePath); err != nil {
log.Fatal().Err(err).Msg("could not call AddGitleaksIgnore")
}
findings, err := detector.DetectGit(tt.source, tt.logOpts, DetectType)
if err != nil {
t.Error(err)
@@ -554,7 +571,9 @@ func TestFromGitStaged(t *testing.T) {
t.Error(err)
}
detector := NewDetector(cfg)
detector.AddGitleaksIgnore(filepath.Join(tt.source, ".gitleaksignore"))
if err = detector.AddGitleaksIgnore(filepath.Join(tt.source, ".gitleaksignore")); err != nil {
log.Fatal().Err(err).Msg("could not call AddGitleaksIgnore")
}
findings, err := detector.DetectGit(tt.source, tt.logOpts, ProtectStagedType)
if err != nil {
t.Error(err)
@@ -617,6 +636,11 @@ func TestFromFiles(t *testing.T) {
},
},
},
{
source: filepath.Join(repoBasePath, "nogit", "api.go"),
cfgName: "simple",
expectedFindings: []report.Finding{},
},
}
for _, tt := range tests {
@@ -635,6 +659,21 @@ func TestFromFiles(t *testing.T) {
}
cfg, _ := vc.Translate()
detector := NewDetector(cfg)
var ignorePath string
info, err := os.Stat(tt.source)
if err != nil {
log.Fatal().Err(err).Msg("could not call AddGitleaksIgnore")
}
if info.IsDir() {
ignorePath = filepath.Join(tt.source, ".gitleaksignore")
} else {
ignorePath = filepath.Join(filepath.Dir(tt.source), ".gitleaksignore")
}
if err = detector.AddGitleaksIgnore(ignorePath); err != nil {
log.Fatal().Err(err).Msg("could not call AddGitleaksIgnore")
}
detector.FollowSymlinks = true
findings, err := detector.DetectFiles(tt.source)
if err != nil {