diff --git a/scan/rule.go b/scan/rule.go index 29bbd3f8..1d283ec8 100644 --- a/scan/rule.go +++ b/scan/rule.go @@ -200,7 +200,15 @@ func extractAndInjectLineNumber(leak *manager.Leak, bundle *Bundle, repo *Repo) if bundle.Patch == "" { return } + + // This is needed as some patches generate strings that are larger than + // scanners max size (MaxScanTokenSize = 64 * 1024) + // https://github.com/zricethezav/gitleaks/issues/413 + buf := make([]byte, len(bundle.Patch)) scanner := bufio.NewScanner(strings.NewReader(bundle.Patch)) + scanner.Buffer(buf, len(bundle.Patch)) + scanner.Split(bufio.ScanLines) + currFile := "" currLine := 0 currStartDiffLine := 0 diff --git a/scan/scan.go b/scan/scan.go index 445760c2..668cd03c 100644 --- a/scan/scan.go +++ b/scan/scan.go @@ -113,41 +113,46 @@ func (repo *Repo) Scan() error { // increase Commit counter cc++ - err = c.Parents().ForEach(func(parent *object.Commit) error { - defer func() { - if err := recover(); err != nil { - // sometimes the Patch generation will fail due to a known bug in - // sergi's go-diff: https://github.com/sergi/go-diff/issues/89. - // Once a fix has been merged I will remove this recover. - return - } - }() - if repo.timeoutReached() { - return nil - } - if parent == nil { - // shouldn't reach this point but just in case - return nil - } + // inspect first parent only as all other parents will be eventually reached + // (they exist as the tip of other branches, etc) + // See https://github.com/zricethezav/gitleaks/issues/413 for details + parent, err := c.Parent(0) + if err != nil { + return err + } - start := time.Now() - patch, err := parent.Patch(c) - if err != nil { - return fmt.Errorf("could not generate Patch") + defer func() { + if err := recover(); err != nil { + // sometimes the Patch generation will fail due to a known bug in + // sergi's go-diff: https://github.com/sergi/go-diff/issues/89. + // Once a fix has been merged I will remove this recover. + return } - repo.Manager.RecordTime(manager.PatchTime(howLong(start))) - wg.Add(1) - semaphore <- true - go func(c *object.Commit, patch *object.Patch) { - defer func() { - <-semaphore - wg.Done() - }() - scanPatch(patch, c, repo) - }(c, patch) - + }() + if repo.timeoutReached() { return nil - }) + } + if parent == nil { + // shouldn't reach this point but just in case + return nil + } + + start := time.Now() + patch, err := parent.Patch(c) + if err != nil { + return fmt.Errorf("could not generate Patch") + } + repo.Manager.RecordTime(manager.PatchTime(howLong(start))) + wg.Add(1) + semaphore <- true + go func(c *object.Commit, patch *object.Patch) { + defer func() { + <-semaphore + wg.Done() + }() + scanPatch(patch, c, repo) + }(c, patch) + if c.Hash.String() == repo.Manager.Opts.CommitTo { return storer.ErrStop } diff --git a/scan/scan_test.go b/scan/scan_test.go index 9e22daff..39b4d2ec 100644 --- a/scan/scan_test.go +++ b/scan/scan_test.go @@ -376,6 +376,15 @@ func TestScan(t *testing.T) { }, wantPath: "../test_data/test_local_repo_two_leaks_deletion.json", }, + { + description: "test local repo eight (merges)", + opts: options.Options{ + RepoPath: "../test_data/test_repos/test_repo_8", + Report: "../test_data/test_local_repo_eight.json.got", + ReportFormat: "json", + }, + wantPath: "../test_data/test_local_repo_eight.json", + }, } for _, test := range tests { diff --git a/test_data/test_local_owner_aws_leak.json b/test_data/test_local_owner_aws_leak.json index dd2d44f4..373920d7 100644 --- a/test_data/test_local_owner_aws_leak.json +++ b/test_data/test_local_owner_aws_leak.json @@ -133,5 +133,50 @@ "date": "2020-02-24T14:13:15-05:00", "tags": "key, AWS", "operation": "addition" + }, + { + "line": "aws_access_key_id='AKIAIO5FODNN7EXAMPLE',", + "lineNumber": 6, + "offender": "AKIAIO5FODNN7EXAMPLE", + "commit": "748f11eaf2c38c3cf0ac6a22e44208777e79fa6f", + "repo": "test_repo_8", + "rule": "AWS Manager ID", + "commitMessage": "Merge pull request #1 from zricethezav/additional-secret-branch\n\nUpdate dummy.txt", + "author": "Zachary Rice", + "email": "zricer@protonmail.com", + "file": "dummy.txt", + "date": "2020-07-25T14:44:48-04:00", + "tags": "key, AWS", + "operation": "addition" + }, + { + "line": "aws_access_key_id='AKIAIO5FODNN7EXAMPLE',", + "lineNumber": 2, + "offender": "AKIAIO5FODNN7EXAMPLE", + "commit": "ce7e8177bbf8a172c06b6a1e370a374d5c19f660", + "repo": "test_repo_8", + "rule": "AWS Manager ID", + "commitMessage": "dummy.txt w/ text", + "author": "Zachary Rice", + "email": "zricer@protonmail.com", + "file": "dummy.txt", + "date": "2020-07-25T14:39:11-04:00", + "tags": "key, AWS", + "operation": "addition" + }, + { + "line": "aws_access_key_id='AKIAIO5FODNN7EXAMPLE',", + "lineNumber": 6, + "offender": "AKIAIO5FODNN7EXAMPLE", + "commit": "9267bc86ec1497471cbc6f3308f3527f7ef34b9d", + "repo": "test_repo_8", + "rule": "AWS Manager ID", + "commitMessage": "Update dummy.txt", + "author": "Zachary Rice", + "email": "zricer@protonmail.com", + "file": "dummy.txt", + "date": "2020-07-25T14:41:11-04:00", + "tags": "key, AWS", + "operation": "addition" } ] diff --git a/test_data/test_local_owner_aws_leak_allowlist_repo.json b/test_data/test_local_owner_aws_leak_allowlist_repo.json index 85a1801c..cd379e84 100644 --- a/test_data/test_local_owner_aws_leak_allowlist_repo.json +++ b/test_data/test_local_owner_aws_leak_allowlist_repo.json @@ -133,5 +133,50 @@ "date": "2019-10-24T09:29:27-04:00", "tags": "key, AWS", "operation": "addition" + }, + { + "line": "aws_access_key_id='AKIAIO5FODNN7EXAMPLE',", + "lineNumber": 6, + "offender": "AKIAIO5FODNN7EXAMPLE", + "commit": "748f11eaf2c38c3cf0ac6a22e44208777e79fa6f", + "repo": "test_repo_8", + "rule": "AWS Manager ID", + "commitMessage": "Merge pull request #1 from zricethezav/additional-secret-branch\n\nUpdate dummy.txt", + "author": "Zachary Rice", + "email": "zricer@protonmail.com", + "file": "dummy.txt", + "date": "2020-07-25T14:44:48-04:00", + "tags": "key, AWS", + "operation": "addition" + }, + { + "line": "aws_access_key_id='AKIAIO5FODNN7EXAMPLE',", + "lineNumber": 2, + "offender": "AKIAIO5FODNN7EXAMPLE", + "commit": "ce7e8177bbf8a172c06b6a1e370a374d5c19f660", + "repo": "test_repo_8", + "rule": "AWS Manager ID", + "commitMessage": "dummy.txt w/ text", + "author": "Zachary Rice", + "email": "zricer@protonmail.com", + "file": "dummy.txt", + "date": "2020-07-25T14:39:11-04:00", + "tags": "key, AWS", + "operation": "addition" + }, + { + "line": "aws_access_key_id='AKIAIO5FODNN7EXAMPLE',", + "lineNumber": 6, + "offender": "AKIAIO5FODNN7EXAMPLE", + "commit": "9267bc86ec1497471cbc6f3308f3527f7ef34b9d", + "repo": "test_repo_8", + "rule": "AWS Manager ID", + "commitMessage": "Update dummy.txt", + "author": "Zachary Rice", + "email": "zricer@protonmail.com", + "file": "dummy.txt", + "date": "2020-07-25T14:41:11-04:00", + "tags": "key, AWS", + "operation": "addition" } ] diff --git a/test_data/test_local_owner_aws_leak_depth_2.json b/test_data/test_local_owner_aws_leak_depth_2.json index 3df795dd..969bb6eb 100644 --- a/test_data/test_local_owner_aws_leak_depth_2.json +++ b/test_data/test_local_owner_aws_leak_depth_2.json @@ -73,5 +73,20 @@ "date": "2020-02-24T14:13:15-05:00", "tags": "key, AWS", "operation": "addition" + }, + { + "line": "aws_access_key_id='AKIAIO5FODNN7EXAMPLE',", + "lineNumber": 6, + "offender": "AKIAIO5FODNN7EXAMPLE", + "commit": "748f11eaf2c38c3cf0ac6a22e44208777e79fa6f", + "repo": "test_repo_8", + "rule": "AWS Manager ID", + "commitMessage": "Merge pull request #1 from zricethezav/additional-secret-branch\n\nUpdate dummy.txt", + "author": "Zachary Rice", + "email": "zricer@protonmail.com", + "file": "dummy.txt", + "date": "2020-07-25T14:44:48-04:00", + "tags": "key, AWS", + "operation": "addition" } ] diff --git a/test_data/test_local_repo_eight.json b/test_data/test_local_repo_eight.json new file mode 100644 index 00000000..66673c60 --- /dev/null +++ b/test_data/test_local_repo_eight.json @@ -0,0 +1,47 @@ +[ + { + "line": "aws_access_key_id='AKIAIO5FODNN7EXAMPLE',", + "lineNumber": 6, + "offender": "AKIAIO5FODNN7EXAMPLE", + "commit": "748f11eaf2c38c3cf0ac6a22e44208777e79fa6f", + "repo": "test_repo_8", + "rule": "AWS Manager ID", + "commitMessage": "Merge pull request #1 from zricethezav/additional-secret-branch\n\nUpdate dummy.txt", + "author": "Zachary Rice", + "email": "zricer@protonmail.com", + "file": "dummy.txt", + "date": "2020-07-25T14:44:48-04:00", + "tags": "key, AWS", + "operation": "addition" + }, + { + "line": "aws_access_key_id='AKIAIO5FODNN7EXAMPLE',", + "lineNumber": 2, + "offender": "AKIAIO5FODNN7EXAMPLE", + "commit": "ce7e8177bbf8a172c06b6a1e370a374d5c19f660", + "repo": "test_repo_8", + "rule": "AWS Manager ID", + "commitMessage": "dummy.txt w/ text", + "author": "Zachary Rice", + "email": "zricer@protonmail.com", + "file": "dummy.txt", + "date": "2020-07-25T14:39:11-04:00", + "tags": "key, AWS", + "operation": "addition" + }, + { + "line": "aws_access_key_id='AKIAIO5FODNN7EXAMPLE',", + "lineNumber": 6, + "offender": "AKIAIO5FODNN7EXAMPLE", + "commit": "9267bc86ec1497471cbc6f3308f3527f7ef34b9d", + "repo": "test_repo_8", + "rule": "AWS Manager ID", + "commitMessage": "Update dummy.txt", + "author": "Zachary Rice", + "email": "zricer@protonmail.com", + "file": "dummy.txt", + "date": "2020-07-25T14:41:11-04:00", + "tags": "key, AWS", + "operation": "addition" + } +] diff --git a/test_data/test_repos/test_repo_8/README.md b/test_data/test_repos/test_repo_8/README.md new file mode 100644 index 00000000..b0e4c4f2 --- /dev/null +++ b/test_data/test_repos/test_repo_8/README.md @@ -0,0 +1,2 @@ +# test_repo_8 +test repo for 413 diff --git a/test_data/test_repos/test_repo_8/dotGit/HEAD b/test_data/test_repos/test_repo_8/dotGit/HEAD new file mode 100644 index 00000000..cb089cd8 --- /dev/null +++ b/test_data/test_repos/test_repo_8/dotGit/HEAD @@ -0,0 +1 @@ +ref: refs/heads/master diff --git a/test_data/test_repos/test_repo_8/dotGit/config b/test_data/test_repos/test_repo_8/dotGit/config new file mode 100644 index 00000000..137b81bb --- /dev/null +++ b/test_data/test_repos/test_repo_8/dotGit/config @@ -0,0 +1,13 @@ +[core] + repositoryformatversion = 0 + filemode = true + bare = false + logallrefupdates = true + ignorecase = true + precomposeunicode = true +[remote "origin"] + url = https://github.com/zricethezav/test_repo_8.git + fetch = +refs/heads/*:refs/remotes/origin/* +[branch "master"] + remote = origin + merge = refs/heads/master diff --git a/test_data/test_repos/test_repo_8/dotGit/description b/test_data/test_repos/test_repo_8/dotGit/description new file mode 100644 index 00000000..498b267a --- /dev/null +++ b/test_data/test_repos/test_repo_8/dotGit/description @@ -0,0 +1 @@ +Unnamed repository; edit this file 'description' to name the repository. diff --git a/test_data/test_repos/test_repo_8/dotGit/index b/test_data/test_repos/test_repo_8/dotGit/index new file mode 100644 index 00000000..fa771231 Binary files /dev/null and b/test_data/test_repos/test_repo_8/dotGit/index differ diff --git a/test_data/test_repos/test_repo_8/dotGit/info/exclude b/test_data/test_repos/test_repo_8/dotGit/info/exclude new file mode 100644 index 00000000..a5196d1b --- /dev/null +++ b/test_data/test_repos/test_repo_8/dotGit/info/exclude @@ -0,0 +1,6 @@ +# git ls-files --others --exclude-from=.git/info/exclude +# Lines that start with '#' are comments. +# For a project mostly in C, the following would be a good set of +# exclude patterns (uncomment them if you want to use them): +# *.[oa] +# *~ diff --git a/test_data/test_repos/test_repo_8/dotGit/logs/HEAD b/test_data/test_repos/test_repo_8/dotGit/logs/HEAD new file mode 100644 index 00000000..c1e43ae7 --- /dev/null +++ b/test_data/test_repos/test_repo_8/dotGit/logs/HEAD @@ -0,0 +1 @@ +0000000000000000000000000000000000000000 748f11eaf2c38c3cf0ac6a22e44208777e79fa6f Zach Rice 1595702825 -0400 clone: from https://github.com/zricethezav/test_repo_8.git diff --git a/test_data/test_repos/test_repo_8/dotGit/logs/refs/heads/master b/test_data/test_repos/test_repo_8/dotGit/logs/refs/heads/master new file mode 100644 index 00000000..c1e43ae7 --- /dev/null +++ b/test_data/test_repos/test_repo_8/dotGit/logs/refs/heads/master @@ -0,0 +1 @@ +0000000000000000000000000000000000000000 748f11eaf2c38c3cf0ac6a22e44208777e79fa6f Zach Rice 1595702825 -0400 clone: from https://github.com/zricethezav/test_repo_8.git diff --git a/test_data/test_repos/test_repo_8/dotGit/logs/refs/remotes/origin/HEAD b/test_data/test_repos/test_repo_8/dotGit/logs/refs/remotes/origin/HEAD new file mode 100644 index 00000000..c1e43ae7 --- /dev/null +++ b/test_data/test_repos/test_repo_8/dotGit/logs/refs/remotes/origin/HEAD @@ -0,0 +1 @@ +0000000000000000000000000000000000000000 748f11eaf2c38c3cf0ac6a22e44208777e79fa6f Zach Rice 1595702825 -0400 clone: from https://github.com/zricethezav/test_repo_8.git diff --git a/test_data/test_repos/test_repo_8/dotGit/objects/0b/aadf22eda42ba55c4fec3e14973edc6cec783a b/test_data/test_repos/test_repo_8/dotGit/objects/0b/aadf22eda42ba55c4fec3e14973edc6cec783a new file mode 100644 index 00000000..1f425766 --- /dev/null +++ b/test_data/test_repos/test_repo_8/dotGit/objects/0b/aadf22eda42ba55c4fec3e14973edc6cec783a @@ -0,0 +1,4 @@ +xuRÉnã8œ³¾‚@�ÄZ¨ è„TdYqäEŠ×K@RÔb[–LQV¬¯O7èÃÌ»ÔC¡^…z¬®ªRG×þ’‚s 2èºÐ´lJufÛ”SmÇrRƒfZšêŽ�ê*4u¥!‚_$ åÌ6 �šaÚ :*µÓâªî:)· KÌŒgü­· …†ûpÊtj¸nFM]s8‡�e®ªiZjd6#.QH'‹Z€aw—Œƒïƒx€xiD-ëKEÊó3««¿�fº¦­ê–j�'ªªò`©$ (å´£àû¥¼9ß_òRýŸ³¼ÉÛ2Oÿöƒp–Á$a0GëØÿÉ+@}‹F{­ðê-›Nµ…ã™]áâfˆ°G(�†!òÄ~ð4áÒ‘úfLÔû—~9š>¶›Ó¹Û|xóþ€ ʰÃw iï«à–ònN‰#Í>¾ÎǘàÅy»vŠt¾Šö´‰fazñ�.CŠ¢‰w5o{ý}t�,'Fñ ¡Äóߨ–Å_ì ÓXÏB¯â÷­y<Ö¢e¥)¨lb3Q@\‡ñ¨1d†ü#´ÛÛr¶ÓG˜«�÷±WΤìó­iä§0y_ÌÔà.Û4êG:ZìÛ`2™( ›M§$zõÔè÷E8>ýk»,o²º-† +LJ'zÇîv¸.ñán 9m±ø¥En) ×‹ä}2ŽŽFP¿n/Á»7 %J®î~—0'¯¨» ö;˜Ÿ®«‰9ЈR»ðf±ì“y'ò +øAOh­üêÌŸ¿þwcJÄEÎAÓ�Ï@ðkÇ[ ¾é u~þ”,ø@nã?ö§†HVHçg›“�À�Ä‘p„‡ ½¬ÚÂ=ÅŒ£ÄãáSá©Ûvw½*©ôiªªÀRà7Á)ŠÉôÌd–i«zšáÌ0m˜ÊŒ˜B šc© Ã�ZÌi-€œbœ1U¥ÖÔë:Ñ%�*šmBšƒPbZÿõjPÚÉ,¬É²¥œ)*¶˜Flƒê–LTU–‰"á^ L +Ì`] +žþ9økËÑÔ.Ï?HSýŠn리–žäÏ«Ò'ýüJP¼Rø} +žë†ÓöüxÍKQôéÖò6ïÊ<ý–3ñ‚XzKÞmâõä‹K@·Î!BÎB+g2ßW‹·µ35‹£öq…<¸!”ùA€œ²� Ûe”Íp…®Ó½}¹�$`LœÕ)Q£—°¸Äè^Ò…u8.šážTs@â8F†«ØÃ›ÊÇ—õUÓù¶‰NLënåó�ûÍ=ÉX�?|÷Ñi¥Ÿˆ“DiÂín}བྷzrXoº8!^³XmÇm©—K eµ›¸§B› ½ºôµÃ¡×s¦T—+Šxn÷óñu÷ü˜ôÓª•å5W½ò¨ɺ—€=«¶¡zàGy£—íþ�Ù‘æN{\