commit.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. leak.Log(cs.opts)
  82. scannerReport.Leaks = append(scannerReport.Leaks, leak)
  83. continue
  84. }
  85. }
  86. lineLookup := make(map[string]bool)
  87. // Check the actual content
  88. for _, line := range strings.Split(chunk.Content(), "\n") {
  89. for _, rule := range cs.cfg.Rules {
  90. if rule.AllowList.FileAllowed(filepath.Base(to.Path())) ||
  91. rule.AllowList.PathAllowed(to.Path()) ||
  92. rule.AllowList.CommitAllowed(cs.commit.Hash.String()) {
  93. continue
  94. }
  95. offender := rule.Inspect(line)
  96. if offender == "" {
  97. continue
  98. }
  99. if cs.cfg.Allowlist.RegexAllowed(line) {
  100. continue
  101. }
  102. if rule.File.String() != "" && !rule.HasFileLeak(filepath.Base(to.Path())) {
  103. continue
  104. }
  105. if rule.Path.String() != "" && !rule.HasFilePathLeak(to.Path()) {
  106. continue
  107. }
  108. leak := NewLeak(line, offender, defaultLineNumber).WithCommit(cs.commit)
  109. leak.File = to.Path()
  110. leak.LineNumber = extractLine(patchContent, leak, lineLookup)
  111. leak.RepoURL = cs.opts.RepoURL
  112. leak.Repo = cs.repoName
  113. leak.LeakURL = leak.URL()
  114. leak.Rule = rule.Description
  115. leak.Tags = strings.Join(rule.Tags, ", ")
  116. leak.Log(cs.opts)
  117. scannerReport.Leaks = append(scannerReport.Leaks, leak)
  118. }
  119. }
  120. }
  121. }
  122. }
  123. scannerReport.Commits = 1
  124. return scannerReport, nil
  125. }