detect_test.go 4.0 KB

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