config_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. package config
  2. import (
  3. "fmt"
  4. "github.com/google/go-cmp/cmp/cmpopts"
  5. "testing"
  6. "github.com/google/go-cmp/cmp"
  7. "github.com/spf13/viper"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. "github.com/zricethezav/gitleaks/v8/regexp"
  11. )
  12. const configPath = "../testdata/config/"
  13. func TestTranslate(t *testing.T) {
  14. tests := []struct {
  15. // Configuration file basename to load, from `../testdata/config/`.
  16. cfgName string
  17. // Expected result.
  18. cfg Config
  19. // Rules to compare.
  20. rules []string
  21. // Error to expect.
  22. wantError error
  23. }{
  24. {
  25. cfgName: "allowlist_old_compat",
  26. cfg: Config{
  27. Rules: map[string]Rule{"example": {
  28. RuleID: "example",
  29. Regex: regexp.MustCompile(`example\d+`),
  30. Tags: []string{},
  31. Keywords: []string{},
  32. Allowlists: []*Allowlist{
  33. {
  34. MatchCondition: AllowlistMatchOr,
  35. Regexes: []*regexp.Regexp{regexp.MustCompile("123")},
  36. },
  37. },
  38. }},
  39. },
  40. },
  41. {
  42. cfgName: "allowlist_invalid_empty",
  43. cfg: Config{},
  44. wantError: fmt.Errorf("example: [[rules.allowlists]] must contain at least one check for: commits, paths, regexes, or stopwords"),
  45. },
  46. {
  47. cfgName: "allowlist_invalid_old_and_new",
  48. cfg: Config{},
  49. wantError: fmt.Errorf("example: [rules.allowlist] is deprecated, it cannot be used alongside [[rules.allowlist]]"),
  50. },
  51. {
  52. cfgName: "allowlist_invalid_regextarget",
  53. cfg: Config{},
  54. wantError: fmt.Errorf("example: unknown allowlist |regexTarget| 'mtach' (expected 'match', 'line')"),
  55. },
  56. {
  57. cfgName: "allow_aws_re",
  58. cfg: Config{
  59. Rules: map[string]Rule{"aws-access-key": {
  60. RuleID: "aws-access-key",
  61. Description: "AWS Access Key",
  62. Regex: regexp.MustCompile("(?:A3T[A-Z0-9]|AKIA|ASIA|ABIA|ACCA)[A-Z0-9]{16}"),
  63. Keywords: []string{},
  64. Tags: []string{"key", "AWS"},
  65. Allowlists: []*Allowlist{
  66. {
  67. MatchCondition: AllowlistMatchOr,
  68. Regexes: []*regexp.Regexp{regexp.MustCompile("AKIALALEMEL33243OLIA")},
  69. },
  70. },
  71. }},
  72. },
  73. },
  74. {
  75. cfgName: "allow_commit",
  76. cfg: Config{
  77. Rules: map[string]Rule{"aws-access-key": {
  78. RuleID: "aws-access-key",
  79. Description: "AWS Access Key",
  80. Regex: regexp.MustCompile("(?:A3T[A-Z0-9]|AKIA|ASIA|ABIA|ACCA)[A-Z0-9]{16}"),
  81. Keywords: []string{},
  82. Tags: []string{"key", "AWS"},
  83. Allowlists: []*Allowlist{
  84. {
  85. MatchCondition: AllowlistMatchOr,
  86. Commits: []string{"allowthiscommit"},
  87. },
  88. },
  89. }},
  90. },
  91. },
  92. {
  93. cfgName: "allow_path",
  94. cfg: Config{
  95. Rules: map[string]Rule{"aws-access-key": {
  96. RuleID: "aws-access-key",
  97. Description: "AWS Access Key",
  98. Regex: regexp.MustCompile("(?:A3T[A-Z0-9]|AKIA|ASIA|ABIA|ACCA)[A-Z0-9]{16}"),
  99. Keywords: []string{},
  100. Tags: []string{"key", "AWS"},
  101. Allowlists: []*Allowlist{
  102. {
  103. MatchCondition: AllowlistMatchOr,
  104. Paths: []*regexp.Regexp{regexp.MustCompile(".go")},
  105. },
  106. },
  107. }},
  108. },
  109. },
  110. {
  111. cfgName: "entropy_group",
  112. cfg: Config{
  113. Rules: map[string]Rule{"discord-api-key": {
  114. RuleID: "discord-api-key",
  115. Description: "Discord API key",
  116. Regex: regexp.MustCompile(`(?i)(discord[a-z0-9_ .\-,]{0,25})(=|>|:=|\|\|:|<=|=>|:).{0,5}['\"]([a-h0-9]{64})['\"]`),
  117. Entropy: 3.5,
  118. SecretGroup: 3,
  119. Keywords: []string{},
  120. Tags: []string{},
  121. }},
  122. },
  123. },
  124. {
  125. cfgName: "missing_id",
  126. cfg: Config{},
  127. wantError: fmt.Errorf("rule |id| is missing or empty, regex: (?i)(discord[a-z0-9_ .\\-,]{0,25})(=|>|:=|\\|\\|:|<=|=>|:).{0,5}['\\\"]([a-h0-9]{64})['\\\"]"),
  128. },
  129. {
  130. cfgName: "no_regex_or_path",
  131. cfg: Config{},
  132. wantError: fmt.Errorf("discord-api-key: both |regex| and |path| are empty, this rule will have no effect"),
  133. },
  134. {
  135. cfgName: "bad_entropy_group",
  136. cfg: Config{},
  137. wantError: fmt.Errorf("discord-api-key: invalid regex secret group 5, max regex secret group 3"),
  138. },
  139. {
  140. cfgName: "base",
  141. cfg: Config{
  142. Rules: map[string]Rule{
  143. "aws-access-key": {
  144. RuleID: "aws-access-key",
  145. Description: "AWS Access Key",
  146. Regex: regexp.MustCompile("(?:A3T[A-Z0-9]|AKIA|ASIA|ABIA|ACCA)[A-Z0-9]{16}"),
  147. Keywords: []string{},
  148. Tags: []string{"key", "AWS"},
  149. },
  150. "aws-secret-key": {
  151. RuleID: "aws-secret-key",
  152. Description: "AWS Secret Key",
  153. Regex: regexp.MustCompile(`(?i)aws_(.{0,20})?=?.[\'\"0-9a-zA-Z\/+]{40}`),
  154. Keywords: []string{},
  155. Tags: []string{"key", "AWS"},
  156. },
  157. "aws-secret-key-again": {
  158. RuleID: "aws-secret-key-again",
  159. Description: "AWS Secret Key",
  160. Regex: regexp.MustCompile(`(?i)aws_(.{0,20})?=?.[\'\"0-9a-zA-Z\/+]{40}`),
  161. Keywords: []string{},
  162. Tags: []string{"key", "AWS"},
  163. },
  164. },
  165. },
  166. },
  167. {
  168. cfgName: "extend_rule_allowlist_or",
  169. cfg: Config{
  170. Rules: map[string]Rule{
  171. "aws-secret-key-again-again": {
  172. RuleID: "aws-secret-key-again-again",
  173. Description: "AWS Secret Key",
  174. Regex: regexp.MustCompile(`(?i)aws_(.{0,20})?=?.[\'\"0-9a-zA-Z\/+]{40}`),
  175. Keywords: []string{},
  176. Tags: []string{"key", "AWS"},
  177. Allowlists: []*Allowlist{
  178. {
  179. MatchCondition: AllowlistMatchOr,
  180. StopWords: []string{"fake"},
  181. },
  182. {
  183. MatchCondition: AllowlistMatchOr,
  184. Commits: []string{"abcdefg1"},
  185. Paths: []*regexp.Regexp{regexp.MustCompile(`ignore\.xaml`)},
  186. Regexes: []*regexp.Regexp{regexp.MustCompile(`foo.+bar`)},
  187. RegexTarget: "line",
  188. StopWords: []string{"example"},
  189. },
  190. },
  191. },
  192. },
  193. },
  194. },
  195. {
  196. cfgName: "extend_rule_allowlist_and",
  197. cfg: Config{
  198. Rules: map[string]Rule{
  199. "aws-secret-key-again-again": {
  200. RuleID: "aws-secret-key-again-again",
  201. Description: "AWS Secret Key",
  202. Regex: regexp.MustCompile(`(?i)aws_(.{0,20})?=?.[\'\"0-9a-zA-Z\/+]{40}`),
  203. Keywords: []string{},
  204. Tags: []string{"key", "AWS"},
  205. Allowlists: []*Allowlist{
  206. {
  207. MatchCondition: AllowlistMatchOr,
  208. StopWords: []string{"fake"},
  209. },
  210. {
  211. MatchCondition: AllowlistMatchAnd,
  212. Commits: []string{"abcdefg1"},
  213. Paths: []*regexp.Regexp{regexp.MustCompile(`ignore\.xaml`)},
  214. Regexes: []*regexp.Regexp{regexp.MustCompile(`foo.+bar`)},
  215. RegexTarget: "line",
  216. StopWords: []string{"example"},
  217. },
  218. },
  219. },
  220. },
  221. },
  222. },
  223. {
  224. cfgName: "extend_empty_regexpath",
  225. cfg: Config{
  226. Rules: map[string]Rule{
  227. "aws-secret-key-again-again": {
  228. RuleID: "aws-secret-key-again-again",
  229. Description: "AWS Secret Key",
  230. Regex: regexp.MustCompile(`(?i)aws_(.{0,20})?=?.[\'\"0-9a-zA-Z\/+]{40}`),
  231. Keywords: []string{},
  232. Tags: []string{"key", "AWS"},
  233. Allowlists: []*Allowlist{
  234. {
  235. Description: "False positive. Keys used for colors match the rule, and should be excluded.",
  236. MatchCondition: AllowlistMatchOr,
  237. Paths: []*regexp.Regexp{regexp.MustCompile(`something.py`)},
  238. },
  239. },
  240. },
  241. },
  242. },
  243. },
  244. {
  245. cfgName: "override_description",
  246. rules: []string{"aws-access-key"},
  247. cfg: Config{
  248. Rules: map[string]Rule{"aws-access-key": {
  249. RuleID: "aws-access-key",
  250. Description: "Puppy Doggy",
  251. Regex: regexp.MustCompile("(?:A3T[A-Z0-9]|AKIA|ASIA|ABIA|ACCA)[A-Z0-9]{16}"),
  252. Keywords: []string{},
  253. Tags: []string{"key", "AWS"},
  254. },
  255. },
  256. },
  257. },
  258. {
  259. cfgName: "override_entropy",
  260. rules: []string{"aws-access-key"},
  261. cfg: Config{
  262. Rules: map[string]Rule{"aws-access-key": {
  263. RuleID: "aws-access-key",
  264. Description: "AWS Access Key",
  265. Regex: regexp.MustCompile("(?:A3T[A-Z0-9]|AKIA|ASIA|ABIA|ACCA)[A-Z0-9]{16}"),
  266. Entropy: 999.0,
  267. Keywords: []string{},
  268. Tags: []string{"key", "AWS"},
  269. },
  270. },
  271. },
  272. },
  273. {
  274. cfgName: "override_secret_group",
  275. rules: []string{"aws-access-key"},
  276. cfg: Config{
  277. Rules: map[string]Rule{"aws-access-key": {
  278. RuleID: "aws-access-key",
  279. Description: "AWS Access Key",
  280. Regex: regexp.MustCompile("(?:a)(?:a)"),
  281. SecretGroup: 2,
  282. Keywords: []string{},
  283. Tags: []string{"key", "AWS"},
  284. },
  285. },
  286. },
  287. },
  288. {
  289. cfgName: "override_regex",
  290. rules: []string{"aws-access-key"},
  291. cfg: Config{
  292. Rules: map[string]Rule{"aws-access-key": {
  293. RuleID: "aws-access-key",
  294. Description: "AWS Access Key",
  295. Regex: regexp.MustCompile("(?:a)"),
  296. Keywords: []string{},
  297. Tags: []string{"key", "AWS"},
  298. },
  299. },
  300. },
  301. },
  302. {
  303. cfgName: "override_path",
  304. rules: []string{"aws-access-key"},
  305. cfg: Config{
  306. Rules: map[string]Rule{"aws-access-key": {
  307. RuleID: "aws-access-key",
  308. Description: "AWS Access Key",
  309. Regex: regexp.MustCompile("(?:A3T[A-Z0-9]|AKIA|ASIA|ABIA|ACCA)[A-Z0-9]{16}"),
  310. Path: regexp.MustCompile("(?:puppy)"),
  311. Keywords: []string{},
  312. Tags: []string{"key", "AWS"},
  313. },
  314. },
  315. },
  316. },
  317. {
  318. cfgName: "override_tags",
  319. rules: []string{"aws-access-key"},
  320. cfg: Config{
  321. Rules: map[string]Rule{"aws-access-key": {
  322. RuleID: "aws-access-key",
  323. Description: "AWS Access Key",
  324. Regex: regexp.MustCompile("(?:A3T[A-Z0-9]|AKIA|ASIA|ABIA|ACCA)[A-Z0-9]{16}"),
  325. Keywords: []string{},
  326. Tags: []string{"key", "AWS", "puppy"},
  327. },
  328. },
  329. },
  330. },
  331. {
  332. cfgName: "override_keywords",
  333. rules: []string{"aws-access-key"},
  334. cfg: Config{
  335. Rules: map[string]Rule{"aws-access-key": {
  336. RuleID: "aws-access-key",
  337. Description: "AWS Access Key",
  338. Regex: regexp.MustCompile("(?:A3T[A-Z0-9]|AKIA|ASIA|ABIA|ACCA)[A-Z0-9]{16}"),
  339. Keywords: []string{"puppy"},
  340. Tags: []string{"key", "AWS"},
  341. },
  342. },
  343. },
  344. },
  345. {
  346. cfgName: "extend_disabled",
  347. cfg: Config{
  348. Rules: map[string]Rule{
  349. "aws-secret-key": {
  350. RuleID: "aws-secret-key",
  351. Regex: regexp.MustCompile(`(?i)aws_(.{0,20})?=?.[\'\"0-9a-zA-Z\/+]{40}`),
  352. Tags: []string{"key", "AWS"},
  353. Keywords: []string{},
  354. },
  355. "pypi-upload-token": {
  356. RuleID: "pypi-upload-token",
  357. Regex: regexp.MustCompile(`pypi-AgEIcHlwaS5vcmc[A-Za-z0-9\-_]{50,1000}`),
  358. Tags: []string{},
  359. Keywords: []string{},
  360. },
  361. },
  362. },
  363. },
  364. }
  365. for _, tt := range tests {
  366. t.Run(tt.cfgName, func(t *testing.T) {
  367. t.Cleanup(func() {
  368. extendDepth = 0
  369. viper.Reset()
  370. })
  371. viper.AddConfigPath(configPath)
  372. viper.SetConfigName(tt.cfgName)
  373. viper.SetConfigType("toml")
  374. err := viper.ReadInConfig()
  375. require.NoError(t, err)
  376. var vc ViperConfig
  377. err = viper.Unmarshal(&vc)
  378. require.NoError(t, err)
  379. cfg, err := vc.Translate()
  380. if err != nil && !assert.EqualError(t, tt.wantError, err.Error()) {
  381. return
  382. }
  383. if len(tt.rules) > 0 {
  384. rules := make(map[string]Rule)
  385. for _, name := range tt.rules {
  386. rules[name] = cfg.Rules[name]
  387. }
  388. cfg.Rules = rules
  389. }
  390. var regexComparer = func(x, y *regexp.Regexp) bool {
  391. if x == nil || y == nil {
  392. return x == y
  393. }
  394. return x.String() == y.String()
  395. }
  396. opts := cmp.Options{
  397. cmp.Comparer(regexComparer),
  398. cmpopts.IgnoreUnexported(Rule{}, Allowlist{}),
  399. }
  400. if diff := cmp.Diff(tt.cfg.Rules, cfg.Rules, opts); diff != "" {
  401. t.Errorf("%s diff: (-want +got)\n%s", tt.cfgName, diff)
  402. }
  403. })
  404. }
  405. }
  406. func TestExtendedRuleKeywordsAreDowncase(t *testing.T) {
  407. tests := []struct {
  408. name string
  409. cfgName string
  410. expectedKeywords string
  411. }{
  412. {
  413. name: "Extend base rule that includes AWS keyword with new attribute",
  414. cfgName: "extend_base_rule_including_keysword_with_attribute",
  415. expectedKeywords: "aws",
  416. },
  417. {
  418. name: "Extend base with a new rule with CMS keyword",
  419. cfgName: "extend_with_new_rule",
  420. expectedKeywords: "cms",
  421. },
  422. }
  423. for _, tt := range tests {
  424. t.Run(tt.name, func(t *testing.T) {
  425. t.Cleanup(func() {
  426. viper.Reset()
  427. })
  428. viper.AddConfigPath(configPath)
  429. viper.SetConfigName(tt.cfgName)
  430. viper.SetConfigType("toml")
  431. err := viper.ReadInConfig()
  432. require.NoError(t, err)
  433. var vc ViperConfig
  434. err = viper.Unmarshal(&vc)
  435. require.NoError(t, err)
  436. cfg, err := vc.Translate()
  437. require.NoError(t, err)
  438. _, exists := cfg.Keywords[tt.expectedKeywords]
  439. require.Truef(t, exists, "The expected keyword %s did not exist as a key of cfg.Keywords", tt.expectedKeywords)
  440. })
  441. }
  442. }