commit.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package scan
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "strings"
  6. "github.com/zricethezav/gitleaks/v7/config"
  7. "github.com/zricethezav/gitleaks/v7/options"
  8. "github.com/go-git/go-git/v5"
  9. fdiff "github.com/go-git/go-git/v5/plumbing/format/diff"
  10. "github.com/go-git/go-git/v5/plumbing/object"
  11. )
  12. // CommitScanner is a commit scanner
  13. type CommitScanner struct {
  14. cfg config.Config
  15. opts options.Options
  16. repo *git.Repository
  17. repoName string
  18. commit *object.Commit
  19. }
  20. // NewCommitScanner creates and returns a commit scanner
  21. func NewCommitScanner(opts options.Options, cfg config.Config, repo *git.Repository, commit *object.Commit) *CommitScanner {
  22. cs := &CommitScanner{
  23. cfg: cfg,
  24. opts: opts,
  25. repo: repo,
  26. commit: commit,
  27. repoName: getRepoName(opts),
  28. }
  29. return cs
  30. }
  31. // SetRepoName sets the repo name of the scanner.
  32. func (cs *CommitScanner) SetRepoName(repoName string) {
  33. cs.repoName = repoName
  34. }
  35. // Scan kicks off a CommitScanner Scan
  36. func (cs *CommitScanner) Scan() (Report, error) {
  37. var scannerReport Report
  38. if cs.cfg.Allowlist.CommitAllowed(cs.commit.Hash.String()) {
  39. return scannerReport, nil
  40. }
  41. if len(cs.commit.ParentHashes) == 0 {
  42. facScanner := NewFilesAtCommitScanner(cs.opts, cs.cfg, cs.repo, cs.commit)
  43. return facScanner.Scan()
  44. }
  45. parent, err := cs.commit.Parent(0)
  46. if err != nil {
  47. return scannerReport, err
  48. }
  49. if parent == nil {
  50. return scannerReport, nil
  51. }
  52. patch, err := parent.Patch(cs.commit)
  53. if err != nil {
  54. return scannerReport, fmt.Errorf("could not generate Patch")
  55. }
  56. patchContent := patch.String()
  57. for _, f := range patch.FilePatches() {
  58. if f.IsBinary() {
  59. continue
  60. }
  61. for _, chunk := range f.Chunks() {
  62. if chunk.Type() == fdiff.Add {
  63. _, to := f.Files()
  64. if cs.cfg.Allowlist.FileAllowed(filepath.Base(to.Path())) ||
  65. cs.cfg.Allowlist.PathAllowed(to.Path()) {
  66. continue
  67. }
  68. // Check individual file path ONLY rules
  69. for _, rule := range cs.cfg.Rules {
  70. if rule.CommitAllowed(cs.commit.Hash.String()) {
  71. continue
  72. }
  73. if rule.HasFileOrPathLeakOnly(to.Path()) {
  74. leak := NewLeak("", "Filename or path offender: "+to.Path(), defaultLineNumber).WithCommit(cs.commit)
  75. leak.Repo = cs.repoName
  76. leak.File = to.Path()
  77. leak.RepoURL = cs.opts.RepoURL
  78. leak.LeakURL = leak.URL()
  79. leak.Rule = rule.Description
  80. leak.Tags = strings.Join(rule.Tags, ", ")
  81. if cs.opts.Verbose {
  82. leak.Log(cs.opts.Redact)
  83. }
  84. scannerReport.Leaks = append(scannerReport.Leaks, leak)
  85. continue
  86. }
  87. }
  88. lineLookup := make(map[string]bool)
  89. // Check the actual content
  90. for _, line := range strings.Split(chunk.Content(), "\n") {
  91. for _, rule := range cs.cfg.Rules {
  92. if rule.AllowList.FileAllowed(filepath.Base(to.Path())) ||
  93. rule.AllowList.PathAllowed(to.Path()) ||
  94. rule.AllowList.CommitAllowed(cs.commit.Hash.String()) {
  95. continue
  96. }
  97. offender := rule.Inspect(line)
  98. if offender == "" {
  99. continue
  100. }
  101. if cs.cfg.Allowlist.RegexAllowed(line) {
  102. continue
  103. }
  104. if rule.File.String() != "" && !rule.HasFileLeak(filepath.Base(to.Path())) {
  105. continue
  106. }
  107. if rule.Path.String() != "" && !rule.HasFilePathLeak(to.Path()) {
  108. continue
  109. }
  110. leak := NewLeak(line, offender, defaultLineNumber).WithCommit(cs.commit)
  111. leak.File = to.Path()
  112. leak.LineNumber = extractLine(patchContent, leak, lineLookup)
  113. leak.RepoURL = cs.opts.RepoURL
  114. leak.Repo = cs.repoName
  115. leak.LeakURL = leak.URL()
  116. leak.Rule = rule.Description
  117. leak.Tags = strings.Join(rule.Tags, ", ")
  118. if cs.opts.Verbose {
  119. leak.Log(cs.opts.Redact)
  120. }
  121. scannerReport.Leaks = append(scannerReport.Leaks, leak)
  122. }
  123. }
  124. }
  125. }
  126. }
  127. scannerReport.Commits = 1
  128. return scannerReport, nil
  129. }