From 7dfa3f2b481aba1ef2225d83c9d870976ebd02bc Mon Sep 17 00:00:00 2001 From: zach rice Date: Tue, 16 Apr 2019 18:13:24 -0500 Subject: [PATCH] last updates? --- .gitignore | 2 +- gitleaks.toml | 17 +++++++++++------ src/constants_test.go | 9 ++++----- src/gitleaks_test.go | 19 ++++++------------- src/options.go | 6 +++--- src/utils.go | 20 ++++++++++---------- 6 files changed, 35 insertions(+), 38 deletions(-) diff --git a/.gitignore b/.gitignore index 2f51e5a2..03d149d9 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,4 @@ *.test *.out -tmp +examples diff --git a/gitleaks.toml b/gitleaks.toml index dc8ac508..7f7966ab 100644 --- a/gitleaks.toml +++ b/gitleaks.toml @@ -66,11 +66,16 @@ files = [ # [[rules]] # description = "Generic Key" # regex = '''(?i)key(.{0,6})?(:|=|=>|:=)''' -# entropies = ["4.1-4.3"] +# entropies = [ +# "4.1-4.3", +# "5.5-6.3", +# ] # entropyROI = "line" +# filetypes = [".go", ".py", ".c"] # tags = ["key"] # severity = "8" # +# # [[rules]] # description = "Generic Key" # regex = '''(?i)key(.{0,6})?(:|=|=>|:=)''' @@ -79,9 +84,9 @@ files = [ # entropyROI = "line" # tags = ["key"] # severity = "medium" -# + # [[rules]] -# description = "Any go file" -# filetypes = [".go"] -# tags = ["go files"] -# severity = "low" +# description = "Any pem file" +# filetypes = [".key"] +# tags = ["pem"] +# severity = "high" diff --git a/src/constants_test.go b/src/constants_test.go index c637f0e5..73261049 100644 --- a/src/constants_test.go +++ b/src/constants_test.go @@ -71,14 +71,14 @@ entropies = [ ] ` -const testEntropyLineRegexRange = ` +const testEntropyWordRegexRange = ` [[rules]] description = "test entropy regex ranges" regex = '''(?i)key(.{0,6})?(:|=|=>|:=)''' entropies = [ "4.1-4.3", ] -entropyROI="line" +entropyROI="word" ` const testEntropyRegexRange = ` @@ -104,13 +104,12 @@ entropies = [ "4.1-4.3", ] filetypes = [".go"] -entropyROI="line" ` func testTomlLoader() string { tmpDir, _ := ioutil.TempDir("", "whiteListConfigs") ioutil.WriteFile(path.Join(tmpDir, "regex"), []byte(testWhitelistRegex), 0644) - ioutil.WriteFile(path.Join(tmpDir, "entropyLineRegex"), []byte(testEntropyLineRegexRange), 0644) + ioutil.WriteFile(path.Join(tmpDir, "entropyWordRegex"), []byte(testEntropyWordRegexRange), 0644) ioutil.WriteFile(path.Join(tmpDir, "entropyRegex"), []byte(testEntropyRegexRange), 0644) ioutil.WriteFile(path.Join(tmpDir, "commit"), []byte(testWhitelistCommit), 0644) ioutil.WriteFile(path.Join(tmpDir, "file"), []byte(testWhitelistFile), 0644) @@ -119,6 +118,6 @@ func testTomlLoader() string { ioutil.WriteFile(path.Join(tmpDir, "badEntropy"), []byte(testBadEntropyRange), 0644) ioutil.WriteFile(path.Join(tmpDir, "badEntropy2"), []byte(testBadEntropyRange2), 0644) ioutil.WriteFile(path.Join(tmpDir, "mdFiles"), []byte(testMDFileType), 0644) - ioutil.WriteFile(path.Join(tmpDir, "entropyLineRegexGo"), []byte(testEntropyRegexRangeGoFilter), 0644) + ioutil.WriteFile(path.Join(tmpDir, "entropyRegexGo"), []byte(testEntropyRegexRangeGoFilter), 0644) return tmpDir } diff --git a/src/gitleaks_test.go b/src/gitleaks_test.go index 0ad49d02..8fb587f0 100644 --- a/src/gitleaks_test.go +++ b/src/gitleaks_test.go @@ -561,29 +561,22 @@ func TestAuditRepo(t *testing.T) { { repo: leaksRepo, description: "toml entropy range from opts", - numLeaks: 426, + numLeaks: 266, testOpts: &Options{ ConfigPath: path.Join(configsDir, "entropy"), }, }, { repo: leaksRepo, - description: "toml entropy range", - numLeaks: 426, + description: "toml entropy regex word range", + numLeaks: 0, testOpts: &Options{}, - configPath: path.Join(configsDir, "entropy"), - }, - { - repo: leaksRepo, - description: "toml entropy regex line range", - numLeaks: 2, - testOpts: &Options{}, - configPath: path.Join(configsDir, "entropyLineRegex"), + configPath: path.Join(configsDir, "entropyWordRegex"), }, { repo: leaksRepo, description: "toml entropy regex range", - numLeaks: 0, + numLeaks: 2, testOpts: &Options{}, configPath: path.Join(configsDir, "entropyRegex"), }, @@ -615,7 +608,7 @@ func TestAuditRepo(t *testing.T) { description: "toml entropys line regex go", numLeaks: 2, testOpts: &Options{}, - configPath: path.Join(configsDir, "entropyLineRegexGo"), + configPath: path.Join(configsDir, "entropyRegexGo"), }, } g := goblin.Goblin(t) diff --git a/src/options.go b/src/options.go index 75157958..0e3505a9 100644 --- a/src/options.go +++ b/src/options.go @@ -60,13 +60,13 @@ func ParseOpts() *Options { _, err := parser.Parse() if err != nil { - if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp { - os.Exit(0) + if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type != flags.ErrHelp { + parser.WriteHelp(os.Stdout) } + os.Exit(0) } if len(os.Args) == 1 { - // TODO: this will be a feature, check locally parser.WriteHelp(os.Stdout) os.Exit(0) } diff --git a/src/utils.go b/src/utils.go index 6a1fa40c..5445abff 100644 --- a/src/utils.go +++ b/src/utils.go @@ -103,16 +103,7 @@ func (rule *Rule) check(line string, commit *commitInfo) (*Leak, error) { } if rule.entropies != nil { - if rule.entropyROI == "line" { - _entropy := getShannonEntropy(line) - for _, e := range rule.entropies { - if _entropy > e.v1 && _entropy < e.v2 { - entropy = _entropy - entropyWord = line - goto postEntropy - } - } - } else { + if rule.entropyROI == "word" { words := strings.Fields(line) for _, word := range words { _entropy := getShannonEntropy(word) @@ -124,6 +115,15 @@ func (rule *Rule) check(line string, commit *commitInfo) (*Leak, error) { } } } + } else { + _entropy := getShannonEntropy(line) + for _, e := range rule.entropies { + if _entropy > e.v1 && _entropy < e.v2 { + entropy = _entropy + entropyWord = line + goto postEntropy + } + } } }