scan_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. package scan
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "reflect"
  8. "runtime"
  9. "sort"
  10. "testing"
  11. "github.com/zricethezav/gitleaks/v5/config"
  12. "github.com/zricethezav/gitleaks/v5/manager"
  13. "github.com/zricethezav/gitleaks/v5/options"
  14. "github.com/sergi/go-diff/diffmatchpatch"
  15. )
  16. const testRepoBase = "../test_data/test_repos/"
  17. func TestScan(t *testing.T) {
  18. moveDotGit("dotGit", ".git")
  19. defer moveDotGit(".git", "dotGit")
  20. tests := []struct {
  21. description string
  22. opts options.Options
  23. wantPath string
  24. wantErr error
  25. emptyRepo bool
  26. wantEmpty bool
  27. }{
  28. {
  29. description: "test local repo one aws leak",
  30. opts: options.Options{
  31. RepoPath: "../test_data/test_repos/test_repo_1",
  32. Report: "../test_data/test_local_repo_one_aws_leak.json.got",
  33. ReportFormat: "json",
  34. },
  35. wantPath: "../test_data/test_local_repo_one_aws_leak.json",
  36. },
  37. {
  38. description: "test local repo one aws leak threaded",
  39. opts: options.Options{
  40. Threads: runtime.GOMAXPROCS(0),
  41. RepoPath: "../test_data/test_repos/test_repo_1",
  42. Report: "../test_data/test_local_repo_one_aws_leak.json.got",
  43. ReportFormat: "json",
  44. },
  45. wantPath: "../test_data/test_local_repo_one_aws_leak.json",
  46. },
  47. {
  48. description: "test non existent repo",
  49. opts: options.Options{
  50. RepoPath: "../test_data/test_repos/no_repo_here",
  51. ReportFormat: "json",
  52. },
  53. emptyRepo: true,
  54. },
  55. {
  56. description: "test local repo one aws leak allowlisted",
  57. opts: options.Options{
  58. RepoPath: "../test_data/test_repos/test_repo_1",
  59. ReportFormat: "json",
  60. Config: "../test_data/test_configs/aws_key_allowlist_python_files.toml",
  61. },
  62. wantEmpty: true,
  63. },
  64. {
  65. description: "test local repo two leaks",
  66. opts: options.Options{
  67. RepoPath: "../test_data/test_repos/test_repo_2",
  68. Report: "../test_data/test_local_repo_two_leaks.json.got",
  69. ReportFormat: "json",
  70. },
  71. wantPath: "../test_data/test_local_repo_two_leaks.json",
  72. },
  73. {
  74. description: "test local repo two leaks from Commit",
  75. opts: options.Options{
  76. RepoPath: "../test_data/test_repos/test_repo_2",
  77. Report: "../test_data/test_local_repo_two_leaks_commit_from.json.got",
  78. ReportFormat: "json",
  79. CommitFrom: "996865bb912f3bc45898a370a13aadb315014b55",
  80. },
  81. wantPath: "../test_data/test_local_repo_two_leaks_commit_from.json",
  82. },
  83. {
  84. description: "test local repo two leaks to Commit",
  85. opts: options.Options{
  86. RepoPath: "../test_data/test_repos/test_repo_2",
  87. Report: "../test_data/test_local_repo_two_leaks_commit_to.json.got",
  88. ReportFormat: "json",
  89. CommitTo: "996865bb912f3bc45898a370a13aadb315014b55",
  90. },
  91. wantPath: "../test_data/test_local_repo_two_leaks_commit_to.json",
  92. },
  93. {
  94. description: "test local repo two leaks range Commit",
  95. opts: options.Options{
  96. RepoPath: "../test_data/test_repos/test_repo_2",
  97. Report: "../test_data/test_local_repo_two_leaks_commit_range.json.got",
  98. ReportFormat: "json",
  99. CommitFrom: "d8ac0b73aeeb45843319cdc5ce506516eb49bf7a",
  100. CommitTo: "51f6dcf6b89b93f4075ba92c400b075631a6cc93",
  101. },
  102. wantPath: "../test_data/test_local_repo_two_leaks_commit_range.json",
  103. },
  104. {
  105. description: "test local repo two leaks globally allowlisted",
  106. opts: options.Options{
  107. RepoPath: "../test_data/test_repos/test_repo_2",
  108. Config: "../test_data/test_configs/aws_key_global_allowlist_file.toml",
  109. ReportFormat: "json",
  110. },
  111. wantEmpty: true,
  112. },
  113. {
  114. description: "test local repo two leaks allowlisted",
  115. opts: options.Options{
  116. RepoPath: "../test_data/test_repos/test_repo_2",
  117. Config: "../test_data/test_configs/aws_key_allowlist_files.toml",
  118. ReportFormat: "json",
  119. },
  120. wantEmpty: true,
  121. },
  122. {
  123. description: "test local repo three leaks dev branch",
  124. opts: options.Options{
  125. RepoPath: "../test_data/test_repos/test_repo_3",
  126. Report: "../test_data/test_local_repo_three_leaks.json.got",
  127. Config: "../test_data/test_configs/aws_key.toml",
  128. Branch: "dev",
  129. ReportFormat: "json",
  130. },
  131. wantPath: "../test_data/test_local_repo_three_leaks.json",
  132. },
  133. {
  134. description: "test local repo branch does not exist",
  135. opts: options.Options{
  136. RepoPath: "../test_data/test_repos/test_repo_3",
  137. Branch: "nobranch",
  138. ReportFormat: "json",
  139. },
  140. wantEmpty: true,
  141. },
  142. {
  143. description: "test local repo one aws leak single Commit",
  144. opts: options.Options{
  145. RepoPath: "../test_data/test_repos/test_repo_1",
  146. Report: "../test_data/test_local_repo_one_aws_leak_commit.json.got",
  147. Commit: "6557c92612d3b35979bd426d429255b3bf9fab74",
  148. ReportFormat: "json",
  149. },
  150. wantPath: "../test_data/test_local_repo_one_aws_leak_commit.json",
  151. },
  152. {
  153. description: "test local repo one aws leak AND leak on python files",
  154. opts: options.Options{
  155. RepoPath: "../test_data/test_repos/test_repo_1",
  156. Report: "../test_data/test_local_repo_one_aws_leak_and_file_leak.json.got",
  157. Config: "../test_data/test_configs/aws_key_file_regex.toml",
  158. ReportFormat: "json",
  159. },
  160. wantPath: "../test_data/test_local_repo_one_aws_leak_and_file_leak.json",
  161. },
  162. {
  163. description: "test owner path",
  164. opts: options.Options{
  165. OwnerPath: "../test_data/test_repos/",
  166. Report: "../test_data/test_local_owner_aws_leak.json.got",
  167. ReportFormat: "json",
  168. },
  169. wantPath: "../test_data/test_local_owner_aws_leak.json",
  170. },
  171. {
  172. description: "test owner path allowlist repo",
  173. opts: options.Options{
  174. OwnerPath: "../test_data/test_repos/",
  175. Report: "../test_data/test_local_owner_aws_leak_allowlist_repo.json.got",
  176. ReportFormat: "json",
  177. Config: "../test_data/test_configs/aws_key_local_owner_allowlist_repo.toml",
  178. },
  179. wantPath: "../test_data/test_local_owner_aws_leak_allowlist_repo.json",
  180. },
  181. {
  182. description: "test entropy and regex",
  183. opts: options.Options{
  184. RepoPath: "../test_data/test_repos/test_repo_1",
  185. Report: "../test_data/test_regex_entropy.json.got",
  186. Config: "../test_data/test_configs/regex_entropy.toml",
  187. ReportFormat: "json",
  188. },
  189. wantPath: "../test_data/test_regex_entropy.json",
  190. },
  191. {
  192. description: "test local repo four entropy alternative config",
  193. opts: options.Options{
  194. RepoPath: "../test_data/test_repos/test_repo_4",
  195. Report: "../test_data/test_local_repo_four_alt_config_entropy.json.got",
  196. RepoConfig: true,
  197. ReportFormat: "json",
  198. },
  199. wantPath: "../test_data/test_local_repo_four_alt_config_entropy.json",
  200. },
  201. {
  202. description: "test local repo four entropy alternative config",
  203. opts: options.Options{
  204. RepoPath: "../test_data/test_repos/test_repo_1",
  205. Report: "../test_data/test_regex_allowlist.json.got",
  206. Config: "../test_data/test_configs/aws_key_aws_allowlisted.toml",
  207. ReportFormat: "json",
  208. },
  209. wantEmpty: true,
  210. },
  211. {
  212. description: "test local repo one aws leak timeout",
  213. opts: options.Options{
  214. RepoPath: "../test_data/test_repos/test_repo_1",
  215. Report: "../test_data/test_local_repo_one_aws_leak.json.got",
  216. ReportFormat: "json",
  217. Timeout: "10ns",
  218. },
  219. wantEmpty: true,
  220. },
  221. {
  222. description: "test local repo one aws leak long timeout",
  223. opts: options.Options{
  224. RepoPath: "../test_data/test_repos/test_repo_1",
  225. Report: "../test_data/test_local_repo_one_aws_leak.json.got",
  226. ReportFormat: "json",
  227. Timeout: "2m",
  228. },
  229. wantPath: "../test_data/test_local_repo_one_aws_leak.json",
  230. },
  231. {
  232. description: "test owner path depth=2",
  233. opts: options.Options{
  234. OwnerPath: "../test_data/test_repos/",
  235. Report: "../test_data/test_local_owner_aws_leak_depth_2.json.got",
  236. ReportFormat: "json",
  237. Depth: 2,
  238. },
  239. wantPath: "../test_data/test_local_owner_aws_leak_depth_2.json",
  240. },
  241. {
  242. description: "test local repo five files at Commit",
  243. opts: options.Options{
  244. RepoPath: "../test_data/test_repos/test_repo_5",
  245. Report: "../test_data/test_local_repo_five_files_at_commit.json.got",
  246. FilesAtCommit: "a4c9fb737d5552fd96fce5cc7eedb23353ba9ed0",
  247. ReportFormat: "json",
  248. },
  249. wantPath: "../test_data/test_local_repo_five_files_at_commit.json",
  250. },
  251. {
  252. description: "test local repo five files at latest Commit",
  253. opts: options.Options{
  254. RepoPath: "../test_data/test_repos/test_repo_5",
  255. Report: "../test_data/test_local_repo_five_files_at_latest_commit.json.got",
  256. FilesAtCommit: "latest",
  257. ReportFormat: "json",
  258. },
  259. wantPath: "../test_data/test_local_repo_five_files_at_commit.json",
  260. },
  261. {
  262. description: "test local repo five at Commit",
  263. opts: options.Options{
  264. RepoPath: "../test_data/test_repos/test_repo_5",
  265. Report: "../test_data/test_local_repo_five_commit.json.got",
  266. Commit: "a4c9fb737d5552fd96fce5cc7eedb23353ba9ed0",
  267. ReportFormat: "json",
  268. Config: "../test_data/test_configs/generic.toml",
  269. },
  270. wantPath: "../test_data/test_local_repo_five_commit.json",
  271. },
  272. {
  273. description: "test local repo five at latest Commit",
  274. opts: options.Options{
  275. RepoPath: "../test_data/test_repos/test_repo_5",
  276. Report: "../test_data/test_local_repo_five_at_latest_commit.json.got",
  277. Commit: "latest",
  278. ReportFormat: "json",
  279. Config: "../test_data/test_configs/generic.toml",
  280. },
  281. wantPath: "../test_data/test_local_repo_five_at_latest_commit.json",
  282. },
  283. {
  284. description: "test local repo six filename",
  285. opts: options.Options{
  286. RepoPath: "../test_data/test_repos/test_repo_6",
  287. Report: "../test_data/test_local_repo_six_filename.json.got",
  288. Config: "../test_data/test_configs/regex_filename.toml",
  289. ReportFormat: "json",
  290. },
  291. wantPath: "../test_data/test_local_repo_six_filename.json",
  292. },
  293. {
  294. description: "test local repo six filepath",
  295. opts: options.Options{
  296. RepoPath: "../test_data/test_repos/test_repo_6",
  297. Report: "../test_data/test_local_repo_six_filepath.json.got",
  298. Config: "../test_data/test_configs/regex_filepath.toml",
  299. ReportFormat: "json",
  300. },
  301. wantPath: "../test_data/test_local_repo_six_filepath.json",
  302. },
  303. {
  304. description: "test local repo six filename and filepath",
  305. opts: options.Options{
  306. RepoPath: "../test_data/test_repos/test_repo_6",
  307. Report: "../test_data/test_local_repo_six_filepath_filename.json.got",
  308. Config: "../test_data/test_configs/regex_filepath_filename.toml",
  309. ReportFormat: "json",
  310. },
  311. wantPath: "../test_data/test_local_repo_six_filepath_filename.json",
  312. },
  313. {
  314. description: "test local repo six path globally allowlisted",
  315. opts: options.Options{
  316. RepoPath: "../test_data/test_repos/test_repo_6",
  317. Report: "../test_data/test_local_repo_six_path_globally_allowlisted.json.got",
  318. Config: "../test_data/test_configs/aws_key_global_allowlist_path.toml",
  319. ReportFormat: "json",
  320. },
  321. wantPath: "../test_data/test_local_repo_six_path_globally_allowlisted.json",
  322. },
  323. {
  324. description: "test local repo six leaks since date",
  325. opts: options.Options{
  326. RepoPath: "../test_data/test_repos/test_repo_6",
  327. Report: "../test_data/test_local_repo_six_leaks_since_date.json.got",
  328. ReportFormat: "json",
  329. CommitSince: "2019-10-25",
  330. },
  331. wantPath: "../test_data/test_local_repo_six_leaks_since_date.json",
  332. },
  333. {
  334. description: "test local repo two leaks until date",
  335. opts: options.Options{
  336. RepoPath: "../test_data/test_repos/test_repo_6",
  337. Report: "../test_data/test_local_repo_six_leaks_until_date.json.got",
  338. ReportFormat: "json",
  339. CommitUntil: "2019-10-25",
  340. },
  341. wantPath: "../test_data/test_local_repo_six_leaks_until_date.json",
  342. },
  343. {
  344. description: "test local repo four leaks timerange Commit",
  345. opts: options.Options{
  346. RepoPath: "../test_data/test_repos/test_repo_4",
  347. Report: "../test_data/test_local_repo_four_leaks_commit_timerange.json.got",
  348. ReportFormat: "json",
  349. CommitSince: "2019-10-25T13:01:27-0400",
  350. CommitUntil: "2019-10-25T13:12:32-0400",
  351. },
  352. wantPath: "../test_data/test_local_repo_four_leaks_commit_timerange.json",
  353. },
  354. {
  355. description: "test local repo two allowlist Commit config",
  356. opts: options.Options{
  357. RepoPath: "../test_data/test_repos/test_repo_2",
  358. Report: "../test_data/test_local_repo_two_allowlist_commits.json.got",
  359. Config: "../test_data/test_configs/allowlist_commit.toml",
  360. ReportFormat: "json",
  361. },
  362. wantPath: "../test_data/test_local_repo_two_allowlist_commits.json",
  363. },
  364. {
  365. description: "test local repo two deletion",
  366. opts: options.Options{
  367. RepoPath: "../test_data/test_repos/test_repo_2",
  368. Report: "../test_data/test_local_repo_two_leaks_deletion.json.got",
  369. ReportFormat: "json",
  370. Deletion: true,
  371. },
  372. wantPath: "../test_data/test_local_repo_two_leaks_deletion.json",
  373. },
  374. }
  375. for _, test := range tests {
  376. fmt.Println(test.description)
  377. cfg, err := config.NewConfig(test.opts)
  378. if err != nil {
  379. t.Error(err)
  380. }
  381. m, err := manager.NewManager(test.opts, cfg)
  382. if err != nil {
  383. t.Error(err)
  384. }
  385. err = Run(m)
  386. if test.wantErr != nil {
  387. if err == nil {
  388. t.Errorf("did not receive wantErr: %v", test.wantErr)
  389. }
  390. if err.Error() != test.wantErr.Error() {
  391. t.Errorf("wantErr does not equal err received: %v", err.Error())
  392. }
  393. continue
  394. }
  395. err = m.Report()
  396. if test.wantEmpty {
  397. if len(m.GetLeaks()) != 0 {
  398. t.Errorf("wanted no leaks but got some instead: %+v", m.GetLeaks())
  399. }
  400. continue
  401. }
  402. if test.wantPath != "" {
  403. err := fileCheck(test.wantPath, test.opts.Report)
  404. if err != nil {
  405. t.Error(err)
  406. }
  407. }
  408. }
  409. }
  410. func TestScanUncommited(t *testing.T) {
  411. moveDotGit("dotGit", ".git")
  412. defer moveDotGit(".git", "dotGit")
  413. tests := []struct {
  414. description string
  415. opts options.Options
  416. wantPath string
  417. wantErr error
  418. emptyRepo bool
  419. wantEmpty bool
  420. fileToChange string
  421. addition string
  422. }{
  423. {
  424. description: "test scan local one leak",
  425. opts: options.Options{
  426. RepoPath: "../test_data/test_repos/test_repo_1",
  427. Report: "../test_data/test_local_repo_one_aws_leak_uncommitted.json.got",
  428. Uncommited: true,
  429. ReportFormat: "json",
  430. },
  431. wantPath: "../test_data/test_local_repo_one_aws_leak_uncommitted.json",
  432. fileToChange: "server.test.py",
  433. addition: " aws_access_key_id='AKIAIO5FODNN7DXAMPLE'\n\n",
  434. },
  435. {
  436. description: "test scan local no leak",
  437. opts: options.Options{
  438. RepoPath: "../test_data/test_repos/test_repo_1",
  439. Uncommited: true,
  440. ReportFormat: "json",
  441. },
  442. wantEmpty: true,
  443. fileToChange: "server.test.py",
  444. addition: "nothing bad",
  445. },
  446. {
  447. description: "test scan repo with no commits",
  448. opts: options.Options{
  449. RepoPath: "../test_data/test_repos/test_repo_7",
  450. Report: "../test_data/test_local_repo_seven_aws_leak_uncommitted.json.got",
  451. Uncommited: true,
  452. ReportFormat: "json",
  453. },
  454. wantPath: "../test_data/test_local_repo_seven_aws_leak_uncommitted.json",
  455. },
  456. }
  457. for _, test := range tests {
  458. var (
  459. old []byte
  460. err error
  461. )
  462. fmt.Println(test.description)
  463. if test.fileToChange != "" {
  464. old, err = ioutil.ReadFile(fmt.Sprintf("%s/%s", test.opts.RepoPath, test.fileToChange))
  465. if err != nil {
  466. t.Error(err)
  467. }
  468. altered, err := os.OpenFile(fmt.Sprintf("%s/%s", test.opts.RepoPath, test.fileToChange),
  469. os.O_WRONLY|os.O_APPEND, 0644)
  470. if err != nil {
  471. t.Error(err)
  472. }
  473. _, err = altered.WriteString(test.addition)
  474. if err != nil {
  475. t.Error(err)
  476. }
  477. }
  478. cfg, err := config.NewConfig(test.opts)
  479. if err != nil {
  480. t.Error(err)
  481. }
  482. m, err := manager.NewManager(test.opts, cfg)
  483. if err != nil {
  484. t.Error(err)
  485. }
  486. if err := Run(m); err != nil {
  487. t.Error(err)
  488. }
  489. if err := m.Report(); err != nil {
  490. t.Error(err)
  491. }
  492. if test.fileToChange != "" {
  493. err = ioutil.WriteFile(fmt.Sprintf("%s/%s", test.opts.RepoPath, test.fileToChange), old, 0)
  494. if err != nil {
  495. t.Error(err)
  496. }
  497. }
  498. if test.wantEmpty {
  499. continue
  500. }
  501. if test.wantPath != "" {
  502. err := fileCheck(test.wantPath, test.opts.Report)
  503. if err != nil {
  504. t.Error(err)
  505. }
  506. }
  507. }
  508. }
  509. func fileCheck(wantPath, gotPath string) error {
  510. var (
  511. gotLeaks []manager.Leak
  512. wantLeaks []manager.Leak
  513. )
  514. want, err := ioutil.ReadFile(wantPath)
  515. if err != nil {
  516. return err
  517. }
  518. got, err := ioutil.ReadFile(gotPath)
  519. if err != nil {
  520. return err
  521. }
  522. err = json.Unmarshal(got, &gotLeaks)
  523. if err != nil {
  524. return err
  525. }
  526. err = json.Unmarshal(want, &wantLeaks)
  527. if err != nil {
  528. return nil
  529. }
  530. sort.Slice(gotLeaks, func(i, j int) bool { return (gotLeaks)[i].Commit < (gotLeaks)[j].Commit })
  531. sort.Slice(wantLeaks, func(i, j int) bool { return (wantLeaks)[i].Commit < (wantLeaks)[j].Commit })
  532. if !reflect.DeepEqual(gotLeaks, wantLeaks) {
  533. dmp := diffmatchpatch.New()
  534. diffs := dmp.DiffMain(string(want), string(got), false)
  535. return fmt.Errorf("%s does not equal %s: %s", wantPath, gotPath, dmp.DiffPrettyText(diffs))
  536. }
  537. if err := os.Remove(gotPath); err != nil {
  538. return err
  539. }
  540. return nil
  541. }
  542. func moveDotGit(from, to string) error {
  543. repoDirs, err := ioutil.ReadDir("../test_data/test_repos")
  544. if err != nil {
  545. return err
  546. }
  547. for _, dir := range repoDirs {
  548. if !dir.IsDir() {
  549. continue
  550. }
  551. err = os.Rename(fmt.Sprintf("%s/%s/%s", testRepoBase, dir.Name(), from),
  552. fmt.Sprintf("%s/%s/%s", testRepoBase, dir.Name(), to))
  553. if err != nil {
  554. return err
  555. }
  556. }
  557. return nil
  558. }