diff --git a/.gitleaksignore b/.gitleaksignore index 1d30b3bd..2b5a821f 100644 --- a/.gitleaksignore +++ b/.gitleaksignore @@ -716,6 +716,9 @@ testdata/expected/git/small-branch-foo.txt:aws-access-token:15 testdata/expected/git/small.txt:aws-access-token:15 testdata/expected/git/small.txt:aws-access-token:44 testdata/repos/nogit/main.go:aws-access-token:20 +testdata/repos/nogit/api.go:aws-access-token:20 +testdata/repos/small/api/ignoreCommit.go:aws-access-token:20 +testdata/repos/small/api/ignoreGlobal.go:aws-access-token:20 3df8c3deb7bc1e34210bdbce114f1c6165bc6ac8:detect/detect_test.go:aws-access-token:513 3df8c3deb7bc1e34210bdbce114f1c6165bc6ac8:detect/detect_test.go:aws-access-token:492 3df8c3deb7bc1e34210bdbce114f1c6165bc6ac8:detect/detect_test.go:aws-access-token:414 @@ -756,5 +759,7 @@ acce01f2338434a78f6a4a06a097b0fd23280484:README.md:aws-access-token:220 9701bf1724d822c0d5bbb7627535dd639a37bf56:detect/detect_test.go:aws-access-token:507 9701bf1724d822c0d5bbb7627535dd639a37bf56:testdata/repos/staged/api/api.go:aws-access-token:6 9701bf1724d822c0d5bbb7627535dd639a37bf56:testdata/repos/staged/api/api.go:aws-access-token:7 - +adf617b3b4628e1160fa3d135b4c3dfd45c05e15:testdata/repos/nogit/api.go:aws-access-token:20 +adf617b3b4628e1160fa3d135b4c3dfd45c05e15:testdata/repos/small/api/ignoreGlobal.go:aws-access-token:20 +adf617b3b4628e1160fa3d135b4c3dfd45c05e15:testdata/repos/small/api/ignoreCommit.go:aws-access-token:20 diff --git a/detect/detect.go b/detect/detect.go index 0068d277..d56839c9 100644 --- a/detect/detect.go +++ b/detect/detect.go @@ -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) { diff --git a/detect/detect_test.go b/detect/detect_test.go index 750139f0..080e14ea 100644 --- a/detect/detect_test.go +++ b/detect/detect_test.go @@ -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 { diff --git a/testdata/repos/nogit/.gitleaksignore b/testdata/repos/nogit/.gitleaksignore new file mode 100644 index 00000000..981ab841 --- /dev/null +++ b/testdata/repos/nogit/.gitleaksignore @@ -0,0 +1 @@ +../testdata/repos/nogit/api.go:aws-access-key:20 diff --git a/testdata/repos/nogit/api.go b/testdata/repos/nogit/api.go new file mode 100644 index 00000000..acbef43f --- /dev/null +++ b/testdata/repos/nogit/api.go @@ -0,0 +1,24 @@ +package main + +import "fmt" + +func main() { + + var a = "initial" + fmt.Println(a) + + var b, c int = 1, 2 + fmt.Println(b, c) + + var d = true + fmt.Println(d) + + var e int + fmt.Println(e) + + // opps I added a secret at line 20 + awsToken := "AKIALALEMEL33243OLIA" + + f := "apple" + fmt.Println(f) +} diff --git a/testdata/repos/small/.gitleaksignore b/testdata/repos/small/.gitleaksignore new file mode 100644 index 00000000..8a845a7e --- /dev/null +++ b/testdata/repos/small/.gitleaksignore @@ -0,0 +1,2 @@ +api/ignoreGlobal.go:aws-access-key:20 +53cd7a3c6eb4937f413e3c25e4a9f39289afa69e:api/ignoreCommit.go:aws-access-key:20 diff --git a/testdata/repos/small/api/ignoreCommit.go b/testdata/repos/small/api/ignoreCommit.go new file mode 100644 index 00000000..acbef43f --- /dev/null +++ b/testdata/repos/small/api/ignoreCommit.go @@ -0,0 +1,24 @@ +package main + +import "fmt" + +func main() { + + var a = "initial" + fmt.Println(a) + + var b, c int = 1, 2 + fmt.Println(b, c) + + var d = true + fmt.Println(d) + + var e int + fmt.Println(e) + + // opps I added a secret at line 20 + awsToken := "AKIALALEMEL33243OLIA" + + f := "apple" + fmt.Println(f) +} diff --git a/testdata/repos/small/api/ignoreGlobal.go b/testdata/repos/small/api/ignoreGlobal.go new file mode 100644 index 00000000..acbef43f --- /dev/null +++ b/testdata/repos/small/api/ignoreGlobal.go @@ -0,0 +1,24 @@ +package main + +import "fmt" + +func main() { + + var a = "initial" + fmt.Println(a) + + var b, c int = 1, 2 + fmt.Println(b, c) + + var d = true + fmt.Println(d) + + var e int + fmt.Println(e) + + // opps I added a secret at line 20 + awsToken := "AKIALALEMEL33243OLIA" + + f := "apple" + fmt.Println(f) +} diff --git a/testdata/repos/small/dotGit/COMMIT_EDITMSG b/testdata/repos/small/dotGit/COMMIT_EDITMSG index 0ba1543f..940da734 100644 --- a/testdata/repos/small/dotGit/COMMIT_EDITMSG +++ b/testdata/repos/small/dotGit/COMMIT_EDITMSG @@ -1 +1 @@ -removing secret from foo package +add .gitleaksignore test files diff --git a/testdata/repos/small/dotGit/ORIG_HEAD b/testdata/repos/small/dotGit/ORIG_HEAD index 96321ccd..b8378305 100644 --- a/testdata/repos/small/dotGit/ORIG_HEAD +++ b/testdata/repos/small/dotGit/ORIG_HEAD @@ -1 +1 @@ -1b6da43b82b22e4eaa10bcf8ee591e91abbfc587 +cada78a9bf157ec05573f19e682d211f811c2e2d diff --git a/testdata/repos/small/dotGit/index b/testdata/repos/small/dotGit/index index fec9889a..3cde9218 100644 Binary files a/testdata/repos/small/dotGit/index and b/testdata/repos/small/dotGit/index differ diff --git a/testdata/repos/small/dotGit/logs/HEAD b/testdata/repos/small/dotGit/logs/HEAD index 8fc59bb3..e22c1ce7 100644 --- a/testdata/repos/small/dotGit/logs/HEAD +++ b/testdata/repos/small/dotGit/logs/HEAD @@ -11,3 +11,7 @@ a122b33c6bad3ee54724f52f2caad385ab1982ab 1b6da43b82b22e4eaa10bcf8ee591e91abbfc58 f1b58b97808f8e744f6a23c693859df5b5968901 2e1db472eeba53f06c4026ae4566ea022e36598e Zach Rice 1635897009 -0500 checkout: moving from foo to main 2e1db472eeba53f06c4026ae4566ea022e36598e f1b58b97808f8e744f6a23c693859df5b5968901 Zach Rice 1635897062 -0500 checkout: moving from main to foo f1b58b97808f8e744f6a23c693859df5b5968901 2e1db472eeba53f06c4026ae4566ea022e36598e Zach Rice 1635897508 -0500 checkout: moving from foo to main +2e1db472eeba53f06c4026ae4566ea022e36598e 4f77f1b3cc39d4b17e4cf4ba0a38f5daed9875b4 Richard Gomez 1681920523 -0400 commit: add .gitleaksignore test +4f77f1b3cc39d4b17e4cf4ba0a38f5daed9875b4 cada78a9bf157ec05573f19e682d211f811c2e2d Richard Gomez 1681920658 -0400 commit (amend): add .gitleaksignore test +cada78a9bf157ec05573f19e682d211f811c2e2d 2e1db472eeba53f06c4026ae4566ea022e36598e Richard Gomez 1681920730 -0400 reset: moving to HEAD~ +2e1db472eeba53f06c4026ae4566ea022e36598e 53cd7a3c6eb4937f413e3c25e4a9f39289afa69e Richard Gomez 1681920759 -0400 commit: add .gitleaksignore test files diff --git a/testdata/repos/small/dotGit/logs/refs/heads/main b/testdata/repos/small/dotGit/logs/refs/heads/main index 50148f0e..456f8e99 100644 --- a/testdata/repos/small/dotGit/logs/refs/heads/main +++ b/testdata/repos/small/dotGit/logs/refs/heads/main @@ -1,2 +1,6 @@ 0000000000000000000000000000000000000000 1b6da43b82b22e4eaa10bcf8ee591e91abbfc587 Zach Rice 1635896329 -0500 clone: from github.com:gitleaks/test.git 1b6da43b82b22e4eaa10bcf8ee591e91abbfc587 2e1db472eeba53f06c4026ae4566ea022e36598e Zach Rice 1635896648 -0500 pull origin main: Fast-forward +2e1db472eeba53f06c4026ae4566ea022e36598e 4f77f1b3cc39d4b17e4cf4ba0a38f5daed9875b4 Richard Gomez 1681920523 -0400 commit: add .gitleaksignore test +4f77f1b3cc39d4b17e4cf4ba0a38f5daed9875b4 cada78a9bf157ec05573f19e682d211f811c2e2d Richard Gomez 1681920658 -0400 commit (amend): add .gitleaksignore test +cada78a9bf157ec05573f19e682d211f811c2e2d 2e1db472eeba53f06c4026ae4566ea022e36598e Richard Gomez 1681920730 -0400 reset: moving to HEAD~ +2e1db472eeba53f06c4026ae4566ea022e36598e 53cd7a3c6eb4937f413e3c25e4a9f39289afa69e Richard Gomez 1681920759 -0400 commit: add .gitleaksignore test files diff --git a/testdata/repos/small/dotGit/objects/32/bfaec6697c1a693f71b183d3389ad2547bbb3c b/testdata/repos/small/dotGit/objects/32/bfaec6697c1a693f71b183d3389ad2547bbb3c new file mode 100644 index 00000000..045c095a Binary files /dev/null and b/testdata/repos/small/dotGit/objects/32/bfaec6697c1a693f71b183d3389ad2547bbb3c differ diff --git a/testdata/repos/small/dotGit/objects/4f/77f1b3cc39d4b17e4cf4ba0a38f5daed9875b4 b/testdata/repos/small/dotGit/objects/4f/77f1b3cc39d4b17e4cf4ba0a38f5daed9875b4 new file mode 100644 index 00000000..292c67b2 Binary files /dev/null and b/testdata/repos/small/dotGit/objects/4f/77f1b3cc39d4b17e4cf4ba0a38f5daed9875b4 differ diff --git a/testdata/repos/small/dotGit/objects/53/cd7a3c6eb4937f413e3c25e4a9f39289afa69e b/testdata/repos/small/dotGit/objects/53/cd7a3c6eb4937f413e3c25e4a9f39289afa69e new file mode 100644 index 00000000..d0b42fb5 Binary files /dev/null and b/testdata/repos/small/dotGit/objects/53/cd7a3c6eb4937f413e3c25e4a9f39289afa69e differ diff --git a/testdata/repos/small/dotGit/objects/8d/c20a62e189d3446cfb6ec328dbd379d64feb20 b/testdata/repos/small/dotGit/objects/8d/c20a62e189d3446cfb6ec328dbd379d64feb20 new file mode 100644 index 00000000..740ed693 Binary files /dev/null and b/testdata/repos/small/dotGit/objects/8d/c20a62e189d3446cfb6ec328dbd379d64feb20 differ diff --git a/testdata/repos/small/dotGit/objects/98/1ab8417104c91bb9a4657f5d436c760c0aff50 b/testdata/repos/small/dotGit/objects/98/1ab8417104c91bb9a4657f5d436c760c0aff50 new file mode 100644 index 00000000..d4a92391 Binary files /dev/null and b/testdata/repos/small/dotGit/objects/98/1ab8417104c91bb9a4657f5d436c760c0aff50 differ diff --git a/testdata/repos/small/dotGit/objects/ac/bef43fdb053ec01e8e16697536d47853460cbc b/testdata/repos/small/dotGit/objects/ac/bef43fdb053ec01e8e16697536d47853460cbc new file mode 100644 index 00000000..c4ff9092 Binary files /dev/null and b/testdata/repos/small/dotGit/objects/ac/bef43fdb053ec01e8e16697536d47853460cbc differ diff --git a/testdata/repos/small/dotGit/objects/ca/da78a9bf157ec05573f19e682d211f811c2e2d b/testdata/repos/small/dotGit/objects/ca/da78a9bf157ec05573f19e682d211f811c2e2d new file mode 100644 index 00000000..f64d3856 --- /dev/null +++ b/testdata/repos/small/dotGit/objects/ca/da78a9bf157ec05573f19e682d211f811c2e2d @@ -0,0 +1 @@ +x•ÎAjÃ0…á®uй@Ãh$�e!»ì{ƒ±5±E-+(ʦ§¯ ½@v�>xs-%w >zS…$:YF±ŽÅy"�,G¢k�bbó�¦ûզɤ:IpwäÙ#±¨Ì*xpÇaŒjäÕ×Úà+Ï«´·Zôέ,Ãq¼.Eòvšk¹€åhGÂ@>Ñ#š£/»¾ç9Äo$%8-¹o*ßϼìµ)t}vó ¸ÁLë \ No newline at end of file diff --git a/testdata/repos/small/dotGit/objects/da/eb160a13200a3422e73296a6892784a113e0d6 b/testdata/repos/small/dotGit/objects/da/eb160a13200a3422e73296a6892784a113e0d6 new file mode 100644 index 00000000..d8c9377b Binary files /dev/null and b/testdata/repos/small/dotGit/objects/da/eb160a13200a3422e73296a6892784a113e0d6 differ diff --git a/testdata/repos/small/dotGit/objects/df/7eaa59fc89b3e7258167605db2e582f1a78e6f b/testdata/repos/small/dotGit/objects/df/7eaa59fc89b3e7258167605db2e582f1a78e6f new file mode 100644 index 00000000..e0b61542 Binary files /dev/null and b/testdata/repos/small/dotGit/objects/df/7eaa59fc89b3e7258167605db2e582f1a78e6f differ diff --git a/testdata/repos/small/dotGit/objects/fa/468655086f149d41a8e7db14ed054bebec7687 b/testdata/repos/small/dotGit/objects/fa/468655086f149d41a8e7db14ed054bebec7687 new file mode 100644 index 00000000..9cae7844 Binary files /dev/null and b/testdata/repos/small/dotGit/objects/fa/468655086f149d41a8e7db14ed054bebec7687 differ diff --git a/testdata/repos/small/dotGit/refs/heads/main b/testdata/repos/small/dotGit/refs/heads/main index 98f12e92..80ff7e8a 100644 --- a/testdata/repos/small/dotGit/refs/heads/main +++ b/testdata/repos/small/dotGit/refs/heads/main @@ -1 +1 @@ -2e1db472eeba53f06c4026ae4566ea022e36598e +53cd7a3c6eb4937f413e3c25e4a9f39289afa69e