config_test.go 7.7 KB

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