forked from gitleaks/gitleaks
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add baseline * Update doc, add error, move baseline to detect namespace, ignore findings instead of reactively filter them out * Update detect/detect.go Co-authored-by: Zachary Rice <zricezrice@gmail.com> * Update IsNew function (no check on tags - omit finger print check) * Update README.md Co-authored-by: Zachary Rice <zricezrice@gmail.com> * Update examples in readme to make it ensure it's clear that a baseline is indeed a gitleaks report * Fix test - updated tags doesn't make a finding new * Add missing err assignment * Allow scanner to continue without baseline if file is malformed * Fix typo in comment * Fix control flow err. (Real life testing) * Fix wording * Auto-ignore baseline path
- Loading branch information
Showing
9 changed files
with
295 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package detect | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
|
||
"github.com/zricethezav/gitleaks/v8/report" | ||
) | ||
|
||
func IsNew(finding report.Finding, baseline []report.Finding) bool { | ||
// Explicitly testing each property as it gives significantly better performance in comparison to cmp.Equal(). Drawback is that | ||
// the code requires maintanance if/when the Finding struct changes | ||
for _, b := range baseline { | ||
|
||
if finding.Author == b.Author && | ||
finding.Commit == b.Commit && | ||
finding.Date == b.Date && | ||
finding.Description == b.Description && | ||
finding.Email == b.Email && | ||
finding.EndColumn == b.EndColumn && | ||
finding.EndLine == b.EndLine && | ||
finding.Entropy == b.Entropy && | ||
finding.File == b.File && | ||
// Omit checking finding.Fingerprint - if the format of the fingerprint changes, the users will see unexpected behaviour | ||
finding.Match == b.Match && | ||
finding.Message == b.Message && | ||
finding.RuleID == b.RuleID && | ||
finding.Secret == b.Secret && | ||
finding.StartColumn == b.StartColumn && | ||
finding.StartLine == b.StartLine { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
func LoadBaseline(baselinePath string) ([]report.Finding, error) { | ||
var previousFindings []report.Finding | ||
jsonFile, err := os.Open(baselinePath) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not open %s", baselinePath) | ||
} | ||
|
||
bytes, err := ioutil.ReadAll(jsonFile) | ||
jsonFile.Close() | ||
if err != nil { | ||
return nil, fmt.Errorf("could not read data from the file %s", baselinePath) | ||
} | ||
|
||
err = json.Unmarshal(bytes, &previousFindings) | ||
if err != nil { | ||
return nil, fmt.Errorf("the format of the file %s is not supported", baselinePath) | ||
} | ||
|
||
return previousFindings, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package detect | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/zricethezav/gitleaks/v8/report" | ||
) | ||
|
||
func TestIsNew(t *testing.T) { | ||
tests := []struct { | ||
findings report.Finding | ||
baseline []report.Finding | ||
expect bool | ||
}{ | ||
{ | ||
findings: report.Finding{ | ||
Author: "a", | ||
Commit: "0000", | ||
}, | ||
baseline: []report.Finding{ | ||
{ | ||
Author: "a", | ||
Commit: "0000", | ||
}, | ||
}, | ||
expect: false, | ||
}, | ||
{ | ||
findings: report.Finding{ | ||
Author: "a", | ||
Commit: "0000", | ||
}, | ||
baseline: []report.Finding{ | ||
{ | ||
Author: "a", | ||
Commit: "0002", | ||
}, | ||
}, | ||
expect: true, | ||
}, | ||
{ | ||
findings: report.Finding{ | ||
Author: "a", | ||
Commit: "0000", | ||
Tags: []string{"a", "b"}, | ||
}, | ||
baseline: []report.Finding{ | ||
{ | ||
Author: "a", | ||
Commit: "0000", | ||
Tags: []string{"a", "c"}, | ||
}, | ||
}, | ||
expect: false, // Updated tags doesn't make it a new finding | ||
}, | ||
} | ||
for _, test := range tests { | ||
assert.Equal(t, test.expect, IsNew(test.findings, test.baseline)) | ||
} | ||
} | ||
|
||
func TestFileLoadBaseline(t *testing.T) { | ||
tests := []struct { | ||
Filename string | ||
ExpectedError error | ||
}{ | ||
{ | ||
Filename: "../testdata/baseline/baseline.csv", | ||
ExpectedError: errors.New("the format of the file ../testdata/baseline/baseline.csv is not supported"), | ||
}, | ||
{ | ||
Filename: "../testdata/baseline/baseline.sarif", | ||
ExpectedError: errors.New("the format of the file ../testdata/baseline/baseline.sarif is not supported"), | ||
}, | ||
{ | ||
Filename: "../testdata/baseline/notfound.json", | ||
ExpectedError: errors.New("could not open ../testdata/baseline/notfound.json"), | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
_, err := LoadBaseline(test.Filename) | ||
assert.Equal(t, test.ExpectedError.Error(), err.Error()) | ||
} | ||
} | ||
|
||
func TestIgnoreIssuesInBaseline(t *testing.T) { | ||
tests := []struct { | ||
findings []report.Finding | ||
baseline []report.Finding | ||
expectCount int | ||
}{ | ||
{ | ||
findings: []report.Finding{ | ||
{ | ||
Author: "a", | ||
Commit: "5", | ||
}, | ||
}, | ||
baseline: []report.Finding{ | ||
{ | ||
Author: "a", | ||
Commit: "5", | ||
}, | ||
}, | ||
expectCount: 0, | ||
}, | ||
{ | ||
findings: []report.Finding{ | ||
{ | ||
Author: "a", | ||
Commit: "5", | ||
Fingerprint: "a", | ||
}, | ||
}, | ||
baseline: []report.Finding{ | ||
{ | ||
Author: "a", | ||
Commit: "5", | ||
Fingerprint: "b", | ||
}, | ||
}, | ||
expectCount: 0, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
d, _ := NewDetectorDefaultConfig() | ||
d.baseline = test.baseline | ||
for _, finding := range test.findings { | ||
d.addFinding(finding) | ||
} | ||
assert.Equal(t, test.expectCount, len(d.findings)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
RuleID,Commit,File,Secret,Match,StartLine,EndLine,StartColumn,EndColumn,Author,Message,Date,Email,Fingerprint | ||
1,b,c,f,s,m,s,e,s,e,a,m,f,r,f |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
[ | ||
{ | ||
"Description": "PyPI upload token", | ||
"StartLine": 32, | ||
"EndLine": 32, | ||
"StartColumn": 21, | ||
"EndColumn": 106, | ||
"Match": "************************", | ||
"Secret": "************************", | ||
"File": "detect/detect_test.go", | ||
"Commit": "9326f35380636bcbe61e94b0584d1618c4b5c2c2", | ||
"Entropy": 1.9606875, | ||
"Author": "****", | ||
"Email": "****", | ||
"Date": "2022-03-07T14:33:06Z", | ||
"Message": "Escape - character in regex character groups (#802)\n\n* fix char escape\n\n* add test\n\n* fix verbosity in make test", | ||
"Tags": [], | ||
"RuleID": "pypi-upload-token", | ||
"Fingerprint": "9326f35380636bcbe61e94b0584d1618c4b5c2c2:detect/detect_test.go:pypi-upload-token:32" | ||
}, | ||
{ | ||
"Description": "PyPI upload token", | ||
"StartLine": 33, | ||
"EndLine": 33, | ||
"StartColumn": 21, | ||
"EndColumn": 106, | ||
"Match": "************************", | ||
"Secret": "************************", | ||
"File": "detect/detect_test.go", | ||
"Commit": "9326f35380636bcbe61e94b0584d1618c4b5c2c2", | ||
"Entropy": 1.9606875, | ||
"Author": "****", | ||
"Email": "****", | ||
"Date": "2022-03-07T14:33:06Z", | ||
"Message": "Escape - character in regex character groups (#802)\n\n* fix char escape\n\n* add test\n\n* fix verbosity in make test", | ||
"Tags": [], | ||
"RuleID": "pypi-upload-token", | ||
"Fingerprint": "9326f35380636bcbe61e94b0584d1618c4b5c2c2:detect/detect_test.go:pypi-upload-token:33" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"$schema": "https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0-rtm.5.json", | ||
"version": "2.1.0", | ||
"runs": [ | ||
] | ||
} |