Merge branch 'master' into fix_default_config

This commit is contained in:
amith-legit committed 2021-09-09 10:02:53 +03:00
commit be13bba64c
67 files changed
+509 -237

No files matched your search

+5 -1
View File
@@ -1,6 +1,7 @@
package config
import (
_ "embed"
"fmt"
"io"
"os"
@@ -16,6 +17,9 @@ import (
log "github.com/sirupsen/logrus"
)
//go:embed gitleaks.toml
var DefaultConfig string
// Config is a composite struct of Rules and Allowlists
// Each Rule contains a description, regular expression, tags, and allowlists if available
type Config struct {
@@ -42,7 +46,7 @@ type TomlAllowList struct {
}
// TomlLoader gets loaded with the values from a gitleaks toml config
// see the config in config/defaults.go for an example. TomlLoader is used
// see the config in config/gitleaks.toml for an example. TomlLoader is used
// to generate Config values (compiling regexes, etc).
type TomlLoader struct {
AllowList TomlAllowList
+2 -8
View File
@@ -1,9 +1,4 @@
package config
// DefaultConfig is the default gitleaks configuration. If --config={path-to-config} is set than the config located
// at {path-to-config} will be used. Alternatively, if --repo-config is set then gitleaks will attempt to
// use the config set in a gitleaks.toml or .gitleaks.toml file in the repo that is run with --repo-config set.
const DefaultConfig = `
title = "gitleaks config"
[[rules]]
@@ -33,12 +28,12 @@ title = "gitleaks config"
[[rules]]
description = "Twitter Secret Key"
regex = '''(?i)twitter(.{0,20})?[0-9a-z]{35,44}'''
regex = '''(?i)twitter(.{0,20})?['\"][0-9a-z]{35,44}['\"]'''
tags = ["key", "Twitter"]
[[rules]]
description = "Twitter Client ID"
regex = '''(?i)twitter(.{0,20})?[0-9a-z]{18,25}'''
regex = '''(?i)twitter(.{0,20})?['\"][0-9a-z]{18,25}['\"]'''
tags = ["client", "Twitter"]
[[rules]]
@@ -178,4 +173,3 @@ title = "gitleaks config"
files = ['''^\.?gitleaks.toml$''',
'''(.*?)(png|jpg|gif|doc|docx|pdf|bin|xls|pyc|zip)$''',
'''(go.mod|go.sum)$''']
`
+60 -14
View File
@@ -6,6 +6,23 @@ import (
"regexp"
)
// Offender is a struct that contains the information matched when searching
// content and information on why it matched (i.e. the EntropyLevel)
type Offender struct {
Match string
EntropyLevel float64
}
// IsEmpty checks to see if nothing was found in the match
func (o *Offender) IsEmpty() bool {
return o.Match == ""
}
// ToString the contents of the match
func (o *Offender) ToString() string {
return o.Match
}
// Rule is a struct that contains information that is loaded from a gitleaks config.
// This struct is used in the Config struct as an array of Rules and is iterated
// over during an scan. Each rule will be checked. If a regex match is found AND
@@ -23,28 +40,46 @@ type Rule struct {
}
// Inspect checks the content of a line for a leak
func (r *Rule) Inspect(line string) string {
offender := r.Regex.FindString(line)
if offender == "" {
return ""
func (r *Rule) Inspect(line string) *Offender {
match := r.Regex.FindString(line)
// EntropyLevel -1 means not checked
if match == "" {
return &Offender{
Match: "",
EntropyLevel: -1,
}
}
// check if offender is allowed
// EntropyLevel -1 means not checked
if r.RegexAllowed(line) {
return ""
return &Offender{
Match: "",
EntropyLevel: -1,
}
}
// check entropy
groups := r.Regex.FindStringSubmatch(offender)
if len(r.Entropies) != 0 && !r.ContainsEntropyLeak(groups) {
return ""
groups := r.Regex.FindStringSubmatch(match)
entropyWithinRange, entropyLevel := r.CheckEntropy(groups)
if len(r.Entropies) != 0 && !entropyWithinRange {
return &Offender{
Match: "",
EntropyLevel: entropyLevel,
}
}
// 0 is a match for the full regex pattern
if 0 < r.ReportGroup && r.ReportGroup < len(groups) {
offender = groups[r.ReportGroup]
match = groups[r.ReportGroup]
}
return &Offender{
Match: match,
EntropyLevel: entropyLevel,
}
return offender
}
// RegexAllowed checks if the content is allowlisted
@@ -57,17 +92,28 @@ func (r *Rule) CommitAllowed(commit string) bool {
return r.AllowList.CommitAllowed(commit)
}
// ContainsEntropyLeak checks if there is an entropy leak
func (r *Rule) ContainsEntropyLeak(groups []string) bool {
// CheckEntropy checks if there is an entropy leak
func (r *Rule) CheckEntropy(groups []string) (bool, float64) {
var highestFound float64 = 0
for _, e := range r.Entropies {
if len(groups) > e.Group {
entropy := shannonEntropy(groups[e.Group])
if entropy >= e.Min && entropy <= e.Max {
return true
return true, entropy
} else if entropy > highestFound {
highestFound = entropy
}
}
}
return false
if len(r.Entropies) == 0 {
// entropies not checked
return false, -1
}
// entropies checked but not within the range
return false, highestFound
}
// HasFileOrPathLeakOnly first checks if there are no entropy/regex rules, then checks if