mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-30 05:04:22 +01:00
Make config generation utils public (#1480)
* chore(generate): make utils public * chore(generate): change validate log messages * chore(generate): split functions into files * chore(config): add warning for public funcs
This commit is contained in:
1 parent
3be7faa1ff
commit
bd81872eee
114 files changed
+862
-713
No files matched your search
@@ -0,0 +1,85 @@
|
||||
// == WARNING ==
|
||||
// These functions are used to generate GitLeak's default config.
|
||||
// You are free to use these in your own project, HOWEVER, no API stability is guaranteed.
|
||||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/zricethezav/gitleaks/v8/config"
|
||||
"github.com/zricethezav/gitleaks/v8/detect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Validate(r config.Rule, truePositives []string, falsePositives []string) *config.Rule {
|
||||
// normalize keywords like in the config package
|
||||
var keywords []string
|
||||
for _, k := range r.Keywords {
|
||||
keywords = append(keywords, strings.ToLower(k))
|
||||
}
|
||||
r.Keywords = keywords
|
||||
|
||||
rules := make(map[string]config.Rule)
|
||||
rules[r.RuleID] = r
|
||||
d := detect.NewDetector(config.Config{
|
||||
Rules: rules,
|
||||
Keywords: keywords,
|
||||
})
|
||||
for _, tp := range truePositives {
|
||||
if len(d.DetectString(tp)) != 1 {
|
||||
log.Fatal().
|
||||
Str("rule", r.RuleID).
|
||||
Str("value", tp).
|
||||
Str("regex", r.Regex.String()).
|
||||
Msg("Failed to Validate. True positive was not detected by regex.")
|
||||
}
|
||||
}
|
||||
for _, fp := range falsePositives {
|
||||
if len(d.DetectString(fp)) != 0 {
|
||||
log.Fatal().
|
||||
Str("rule", r.RuleID).
|
||||
Str("value", fp).
|
||||
Str("regex", r.Regex.String()).
|
||||
Msg("Failed to Validate. False positive was detected by regex.")
|
||||
}
|
||||
}
|
||||
return &r
|
||||
}
|
||||
|
||||
func ValidateWithPaths(r config.Rule, truePositives map[string]string, falsePositives map[string]string) *config.Rule {
|
||||
var keywords []string
|
||||
for _, k := range r.Keywords {
|
||||
keywords = append(keywords, strings.ToLower(k))
|
||||
}
|
||||
r.Keywords = keywords
|
||||
|
||||
rules := make(map[string]config.Rule)
|
||||
rules[r.RuleID] = r
|
||||
d := detect.NewDetector(config.Config{
|
||||
Rules: rules,
|
||||
Keywords: keywords,
|
||||
})
|
||||
for path, tp := range truePositives {
|
||||
f := detect.Fragment{Raw: tp, FilePath: path}
|
||||
if len(d.Detect(f)) != 1 {
|
||||
log.Fatal().
|
||||
Str("rule", r.RuleID).
|
||||
Str("value", tp).
|
||||
Str("regex", r.Regex.String()).
|
||||
Str("path", r.Path.String()).
|
||||
Msg("Failed to Validate. True positive was not detected by regex and/or path.")
|
||||
}
|
||||
}
|
||||
for path, fp := range falsePositives {
|
||||
f := detect.Fragment{Raw: fp, FilePath: path}
|
||||
if len(d.Detect(f)) != 0 {
|
||||
log.Fatal().
|
||||
Str("rule", r.RuleID).
|
||||
Str("value", fp).
|
||||
Str("regex", r.Regex.String()).
|
||||
Str("path", r.Path.String()).
|
||||
Msg("Failed to Validate. False positive was detected by regex and/or path.")
|
||||
}
|
||||
}
|
||||
return &r
|
||||
}
|
||||
Reference in new issue
Block a user