config_test.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. package config_test
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "regexp"
  8. "testing"
  9. "github.com/zricethezav/gitleaks/v7/config"
  10. "github.com/zricethezav/gitleaks/v7/options"
  11. )
  12. const configPath = "../testdata/configs/"
  13. func TestParse(t *testing.T) {
  14. tests := []struct {
  15. description string
  16. opts options.Options
  17. wantErr error
  18. wantFileRegex *regexp.Regexp
  19. wantMessages *regexp.Regexp
  20. wantAllowlist config.AllowList
  21. }{
  22. {
  23. description: "default config",
  24. opts: options.Options{},
  25. },
  26. {
  27. description: "test successful load",
  28. opts: options.Options{
  29. ConfigPath: filepath.Join(configPath, "aws_key.toml"),
  30. },
  31. },
  32. {
  33. description: "test bad toml",
  34. opts: options.Options{
  35. ConfigPath: filepath.Join(configPath, "bad_aws_key.toml"),
  36. },
  37. wantErr: fmt.Errorf("Near line 7 (last key parsed 'rules.description'): expected value but found \"AWS\" instead"),
  38. },
  39. {
  40. description: "test bad regex",
  41. opts: options.Options{
  42. ConfigPath: filepath.Join(configPath, "bad_regex_aws_key.toml"),
  43. },
  44. wantErr: fmt.Errorf("problem loading config: error parsing regexp: invalid nested repetition operator: `???`"),
  45. },
  46. {
  47. description: "test bad global allowlist file regex",
  48. opts: options.Options{
  49. ConfigPath: filepath.Join(configPath, "bad_aws_key_global_allowlist_file.toml"),
  50. },
  51. wantErr: fmt.Errorf("problem loading config: error parsing regexp: missing argument to repetition operator: `??`"),
  52. },
  53. {
  54. description: "test bad global file regex",
  55. opts: options.Options{
  56. ConfigPath: filepath.Join(configPath, "bad_aws_key_file_regex.toml"),
  57. },
  58. wantErr: fmt.Errorf("problem loading config: error parsing regexp: missing argument to repetition operator: `??`"),
  59. },
  60. {
  61. description: "test successful load big ol thing",
  62. opts: options.Options{
  63. ConfigPath: filepath.Join(configPath, "large.toml"),
  64. },
  65. },
  66. {
  67. description: "test load entropy",
  68. opts: options.Options{
  69. ConfigPath: filepath.Join(configPath, "entropy.toml"),
  70. },
  71. },
  72. {
  73. description: "test entropy bad range",
  74. opts: options.Options{
  75. ConfigPath: filepath.Join(configPath, "bad_entropy_1.toml"),
  76. },
  77. wantErr: fmt.Errorf("problem loading config: entropy Min value cannot be higher than Max value"),
  78. },
  79. {
  80. description: "test entropy value max",
  81. opts: options.Options{
  82. ConfigPath: filepath.Join(configPath, "bad_entropy_2.toml"),
  83. },
  84. wantErr: fmt.Errorf("strconv.ParseFloat: parsing \"x\": invalid syntax"),
  85. },
  86. {
  87. description: "test entropy value min",
  88. opts: options.Options{
  89. ConfigPath: filepath.Join(configPath, "bad_entropy_3.toml"),
  90. },
  91. wantErr: fmt.Errorf("strconv.ParseFloat: parsing \"x\": invalid syntax"),
  92. },
  93. {
  94. description: "test entropy value group",
  95. opts: options.Options{
  96. ConfigPath: filepath.Join(configPath, "bad_entropy_4.toml"),
  97. },
  98. wantErr: fmt.Errorf("strconv.ParseInt: parsing \"x\": invalid syntax"),
  99. },
  100. {
  101. description: "test entropy value group",
  102. opts: options.Options{
  103. ConfigPath: filepath.Join(configPath, "bad_entropy_5.toml"),
  104. },
  105. wantErr: fmt.Errorf("problem loading config: group cannot be lower than 0"),
  106. },
  107. {
  108. description: "test entropy value group",
  109. opts: options.Options{
  110. ConfigPath: filepath.Join(configPath, "bad_entropy_6.toml"),
  111. },
  112. wantErr: fmt.Errorf("problem loading config: group cannot be higher than number of groups in regexp"),
  113. },
  114. {
  115. description: "test entropy range limits",
  116. opts: options.Options{
  117. ConfigPath: filepath.Join(configPath, "bad_entropy_7.toml"),
  118. },
  119. wantErr: fmt.Errorf("problem loading config: invalid entropy ranges, must be within 0.0-8.0"),
  120. },
  121. }
  122. for _, test := range tests {
  123. _, err := config.NewConfig(test.opts)
  124. if err != nil {
  125. if test.wantErr == nil {
  126. t.Error(test.description, err)
  127. } else if test.wantErr.Error() != err.Error() {
  128. t.Errorf("expected err: %s, got %s", test.wantErr, err)
  129. }
  130. }
  131. }
  132. }
  133. // TestParseFields will test that fields are properly parsed from a config. As fields are added, then please
  134. // add tests here.
  135. func TestParseFields(t *testing.T) {
  136. tomlConfig := `
  137. [[rules]]
  138. description = "Some Groups without a reportGroup"
  139. regex = '(.)(.)'
  140. [[rules]]
  141. description = "Some Groups"
  142. regex = '(.)(.)'
  143. reportGroup = 1
  144. `
  145. configPath, err := writeTestConfig(tomlConfig)
  146. defer os.Remove(configPath)
  147. if err != nil {
  148. t.Fatal(err)
  149. }
  150. config, err := config.NewConfig(options.Options{ConfigPath: configPath})
  151. if err != nil {
  152. t.Fatalf("Couldn't parse config: %v", err)
  153. }
  154. expectedRuleFields := []struct {
  155. Description string
  156. ReportGroup int
  157. }{
  158. {
  159. Description: "Some Groups without a reportGroup",
  160. ReportGroup: 0,
  161. },
  162. {
  163. Description: "Some Groups",
  164. ReportGroup: 1,
  165. },
  166. }
  167. if len(config.Rules) != len(expectedRuleFields) {
  168. t.Fatalf("expected %v rules", len(expectedRuleFields))
  169. }
  170. for _, expected := range expectedRuleFields {
  171. rule, err := findRuleByDescription(config.Rules, expected.Description)
  172. if err != nil {
  173. t.Fatal(err)
  174. }
  175. if rule.ReportGroup != expected.ReportGroup {
  176. t.Errorf("expected the rule with description '%v' to have a ReportGroup of %v", expected.Description, expected.ReportGroup)
  177. }
  178. }
  179. }
  180. func findRuleByDescription(rules []config.Rule, description string) (*config.Rule, error) {
  181. for _, rule := range rules {
  182. if rule.Description == description {
  183. return &rule, nil
  184. }
  185. }
  186. return nil, fmt.Errorf("Couldn't find rule with the description: %s", description)
  187. }
  188. func writeTestConfig(toml string) (string, error) {
  189. tmpfile, err := ioutil.TempFile("", "testConfig")
  190. if err != nil {
  191. return "", fmt.Errorf("Couldn't create test config got: %w", err)
  192. }
  193. if _, err := tmpfile.Write([]byte(toml)); err != nil {
  194. return "", fmt.Errorf("Couldn't create test config got: %w", err)
  195. }
  196. if err := tmpfile.Close(); err != nil {
  197. return "", fmt.Errorf("Couldn't create test config got: %w", err)
  198. }
  199. return tmpfile.Name(), nil
  200. }
  201. func TestAppendingConfiguration(t *testing.T) {
  202. testRegexA, _ := regexp.Compile("a")
  203. testRegexB, _ := regexp.Compile("b")
  204. allowListA := config.AllowList{
  205. Description: "Test Description",
  206. Commits: []string{"a"},
  207. Files: []*regexp.Regexp{testRegexA},
  208. Paths: []*regexp.Regexp{testRegexA},
  209. Regexes: []*regexp.Regexp{testRegexA},
  210. Repos: []*regexp.Regexp{testRegexA},
  211. }
  212. allowListB := config.AllowList{
  213. Description: "Test Description",
  214. Commits: []string{"b"},
  215. Files: []*regexp.Regexp{testRegexB},
  216. Paths: []*regexp.Regexp{testRegexB},
  217. Regexes: []*regexp.Regexp{testRegexB},
  218. Repos: []*regexp.Regexp{testRegexB},
  219. }
  220. ruleA := config.Rule{Description: "a"}
  221. ruleB := config.Rule{Description: "b"}
  222. rulesA := []config.Rule{ruleA}
  223. rulesB := []config.Rule{ruleB}
  224. cfgA := config.Config{
  225. Rules: rulesA,
  226. Allowlist: allowListA,
  227. }
  228. cfgB := config.Config{
  229. Rules: rulesB,
  230. Allowlist: allowListB,
  231. }
  232. cfgAppended := cfgA.AppendConfig(cfgB)
  233. if !(len(cfgAppended.Rules) == 2) {
  234. t.Errorf("Length of Appended Rules = %d; want 2", len(cfgAppended.Rules))
  235. }
  236. if !(len(cfgAppended.Allowlist.Commits) == 2) {
  237. t.Errorf("Length of Appended Allowed Commits = %d; want 2", len(cfgAppended.Allowlist.Commits))
  238. }
  239. if !(len(cfgAppended.Allowlist.Files) == 2) {
  240. t.Errorf("Length of Appended Allowed Files = %d; want 2", len(cfgAppended.Allowlist.Files))
  241. }
  242. if !(len(cfgAppended.Allowlist.Paths) == 2) {
  243. t.Errorf("Length of Appended Allowed Paths = %d; want 2", len(cfgAppended.Allowlist.Paths))
  244. }
  245. if !(len(cfgAppended.Allowlist.Regexes) == 2) {
  246. t.Errorf("Length of Appended Allowed Regexes = %d; want 2", len(cfgAppended.Allowlist.Regexes))
  247. }
  248. if !(len(cfgAppended.Allowlist.Repos) == 2) {
  249. t.Errorf("Length of Appended Allowed Repos = %d; want 2", len(cfgAppended.Allowlist.Repos))
  250. }
  251. if cfgAppended.Allowlist.Description != "Appended Configuration" {
  252. t.Errorf("Allow List Description is = \"%s\"; want \"Appended Configuration\"", cfgAppended.Allowlist.Description)
  253. }
  254. }