mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-30 13:14:22 +01:00
fixing --commit bug, adding --files-at-commit option
This commit is contained in:
1 parent
d8ddb9630a
commit
5a300c3c64
4 files changed
+85
-44
No files matched your search
+5
-4
@@ -145,12 +145,13 @@ 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
@@ -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
@@ -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
|
||||||
|
|||||||
+26
-25
@@ -27,31 +27,32 @@ const (
|
|||||||
|
|
||||||
// Options stores values of command line options
|
// Options stores values of command line options
|
||||||
type Options struct {
|
type Options struct {
|
||||||
Verbose bool `short:"v" long:"verbose" description:"Show verbose output from audit"`
|
Verbose bool `short:"v" long:"verbose" description:"Show verbose output from audit"`
|
||||||
Repo string `short:"r" long:"repo" description:"Target repository"`
|
Repo string `short:"r" long:"repo" description:"Target repository"`
|
||||||
Config string `long:"config" description:"config path"`
|
Config string `long:"config" description:"config path"`
|
||||||
Disk bool `long:"disk" description:"Clones repo(s) to disk"`
|
Disk bool `long:"disk" description:"Clones repo(s) to disk"`
|
||||||
Version bool `long:"version" description:"version number"`
|
Version bool `long:"version" description:"version number"`
|
||||||
Username string `long:"username" description:"Username for git repo"`
|
Username string `long:"username" description:"Username for git repo"`
|
||||||
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"`
|
||||||
Threads int `long:"threads" description:"Maximum number of threads gitleaks spawns"`
|
FilesAtCommit string `long:"files-at-commit" description:"sha of commit to audit all files at commit"`
|
||||||
SSH string `long:"ssh-key" description:"path to ssh key used for auth"`
|
Threads int `long:"threads" description:"Maximum number of threads gitleaks spawns"`
|
||||||
Uncommited bool `long:"uncommitted" description:"run gitleaks on uncommitted code"`
|
SSH string `long:"ssh-key" description:"path to ssh key used for auth"`
|
||||||
RepoPath string `long:"repo-path" description:"Path to repo"`
|
Uncommited bool `long:"uncommitted" description:"run gitleaks on uncommitted code"`
|
||||||
OwnerPath string `long:"owner-path" description:"Path to owner directory (repos discovered)"`
|
RepoPath string `long:"repo-path" description:"Path to repo"`
|
||||||
Branch string `long:"branch" description:"Branch to audit"`
|
OwnerPath string `long:"owner-path" description:"Path to owner directory (repos discovered)"`
|
||||||
Report string `long:"report" description:"path to write json leaks file"`
|
Branch string `long:"branch" description:"Branch to audit"`
|
||||||
ReportFormat string `long:"report-format" default:"json" description:"json or csv"`
|
Report string `long:"report" description:"path to write json leaks file"`
|
||||||
Redact bool `long:"redact" description:"redact secrets from log messages and leaks"`
|
ReportFormat string `long:"report-format" default:"json" description:"json or csv"`
|
||||||
Debug bool `long:"debug" description:"log debug messages"`
|
Redact bool `long:"redact" description:"redact secrets from log messages and leaks"`
|
||||||
RepoConfig bool `long:"repo-config" description:"Load config from target repo. Config file must be \".gitleaks.toml\" or \"gitleaks.toml\""`
|
Debug bool `long:"debug" description:"log debug messages"`
|
||||||
PrettyPrint bool `long:"pretty" description:"Pretty print json if leaks are present"`
|
RepoConfig bool `long:"repo-config" description:"Load config from target repo. Config file must be \".gitleaks.toml\" or \"gitleaks.toml\""`
|
||||||
CommitFrom string `long:"commit-from" description:"Commit to start audit from"`
|
PrettyPrint bool `long:"pretty" description:"Pretty print json if leaks are present"`
|
||||||
CommitTo string `long:"commit-to" description:"Commit to stop audit"`
|
CommitFrom string `long:"commit-from" description:"Commit to start audit from"`
|
||||||
Timeout string `long:"timeout" description:"Time allowed per audit. Ex: 10us, 30s, 1m, 1h10m1s"`
|
CommitTo string `long:"commit-to" description:"Commit to stop audit"`
|
||||||
Depth int `long:"depth" description:"Number of commits to audit"`
|
Timeout string `long:"timeout" description:"Time allowed per audit. Ex: 10us, 30s, 1m, 1h10m1s"`
|
||||||
|
Depth int `long:"depth" description:"Number of commits to audit"`
|
||||||
|
|
||||||
// Hosts
|
// Hosts
|
||||||
Host string `long:"host" description:"git hosting service like gitlab or github. Supported hosts include: Github, Gitlab"`
|
Host string `long:"host" description:"git hosting service like gitlab or github. Supported hosts include: Github, Gitlab"`
|
||||||
|
|||||||
Reference in new issue
Block a user