mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-28 20:24:22 +01:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f3a8359ac0 | ||
|
|
829e8fed8c | ||
|
|
c7e3605bff | ||
|
|
c1774b1596 | ||
|
|
35710a7428 | ||
|
|
c7ba4d8bbf | ||
|
|
5585e10933 | ||
|
|
23d5bf0b51 | ||
|
|
b1c7a84fae |
No files matched your search
@@ -1,6 +1,14 @@
|
|||||||
CHANGELOG
|
CHANGELOG
|
||||||
=========
|
=========
|
||||||
|
|
||||||
|
1.2.1
|
||||||
|
-----
|
||||||
|
- Check errors when generating commit patch
|
||||||
|
|
||||||
|
1.2.0
|
||||||
|
-----
|
||||||
|
- Added support for providing an alternate GitHub URL to support scanning GitHub Enteprise repositories
|
||||||
|
|
||||||
1.1.2
|
1.1.2
|
||||||
-----
|
-----
|
||||||
- Added version option
|
- Added version option
|
||||||
|
|||||||
@@ -28,7 +28,9 @@ Application Options:
|
|||||||
-r, --repo= Repo url to audit
|
-r, --repo= Repo url to audit
|
||||||
--github-user= User url to audit
|
--github-user= User url to audit
|
||||||
--github-org= Organization url to audit
|
--github-org= Organization url to audit
|
||||||
|
--github-url= GitHub API Base URL, use for GitHub Enterprise. Example: https://github.example.com/api/v3/ (default: https://api.github.com/)
|
||||||
-p, --private Include private repos in audit
|
-p, --private Include private repos in audit
|
||||||
|
-b, --branch= branch name to audit (defaults to HEAD)
|
||||||
-c, --commit= sha of commit to stop at
|
-c, --commit= sha of commit to stop at
|
||||||
--repo-path= Path to repo
|
--repo-path= Path to repo
|
||||||
--owner-path= Path to owner directory (repos discovered)
|
--owner-path= Path to owner directory (repos discovered)
|
||||||
@@ -42,6 +44,7 @@ Application Options:
|
|||||||
-v, --verbose Show verbose output from gitleaks audit
|
-v, --verbose Show verbose output from gitleaks audit
|
||||||
--report= path to write report file
|
--report= path to write report file
|
||||||
--redact redact secrets from log messages and report
|
--redact redact secrets from log messages and report
|
||||||
|
--version version number
|
||||||
|
|
||||||
Help Options:
|
Help Options:
|
||||||
-h, --help Show this help message
|
-h, --help Show this help message
|
||||||
|
|||||||
@@ -139,6 +139,7 @@ func TestGetRepo(t *testing.T) {
|
|||||||
|
|
||||||
func TestGetOwnerRepo(t *testing.T) {
|
func TestGetOwnerRepo(t *testing.T) {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
dir, err = ioutil.TempDir("", "gitleaksTestOwner")
|
dir, err = ioutil.TempDir("", "gitleaksTestOwner")
|
||||||
defer os.RemoveAll(dir)
|
defer os.RemoveAll(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -6,7 +6,9 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"os/user"
|
"os/user"
|
||||||
"path"
|
"path"
|
||||||
@@ -14,6 +16,7 @@ import (
|
|||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"gopkg.in/src-d/go-git.v4/plumbing"
|
"gopkg.in/src-d/go-git.v4/plumbing"
|
||||||
|
|
||||||
@@ -48,6 +51,7 @@ type Repo struct {
|
|||||||
name string
|
name string
|
||||||
leaks []Leak
|
leaks []Leak
|
||||||
repository *git.Repository
|
repository *git.Repository
|
||||||
|
err error
|
||||||
}
|
}
|
||||||
|
|
||||||
// Owner contains a collection of repos. This could represent an org or user.
|
// Owner contains a collection of repos. This could represent an org or user.
|
||||||
@@ -63,6 +67,7 @@ type Options struct {
|
|||||||
Repo string `short:"r" long:"repo" description:"Repo url to audit"`
|
Repo string `short:"r" long:"repo" description:"Repo url to audit"`
|
||||||
GithubUser string `long:"github-user" description:"User url to audit"`
|
GithubUser string `long:"github-user" description:"User url to audit"`
|
||||||
GithubOrg string `long:"github-org" description:"Organization url to audit"`
|
GithubOrg string `long:"github-org" description:"Organization url to audit"`
|
||||||
|
GithubURL string `long:"github-url" default:"https://api.github.com/" description:"GitHub API Base URL, use for GitHub Enterprise. Example: https://github.example.com/api/v3/"`
|
||||||
IncludePrivate bool `short:"p" long:"private" description:"Include private repos in audit"`
|
IncludePrivate bool `short:"p" long:"private" description:"Include private repos in audit"`
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -109,6 +114,7 @@ type Config struct {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const defaultGithubURL = "https://api.github.com/"
|
||||||
const version = "1.1.2"
|
const version = "1.1.2"
|
||||||
const defaultConfig = `
|
const defaultConfig = `
|
||||||
title = "gitleaks config"
|
title = "gitleaks config"
|
||||||
@@ -227,6 +233,10 @@ func main() {
|
|||||||
repos, err = getOwnerRepos()
|
repos, err = getOwnerRepos()
|
||||||
}
|
}
|
||||||
for _, r := range repos {
|
for _, r := range repos {
|
||||||
|
if r.err != nil {
|
||||||
|
log.Warnf("skipping audit for repo %s due to cloning error: %s", r.name, r.err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
l, err := auditRepo(r.repository)
|
l, err := auditRepo(r.repository)
|
||||||
if len(l) == 0 {
|
if len(l) == 0 {
|
||||||
log.Infof("no leaks found for repo %s", r.name)
|
log.Infof("no leaks found for repo %s", r.name)
|
||||||
@@ -234,7 +244,7 @@ func main() {
|
|||||||
log.Warnf("leaks found for repo %s", r.name)
|
log.Warnf("leaks found for repo %s", r.name)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatalf("error during audit: %s", err)
|
||||||
}
|
}
|
||||||
leaks = append(leaks, l...)
|
leaks = append(leaks, l...)
|
||||||
}
|
}
|
||||||
@@ -244,7 +254,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(leaks) != 0 {
|
if len(leaks) != 0 {
|
||||||
log.Debug("leaks detected")
|
log.Errorf("leaks detected")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -266,8 +276,8 @@ func getRepo() (Repo, error) {
|
|||||||
r *git.Repository
|
r *git.Repository
|
||||||
)
|
)
|
||||||
|
|
||||||
log.Infof("cloning %s", opts.Repo)
|
|
||||||
if opts.Disk {
|
if opts.Disk {
|
||||||
|
log.Infof("cloning %s", opts.Repo)
|
||||||
cloneTarget := fmt.Sprintf("%s/%x", dir, md5.Sum([]byte(fmt.Sprintf("%s%s", opts.GithubUser, opts.Repo))))
|
cloneTarget := fmt.Sprintf("%s/%x", dir, md5.Sum([]byte(fmt.Sprintf("%s%s", opts.GithubUser, opts.Repo))))
|
||||||
if opts.IncludePrivate {
|
if opts.IncludePrivate {
|
||||||
r, err = git.PlainClone(cloneTarget, false, &git.CloneOptions{
|
r, err = git.PlainClone(cloneTarget, false, &git.CloneOptions{
|
||||||
@@ -283,8 +293,10 @@ func getRepo() (Repo, error) {
|
|||||||
}
|
}
|
||||||
} else if opts.RepoPath != "" {
|
} else if opts.RepoPath != "" {
|
||||||
// use existing repo
|
// use existing repo
|
||||||
|
log.Infof("opening %s", opts.Repo)
|
||||||
r, err = git.PlainOpen(opts.RepoPath)
|
r, err = git.PlainOpen(opts.RepoPath)
|
||||||
} else {
|
} else {
|
||||||
|
log.Infof("cloning %s", opts.Repo)
|
||||||
if opts.IncludePrivate {
|
if opts.IncludePrivate {
|
||||||
r, err = git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
|
r, err = git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
|
||||||
URL: opts.Repo,
|
URL: opts.Repo,
|
||||||
@@ -298,14 +310,12 @@ func getRepo() (Repo, error) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err != nil {
|
|
||||||
return Repo{}, err
|
|
||||||
}
|
|
||||||
return Repo{
|
return Repo{
|
||||||
repository: r,
|
repository: r,
|
||||||
path: opts.RepoPath,
|
path: opts.RepoPath,
|
||||||
url: opts.Repo,
|
url: opts.Repo,
|
||||||
name: filepath.Base(opts.Repo),
|
name: filepath.Base(opts.Repo),
|
||||||
|
err: err,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -343,7 +353,6 @@ func auditRef(r *git.Repository, ref *plumbing.Reference, commitWg *sync.WaitGro
|
|||||||
if prevCommit == nil {
|
if prevCommit == nil {
|
||||||
prevCommit = c
|
prevCommit = c
|
||||||
}
|
}
|
||||||
|
|
||||||
commitWg.Add(1)
|
commitWg.Add(1)
|
||||||
go func(c *object.Commit, prevCommit *object.Commit) {
|
go func(c *object.Commit, prevCommit *object.Commit) {
|
||||||
var (
|
var (
|
||||||
@@ -351,7 +360,15 @@ func auditRef(r *git.Repository, ref *plumbing.Reference, commitWg *sync.WaitGro
|
|||||||
filePath string
|
filePath string
|
||||||
skipFile bool
|
skipFile bool
|
||||||
)
|
)
|
||||||
patch, _ := c.Patch(prevCommit)
|
patch, err := c.Patch(prevCommit)
|
||||||
|
if err != nil {
|
||||||
|
log.Warnf("problem generating patch for commit: %s\n", c.Hash.String())
|
||||||
|
if limitGoRoutines {
|
||||||
|
<-semaphore
|
||||||
|
}
|
||||||
|
commitChan <- leaks
|
||||||
|
return
|
||||||
|
}
|
||||||
for _, f := range patch.FilePatches() {
|
for _, f := range patch.FilePatches() {
|
||||||
skipFile = false
|
skipFile = false
|
||||||
from, to := f.Files()
|
from, to := f.Files()
|
||||||
@@ -510,12 +527,22 @@ func getOwnerRepos() ([]Repo, error) {
|
|||||||
repos, err = discoverRepos(opts.OwnerPath)
|
repos, err = discoverRepos(opts.OwnerPath)
|
||||||
} else if opts.GithubOrg != "" {
|
} else if opts.GithubOrg != "" {
|
||||||
githubClient := github.NewClient(githubToken())
|
githubClient := github.NewClient(githubToken())
|
||||||
|
if opts.GithubURL != "" && opts.GithubURL != defaultGithubURL {
|
||||||
|
ghURL, _ := url.Parse(opts.GithubURL)
|
||||||
|
githubClient.BaseURL = ghURL
|
||||||
|
}
|
||||||
|
|
||||||
githubOptions := github.RepositoryListByOrgOptions{
|
githubOptions := github.RepositoryListByOrgOptions{
|
||||||
ListOptions: github.ListOptions{PerPage: 10},
|
ListOptions: github.ListOptions{PerPage: 10},
|
||||||
}
|
}
|
||||||
repos, err = getOrgGithubRepos(ctx, &githubOptions, githubClient)
|
repos, err = getOrgGithubRepos(ctx, &githubOptions, githubClient)
|
||||||
} else if opts.GithubUser != "" {
|
} else if opts.GithubUser != "" {
|
||||||
githubClient := github.NewClient(githubToken())
|
githubClient := github.NewClient(githubToken())
|
||||||
|
if opts.GithubURL != "" && opts.GithubURL != defaultGithubURL {
|
||||||
|
ghURL, _ := url.Parse(opts.GithubURL)
|
||||||
|
githubClient.BaseURL = ghURL
|
||||||
|
}
|
||||||
|
|
||||||
githubOptions := github.RepositoryListOptions{
|
githubOptions := github.RepositoryListOptions{
|
||||||
Affiliation: "owner",
|
Affiliation: "owner",
|
||||||
ListOptions: github.ListOptions{
|
ListOptions: github.ListOptions{
|
||||||
@@ -575,13 +602,11 @@ func getUserGithubRepos(ctx context.Context, listOpts *github.RepositoryListOpti
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err != nil {
|
|
||||||
return repos, fmt.Errorf("problem cloning %s -- %v", *rDesc.Name, err)
|
|
||||||
}
|
|
||||||
repos = append(repos, Repo{
|
repos = append(repos, Repo{
|
||||||
name: *rDesc.Name,
|
name: *rDesc.Name,
|
||||||
url: *rDesc.SSHURL,
|
url: *rDesc.SSHURL,
|
||||||
repository: r,
|
repository: r,
|
||||||
|
err: err,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
if resp.NextPage == 0 {
|
if resp.NextPage == 0 {
|
||||||
@@ -639,18 +664,14 @@ func getOrgGithubRepos(ctx context.Context, listOpts *github.RepositoryListByOrg
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
repos = append(repos, Repo{
|
repos = append(repos, Repo{
|
||||||
url: *rDesc.SSHURL,
|
url: *rDesc.SSHURL,
|
||||||
name: *rDesc.Name,
|
name: *rDesc.Name,
|
||||||
repository: r,
|
repository: r,
|
||||||
|
err: err,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
if err != nil {
|
if resp.NextPage == 0 {
|
||||||
return nil, err
|
|
||||||
} else if resp.NextPage == 0 {
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
listOpts.Page = resp.NextPage
|
listOpts.Page = resp.NextPage
|
||||||
@@ -729,6 +750,27 @@ func optsGuard() error {
|
|||||||
return fmt.Errorf("user/organization private repos require env var GITHUB_TOKEN to be set")
|
return fmt.Errorf("user/organization private repos require env var GITHUB_TOKEN to be set")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// do the URL Parse and error checking here, so we can skip it later
|
||||||
|
// empty string is OK, it will default to the public github URL.
|
||||||
|
if opts.GithubURL != "" && opts.GithubURL != defaultGithubURL {
|
||||||
|
if !strings.HasSuffix(opts.GithubURL, "/") {
|
||||||
|
opts.GithubURL += "/"
|
||||||
|
}
|
||||||
|
ghURL, err := url.Parse(opts.GithubURL)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
tcpPort := "443"
|
||||||
|
if ghURL.Scheme == "http" {
|
||||||
|
tcpPort = "80"
|
||||||
|
}
|
||||||
|
timeout := time.Duration(1 * time.Second)
|
||||||
|
_, err = net.DialTimeout("tcp", ghURL.Host+":"+tcpPort, timeout)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("%s unreachable, error: %s", ghURL.Host, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if opts.SingleSearch != "" {
|
if opts.SingleSearch != "" {
|
||||||
singleSearchRegex, err = regexp.Compile(opts.SingleSearch)
|
singleSearchRegex, err = regexp.Compile(opts.SingleSearch)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in new issue
Block a user