detect_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. package detect
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "testing"
  7. "github.com/spf13/viper"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/zricethezav/gitleaks/v8/config"
  10. "github.com/zricethezav/gitleaks/v8/report"
  11. )
  12. const configPath = "../testdata/config/"
  13. const repoBasePath = "../testdata/repos/"
  14. func TestDetect(t *testing.T) {
  15. tests := []struct {
  16. cfgName string
  17. fragment Fragment
  18. expectedFindings []report.Finding
  19. wantError error
  20. }{
  21. {
  22. cfgName: "escaped_character_group",
  23. fragment: Fragment{
  24. Raw: `pypi-AgEIcHlwaS5vcmcAAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAAB`,
  25. FilePath: "tmp.go",
  26. },
  27. expectedFindings: []report.Finding{
  28. {
  29. Description: "PyPI upload token",
  30. Secret: "pypi-AgEIcHlwaS5vcmcAAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAAB",
  31. Match: "pypi-AgEIcHlwaS5vcmcAAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAAB",
  32. File: "tmp.go",
  33. RuleID: "pypi-upload-token",
  34. Tags: []string{"key", "pypi"},
  35. StartLine: 1,
  36. EndLine: 1,
  37. StartColumn: 1,
  38. EndColumn: 86,
  39. Entropy: 1.9606875,
  40. },
  41. },
  42. },
  43. {
  44. cfgName: "simple",
  45. fragment: Fragment{
  46. Raw: `awsToken := \"AKIALALEMEL33243OLIA\"`,
  47. FilePath: "tmp.go",
  48. },
  49. expectedFindings: []report.Finding{
  50. {
  51. Description: "AWS Access Key",
  52. Secret: "AKIALALEMEL33243OLIA",
  53. Match: "AKIALALEMEL33243OLIA",
  54. File: "tmp.go",
  55. RuleID: "aws-access-key",
  56. Tags: []string{"key", "AWS"},
  57. StartLine: 1,
  58. EndLine: 1,
  59. StartColumn: 15,
  60. EndColumn: 34,
  61. Entropy: 3.0841837,
  62. },
  63. },
  64. },
  65. {
  66. cfgName: "allow_aws_re",
  67. fragment: Fragment{
  68. Raw: `awsToken := \"AKIALALEMEL33243OLIA\"`,
  69. FilePath: "tmp.go",
  70. },
  71. expectedFindings: []report.Finding{},
  72. },
  73. {
  74. cfgName: "allow_path",
  75. fragment: Fragment{
  76. Raw: `awsToken := \"AKIALALEMEL33243OLIA\"`,
  77. FilePath: "tmp.go",
  78. },
  79. expectedFindings: []report.Finding{},
  80. },
  81. {
  82. cfgName: "allow_commit",
  83. fragment: Fragment{
  84. Raw: `awsToken := \"AKIALALEMEL33243OLIA\"`,
  85. FilePath: "tmp.go",
  86. CommitSHA: "allowthiscommit",
  87. },
  88. expectedFindings: []report.Finding{},
  89. },
  90. {
  91. cfgName: "entropy_group",
  92. fragment: Fragment{
  93. Raw: `const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`,
  94. FilePath: "tmp.go",
  95. },
  96. expectedFindings: []report.Finding{
  97. {
  98. Description: "Discord API key",
  99. Match: "Discord_Public_Key = \"e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5\"",
  100. Secret: "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5",
  101. File: "tmp.go",
  102. RuleID: "discord-api-key",
  103. Tags: []string{},
  104. Entropy: 3.7906237,
  105. StartLine: 1,
  106. EndLine: 1,
  107. StartColumn: 7,
  108. EndColumn: 93,
  109. },
  110. },
  111. },
  112. {
  113. cfgName: "generic_with_py_path",
  114. fragment: Fragment{
  115. Raw: `const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`,
  116. FilePath: "tmp.go",
  117. },
  118. expectedFindings: []report.Finding{},
  119. },
  120. {
  121. cfgName: "generic_with_py_path",
  122. fragment: Fragment{
  123. Raw: `const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`,
  124. FilePath: "tmp.py",
  125. },
  126. expectedFindings: []report.Finding{
  127. {
  128. Description: "Generic API Key",
  129. Match: "Key = \"e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5\"",
  130. Secret: "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5",
  131. File: "tmp.py",
  132. RuleID: "generic-api-key",
  133. Tags: []string{},
  134. Entropy: 3.7906237,
  135. StartLine: 1,
  136. EndLine: 1,
  137. StartColumn: 22,
  138. EndColumn: 93,
  139. },
  140. },
  141. },
  142. {
  143. cfgName: "path_only",
  144. fragment: Fragment{
  145. Raw: `const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`,
  146. FilePath: "tmp.py",
  147. },
  148. expectedFindings: []report.Finding{
  149. {
  150. Description: "Python Files",
  151. Match: "file detected: tmp.py",
  152. File: "tmp.py",
  153. RuleID: "python-files-only",
  154. Tags: []string{},
  155. },
  156. },
  157. },
  158. {
  159. cfgName: "bad_entropy_group",
  160. fragment: Fragment{
  161. Raw: `const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`,
  162. FilePath: "tmp.go",
  163. },
  164. expectedFindings: []report.Finding{},
  165. wantError: fmt.Errorf("Discord API key invalid regex secret group 5, max regex secret group 3"),
  166. },
  167. {
  168. cfgName: "simple",
  169. fragment: Fragment{
  170. Raw: `awsToken := \"AKIALALEMEL33243OLIA\"`,
  171. FilePath: filepath.Join(configPath, "simple.toml"),
  172. },
  173. expectedFindings: []report.Finding{},
  174. },
  175. {
  176. cfgName: "allow_global_aws_re",
  177. fragment: Fragment{
  178. Raw: `awsToken := \"AKIALALEMEL33243OLIA\"`,
  179. FilePath: "tmp.go",
  180. },
  181. expectedFindings: []report.Finding{},
  182. },
  183. {
  184. cfgName: "generic_with_py_path",
  185. fragment: Fragment{
  186. Raw: `const Discord_Public_Key = "load2523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`,
  187. FilePath: "tmp.py",
  188. },
  189. expectedFindings: []report.Finding{},
  190. },
  191. }
  192. for _, tt := range tests {
  193. viper.Reset()
  194. viper.AddConfigPath(configPath)
  195. viper.SetConfigName(tt.cfgName)
  196. viper.SetConfigType("toml")
  197. err := viper.ReadInConfig()
  198. if err != nil {
  199. t.Error(err)
  200. }
  201. var vc config.ViperConfig
  202. err = viper.Unmarshal(&vc)
  203. if err != nil {
  204. t.Error(err)
  205. }
  206. cfg, err := vc.Translate()
  207. cfg.Path = filepath.Join(configPath, tt.cfgName+".toml")
  208. if tt.wantError != nil {
  209. if err == nil {
  210. t.Errorf("expected error")
  211. }
  212. assert.Equal(t, tt.wantError, err)
  213. }
  214. d := NewDetector(cfg)
  215. findings := d.Detect(tt.fragment)
  216. assert.ElementsMatch(t, tt.expectedFindings, findings)
  217. }
  218. }
  219. // TestFromGit tests the FromGit function
  220. func TestFromGit(t *testing.T) {
  221. tests := []struct {
  222. cfgName string
  223. source string
  224. logOpts string
  225. expectedFindings []report.Finding
  226. }{
  227. {
  228. source: filepath.Join(repoBasePath, "small"),
  229. cfgName: "simple",
  230. expectedFindings: []report.Finding{
  231. {
  232. Description: "AWS Access Key",
  233. StartLine: 20,
  234. EndLine: 20,
  235. StartColumn: 19,
  236. EndColumn: 38,
  237. Secret: "AKIALALEMEL33243OLIA",
  238. Match: "AKIALALEMEL33243OLIA",
  239. File: "main.go",
  240. Date: "2021-11-02T23:37:53Z",
  241. Commit: "1b6da43b82b22e4eaa10bcf8ee591e91abbfc587",
  242. Author: "Zachary Rice",
  243. Email: "zricer@protonmail.com",
  244. Message: "Accidentally add a secret",
  245. RuleID: "aws-access-key",
  246. Tags: []string{"key", "AWS"},
  247. Entropy: 3.0841837,
  248. },
  249. {
  250. Description: "AWS Access Key",
  251. StartLine: 9,
  252. EndLine: 9,
  253. StartColumn: 17,
  254. EndColumn: 36,
  255. Secret: "AKIALALEMEL33243OLIA",
  256. Match: "AKIALALEMEL33243OLIA",
  257. File: "foo/foo.go",
  258. Date: "2021-11-02T23:48:06Z",
  259. Commit: "491504d5a31946ce75e22554cc34203d8e5ff3ca",
  260. Author: "Zach Rice",
  261. Email: "zricer@protonmail.com",
  262. Message: "adding foo package with secret",
  263. RuleID: "aws-access-key",
  264. Tags: []string{"key", "AWS"},
  265. Entropy: 3.0841837,
  266. },
  267. },
  268. },
  269. {
  270. source: filepath.Join(repoBasePath, "small"),
  271. logOpts: "--all foo...",
  272. cfgName: "simple",
  273. expectedFindings: []report.Finding{
  274. {
  275. Description: "AWS Access Key",
  276. StartLine: 9,
  277. EndLine: 9,
  278. StartColumn: 17,
  279. EndColumn: 36,
  280. Secret: "AKIALALEMEL33243OLIA",
  281. Match: "AKIALALEMEL33243OLIA",
  282. Date: "2021-11-02T23:48:06Z",
  283. File: "foo/foo.go",
  284. Commit: "491504d5a31946ce75e22554cc34203d8e5ff3ca",
  285. Author: "Zach Rice",
  286. Email: "zricer@protonmail.com",
  287. Message: "adding foo package with secret",
  288. RuleID: "aws-access-key",
  289. Tags: []string{"key", "AWS"},
  290. Entropy: 3.0841837,
  291. },
  292. },
  293. },
  294. }
  295. err := moveDotGit("dotGit", ".git")
  296. if err != nil {
  297. t.Fatal(err)
  298. }
  299. defer func() {
  300. if err := moveDotGit(".git", "dotGit"); err != nil {
  301. t.Error(err)
  302. }
  303. }()
  304. for _, tt := range tests {
  305. viper.AddConfigPath(configPath)
  306. viper.SetConfigName("simple")
  307. viper.SetConfigType("toml")
  308. err = viper.ReadInConfig()
  309. if err != nil {
  310. t.Error(err)
  311. }
  312. var vc config.ViperConfig
  313. err = viper.Unmarshal(&vc)
  314. if err != nil {
  315. t.Error(err)
  316. }
  317. cfg, err := vc.Translate()
  318. if err != nil {
  319. t.Error(err)
  320. }
  321. detector := NewDetector(cfg)
  322. findings, err := detector.DetectGit(tt.source, tt.logOpts, DetectType)
  323. if err != nil {
  324. t.Error(err)
  325. }
  326. for _, f := range findings {
  327. f.Match = "" // remove lines cause copying and pasting them has some wack formatting
  328. }
  329. assert.ElementsMatch(t, tt.expectedFindings, findings)
  330. }
  331. }
  332. // TestFromGit tests the FromGit function
  333. func TestFromFiles(t *testing.T) {
  334. tests := []struct {
  335. cfgName string
  336. source string
  337. expectedFindings []report.Finding
  338. }{
  339. {
  340. source: filepath.Join(repoBasePath, "nogit"),
  341. cfgName: "simple",
  342. expectedFindings: []report.Finding{
  343. {
  344. Description: "AWS Access Key",
  345. StartLine: 20,
  346. EndLine: 20,
  347. StartColumn: 16,
  348. EndColumn: 35,
  349. Match: "AKIALALEMEL33243OLIA",
  350. Secret: "AKIALALEMEL33243OLIA",
  351. File: "../testdata/repos/nogit/main.go",
  352. RuleID: "aws-access-key",
  353. Tags: []string{"key", "AWS"},
  354. Entropy: 3.0841837,
  355. },
  356. },
  357. },
  358. {
  359. source: filepath.Join(repoBasePath, "nogit", "main.go"),
  360. cfgName: "simple",
  361. expectedFindings: []report.Finding{
  362. {
  363. Description: "AWS Access Key",
  364. StartLine: 20,
  365. EndLine: 20,
  366. StartColumn: 16,
  367. EndColumn: 35,
  368. Match: "AKIALALEMEL33243OLIA",
  369. Secret: "AKIALALEMEL33243OLIA",
  370. File: "../testdata/repos/nogit/main.go",
  371. RuleID: "aws-access-key",
  372. Tags: []string{"key", "AWS"},
  373. Entropy: 3.0841837,
  374. },
  375. },
  376. },
  377. }
  378. for _, tt := range tests {
  379. viper.AddConfigPath(configPath)
  380. viper.SetConfigName("simple")
  381. viper.SetConfigType("toml")
  382. err := viper.ReadInConfig()
  383. if err != nil {
  384. t.Error(err)
  385. }
  386. var vc config.ViperConfig
  387. err = viper.Unmarshal(&vc)
  388. if err != nil {
  389. t.Error(err)
  390. }
  391. cfg, _ := vc.Translate()
  392. detector := NewDetector(cfg)
  393. findings, err := detector.DetectFiles(tt.source)
  394. if err != nil {
  395. t.Error(err)
  396. }
  397. assert.ElementsMatch(t, tt.expectedFindings, findings)
  398. }
  399. }
  400. func moveDotGit(from, to string) error {
  401. repoDirs, err := os.ReadDir("../testdata/repos")
  402. if err != nil {
  403. return err
  404. }
  405. for _, dir := range repoDirs {
  406. if to == ".git" {
  407. _, err := os.Stat(fmt.Sprintf("%s/%s/%s", repoBasePath, dir.Name(), "dotGit"))
  408. if os.IsNotExist(err) {
  409. // dont want to delete the only copy of .git accidentally
  410. continue
  411. }
  412. os.RemoveAll(fmt.Sprintf("%s/%s/%s", repoBasePath, dir.Name(), ".git"))
  413. }
  414. if !dir.IsDir() {
  415. continue
  416. }
  417. _, err := os.Stat(fmt.Sprintf("%s/%s/%s", repoBasePath, dir.Name(), from))
  418. if os.IsNotExist(err) {
  419. continue
  420. }
  421. err = os.Rename(fmt.Sprintf("%s/%s/%s", repoBasePath, dir.Name(), from),
  422. fmt.Sprintf("%s/%s/%s", repoBasePath, dir.Name(), to))
  423. if err != nil {
  424. return err
  425. }
  426. }
  427. return nil
  428. }