validate.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // == WARNING ==
  2. // These functions are used to generate GitLeak's default config.
  3. // You are free to use these in your own project, HOWEVER, no API stability is guaranteed.
  4. package utils
  5. import (
  6. "strings"
  7. "github.com/rs/zerolog/log"
  8. "github.com/zricethezav/gitleaks/v8/cmd/generate/config/base"
  9. "github.com/zricethezav/gitleaks/v8/config"
  10. "github.com/zricethezav/gitleaks/v8/detect"
  11. )
  12. func Validate(rule config.Rule, truePositives []string, falsePositives []string) *config.Rule {
  13. r := &rule
  14. d := createSingleRuleDetector(r)
  15. for _, tp := range truePositives {
  16. if len(d.DetectString(tp)) < 1 {
  17. log.Fatal().
  18. Str("rule", r.RuleID).
  19. Str("value", tp).
  20. Str("regex", r.Regex.String()).
  21. Msg("Failed to Validate. True positive was not detected by regex.")
  22. }
  23. }
  24. for _, fp := range falsePositives {
  25. findings := d.DetectString(fp)
  26. if len(findings) != 0 {
  27. log.Fatal().
  28. Str("rule", r.RuleID).
  29. Str("value", fp).
  30. Str("regex", r.Regex.String()).
  31. Msg("Failed to Validate. False positive was detected by regex.")
  32. }
  33. }
  34. return r
  35. }
  36. func ValidateWithPaths(rule config.Rule, truePositives map[string]string, falsePositives map[string]string) *config.Rule {
  37. r := &rule
  38. d := createSingleRuleDetector(r)
  39. for path, tp := range truePositives {
  40. f := detect.Fragment{Raw: tp, FilePath: path}
  41. if len(d.Detect(f)) != 1 {
  42. log.Fatal().
  43. Str("rule", r.RuleID).
  44. Str("value", tp).
  45. Str("regex", r.Regex.String()).
  46. Str("path", r.Path.String()).
  47. Msg("Failed to Validate. True positive was not detected by regex and/or path.")
  48. }
  49. }
  50. for path, fp := range falsePositives {
  51. f := detect.Fragment{Raw: fp, FilePath: path}
  52. if len(d.Detect(f)) != 0 {
  53. log.Fatal().
  54. Str("rule", r.RuleID).
  55. Str("value", fp).
  56. Str("regex", r.Regex.String()).
  57. Str("path", r.Path.String()).
  58. Msg("Failed to Validate. False positive was detected by regex and/or path.")
  59. }
  60. }
  61. return r
  62. }
  63. func createSingleRuleDetector(r *config.Rule) *detect.Detector {
  64. // normalize keywords like in the config package
  65. var (
  66. uniqueKeywords = make(map[string]struct{})
  67. keywords []string
  68. )
  69. for _, keyword := range r.Keywords {
  70. k := strings.ToLower(keyword)
  71. if _, ok := uniqueKeywords[k]; ok {
  72. continue
  73. }
  74. keywords = append(keywords, k)
  75. uniqueKeywords[k] = struct{}{}
  76. }
  77. r.Keywords = keywords
  78. rules := map[string]config.Rule{
  79. r.RuleID: *r,
  80. }
  81. cfg := base.CreateGlobalConfig()
  82. cfg.Rules = rules
  83. cfg.Keywords = uniqueKeywords
  84. return detect.NewDetector(cfg)
  85. }