mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-29 20:54:22 +01:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
160015b872 | ||
|
|
abdf2c47f0 | ||
|
|
8536f16ceb | ||
|
|
6862c13bf7 | ||
|
|
9939c638ec | ||
|
|
ed3a512b4b | ||
|
|
7c988c927d | ||
|
|
bde9af44ea | ||
|
|
eee4d05642 | ||
|
|
9144594d77 | ||
|
|
1cb15242fd |
No files matched your search
@@ -1,6 +1,19 @@
|
|||||||
CHANGELOG
|
CHANGELOG
|
||||||
=========
|
=========
|
||||||
|
|
||||||
|
1.11.1
|
||||||
|
----
|
||||||
|
- fix commit whitelist logic
|
||||||
|
|
||||||
|
1.11.0
|
||||||
|
-----
|
||||||
|
- Commit depth option
|
||||||
|
- Commit stats output
|
||||||
|
|
||||||
|
1.10.0
|
||||||
|
-----
|
||||||
|
- Add entropy option
|
||||||
|
|
||||||
1.9.0
|
1.9.0
|
||||||
-----
|
-----
|
||||||
- exclude fork option
|
- exclude fork option
|
||||||
|
|||||||
@@ -26,12 +26,13 @@ Usage:
|
|||||||
|
|
||||||
Application Options:
|
Application Options:
|
||||||
-r, --repo= Repo url to audit
|
-r, --repo= Repo url to audit
|
||||||
--github-user= User url to audit
|
--github-user= Github user to audit
|
||||||
--github-org= Organization url to audit
|
--github-org= Github organization to audit
|
||||||
--github-url= GitHub API Base URL, use for GitHub Enterprise. Example: https://github.example.com/api/v3/ (default: https://api.github.com/)
|
--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)
|
-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
|
||||||
|
--depth= maximum commit depth
|
||||||
--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)
|
||||||
--max-go= Maximum number of concurrent go-routines gitleaks spawns
|
--max-go= Maximum number of concurrent go-routines gitleaks spawns
|
||||||
@@ -40,6 +41,8 @@ Application Options:
|
|||||||
--single-search= single regular expression to search for
|
--single-search= single regular expression to search for
|
||||||
--config= path to gitleaks config
|
--config= path to gitleaks config
|
||||||
--ssh-key= path to ssh key
|
--ssh-key= path to ssh key
|
||||||
|
--exclude-forks exclude forks for organization/user audits
|
||||||
|
-e, --entropy= Include entropy checks during audit. Entropy scale: 0.0(no entropy) - 8.0(max entropy)
|
||||||
-l, --log= log level
|
-l, --log= log level
|
||||||
-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
|
||||||
|
|||||||
+40
-34
@@ -560,6 +560,38 @@ func TestAuditRepo(t *testing.T) {
|
|||||||
numLeaks: 0,
|
numLeaks: 0,
|
||||||
configPath: path.Join(configsDir, "repo"),
|
configPath: path.Join(configsDir, "repo"),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
repo: leaksRepo,
|
||||||
|
description: "leaks present with entropy",
|
||||||
|
testOpts: Options{
|
||||||
|
Entropy: 4.7,
|
||||||
|
},
|
||||||
|
numLeaks: 7,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
repo: leaksRepo,
|
||||||
|
description: "Audit until specific commit",
|
||||||
|
numLeaks: 1,
|
||||||
|
testOpts: Options{
|
||||||
|
Commit: "f6839959b7bbdcd23008f1fb16f797f35bcd3a0c",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
repo: leaksRepo,
|
||||||
|
description: "commit depth = 1, no leaks",
|
||||||
|
numLeaks: 0,
|
||||||
|
testOpts: Options{
|
||||||
|
Depth: 1,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
repo: leaksRepo,
|
||||||
|
description: "commit depth = 2, one leak",
|
||||||
|
numLeaks: 1,
|
||||||
|
testOpts: Options{
|
||||||
|
Depth: 2,
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
whiteListCommits = make(map[string]bool)
|
whiteListCommits = make(map[string]bool)
|
||||||
@@ -686,6 +718,14 @@ func TestOptionGuard(t *testing.T) {
|
|||||||
description: "single search regex gaurd",
|
description: "single search regex gaurd",
|
||||||
expectedErrMsg: "",
|
expectedErrMsg: "",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
testOpts: Options{
|
||||||
|
GithubOrg: "fakeOrg",
|
||||||
|
Entropy: 9,
|
||||||
|
},
|
||||||
|
description: "Invalid entropy level guard",
|
||||||
|
expectedErrMsg: "The maximum level of entropy is 8",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
g := goblin.Goblin(t)
|
g := goblin.Goblin(t)
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
@@ -850,22 +890,6 @@ func BenchmarkAuditRepo1000Proc(b *testing.B) {
|
|||||||
auditGitRepo(benchmarkRepo)
|
auditGitRepo(benchmarkRepo)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func BenchmarkAuditRepo10000Proc(b *testing.B) {
|
|
||||||
loadToml()
|
|
||||||
opts.MaxGoRoutines = 10000
|
|
||||||
benchmarkRepo = getBenchmarkRepo()
|
|
||||||
for n := 0; n < b.N; n++ {
|
|
||||||
auditGitRepo(benchmarkRepo)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
func BenchmarkAuditRepo100000Proc(b *testing.B) {
|
|
||||||
loadToml()
|
|
||||||
opts.MaxGoRoutines = 100000
|
|
||||||
benchmarkRepo = getBenchmarkRepo()
|
|
||||||
for n := 0; n < b.N; n++ {
|
|
||||||
auditGitRepo(benchmarkRepo)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
func BenchmarkAuditLeakRepo1Proc(b *testing.B) {
|
func BenchmarkAuditLeakRepo1Proc(b *testing.B) {
|
||||||
loadToml()
|
loadToml()
|
||||||
opts.MaxGoRoutines = 1
|
opts.MaxGoRoutines = 1
|
||||||
@@ -926,21 +950,3 @@ func BenchmarkAuditLeakRepo1000Proc(b *testing.B) {
|
|||||||
auditGitRepo(benchmarkRepo)
|
auditGitRepo(benchmarkRepo)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkAuditLeakRepo10000Proc(b *testing.B) {
|
|
||||||
loadToml()
|
|
||||||
opts.MaxGoRoutines = 10000
|
|
||||||
benchmarkLeaksRepo = getBenchmarkLeaksRepo()
|
|
||||||
for n := 0; n < b.N; n++ {
|
|
||||||
auditGitRepo(benchmarkRepo)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkAuditLeakRepo100000Proc(b *testing.B) {
|
|
||||||
loadToml()
|
|
||||||
opts.MaxGoRoutines = 100000
|
|
||||||
benchmarkLeaksRepo = getBenchmarkLeaksRepo()
|
|
||||||
for n := 0; n < b.N; n++ {
|
|
||||||
auditGitRepo(benchmarkRepo)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -5,8 +5,10 @@ import (
|
|||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"encoding/csv"
|
"encoding/csv"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"math"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
@@ -75,19 +77,21 @@ type Options struct {
|
|||||||
|
|
||||||
Branch string `short:"b" long:"branch" description:"branch name to audit (defaults to HEAD)"`
|
Branch string `short:"b" long:"branch" description:"branch name to audit (defaults to HEAD)"`
|
||||||
Commit string `short:"c" long:"commit" description:"sha of commit to stop at"`
|
Commit string `short:"c" long:"commit" description:"sha of commit to stop at"`
|
||||||
|
Depth int `long:"depth" description:"maximum commit depth"`
|
||||||
|
|
||||||
// local target option
|
// local target option
|
||||||
RepoPath string `long:"repo-path" description:"Path to repo"`
|
RepoPath string `long:"repo-path" description:"Path to repo"`
|
||||||
OwnerPath string `long:"owner-path" description:"Path to owner directory (repos discovered)"`
|
OwnerPath string `long:"owner-path" description:"Path to owner directory (repos discovered)"`
|
||||||
|
|
||||||
// Process options
|
// Process options
|
||||||
MaxGoRoutines int `long:"max-go" description:"Maximum number of concurrent go-routines gitleaks spawns"`
|
MaxGoRoutines int `long:"max-go" description:"Maximum number of concurrent go-routines gitleaks spawns"`
|
||||||
Disk bool `long:"disk" description:"Clones repo(s) to disk"`
|
Disk bool `long:"disk" description:"Clones repo(s) to disk"`
|
||||||
AuditAllRefs bool `long:"all-refs" description:"run audit on all refs"`
|
AuditAllRefs bool `long:"all-refs" description:"run audit on all refs"`
|
||||||
SingleSearch string `long:"single-search" description:"single regular expression to search for"`
|
SingleSearch string `long:"single-search" description:"single regular expression to search for"`
|
||||||
ConfigPath string `long:"config" description:"path to gitleaks config"`
|
ConfigPath string `long:"config" description:"path to gitleaks config"`
|
||||||
SSHKey string `long:"ssh-key" description:"path to ssh key"`
|
SSHKey string `long:"ssh-key" description:"path to ssh key"`
|
||||||
ExcludeForks bool `long:"exclude-forks" description:"exclude forks for organization/user audits"`
|
ExcludeForks bool `long:"exclude-forks" description:"exclude forks for organization/user audits"`
|
||||||
|
Entropy float64 `long:"entropy" short:"e" description:"Include entropy checks during audit. Entropy scale: 0.0(no entropy) - 8.0(max entropy)"`
|
||||||
// TODO: IncludeMessages string `long:"messages" description:"include commit messages in audit"`
|
// TODO: IncludeMessages string `long:"messages" description:"include commit messages in audit"`
|
||||||
|
|
||||||
// Output options
|
// Output options
|
||||||
@@ -124,7 +128,7 @@ type gitDiff struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const defaultGithubURL = "https://api.github.com/"
|
const defaultGithubURL = "https://api.github.com/"
|
||||||
const version = "1.9.0"
|
const version = "1.11.1"
|
||||||
const errExit = 2
|
const errExit = 2
|
||||||
const leakExit = 1
|
const leakExit = 1
|
||||||
const defaultConfig = `
|
const defaultConfig = `
|
||||||
@@ -194,6 +198,7 @@ var (
|
|||||||
sshAuth *ssh.PublicKeys
|
sshAuth *ssh.PublicKeys
|
||||||
dir string
|
dir string
|
||||||
maxGo int
|
maxGo int
|
||||||
|
totalCommits int64
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -222,8 +227,9 @@ func main() {
|
|||||||
writeReport(leaks)
|
writeReport(leaks)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Infof("%d commits inspected", totalCommits)
|
||||||
if len(leaks) != 0 {
|
if len(leaks) != 0 {
|
||||||
log.Warnf("leaks detected")
|
log.Warnf("%d leaks detected", len(leaks))
|
||||||
os.Exit(leakExit)
|
os.Exit(leakExit)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -429,13 +435,14 @@ func auditGitRepo(repo *RepoDescriptor) ([]Leak, error) {
|
|||||||
// the --max-go option (default is set to the number of cores on your cpu).
|
// the --max-go option (default is set to the number of cores on your cpu).
|
||||||
func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak {
|
func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak {
|
||||||
var (
|
var (
|
||||||
err error
|
err error
|
||||||
prevCommit *object.Commit
|
prevCommit *object.Commit
|
||||||
semaphore chan bool
|
semaphore chan bool
|
||||||
repoName string
|
repoName string
|
||||||
leaks []Leak
|
leaks []Leak
|
||||||
commitWg sync.WaitGroup
|
commitWg sync.WaitGroup
|
||||||
mutex = &sync.Mutex{}
|
mutex = &sync.Mutex{}
|
||||||
|
commitCount int
|
||||||
)
|
)
|
||||||
repoName = repo.name
|
repoName = repo.name
|
||||||
if opts.MaxGoRoutines != 0 {
|
if opts.MaxGoRoutines != 0 {
|
||||||
@@ -448,10 +455,14 @@ func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
err = cIter.ForEach(func(c *object.Commit) error {
|
err = cIter.ForEach(func(c *object.Commit) error {
|
||||||
if c.Hash.String() == opts.Commit {
|
if c.Hash.String() == opts.Commit || (opts.Depth != 0 && commitCount == opts.Depth) {
|
||||||
cIter.Close()
|
cIter.Close()
|
||||||
|
return errors.New("ErrStop")
|
||||||
}
|
}
|
||||||
|
commitCount = commitCount + 1
|
||||||
|
totalCommits = totalCommits + 1
|
||||||
if whiteListCommits[c.Hash.String()] {
|
if whiteListCommits[c.Hash.String()] {
|
||||||
|
prevCommit = c
|
||||||
log.Infof("skipping commit: %s\n", c.Hash.String())
|
log.Infof("skipping commit: %s\n", c.Hash.String())
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -576,30 +587,68 @@ func inspect(diff gitDiff) []Leak {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
leak := Leak{
|
leaks = addLeak(leaks, line, match, leakType, diff)
|
||||||
Line: line,
|
}
|
||||||
Commit: diff.commit.Hash.String(),
|
|
||||||
Offender: match,
|
if opts.Entropy > 0 {
|
||||||
Type: leakType,
|
words := strings.Fields(line)
|
||||||
Message: diff.commit.Message,
|
for _, word := range words {
|
||||||
Author: diff.commit.Author.String(),
|
if getShannonEntropy(word) >= opts.Entropy {
|
||||||
File: diff.filePath,
|
leaks = addLeak(leaks, line, word, "High Entropy", diff)
|
||||||
Branch: diff.branchName,
|
}
|
||||||
Repo: diff.repoName,
|
|
||||||
}
|
}
|
||||||
if opts.Redact {
|
|
||||||
leak.Offender = "REDACTED"
|
|
||||||
leak.Line = "REDACTED"
|
|
||||||
}
|
|
||||||
if opts.Verbose {
|
|
||||||
leak.log()
|
|
||||||
}
|
|
||||||
leaks = append(leaks, leak)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return leaks
|
return leaks
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getShannonEntropy https://en.wiktionary.org/wiki/Shannon_entropy
|
||||||
|
func getShannonEntropy(data string) (entropy float64) {
|
||||||
|
if data == "" {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
charCounts := make(map[rune]int)
|
||||||
|
for _, char := range data {
|
||||||
|
charCounts[char]++
|
||||||
|
}
|
||||||
|
|
||||||
|
invLength := 1.0 / float64(len(data))
|
||||||
|
for _, count := range charCounts {
|
||||||
|
freq := float64(count) * invLength
|
||||||
|
entropy -= freq * math.Log2(freq)
|
||||||
|
}
|
||||||
|
|
||||||
|
return entropy
|
||||||
|
}
|
||||||
|
|
||||||
|
// addLeak is helper for func inspect() to append leaks if found during a diff check.
|
||||||
|
func addLeak(leaks []Leak, line string, offender string, leakType string, diff gitDiff) []Leak {
|
||||||
|
leak := Leak{
|
||||||
|
Line: line,
|
||||||
|
Commit: diff.commit.Hash.String(),
|
||||||
|
Offender: offender,
|
||||||
|
Type: leakType,
|
||||||
|
Message: diff.commit.Message,
|
||||||
|
Author: diff.commit.Author.String(),
|
||||||
|
File: diff.filePath,
|
||||||
|
Branch: diff.branchName,
|
||||||
|
Repo: diff.repoName,
|
||||||
|
}
|
||||||
|
|
||||||
|
if opts.Redact {
|
||||||
|
leak.Offender = "REDACTED"
|
||||||
|
leak.Line = "REDACTED"
|
||||||
|
}
|
||||||
|
|
||||||
|
if opts.Verbose {
|
||||||
|
leak.log()
|
||||||
|
}
|
||||||
|
|
||||||
|
leaks = append(leaks, leak)
|
||||||
|
return leaks
|
||||||
|
}
|
||||||
|
|
||||||
// auditGithubRepos kicks off audits if --github-user or --github-org options are set.
|
// auditGithubRepos kicks off audits if --github-user or --github-org options are set.
|
||||||
// First, we gather all the github repositories from the github api (this doesnt actually clone the repo).
|
// First, we gather all the github repositories from the github api (this doesnt actually clone the repo).
|
||||||
// After all the repos have been pulled from github's api we proceed to audit the repos by calling auditGithubRepo.
|
// After all the repos have been pulled from github's api we proceed to audit the repos by calling auditGithubRepo.
|
||||||
@@ -866,6 +915,10 @@ func optsGuard() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if opts.Entropy > 8 {
|
||||||
|
return fmt.Errorf("The maximum level of entropy is 8")
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user