detect_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package detect
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "testing"
  6. "github.com/spf13/viper"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/zricethezav/gitleaks/v8/config"
  9. "github.com/zricethezav/gitleaks/v8/report"
  10. )
  11. func TestDetectFindings(t *testing.T) {
  12. tests := []struct {
  13. cfgName string
  14. opts Options
  15. filePath string
  16. bytes []byte
  17. commit string
  18. expectedFindings []report.Finding
  19. wantError error
  20. }{
  21. {
  22. cfgName: "simple",
  23. bytes: []byte(`awsToken := \"AKIALALEMEL33243OLIA\"`),
  24. filePath: "tmp.go",
  25. expectedFindings: []report.Finding{
  26. {
  27. Description: "AWS Access Key",
  28. Secret: "AKIALALEMEL33243OLIA",
  29. File: "tmp.go",
  30. RuleID: "aws-access-key",
  31. Tags: []string{"key", "AWS"},
  32. },
  33. },
  34. },
  35. {
  36. cfgName: "allow_aws_re",
  37. bytes: []byte(`awsToken := \"AKIALALEMEL33243OLIA\"`),
  38. filePath: "tmp.go",
  39. expectedFindings: []report.Finding{},
  40. },
  41. {
  42. cfgName: "allow_path",
  43. bytes: []byte(`awsToken := \"AKIALALEMEL33243OLIA\"`),
  44. filePath: "tmp.go",
  45. expectedFindings: []report.Finding{},
  46. },
  47. {
  48. cfgName: "allow_commit",
  49. bytes: []byte(`awsToken := \"AKIALALEMEL33243OLIA\"`),
  50. filePath: "tmp.go",
  51. expectedFindings: []report.Finding{},
  52. commit: "allowthiscommit",
  53. },
  54. {
  55. cfgName: "entropy_group",
  56. bytes: []byte(`const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`),
  57. filePath: "tmp.go",
  58. expectedFindings: []report.Finding{
  59. {
  60. Description: "Discord API key",
  61. Secret: "Discord_Public_Key = \"e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5\"",
  62. File: "tmp.go",
  63. RuleID: "discord-api-key",
  64. Tags: []string{},
  65. Entropy: 3.7906237,
  66. },
  67. },
  68. },
  69. {
  70. cfgName: "generic_with_py_path",
  71. bytes: []byte(`const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`),
  72. filePath: "tmp.go",
  73. expectedFindings: []report.Finding{},
  74. },
  75. {
  76. cfgName: "generic_with_py_path",
  77. bytes: []byte(`const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`),
  78. filePath: "tmp.py",
  79. expectedFindings: []report.Finding{
  80. {
  81. Description: "Generic API Key",
  82. Secret: "Key = \"e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5\"",
  83. File: "tmp.py",
  84. RuleID: "generic-api-key",
  85. Tags: []string{},
  86. Entropy: 3.7906237,
  87. },
  88. },
  89. },
  90. {
  91. cfgName: "path_only",
  92. bytes: []byte(`const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`),
  93. filePath: "tmp.py",
  94. expectedFindings: []report.Finding{
  95. {
  96. Description: "Python Files",
  97. Context: "file detected: tmp.py",
  98. File: "tmp.py",
  99. RuleID: "python-files-only",
  100. Tags: []string{},
  101. },
  102. },
  103. },
  104. {
  105. cfgName: "bad_entropy_group",
  106. bytes: []byte(`const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`),
  107. filePath: "tmp.go",
  108. expectedFindings: []report.Finding{},
  109. wantError: fmt.Errorf("Discord API key invalid regex entropy group 5, max regex entropy group 3"),
  110. },
  111. {
  112. cfgName: "simple",
  113. bytes: []byte(`awsToken := \"AKIALALEMEL33243OLIA\"`),
  114. filePath: filepath.Join(configPath, "simple.toml"),
  115. expectedFindings: []report.Finding{},
  116. },
  117. }
  118. for _, tt := range tests {
  119. viper.Reset()
  120. viper.AddConfigPath(configPath)
  121. viper.SetConfigName(tt.cfgName)
  122. viper.SetConfigType("toml")
  123. err := viper.ReadInConfig()
  124. if err != nil {
  125. t.Error(err)
  126. }
  127. var vc config.ViperConfig
  128. viper.Unmarshal(&vc)
  129. cfg, err := vc.Translate()
  130. cfg.Path = filepath.Join(configPath, tt.cfgName+".toml")
  131. if tt.wantError != nil {
  132. if err == nil {
  133. t.Errorf("expected error")
  134. }
  135. assert.Equal(t, tt.wantError, err)
  136. }
  137. findings := DetectFindings(cfg, tt.bytes, tt.filePath, tt.commit)
  138. for _, f := range findings {
  139. f.Context = "" // remove lines cause copying and pasting them has some wack formatting
  140. f.Date = ""
  141. }
  142. assert.ElementsMatch(t, tt.expectedFindings, findings)
  143. }
  144. }