detect.go 3.6 KB

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