commit.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 len(cs.commit.ParentHashes) == 0 {
  39. facScanner := NewFilesAtCommitScanner(cs.opts, cs.cfg, cs.repo, cs.commit)
  40. return facScanner.Scan()
  41. }
  42. parent, err := cs.commit.Parent(0)
  43. if err != nil {
  44. return scannerReport, err
  45. }
  46. if parent == nil {
  47. return scannerReport, nil
  48. }
  49. patch, err := parent.Patch(cs.commit)
  50. if err != nil {
  51. return scannerReport, fmt.Errorf("could not generate Patch")
  52. }
  53. patchContent := patch.String()
  54. for _, f := range patch.FilePatches() {
  55. if f.IsBinary() {
  56. continue
  57. }
  58. for _, chunk := range f.Chunks() {
  59. if chunk.Type() == fdiff.Add {
  60. _, to := f.Files()
  61. if cs.cfg.Allowlist.FileAllowed(filepath.Base(to.Path())) ||
  62. cs.cfg.Allowlist.PathAllowed(to.Path()) {
  63. continue
  64. }
  65. // Check individual file path ONLY rules
  66. for _, rule := range cs.cfg.Rules {
  67. if rule.CommitAllowed(cs.commit.Hash.String()) {
  68. continue
  69. }
  70. if rule.HasFileOrPathLeakOnly(to.Path()) {
  71. leak := NewLeak("", "Filename or path offender: "+to.Path(), defaultLineNumber).WithCommit(cs.commit)
  72. leak.Repo = cs.repoName
  73. leak.File = to.Path()
  74. leak.RepoURL = cs.opts.RepoURL
  75. leak.LeakURL = leak.URL()
  76. leak.Rule = rule.Description
  77. leak.Tags = strings.Join(rule.Tags, ", ")
  78. if cs.opts.Verbose {
  79. leak.Log(cs.opts.Redact)
  80. }
  81. scannerReport.Leaks = append(scannerReport.Leaks, leak)
  82. continue
  83. }
  84. }
  85. lineLookup := make(map[string]bool)
  86. // Check the actual content
  87. for _, line := range strings.Split(chunk.Content(), "\n") {
  88. for _, rule := range cs.cfg.Rules {
  89. if rule.AllowList.FileAllowed(filepath.Base(to.Path())) ||
  90. rule.AllowList.PathAllowed(to.Path()) ||
  91. rule.AllowList.CommitAllowed(cs.commit.Hash.String()) {
  92. continue
  93. }
  94. offender := rule.Inspect(line)
  95. if offender == "" {
  96. continue
  97. }
  98. if cs.cfg.Allowlist.RegexAllowed(line) {
  99. continue
  100. }
  101. if rule.File.String() != "" && !rule.HasFileLeak(filepath.Base(to.Path())) {
  102. continue
  103. }
  104. if rule.Path.String() != "" && !rule.HasFilePathLeak(to.Path()) {
  105. continue
  106. }
  107. leak := NewLeak(line, offender, defaultLineNumber).WithCommit(cs.commit)
  108. leak.File = to.Path()
  109. leak.LineNumber = extractLine(patchContent, leak, lineLookup)
  110. leak.RepoURL = cs.opts.RepoURL
  111. leak.Repo = cs.repoName
  112. leak.LeakURL = leak.URL()
  113. leak.Rule = rule.Description
  114. leak.Tags = strings.Join(rule.Tags, ", ")
  115. if cs.opts.Verbose {
  116. leak.Log(cs.opts.Redact)
  117. }
  118. scannerReport.Leaks = append(scannerReport.Leaks, leak)
  119. }
  120. }
  121. }
  122. }
  123. }
  124. scannerReport.Commits = 1
  125. return scannerReport, nil
  126. }