utils.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. package detect
  2. import (
  3. // "encoding/json"
  4. "fmt"
  5. "math"
  6. "path/filepath"
  7. "strings"
  8. "time"
  9. "github.com/zricethezav/gitleaks/v8/cmd/scm"
  10. "github.com/zricethezav/gitleaks/v8/logging"
  11. "github.com/zricethezav/gitleaks/v8/report"
  12. "github.com/charmbracelet/lipgloss"
  13. "github.com/gitleaks/go-gitdiff/gitdiff"
  14. )
  15. // augmentGitFinding updates the start and end line numbers of a finding to include the
  16. // delta from the git diff
  17. func augmentGitFinding(scmPlatform scm.Platform, remoteUrl string, finding report.Finding, textFragment *gitdiff.TextFragment, f *gitdiff.File) report.Finding {
  18. if !strings.HasPrefix(finding.Match, "file detected") {
  19. finding.StartLine += int(textFragment.NewPosition)
  20. finding.EndLine += int(textFragment.NewPosition)
  21. }
  22. if f.PatchHeader != nil {
  23. finding.Commit = f.PatchHeader.SHA
  24. if f.PatchHeader.Author != nil {
  25. finding.Author = f.PatchHeader.Author.Name
  26. finding.Email = f.PatchHeader.Author.Email
  27. }
  28. finding.Date = f.PatchHeader.AuthorDate.UTC().Format(time.RFC3339)
  29. finding.Message = f.PatchHeader.Message()
  30. // Results from `git diff` shouldn't have a link.
  31. if finding.Commit != "" {
  32. finding.Link = createScmLink(scmPlatform, remoteUrl, finding)
  33. }
  34. }
  35. return finding
  36. }
  37. var linkCleaner = strings.NewReplacer(
  38. " ", "%20",
  39. "%", "%25",
  40. )
  41. func createScmLink(scmPlatform scm.Platform, remoteUrl string, finding report.Finding) string {
  42. if scmPlatform == scm.NoPlatform {
  43. return ""
  44. }
  45. // Clean the path.
  46. var (
  47. filePath = linkCleaner.Replace(finding.File)
  48. ext = strings.ToLower(filepath.Ext(filePath))
  49. )
  50. switch scmPlatform {
  51. case scm.GitHubPlatform:
  52. link := fmt.Sprintf("%s/blob/%s/%s", remoteUrl, finding.Commit, filePath)
  53. if ext == ".ipynb" || ext == ".md" {
  54. link += "?plain=1"
  55. }
  56. if finding.StartLine != 0 {
  57. link += fmt.Sprintf("#L%d", finding.StartLine)
  58. }
  59. if finding.EndLine != finding.StartLine {
  60. link += fmt.Sprintf("-L%d", finding.EndLine)
  61. }
  62. return link
  63. case scm.GitLabPlatform:
  64. link := fmt.Sprintf("%s/blob/%s/%s", remoteUrl, finding.Commit, filePath)
  65. if finding.StartLine != 0 {
  66. link += fmt.Sprintf("#L%d", finding.StartLine)
  67. }
  68. if finding.EndLine != finding.StartLine {
  69. link += fmt.Sprintf("-%d", finding.EndLine)
  70. }
  71. return link
  72. default:
  73. // This should never happen.
  74. return ""
  75. }
  76. }
  77. // shannonEntropy calculates the entropy of data using the formula defined here:
  78. // https://en.wiktionary.org/wiki/Shannon_entropy
  79. // Another way to think about what this is doing is calculating the number of bits
  80. // needed to on average encode the data. So, the higher the entropy, the more random the data, the
  81. // more bits needed to encode that data.
  82. func shannonEntropy(data string) (entropy float64) {
  83. if data == "" {
  84. return 0
  85. }
  86. charCounts := make(map[rune]int)
  87. for _, char := range data {
  88. charCounts[char]++
  89. }
  90. invLength := 1.0 / float64(len(data))
  91. for _, count := range charCounts {
  92. freq := float64(count) * invLength
  93. entropy -= freq * math.Log2(freq)
  94. }
  95. return entropy
  96. }
  97. // filter will dedupe and redact findings
  98. func filter(findings []report.Finding, redact uint) []report.Finding {
  99. var retFindings []report.Finding
  100. for _, f := range findings {
  101. include := true
  102. if strings.Contains(strings.ToLower(f.RuleID), "generic") {
  103. for _, fPrime := range findings {
  104. if f.StartLine == fPrime.StartLine &&
  105. f.Commit == fPrime.Commit &&
  106. f.RuleID != fPrime.RuleID &&
  107. strings.Contains(fPrime.Secret, f.Secret) &&
  108. !strings.Contains(strings.ToLower(fPrime.RuleID), "generic") {
  109. genericMatch := strings.Replace(f.Match, f.Secret, "REDACTED", -1)
  110. betterMatch := strings.Replace(fPrime.Match, fPrime.Secret, "REDACTED", -1)
  111. logging.Trace().Msgf("skipping %s finding (%s), %s rule takes precedence (%s)", f.RuleID, genericMatch, fPrime.RuleID, betterMatch)
  112. include = false
  113. break
  114. }
  115. }
  116. }
  117. if redact > 0 {
  118. f.Redact(redact)
  119. }
  120. if include {
  121. retFindings = append(retFindings, f)
  122. }
  123. }
  124. return retFindings
  125. }
  126. func printFinding(f report.Finding, noColor bool) {
  127. // trim all whitespace and tabs
  128. f.Line = strings.TrimSpace(f.Line)
  129. f.Secret = strings.TrimSpace(f.Secret)
  130. f.Match = strings.TrimSpace(f.Match)
  131. isFileMatch := strings.HasPrefix(f.Match, "file detected:")
  132. skipColor := noColor
  133. finding := ""
  134. var secret lipgloss.Style
  135. // Matches from filenames do not have a |line| or |secret|
  136. if !isFileMatch {
  137. matchInLineIDX := strings.Index(f.Line, f.Match)
  138. secretInMatchIdx := strings.Index(f.Match, f.Secret)
  139. skipColor = false
  140. if matchInLineIDX == -1 || noColor {
  141. skipColor = true
  142. matchInLineIDX = 0
  143. }
  144. start := f.Line[0:matchInLineIDX]
  145. startMatchIdx := 0
  146. if matchInLineIDX > 20 {
  147. startMatchIdx = matchInLineIDX - 20
  148. start = "..." + f.Line[startMatchIdx:matchInLineIDX]
  149. }
  150. matchBeginning := lipgloss.NewStyle().SetString(f.Match[0:secretInMatchIdx]).Foreground(lipgloss.Color("#f5d445"))
  151. secret = lipgloss.NewStyle().SetString(f.Secret).
  152. Bold(true).
  153. Italic(true).
  154. Foreground(lipgloss.Color("#f05c07"))
  155. matchEnd := lipgloss.NewStyle().SetString(f.Match[secretInMatchIdx+len(f.Secret):]).Foreground(lipgloss.Color("#f5d445"))
  156. lineEndIdx := matchInLineIDX + len(f.Match)
  157. if len(f.Line)-1 <= lineEndIdx {
  158. lineEndIdx = len(f.Line)
  159. }
  160. lineEnd := f.Line[lineEndIdx:]
  161. if len(f.Secret) > 100 {
  162. secret = lipgloss.NewStyle().SetString(f.Secret[0:100] + "...").
  163. Bold(true).
  164. Italic(true).
  165. Foreground(lipgloss.Color("#f05c07"))
  166. }
  167. if len(lineEnd) > 20 {
  168. lineEnd = lineEnd[0:20] + "..."
  169. }
  170. finding = fmt.Sprintf("%s%s%s%s%s\n", strings.TrimPrefix(strings.TrimLeft(start, " "), "\n"), matchBeginning, secret, matchEnd, lineEnd)
  171. }
  172. if skipColor || isFileMatch {
  173. fmt.Printf("%-12s %s\n", "Finding:", f.Match)
  174. fmt.Printf("%-12s %s\n", "Secret:", f.Secret)
  175. } else {
  176. fmt.Printf("%-12s %s", "Finding:", finding)
  177. fmt.Printf("%-12s %s\n", "Secret:", secret)
  178. }
  179. fmt.Printf("%-12s %s\n", "RuleID:", f.RuleID)
  180. fmt.Printf("%-12s %f\n", "Entropy:", f.Entropy)
  181. if f.File == "" {
  182. fmt.Println("")
  183. return
  184. }
  185. if len(f.Tags) > 0 {
  186. fmt.Printf("%-12s %s\n", "Tags:", f.Tags)
  187. }
  188. fmt.Printf("%-12s %s\n", "File:", f.File)
  189. fmt.Printf("%-12s %d\n", "Line:", f.StartLine)
  190. if f.Commit == "" {
  191. fmt.Printf("%-12s %s\n", "Fingerprint:", f.Fingerprint)
  192. fmt.Println("")
  193. return
  194. }
  195. fmt.Printf("%-12s %s\n", "Commit:", f.Commit)
  196. fmt.Printf("%-12s %s\n", "Author:", f.Author)
  197. fmt.Printf("%-12s %s\n", "Email:", f.Email)
  198. fmt.Printf("%-12s %s\n", "Date:", f.Date)
  199. fmt.Printf("%-12s %s\n", "Fingerprint:", f.Fingerprint)
  200. if f.Link != "" {
  201. fmt.Printf("%-12s %s\n", "Link:", f.Link)
  202. }
  203. fmt.Println("")
  204. }
  205. func isWhitespace(ch byte) bool {
  206. return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'
  207. }