git_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package detect
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "testing"
  7. "github.com/spf13/viper"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/zricethezav/gitleaks/v8/config"
  10. "github.com/zricethezav/gitleaks/v8/git"
  11. "github.com/zricethezav/gitleaks/v8/report"
  12. )
  13. const repoBasePath = "../testdata/repos/"
  14. const expectPath = "../testdata/expected/"
  15. const configPath = "../testdata/config/"
  16. // TestFromGit tests the FromGit function
  17. func TestFromGit(t *testing.T) {
  18. tests := []struct {
  19. cfgName string
  20. opts Options
  21. source string
  22. logOpts string
  23. expected string
  24. expectedFindings []*report.Finding
  25. }{
  26. {
  27. source: filepath.Join(repoBasePath, "small"),
  28. expected: filepath.Join(expectPath, "git", "small.txt"),
  29. cfgName: "simple",
  30. expectedFindings: []*report.Finding{
  31. {
  32. Description: "AWS Access Key",
  33. StartLine: 20,
  34. EndLine: 20,
  35. StartColumn: 19,
  36. EndColumn: 38,
  37. Secret: "AKIALALEMEL33243OLIA",
  38. File: "main.go",
  39. // Line: "\tawsToken := \"AKIALALEMEL33243OLIA\"",
  40. Commit: "1b6da43b82b22e4eaa10bcf8ee591e91abbfc587",
  41. Author: "Zachary Rice",
  42. Email: "zricer@protonmail.com",
  43. Message: "Accidentally add a secret",
  44. RuleID: "aws-access-key",
  45. Tags: []string{"key", "AWS"},
  46. },
  47. {
  48. Description: "AWS Access Key",
  49. StartLine: 9,
  50. EndLine: 9,
  51. StartColumn: 17,
  52. EndColumn: 36,
  53. Secret: "AKIALALEMEL33243OLIA",
  54. File: "foo/foo.go",
  55. // Line: "\taws_token := \"AKIALALEMEL33243OLIA\"",
  56. Commit: "491504d5a31946ce75e22554cc34203d8e5ff3ca",
  57. Author: "Zach Rice",
  58. Email: "zricer@protonmail.com",
  59. Message: "adding foo package with secret",
  60. RuleID: "aws-access-key",
  61. Tags: []string{"key", "AWS"},
  62. },
  63. },
  64. },
  65. {
  66. source: filepath.Join(repoBasePath, "small"),
  67. expected: filepath.Join(expectPath, "git", "small-branch-foo.txt"),
  68. logOpts: "--all foo...",
  69. cfgName: "simple",
  70. expectedFindings: []*report.Finding{
  71. {
  72. Description: "AWS Access Key",
  73. StartLine: 9,
  74. EndLine: 9,
  75. StartColumn: 17,
  76. EndColumn: 36,
  77. Secret: "AKIALALEMEL33243OLIA",
  78. // Line: "\taws_token := \"AKIALALEMEL33243OLIA\"",
  79. File: "foo/foo.go",
  80. Commit: "491504d5a31946ce75e22554cc34203d8e5ff3ca",
  81. Author: "Zach Rice",
  82. Email: "zricer@protonmail.com",
  83. Message: "adding foo package with secret",
  84. RuleID: "aws-access-key",
  85. Tags: []string{"key", "AWS"},
  86. },
  87. },
  88. },
  89. }
  90. err := moveDotGit("dotGit", ".git")
  91. if err != nil {
  92. t.Fatal(err)
  93. }
  94. defer moveDotGit(".git", "dotGit")
  95. for _, tt := range tests {
  96. files, err := git.GitLog(tt.source, tt.logOpts)
  97. if err != nil {
  98. t.Error(err)
  99. }
  100. viper.AddConfigPath(configPath)
  101. viper.SetConfigName("simple")
  102. viper.SetConfigType("toml")
  103. err = viper.ReadInConfig()
  104. if err != nil {
  105. t.Error(err)
  106. }
  107. var vc config.ViperConfig
  108. viper.Unmarshal(&vc)
  109. cfg, _ := vc.Translate()
  110. findings := FromGit(files, cfg, tt.opts)
  111. for _, f := range findings {
  112. f.Context = "" // remove lines cause copying and pasting them has some wack formatting
  113. f.Date = ""
  114. }
  115. assert.ElementsMatch(t, tt.expectedFindings, findings)
  116. }
  117. }
  118. func moveDotGit(from, to string) error {
  119. repoDirs, err := os.ReadDir("../testdata/repos")
  120. if err != nil {
  121. return err
  122. }
  123. for _, dir := range repoDirs {
  124. if to == ".git" {
  125. _, err := os.Stat(fmt.Sprintf("%s/%s/%s", repoBasePath, dir.Name(), "dotGit"))
  126. if os.IsNotExist(err) {
  127. // dont want to delete the only copy of .git accidentally
  128. continue
  129. }
  130. os.RemoveAll(fmt.Sprintf("%s/%s/%s", repoBasePath, dir.Name(), ".git"))
  131. }
  132. if !dir.IsDir() {
  133. continue
  134. }
  135. _, err := os.Stat(fmt.Sprintf("%s/%s/%s", repoBasePath, dir.Name(), from))
  136. if os.IsNotExist(err) {
  137. continue
  138. }
  139. err = os.Rename(fmt.Sprintf("%s/%s/%s", repoBasePath, dir.Name(), from),
  140. fmt.Sprintf("%s/%s/%s", repoBasePath, dir.Name(), to))
  141. if err != nil {
  142. return err
  143. }
  144. }
  145. return nil
  146. }