From 7676690697c03f80357188fe038d666f6759ef14 Mon Sep 17 00:00:00 2001 From: Cassio Berra Date: Sat, 18 May 2019 01:02:46 +0000 Subject: [PATCH 1/6] Audit single commit without iterating through all commits --- src/repo.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/repo.go b/src/repo.go index 8ac2ba64..e53461ad 100644 --- a/src/repo.go +++ b/src/repo.go @@ -133,7 +133,21 @@ func (repoInfo *RepoInfo) audit() ([]Leak, error) { } } - if opts.Branch != "" { + if opts.Commit != "" { + h := plumbing.NewHash(opts.Commit) + c, err := repoInfo.repository.CommitObject(h) + if err != nil { + return leaks, nil + } + + commitCount = commitCount + 1 + totalCommits = totalCommits + 1 + leaksFromSingleCommit := repoInfo.auditSingleCommit(c) + mutex.Lock() + leaks = append(leaksFromSingleCommit, leaks...) + mutex.Unlock() + return leaks, err + } else if opts.Branch != "" { refs, err := repoInfo.repository.Storer.IterReferences() if err != nil { return leaks, err @@ -187,8 +201,8 @@ func (repoInfo *RepoInfo) audit() ([]Leak, error) { return nil } - // commits w/o parent (root of git the git ref) or option for single commit is not empty str - if (len(c.ParentHashes) == 0 && opts.Commit == "") || (len(c.ParentHashes) == 0 && opts.Commit == c.Hash.String()) { + // commits w/o parent (root of git the git ref) + if len(c.ParentHashes) == 0 { commitCount = commitCount + 1 totalCommits = totalCommits + 1 leaksFromSingleCommit := repoInfo.auditSingleCommit(c) From d4be9602e32fed10aa188c96c02854781790e6f0 Mon Sep 17 00:00:00 2001 From: Cassio Berra Date: Sat, 18 May 2019 01:24:18 +0000 Subject: [PATCH 2/6] Removing other unnecessary references to opts.Commit --- src/repo.go | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/repo.go b/src/repo.go index e53461ad..8746d0a5 100644 --- a/src/repo.go +++ b/src/repo.go @@ -209,13 +209,6 @@ func (repoInfo *RepoInfo) audit() ([]Leak, error) { mutex.Lock() leaks = append(leaksFromSingleCommit, leaks...) mutex.Unlock() - if opts.Commit == c.Hash.String() { - return storer.ErrStop - } - return nil - } - - if opts.Commit != "" && opts.Commit != c.Hash.String() { return nil } From d6578df9989252a215810d4e675e546545acab46 Mon Sep 17 00:00:00 2001 From: Eduardo Argollo Date: Tue, 28 May 2019 15:35:10 -0700 Subject: [PATCH 3/6] Solved bug on error being overwritten and not passed to postAudit, closes #223 --- main.go | 7 ++++++- src/core.go | 14 +++++--------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index b02e52d5..4ce83c4a 100644 --- a/main.go +++ b/main.go @@ -2,14 +2,19 @@ package main import ( "os" + "strings" log "github.com/sirupsen/logrus" - "github.com/zricethezav/gitleaks/src" + gitleaks "github.com/zricethezav/gitleaks/src" ) func main() { report, err := gitleaks.Run(gitleaks.ParseOpts()) if err != nil { + if strings.Contains(err.Error(), "whitelisted") { + log.Info(err.Error()) + os.Exit(0) + } log.Error(err) os.Exit(gitleaks.ErrExit) } diff --git a/src/core.go b/src/core.go index 353a72ac..c119c391 100644 --- a/src/core.go +++ b/src/core.go @@ -3,7 +3,6 @@ package gitleaks import ( "io/ioutil" "os" - "strings" "sync" "time" @@ -60,7 +59,8 @@ func Run(optsL *Options) (*Report, error) { // start audits if opts.Repo != "" || opts.RepoPath != "" { - repoInfo, err := newRepoInfo() + var repoInfo *RepoInfo + repoInfo, err = newRepoInfo() if err != nil { goto postAudit } @@ -70,7 +70,8 @@ func Run(optsL *Options) (*Report, error) { } leaks, err = repoInfo.audit() } else if opts.OwnerPath != "" { - repoDs, err := discoverRepos(opts.OwnerPath) + var repoDs []*RepoInfo + repoDs, err = discoverRepos(opts.OwnerPath) if err != nil { goto postAudit } @@ -96,12 +97,7 @@ func Run(optsL *Options) (*Report, error) { postAudit: if err != nil { - if strings.Contains(err.Error(), "whitelisted") { - log.Info(err.Error()) - os.Exit(0) - } - log.Error(err) - os.Exit(ErrExit) + return &Report{}, err } if opts.Report != "" { From faeb0914b26b60210e5b66fb004ce8b86379f85b Mon Sep 17 00:00:00 2001 From: Eduardo Argollo Date: Thu, 30 May 2019 15:09:45 -0700 Subject: [PATCH 4/6] Solving error on authentication --- src/repo.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/repo.go b/src/repo.go index 8ac2ba64..8266d32f 100644 --- a/src/repo.go +++ b/src/repo.go @@ -15,6 +15,7 @@ import ( diffType "gopkg.in/src-d/go-git.v4/plumbing/format/diff" "gopkg.in/src-d/go-git.v4/plumbing/object" "gopkg.in/src-d/go-git.v4/plumbing/storer" + gitHttp "gopkg.in/src-d/go-git.v4/plumbing/transport/http" "gopkg.in/src-d/go-git.v4/storage/memory" ) @@ -98,10 +99,17 @@ func (repoInfo *RepoInfo) clone() error { Auth: config.sshAuth, }) } else { - repo, err = git.Clone(memory.NewStorage(), nil, &git.CloneOptions{ + options := &git.CloneOptions{ URL: opts.Repo, Progress: os.Stdout, - }) + } + if os.Getenv("GITHUB_TOKEN") != "" { + options.Auth = &gitHttp.BasicAuth{ + Username: "fakeUsername", // yes, this can be anything except an empty string + Password: os.Getenv("GITHUB_TOKEN"), + } + } + repo, err = git.Clone(memory.NewStorage(), nil, options) } } repoInfo.repository = repo From e18b1a884a5e8892453d85f7492f8deec24b56e0 Mon Sep 17 00:00:00 2001 From: Eduardo Argollo Date: Thu, 30 May 2019 15:11:37 -0700 Subject: [PATCH 5/6] Updating expected error message --- src/gitleaks_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gitleaks_test.go b/src/gitleaks_test.go index 8fb587f0..3174d9c9 100644 --- a/src/gitleaks_test.go +++ b/src/gitleaks_test.go @@ -206,7 +206,7 @@ func TestRun(t *testing.T) { }, description: "test empty", numLeaks: 0, - expectedErrMsg: "reference not found", + expectedErrMsg: "repository not found", }, { testOpts: &Options{ From 3b6c198715026ddfa7d6a68ba371e84c0f5c6197 Mon Sep 17 00:00:00 2001 From: zach rice Date: Sun, 2 Jun 2019 20:55:24 -0500 Subject: [PATCH 6/6] fixing test --- src/gitleaks_test.go | 8 ++++---- src/repo.go | 11 +++++++++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/gitleaks_test.go b/src/gitleaks_test.go index 57406529..f5c7c1a1 100644 --- a/src/gitleaks_test.go +++ b/src/gitleaks_test.go @@ -64,22 +64,22 @@ func TestGetRepo(t *testing.T) { Repo: "https://github.com/gitleakstest/nope", }, description: "test no repo", - expectedErrMsg: "authentication required", + expectedErrMsg: "repository not found", }, { testOpts: &Options{ Repo: "https://github.com/gitleakstest/private", }, description: "test private repo", - expectedErrMsg: "authentication required", + expectedErrMsg: "repository not found", }, { testOpts: &Options{ Repo: "https://github.com/gitleakstest/private", Disk: true, }, - description: "test private repo", - expectedErrMsg: "authentication required", + description: "test private repo disk", + expectedErrMsg: "repository not found", }, } g := goblin.Goblin(t) diff --git a/src/repo.go b/src/repo.go index 59707a50..c73b3c63 100644 --- a/src/repo.go +++ b/src/repo.go @@ -78,10 +78,17 @@ func (repoInfo *RepoInfo) clone() error { }) } else { // public - repo, err = git.PlainClone(cloneTarget, false, &git.CloneOptions{ + options := &git.CloneOptions{ URL: opts.Repo, Progress: os.Stdout, - }) + } + if os.Getenv("GITHUB_TOKEN") != "" { + options.Auth = &gitHttp.BasicAuth{ + Username: "fakeUsername", // yes, this can be anything except an empty string + Password: os.Getenv("GITHUB_TOKEN"), + } + } + repo, err = git.PlainClone(cloneTarget, false, options) } } else if repoInfo.path != "" { log.Infof("opening %s", repoInfo.path)