fixing --commit bug, adding --files-at-commit option

This commit is contained in:
zricethezav committed 2020-01-30 20:08:01 -05:00
1 parent d8ddb9630a
commit 5a300c3c64
4 files changed
+57 -16

No files matched your search

+2 -1
View File
@@ -145,11 +145,12 @@ func TestAudit(t *testing.T) {
wantEmpty: true, wantEmpty: true,
}, },
{ {
// TODO UPDATE TESTS
description: "test local repo one aws leak single commit", description: "test local repo one aws leak single commit",
opts: options.Options{ opts: options.Options{
RepoPath: "../test_data/test_repos/test_repo_1", RepoPath: "../test_data/test_repos/test_repo_1",
Report: "../test_data/test_local_repo_one_aws_leak_commit.json.got", Report: "../test_data/test_local_repo_one_aws_leak_commit.json.got",
Commit: "6557c92612d3b35979bd426d429255b3bf9fab74", FilesAtCommit: "6557c92612d3b35979bd426d429255b3bf9fab74",
ReportFormat: "json", ReportFormat: "json",
}, },
wantPath: "../test_data/test_local_repo_one_aws_leak_commit.json", wantPath: "../test_data/test_local_repo_one_aws_leak_commit.json",
+6 -13
View File
@@ -236,19 +236,12 @@ func (repo *Repo) Audit() error {
auditTimeStart := time.Now() auditTimeStart := time.Now()
// audit single Commit // audit commit patches OR all files at commit. See https://github.com/zricethezav/gitleaks/issues/326
// TODO having --commit= and --files-at-commit= set should probably be guarded against
if repo.Manager.Opts.Commit != "" { if repo.Manager.Opts.Commit != "" {
h := plumbing.NewHash(repo.Manager.Opts.Commit) return inspectCommit(repo.Manager.Opts.Commit, repo, inspectCommitPatches)
c, err := repo.CommitObject(h) } else if repo.Manager.Opts.FilesAtCommit != "" {
if err != nil { return inspectCommit(repo.Manager.Opts.FilesAtCommit, repo, inspectFilesAtCommit)
return err
}
err = inspectCommit(c, repo)
if err != nil {
return err
}
return nil
} }
logOpts, err := getLogOptions(repo) logOpts, err := getLogOptions(repo)
@@ -271,7 +264,7 @@ func (repo *Repo) Audit() error {
if len(c.ParentHashes) == 0 { if len(c.ParentHashes) == 0 {
cc++ cc++
err = inspectCommit(c, repo) err = inspectFilesAtCommit(c, repo)
if err != nil { if err != nil {
return err return err
} }
+48 -2
View File
@@ -255,11 +255,57 @@ func InspectString(content string, c *object.Commit, repo *Repo, filename string
} }
} }
// inspectCommit accepts a commit object and a repo. This function is only called when the --commit= type commitInspector func(c *object.Commit, repo *Repo) error
func inspectCommit(hash string, repo *Repo, f commitInspector) error {
h := plumbing.NewHash(hash)
c, err := repo.CommitObject(h)
if err != nil {
return err
}
return f(c, repo)
}
// inspectCommitPatches accepts a commit object and a repo. This function is only called when the --commit=
// option has been set. That option tells gitleaks to look only at a single commit and check the contents // option has been set. That option tells gitleaks to look only at a single commit and check the contents
// of said commit. Similar to inspectPatch(), if the files contained in the commit are a binaries or if they are // of said commit. Similar to inspectPatch(), if the files contained in the commit are a binaries or if they are
// whitelisted then those files will be skipped. // whitelisted then those files will be skipped.
func inspectCommit(c *object.Commit, repo *Repo) error { func inspectCommitPatches(c *object.Commit, repo *Repo) error {
if len(c.ParentHashes) == 0 {
err := inspectFilesAtCommit(c, repo)
if err != nil {
return err
}
}
return 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
}
start := time.Now()
patch, err := c.Patch(parent)
if err != nil {
return fmt.Errorf("could not generate patch")
}
repo.Manager.RecordTime(manager.PatchTime(howLong(start)))
inspectPatch(patch, c, repo)
return nil
})
}
// inspectFilesAtCommit accepts a commit object and a repo. This function is only called when the --commit=
// option has been set. That option tells gitleaks to look only at a single commit and check the contents
// of said commit. Similar to inspectPatch(), if the files contained in the commit are a binaries or if they are
// whitelisted then those files will be skipped.
func inspectFilesAtCommit(c *object.Commit, repo *Repo) error {
fIter, err := c.Files() fIter, err := c.Files()
if err != nil { if err != nil {
return err return err
+1
View File
@@ -36,6 +36,7 @@ type Options struct {
Password string `long:"password" description:"Password for git repo"` Password string `long:"password" description:"Password for git repo"`
AccessToken string `long:"access-token" description:"Access token for git repo"` AccessToken string `long:"access-token" description:"Access token for git repo"`
Commit string `long:"commit" description:"sha of commit to audit"` Commit string `long:"commit" description:"sha of commit to audit"`
FilesAtCommit string `long:"files-at-commit" description:"sha of commit to audit all files at commit"`
Threads int `long:"threads" description:"Maximum number of threads gitleaks spawns"` Threads int `long:"threads" description:"Maximum number of threads gitleaks spawns"`
SSH string `long:"ssh-key" description:"path to ssh key used for auth"` SSH string `long:"ssh-key" description:"path to ssh key used for auth"`
Uncommited bool `long:"uncommitted" description:"run gitleaks on uncommitted code"` Uncommited bool `long:"uncommitted" description:"run gitleaks on uncommitted code"`