detect.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package detect
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "regexp"
  6. "strings"
  7. "github.com/rs/zerolog/log"
  8. "github.com/zricethezav/gitleaks/v8/config"
  9. "github.com/zricethezav/gitleaks/v8/report"
  10. )
  11. type Options struct {
  12. Verbose bool
  13. Redact bool
  14. }
  15. const MAXGOROUTINES = 4
  16. func DetectFindings(cfg config.Config, b []byte, filePath string, commit string) []report.Finding {
  17. var findings []report.Finding
  18. linePairs := regexp.MustCompile("\n").FindAllIndex(b, -1)
  19. // check if we should skip file based on the global allowlist or if the file is the same as the gitleaks config
  20. if cfg.Allowlist.PathAllowed(filePath) || filePath == cfg.Path {
  21. return findings
  22. }
  23. for _, r := range cfg.Rules {
  24. pathSkip := false
  25. if r.Allowlist.CommitAllowed(commit) {
  26. continue
  27. }
  28. if r.Allowlist.PathAllowed(filePath) {
  29. continue
  30. }
  31. // Check if path should be considered
  32. if r.Path != nil {
  33. if r.Path.Match([]byte(filePath)) {
  34. if r.Regex == nil {
  35. // This is a path only rule
  36. f := report.Finding{
  37. Description: r.Description,
  38. File: filePath,
  39. RuleID: r.RuleID,
  40. Match: fmt.Sprintf("file detected: %s", filePath),
  41. Tags: r.Tags,
  42. }
  43. findings = append(findings, f)
  44. pathSkip = true
  45. }
  46. } else {
  47. pathSkip = true
  48. }
  49. }
  50. if pathSkip {
  51. continue
  52. }
  53. matchIndices := r.Regex.FindAllIndex(b, -1)
  54. for _, m := range matchIndices {
  55. location := getLocation(linePairs, m[0], m[1])
  56. secret := strings.Trim(string(b[m[0]:m[1]]), "\n")
  57. f := report.Finding{
  58. Description: r.Description,
  59. File: filePath,
  60. RuleID: r.RuleID,
  61. StartLine: location.startLine,
  62. EndLine: location.endLine,
  63. StartColumn: location.startColumn,
  64. EndColumn: location.endColumn,
  65. Secret: secret,
  66. Match: secret,
  67. Tags: r.Tags,
  68. }
  69. if r.Allowlist.RegexAllowed(f.Secret) || cfg.Allowlist.RegexAllowed(f.Secret) {
  70. continue
  71. }
  72. // extract secret from secret group if set
  73. if r.SecretGroup != 0 {
  74. groups := r.Regex.FindStringSubmatch(secret)
  75. if len(groups) <= r.SecretGroup || len(groups) == 0 {
  76. // Config validation should prevent this
  77. break
  78. }
  79. secret = groups[r.SecretGroup]
  80. f.Secret = secret
  81. }
  82. // extract secret from secret group if set
  83. if r.EntropySet() {
  84. include, entropy := r.IncludeEntropy(secret)
  85. if include {
  86. f.Entropy = float32(entropy)
  87. findings = append(findings, f)
  88. }
  89. } else {
  90. findings = append(findings, f)
  91. }
  92. }
  93. }
  94. return dedupe(findings)
  95. }
  96. func printFinding(f report.Finding) {
  97. var b []byte
  98. b, _ = json.MarshalIndent(f, "", " ")
  99. fmt.Println(string(b))
  100. }
  101. func dedupe(findings []report.Finding) []report.Finding {
  102. var retFindings []report.Finding
  103. for _, f := range findings {
  104. include := true
  105. if strings.Contains(strings.ToLower(f.RuleID), "generic") {
  106. for _, fPrime := range findings {
  107. if f.StartLine == fPrime.StartLine &&
  108. f.EndLine == fPrime.EndLine &&
  109. f.Commit == fPrime.Commit &&
  110. f.RuleID != fPrime.RuleID &&
  111. strings.Contains(fPrime.Secret, f.Secret) &&
  112. !strings.Contains(strings.ToLower(fPrime.RuleID), "generic") {
  113. genericMatch := strings.Replace(f.Match, f.Secret, "REDACTED", -1)
  114. betterMatch := strings.Replace(fPrime.Match, fPrime.Secret, "REDACTED", -1)
  115. log.Debug().Msgf("skipping %s finding (%s), %s rule takes precendence (%s)", f.RuleID, genericMatch, fPrime.RuleID, betterMatch)
  116. include = false
  117. break
  118. }
  119. }
  120. }
  121. if include {
  122. retFindings = append(retFindings, f)
  123. }
  124. }
  125. return retFindings
  126. }