detect_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. Match: "AKIALALEMEL33243OLIA",
  30. File: "tmp.go",
  31. RuleID: "aws-access-key",
  32. Tags: []string{"key", "AWS"},
  33. },
  34. },
  35. },
  36. {
  37. cfgName: "allow_aws_re",
  38. bytes: []byte(`awsToken := \"AKIALALEMEL33243OLIA\"`),
  39. filePath: "tmp.go",
  40. expectedFindings: []report.Finding{},
  41. },
  42. {
  43. cfgName: "allow_path",
  44. bytes: []byte(`awsToken := \"AKIALALEMEL33243OLIA\"`),
  45. filePath: "tmp.go",
  46. expectedFindings: []report.Finding{},
  47. },
  48. {
  49. cfgName: "allow_commit",
  50. bytes: []byte(`awsToken := \"AKIALALEMEL33243OLIA\"`),
  51. filePath: "tmp.go",
  52. expectedFindings: []report.Finding{},
  53. commit: "allowthiscommit",
  54. },
  55. {
  56. cfgName: "entropy_group",
  57. bytes: []byte(`const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`),
  58. filePath: "tmp.go",
  59. expectedFindings: []report.Finding{
  60. {
  61. Description: "Discord API key",
  62. Match: "Discord_Public_Key = \"e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5\"",
  63. Secret: "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5",
  64. File: "tmp.go",
  65. RuleID: "discord-api-key",
  66. Tags: []string{},
  67. Entropy: 3.7906237,
  68. },
  69. },
  70. },
  71. {
  72. cfgName: "generic_with_py_path",
  73. bytes: []byte(`const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`),
  74. filePath: "tmp.go",
  75. expectedFindings: []report.Finding{},
  76. },
  77. {
  78. cfgName: "generic_with_py_path",
  79. bytes: []byte(`const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`),
  80. filePath: "tmp.py",
  81. expectedFindings: []report.Finding{
  82. {
  83. Description: "Generic API Key",
  84. Match: "Key = \"e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5\"",
  85. Secret: "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5",
  86. File: "tmp.py",
  87. RuleID: "generic-api-key",
  88. Tags: []string{},
  89. Entropy: 3.7906237,
  90. },
  91. },
  92. },
  93. {
  94. cfgName: "path_only",
  95. bytes: []byte(`const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`),
  96. filePath: "tmp.py",
  97. expectedFindings: []report.Finding{
  98. {
  99. Description: "Python Files",
  100. Match: "file detected: tmp.py",
  101. File: "tmp.py",
  102. RuleID: "python-files-only",
  103. Tags: []string{},
  104. },
  105. },
  106. },
  107. {
  108. cfgName: "bad_entropy_group",
  109. bytes: []byte(`const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`),
  110. filePath: "tmp.go",
  111. expectedFindings: []report.Finding{},
  112. wantError: fmt.Errorf("Discord API key invalid regex secret group 5, max regex secret group 3"),
  113. },
  114. {
  115. cfgName: "simple",
  116. bytes: []byte(`awsToken := \"AKIALALEMEL33243OLIA\"`),
  117. filePath: filepath.Join(configPath, "simple.toml"),
  118. expectedFindings: []report.Finding{},
  119. },
  120. {
  121. cfgName: "allow_global_aws_re",
  122. bytes: []byte(`awsToken := \"AKIALALEMEL33243OLIA\"`),
  123. filePath: "tmp.go",
  124. expectedFindings: []report.Finding{},
  125. },
  126. }
  127. for _, tt := range tests {
  128. viper.Reset()
  129. viper.AddConfigPath(configPath)
  130. viper.SetConfigName(tt.cfgName)
  131. viper.SetConfigType("toml")
  132. err := viper.ReadInConfig()
  133. if err != nil {
  134. t.Error(err)
  135. }
  136. var vc config.ViperConfig
  137. viper.Unmarshal(&vc)
  138. cfg, err := vc.Translate()
  139. cfg.Path = filepath.Join(configPath, tt.cfgName+".toml")
  140. if tt.wantError != nil {
  141. if err == nil {
  142. t.Errorf("expected error")
  143. }
  144. assert.Equal(t, tt.wantError, err)
  145. }
  146. findings := DetectFindings(cfg, tt.bytes, tt.filePath, tt.commit)
  147. assert.ElementsMatch(t, tt.expectedFindings, findings)
  148. }
  149. }