From d9a060a12d12e2538c0cf2885827d13f8d411c04 Mon Sep 17 00:00:00 2001 From: Peter Gallagher Date: Thu, 28 Nov 2019 13:53:18 +0000 Subject: [PATCH 1/7] Honour file whitelist for AuditUncommitted function --- audit/repo.go | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/audit/repo.go b/audit/repo.go index a08de932..662f3f47 100644 --- a/audit/repo.go +++ b/audit/repo.go @@ -146,18 +146,22 @@ func (repo *Repo) AuditUncommitted() error { } } - dmp := diffmatchpatch.New() - diffs := dmp.DiffMain(prevFileContents, currFileContents, false) - var diffContents string - for _, d := range diffs { - switch d.Type { - case diffmatchpatch.DiffInsert: - diffContents += fmt.Sprintf("%s\n", d.Text) - case diffmatchpatch.DiffDelete: - diffContents += fmt.Sprintf("%s\n", d.Text) + if fileMatched(filename, repo.config.Whitelist.File) { + log.Debugf("whitelisted file found, skipping audit of file: %s", filename) + } else { + dmp := diffmatchpatch.New() + diffs := dmp.DiffMain(prevFileContents, currFileContents, false) + var diffContents string + for _, d := range diffs { + switch d.Type { + case diffmatchpatch.DiffInsert: + diffContents += fmt.Sprintf("%s\n", d.Text) + case diffmatchpatch.DiffDelete: + diffContents += fmt.Sprintf("%s\n", d.Text) + } } + InspectString(diffContents, c, repo, filename) } - InspectString(diffContents, c, repo, filename) } } From 9b3342fbff59add3e25d0495c616100451ab2da4 Mon Sep 17 00:00:00 2001 From: Peter Gallagher Date: Thu, 28 Nov 2019 13:53:52 +0000 Subject: [PATCH 2/7] Allow use of --repo-config for AuditUncommitted function --- audit/repo.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/audit/repo.go b/audit/repo.go index 662f3f47..bcea1f50 100644 --- a/audit/repo.go +++ b/audit/repo.go @@ -84,6 +84,15 @@ func (repo *Repo) Clone(cloneOption *git.CloneOptions) error { // AuditUncommitted will do a `git diff` and scan changed files that are being tracked. This is useful functionality // for a pre-commit hook so you can make sure your code does not have any leaks before committing. func (repo *Repo) AuditUncommitted() error { + // load up alternative config if possible, if not use manager's config + if repo.Manager.Opts.RepoConfig { + cfg, err := repo.loadRepoConfig() + if err != nil { + return err + } + repo.config = cfg + } + auditTimeStart := time.Now() r, err := repo.Head() From c04e137b280e4da4e5fc0decb2b88eaa7c738537 Mon Sep 17 00:00:00 2001 From: Peter Gallagher Date: Thu, 28 Nov 2019 22:53:08 +0000 Subject: [PATCH 3/7] Adding global file regex to AuditUncommitted --- audit/repo.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/audit/repo.go b/audit/repo.go index bcea1f50..68dfc27b 100644 --- a/audit/repo.go +++ b/audit/repo.go @@ -157,6 +157,18 @@ func (repo *Repo) AuditUncommitted() error { if fileMatched(filename, repo.config.Whitelist.File) { log.Debugf("whitelisted file found, skipping audit of file: %s", filename) + } else if fileMatched(filename, repo.config.FileRegex) { + repo.Manager.SendLeaks(manager.Leak{ + Line: "N/A", + Offender: filename, + Commit: c.Hash.String(), + Repo: repo.Name, + Rule: "file regex matched" + repo.config.FileRegex.String(), + Author: c.Author.Name, + Email: c.Author.Email, + Date: c.Author.When, + File: filename, + }) } else { dmp := diffmatchpatch.New() diffs := dmp.DiffMain(prevFileContents, currFileContents, false) From 5152e1fc638055b7a77b8078c5df821fd0e27eff Mon Sep 17 00:00:00 2001 From: Peter Gallagher999 Date: Thu, 28 Nov 2019 23:13:59 +0000 Subject: [PATCH 4/7] Reset commit values when auditing uncommitted changes --- audit/repo.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/audit/repo.go b/audit/repo.go index 68dfc27b..c8b27776 100644 --- a/audit/repo.go +++ b/audit/repo.go @@ -104,6 +104,11 @@ func (repo *Repo) AuditUncommitted() error { if err != nil { return err } + c.Hash = plumbing.Hash{} + c.Message = "STAGED CHANGES" + c.Author.Name = "" + c.Author.Email = "" + c.Author.When = time.Now() prevTree, err := c.Tree() if err != nil { @@ -164,6 +169,7 @@ func (repo *Repo) AuditUncommitted() error { Commit: c.Hash.String(), Repo: repo.Name, Rule: "file regex matched" + repo.config.FileRegex.String(), + Message: c.Message, Author: c.Author.Name, Email: c.Author.Email, Date: c.Author.When, From db79a961183fad80c9ee181de7f20da90e1ac9a7 Mon Sep 17 00:00:00 2001 From: Peter Gallagher Date: Fri, 29 Nov 2019 10:07:27 +0000 Subject: [PATCH 5/7] Use Unix Epoch time for staged commit time --- audit/repo.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/audit/repo.go b/audit/repo.go index c8b27776..aec78f5f 100644 --- a/audit/repo.go +++ b/audit/repo.go @@ -104,11 +104,12 @@ func (repo *Repo) AuditUncommitted() error { if err != nil { return err } + // Staged change so the commit details do not yet exist. Insert empty defaults. c.Hash = plumbing.Hash{} - c.Message = "STAGED CHANGES" + c.Message = "***STAGED CHANGES***" c.Author.Name = "" c.Author.Email = "" - c.Author.When = time.Now() + c.Author.When = time.Unix(0, 0).UTC() prevTree, err := c.Tree() if err != nil { From 3655284b573893d731fdd4115713937605f60592 Mon Sep 17 00:00:00 2001 From: Peter Gallagher Date: Fri, 29 Nov 2019 10:07:56 +0000 Subject: [PATCH 6/7] Fix test data for AuditUncommitted --- .../test_local_repo_one_aws_leak_uncommitted.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test_data/test_local_repo_one_aws_leak_uncommitted.json b/test_data/test_local_repo_one_aws_leak_uncommitted.json index 18c60120..46bf7197 100644 --- a/test_data/test_local_repo_one_aws_leak_uncommitted.json +++ b/test_data/test_local_repo_one_aws_leak_uncommitted.json @@ -2,14 +2,14 @@ { "line": " aws_access_key_id='AKIAIO5FODNN7DXAMPLE'", "offender": "AKIAIO5FODNN7DXAMPLE", - "commit": "d274003914c707212cbe84e3e466a00013ccb639", + "commit": "0000000000000000000000000000000000000000", "repo": "test_repo_1", "rule": "AWS Manager ID", - "commitMessage": "comment\n", - "author": "zach rice", - "email": "zricer@protonmail.com", + "commitMessage": "STAGED CHANGES", + "author": "", + "email": "", "file": "server.test.py", - "date": "2019-10-24T10:03:38-04:00", + "date": "1970-01-01T00:00:00Z", "tags": "key, AWS" } ] From 38e0b11995997581b8b6067b5db02da184027f8b Mon Sep 17 00:00:00 2001 From: Peter Gallagher Date: Fri, 29 Nov 2019 10:22:02 +0000 Subject: [PATCH 7/7] Fix test data for AuditUncommitted --- test_data/test_local_repo_one_aws_leak_uncommitted.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test_data/test_local_repo_one_aws_leak_uncommitted.json b/test_data/test_local_repo_one_aws_leak_uncommitted.json index 46bf7197..c624672e 100644 --- a/test_data/test_local_repo_one_aws_leak_uncommitted.json +++ b/test_data/test_local_repo_one_aws_leak_uncommitted.json @@ -5,7 +5,7 @@ "commit": "0000000000000000000000000000000000000000", "repo": "test_repo_1", "rule": "AWS Manager ID", - "commitMessage": "STAGED CHANGES", + "commitMessage": "***STAGED CHANGES***", "author": "", "email": "", "file": "server.test.py",