commit.go 4.0 KB

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