Feat/ignore finding (#938)

This commit is contained in:
Zachary Rice authored and GitHub committed 2022-08-07 12:53:58 -05:00
1 parent e890a8e809
commit 3bace4c735
11 files changed
+713 -8

No files matched your search

+34 -5
View File
@@ -1,6 +1,7 @@
package detect
import (
"bufio"
"context"
"fmt"
"os"
@@ -64,6 +65,9 @@ type Detector struct {
// prefilter is a ahocorasick struct used for doing efficient string
// matching given a set of words (keywords from the rules in the config)
prefilter ahocorasick.AhoCorasick
// gitleaksIgnore
gitleaksIgnore map[string]bool
}
// Fragment contains the data to be scanned
@@ -96,11 +100,12 @@ func NewDetector(cfg config.Config) *Detector {
})
return &Detector{
commitMap: make(map[string]bool),
findingMutex: &sync.Mutex{},
findings: make([]report.Finding, 0),
Config: cfg,
prefilter: builder.Build(cfg.Keywords),
commitMap: make(map[string]bool),
gitleaksIgnore: make(map[string]bool),
findingMutex: &sync.Mutex{},
findings: make([]report.Finding, 0),
Config: cfg,
prefilter: builder.Build(cfg.Keywords),
}
}
@@ -123,6 +128,23 @@ func NewDetectorDefaultConfig() (*Detector, error) {
return NewDetector(cfg), nil
}
func (d *Detector) AddGitleaksIgnore(gitleaksIgnorePath string) error {
log.Debug().Msg("found .gitleaksignore file")
file, err := os.Open(gitleaksIgnorePath)
if err != nil {
return err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
d.gitleaksIgnore[scanner.Text()] = true
}
return nil
}
// DetectBytes scans the given bytes and returns a list of findings
func (d *Detector) DetectBytes(content []byte) []report.Finding {
return d.DetectString(string(content))
@@ -428,6 +450,13 @@ func (d *Detector) Detect(fragment Fragment) []report.Finding {
// addFinding synchronously adds a finding to the findings slice
func (d *Detector) addFinding(finding report.Finding) {
// check if we should ignore this finding
if _, ok := d.gitleaksIgnore[finding.Fingerprint]; ok {
log.Debug().Msgf("ignoring finding with Fingerprint %s",
finding.Fingerprint)
return
}
d.findingMutex.Lock()
d.findings = append(d.findings, finding)
if d.Verbose {
+3
View File
@@ -367,6 +367,7 @@ func TestFromGit(t *testing.T) {
RuleID: "aws-access-key",
Tags: []string{"key", "AWS"},
Entropy: 3.0841837,
Fingerprint: "d0ce473c3c9f6934e8d274604cf9ceae63700f03",
},
{
Description: "AWS Access Key",
@@ -385,6 +386,7 @@ func TestFromGit(t *testing.T) {
RuleID: "aws-access-key",
Tags: []string{"key", "AWS"},
Entropy: 3.0841837,
Fingerprint: "df8d271fa20b928dfb12353cc16ddf9934225cb7",
},
},
},
@@ -410,6 +412,7 @@ func TestFromGit(t *testing.T) {
RuleID: "aws-access-key",
Tags: []string{"key", "AWS"},
Entropy: 3.0841837,
Fingerprint: "df8d271fa20b928dfb12353cc16ddf9934225cb7",
},
},
},
+6
View File
@@ -1,6 +1,7 @@
package detect
import (
"crypto/sha1"
"encoding/json"
"fmt"
"math"
@@ -21,6 +22,11 @@ func augmentGitFinding(finding report.Finding, textFragment *gitdiff.TextFragmen
finding.EndLine += int(textFragment.NewPosition)
}
// generate finding hash
h := sha1.New()
h.Write([]byte(fmt.Sprintf("%s:%s:%s:%d", finding.Commit, finding.File, finding.RuleID, finding.StartLine)))
finding.Fingerprint = fmt.Sprintf("%x", h.Sum(nil))
if f.PatchHeader != nil {
finding.Commit = f.PatchHeader.SHA
finding.Message = f.PatchHeader.Message()