mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-27 19:54:24 +01:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
35531233d9 | ||
|
|
54949bd62a | ||
|
|
e95a8a7158 | ||
|
|
de0a70a835 | ||
|
|
9f8feca926 | ||
|
|
c50187559b | ||
|
|
7d5ed837a4 | ||
|
|
60267523e9 | ||
|
|
8153da6e9c | ||
|
|
5f6fb388ec | ||
|
|
ba49396755 | ||
|
|
40240cb5ee | ||
|
|
bba926693e | ||
|
|
9103834503 | ||
|
|
7e3c740592 | ||
|
|
35a59746df | ||
|
|
23b5ce3264 | ||
|
|
03c53d2978 | ||
|
|
695449d73d | ||
|
|
6bad1e7194 | ||
|
|
77dec1461b | ||
|
|
14d77f59d4 | ||
|
|
afb598cf15 | ||
|
|
b4c6fe92e0 | ||
|
|
f50577be1c | ||
|
|
c358ae9310 | ||
|
|
9554258d02 | ||
|
|
95ee2cc30e | ||
|
|
79a35f0c27 | ||
|
|
7a021cda9e | ||
|
|
447f7a04cb | ||
|
|
e5b62cbc7c | ||
|
|
6739a34f74 | ||
|
|
e502bf29de | ||
|
|
51b504bf99 |
No files matched your search
@@ -1,6 +1,41 @@
|
||||
CHANGELOG
|
||||
=========
|
||||
|
||||
1.19.3
|
||||
----
|
||||
- memoizing root commit
|
||||
- only count commits (not commit + parent) for total commit number
|
||||
|
||||
1.19.2
|
||||
----
|
||||
- fixed a bug where gitleaks was skipping the initial commit
|
||||
- commit cache now checks curr commit + parent commit hash
|
||||
- removed newlines from commit message
|
||||
|
||||
1.19.1
|
||||
----
|
||||
- mistakenly removed default whitelist files
|
||||
|
||||
1.19.0
|
||||
----
|
||||
- removed `--all-refs`. By default gitleaks now scans all branches, remote and local.
|
||||
- added commit memoizer to prevent duplicate commit audits
|
||||
- removed branch whitelist
|
||||
- removed branch from report as a commit is an object independent of branch
|
||||
- Better regexes for facebook, github, twitter (no more unbounded wildcards)
|
||||
|
||||
1.18.0
|
||||
----
|
||||
- fixing slack token
|
||||
- defaulting to single thread process
|
||||
- `--max-go=` --> `--threads=`
|
||||
- guarding `--threads=` > max threads available (you aint gettin any more performance)
|
||||
|
||||
1.17.0
|
||||
----
|
||||
- Default regex added to search: slack, telegram.
|
||||
- Default whitelisting: bin, doc, jpeg, gif
|
||||
|
||||
1.16.1
|
||||
----
|
||||
- Fixing default ssh auth logic
|
||||
|
||||
@@ -68,14 +68,12 @@ Application Options:
|
||||
--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-pr= Github PR url to audit. This does not clone the repo. GITHUB_TOKEN must be set
|
||||
-b, --branch= branch name to audit (defaults to HEAD)
|
||||
-c, --commit= sha of commit to stop at
|
||||
--depth= maximum commit depth
|
||||
--repo-path= Path to repo
|
||||
--owner-path= Path to owner directory (repos discovered)
|
||||
--max-go= Maximum number of concurrent go-routines gitleaks spawns
|
||||
--threads= Maximum number of threads gitleaks spawns
|
||||
--disk Clones repo(s) to disk
|
||||
--all-refs run audit on all refs
|
||||
--single-search= single regular expression to search for
|
||||
--config= path to gitleaks config
|
||||
--ssh-key= path to ssh key
|
||||
|
||||
@@ -143,12 +143,8 @@ func auditGithubRepos() ([]Leak, error) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if opts.Disk {
|
||||
ownerDir, err = ioutil.TempDir(dir, opts.GithubUser)
|
||||
os.RemoveAll(ownerDir)
|
||||
ownerDir, _ = ioutil.TempDir(dir, opts.GithubUser)
|
||||
}
|
||||
for _, githubRepo := range githubRepos {
|
||||
repo, err := cloneGithubRepo(githubRepo)
|
||||
|
||||
@@ -20,6 +20,15 @@ regex = '''(?i)facebook.*['\"][0-9a-f]{32}['\"]'''
|
||||
[[regexes]]
|
||||
description = "Twitter"
|
||||
regex = '''(?i)twitter.*['\"][0-9a-zA-Z]{35,44}['\"]'''
|
||||
[[regexes]]
|
||||
description = "Telegram"
|
||||
regex = '''(?i)telegram.*['\"][0-9]{1,12}+:[0-9a-zA-Z-]{32,44}['\"]'''
|
||||
[[regexes]]
|
||||
description = "PGP"
|
||||
regex = '''-----BEGIN PGP PRIVATE KEY BLOCK-----'''
|
||||
[[regexes]]
|
||||
description = "Slack token"
|
||||
regex = '''xox[baprs]-.*'''
|
||||
|
||||
[whitelist]
|
||||
regexes = [
|
||||
|
||||
+22
-234
@@ -8,6 +8,7 @@ import (
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/franela/goblin"
|
||||
git "gopkg.in/src-d/go-git.v4"
|
||||
@@ -34,16 +35,6 @@ files = [
|
||||
".go",
|
||||
]
|
||||
`
|
||||
const testWhitelistBranch = `
|
||||
[[regexes]]
|
||||
description = "AWS"
|
||||
regex = '''AKIA[0-9A-Z]{16}'''
|
||||
|
||||
[whitelist]
|
||||
branches = [
|
||||
"origin/master",
|
||||
]
|
||||
`
|
||||
|
||||
const testWhitelistRegex = `
|
||||
[[regexes]]
|
||||
@@ -87,35 +78,6 @@ entropy = [
|
||||
]
|
||||
`
|
||||
|
||||
var benchmarkRepo *RepoDescriptor
|
||||
var benchmarkLeaksRepo *RepoDescriptor
|
||||
|
||||
func getBenchmarkLeaksRepo() *RepoDescriptor {
|
||||
if benchmarkLeaksRepo != nil {
|
||||
return benchmarkLeaksRepo
|
||||
}
|
||||
leaksR, _ := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
|
||||
URL: "https://github.com/gitleakstest/gronit.git",
|
||||
})
|
||||
benchmarkLeaksRepo = &RepoDescriptor{
|
||||
repository: leaksR,
|
||||
}
|
||||
return benchmarkLeaksRepo
|
||||
}
|
||||
|
||||
func getBenchmarkRepo() *RepoDescriptor {
|
||||
if benchmarkRepo != nil {
|
||||
return benchmarkRepo
|
||||
}
|
||||
bmRepo, _ := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
|
||||
URL: "https://github.com/apple/swift-package-manager.git",
|
||||
})
|
||||
benchmarkRepo = &RepoDescriptor{
|
||||
repository: bmRepo,
|
||||
}
|
||||
return benchmarkRepo
|
||||
}
|
||||
|
||||
func TestGetRepo(t *testing.T) {
|
||||
var err error
|
||||
dir, err = ioutil.TempDir("", "gitleaksTestRepo")
|
||||
@@ -367,7 +329,7 @@ func TestWriteReport(t *testing.T) {
|
||||
Message: "get",
|
||||
Author: "some",
|
||||
File: "sleep",
|
||||
Branch: "thxu",
|
||||
Date: time.Now(),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -440,7 +402,6 @@ func TestWriteReport(t *testing.T) {
|
||||
func testTomlLoader() string {
|
||||
tmpDir, _ := ioutil.TempDir("", "whiteListConfigs")
|
||||
ioutil.WriteFile(path.Join(tmpDir, "regex"), []byte(testWhitelistRegex), 0644)
|
||||
ioutil.WriteFile(path.Join(tmpDir, "branch"), []byte(testWhitelistBranch), 0644)
|
||||
ioutil.WriteFile(path.Join(tmpDir, "commit"), []byte(testWhitelistCommit), 0644)
|
||||
ioutil.WriteFile(path.Join(tmpDir, "file"), []byte(testWhitelistFile), 0644)
|
||||
ioutil.WriteFile(path.Join(tmpDir, "repo"), []byte(testWhitelistRepo), 0644)
|
||||
@@ -482,18 +443,25 @@ func TestAuditRepo(t *testing.T) {
|
||||
}
|
||||
|
||||
var tests = []struct {
|
||||
testOpts Options
|
||||
description string
|
||||
expectedErrMsg string
|
||||
numLeaks int
|
||||
repo *RepoDescriptor
|
||||
whiteListFiles []*regexp.Regexp
|
||||
whiteListCommits map[string]bool
|
||||
whiteListBranches []string
|
||||
whiteListRepos []*regexp.Regexp
|
||||
whiteListRegexes []*regexp.Regexp
|
||||
configPath string
|
||||
testOpts Options
|
||||
description string
|
||||
expectedErrMsg string
|
||||
numLeaks int
|
||||
repo *RepoDescriptor
|
||||
whiteListFiles []*regexp.Regexp
|
||||
whiteListCommits map[string]bool
|
||||
whiteListRepos []*regexp.Regexp
|
||||
whiteListRegexes []*regexp.Regexp
|
||||
configPath string
|
||||
}{
|
||||
{
|
||||
repo: leaksRepo,
|
||||
description: "commit depth = 1, one leak",
|
||||
numLeaks: 1,
|
||||
testOpts: Options{
|
||||
Depth: 1,
|
||||
},
|
||||
},
|
||||
{
|
||||
repo: leaksRepo,
|
||||
description: "two leaks present",
|
||||
@@ -504,42 +472,7 @@ func TestAuditRepo(t *testing.T) {
|
||||
description: "two leaks present limit goroutines",
|
||||
numLeaks: 2,
|
||||
testOpts: Options{
|
||||
MaxGoRoutines: 4,
|
||||
},
|
||||
},
|
||||
{
|
||||
repo: leaksRepo,
|
||||
description: "audit specific bad branch",
|
||||
numLeaks: 2,
|
||||
testOpts: Options{
|
||||
Branch: "master",
|
||||
},
|
||||
},
|
||||
{
|
||||
repo: leaksRepo,
|
||||
description: "audit specific good branch",
|
||||
numLeaks: 0,
|
||||
testOpts: Options{
|
||||
Branch: "dev",
|
||||
},
|
||||
},
|
||||
{
|
||||
repo: leaksRepo,
|
||||
description: "audit all branch",
|
||||
numLeaks: 6,
|
||||
testOpts: Options{
|
||||
AuditAllRefs: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
repo: leaksRepo,
|
||||
description: "audit all branch whitelist 1",
|
||||
numLeaks: 4,
|
||||
testOpts: Options{
|
||||
AuditAllRefs: true,
|
||||
},
|
||||
whiteListBranches: []string{
|
||||
"origin/master",
|
||||
Threads: 4,
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -590,15 +523,6 @@ func TestAuditRepo(t *testing.T) {
|
||||
configPath: path.Join(configsDir, "regex"),
|
||||
numLeaks: 0,
|
||||
},
|
||||
{
|
||||
repo: leaksRepo,
|
||||
description: "toml whitelist branch",
|
||||
configPath: path.Join(configsDir, "branch"),
|
||||
testOpts: Options{
|
||||
AuditAllRefs: true,
|
||||
},
|
||||
numLeaks: 4,
|
||||
},
|
||||
{
|
||||
repo: leaksRepo,
|
||||
description: "toml whitelist file",
|
||||
@@ -641,14 +565,6 @@ func TestAuditRepo(t *testing.T) {
|
||||
Commit: "f6839959b7bbdcd23008f1fb16f797f35bcd3a0c",
|
||||
},
|
||||
},
|
||||
{
|
||||
repo: leaksRepo,
|
||||
description: "commit depth = 1, one leak",
|
||||
numLeaks: 1,
|
||||
testOpts: Options{
|
||||
Depth: 1,
|
||||
},
|
||||
},
|
||||
{
|
||||
repo: leaksRepo,
|
||||
description: "commit depth = 2, two leaks",
|
||||
@@ -660,7 +576,7 @@ func TestAuditRepo(t *testing.T) {
|
||||
{
|
||||
repo: leaksRepo,
|
||||
description: "toml entropy range",
|
||||
numLeaks: 283,
|
||||
numLeaks: 298,
|
||||
configPath: path.Join(configsDir, "entropy"),
|
||||
},
|
||||
{
|
||||
@@ -695,11 +611,6 @@ func TestAuditRepo(t *testing.T) {
|
||||
} else {
|
||||
whiteListCommits = nil
|
||||
}
|
||||
if test.whiteListBranches != nil {
|
||||
whiteListBranches = test.whiteListBranches
|
||||
} else {
|
||||
whiteListBranches = nil
|
||||
}
|
||||
if test.whiteListRegexes != nil {
|
||||
whiteListRegexes = test.whiteListRegexes
|
||||
} else {
|
||||
@@ -895,126 +806,3 @@ func TestLoadToml(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkAuditRepo1Proc(b *testing.B) {
|
||||
loadToml()
|
||||
opts.MaxGoRoutines = 1
|
||||
benchmarkRepo = getBenchmarkRepo()
|
||||
for n := 0; n < b.N; n++ {
|
||||
auditGitRepo(benchmarkRepo)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkAuditRepo2Proc(b *testing.B) {
|
||||
loadToml()
|
||||
opts.MaxGoRoutines = 2
|
||||
benchmarkRepo = getBenchmarkRepo()
|
||||
for n := 0; n < b.N; n++ {
|
||||
auditGitRepo(benchmarkRepo)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkAuditRepo4Proc(b *testing.B) {
|
||||
loadToml()
|
||||
opts.MaxGoRoutines = 4
|
||||
benchmarkRepo = getBenchmarkRepo()
|
||||
for n := 0; n < b.N; n++ {
|
||||
auditGitRepo(benchmarkRepo)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkAuditRepo8Proc(b *testing.B) {
|
||||
loadToml()
|
||||
opts.MaxGoRoutines = 8
|
||||
benchmarkRepo = getBenchmarkRepo()
|
||||
for n := 0; n < b.N; n++ {
|
||||
auditGitRepo(benchmarkRepo)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkAuditRepo10Proc(b *testing.B) {
|
||||
loadToml()
|
||||
opts.MaxGoRoutines = 10
|
||||
benchmarkRepo = getBenchmarkRepo()
|
||||
for n := 0; n < b.N; n++ {
|
||||
auditGitRepo(benchmarkRepo)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkAuditRepo100Proc(b *testing.B) {
|
||||
loadToml()
|
||||
opts.MaxGoRoutines = 100
|
||||
benchmarkRepo = getBenchmarkRepo()
|
||||
for n := 0; n < b.N; n++ {
|
||||
auditGitRepo(benchmarkRepo)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkAuditRepo1000Proc(b *testing.B) {
|
||||
loadToml()
|
||||
opts.MaxGoRoutines = 1000
|
||||
benchmarkRepo = getBenchmarkRepo()
|
||||
for n := 0; n < b.N; n++ {
|
||||
auditGitRepo(benchmarkRepo)
|
||||
}
|
||||
}
|
||||
func BenchmarkAuditLeakRepo1Proc(b *testing.B) {
|
||||
loadToml()
|
||||
opts.MaxGoRoutines = 1
|
||||
benchmarkLeaksRepo = getBenchmarkLeaksRepo()
|
||||
for n := 0; n < b.N; n++ {
|
||||
auditGitRepo(benchmarkRepo)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkAuditLeakRepo2Proc(b *testing.B) {
|
||||
loadToml()
|
||||
opts.MaxGoRoutines = 2
|
||||
benchmarkLeaksRepo = getBenchmarkLeaksRepo()
|
||||
for n := 0; n < b.N; n++ {
|
||||
auditGitRepo(benchmarkRepo)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkAuditLeakRepo4Proc(b *testing.B) {
|
||||
loadToml()
|
||||
opts.MaxGoRoutines = 4
|
||||
benchmarkLeaksRepo = getBenchmarkLeaksRepo()
|
||||
for n := 0; n < b.N; n++ {
|
||||
auditGitRepo(benchmarkRepo)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkAuditLeakRepo8Proc(b *testing.B) {
|
||||
loadToml()
|
||||
opts.MaxGoRoutines = 8
|
||||
benchmarkLeaksRepo = getBenchmarkLeaksRepo()
|
||||
for n := 0; n < b.N; n++ {
|
||||
auditGitRepo(benchmarkRepo)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkAuditLeakRepo10Proc(b *testing.B) {
|
||||
loadToml()
|
||||
opts.MaxGoRoutines = 10
|
||||
benchmarkLeaksRepo = getBenchmarkLeaksRepo()
|
||||
for n := 0; n < b.N; n++ {
|
||||
auditGitRepo(benchmarkRepo)
|
||||
}
|
||||
}
|
||||
func BenchmarkAuditLeakRepo100Proc(b *testing.B) {
|
||||
loadToml()
|
||||
opts.MaxGoRoutines = 100
|
||||
benchmarkLeaksRepo = getBenchmarkLeaksRepo()
|
||||
for n := 0; n < b.N; n++ {
|
||||
auditGitRepo(benchmarkRepo)
|
||||
}
|
||||
}
|
||||
func BenchmarkAuditLeakRepo1000Proc(b *testing.B) {
|
||||
loadToml()
|
||||
opts.MaxGoRoutines = 1000
|
||||
benchmarkLeaksRepo = getBenchmarkLeaksRepo()
|
||||
for n := 0; n < b.N; n++ {
|
||||
auditGitRepo(benchmarkRepo)
|
||||
}
|
||||
}
|
||||
@@ -37,15 +37,15 @@ import (
|
||||
|
||||
// Leak represents a leaked secret or regex match.
|
||||
type Leak struct {
|
||||
Line string `json:"line"`
|
||||
Commit string `json:"commit"`
|
||||
Offender string `json:"offender"`
|
||||
Type string `json:"reason"`
|
||||
Message string `json:"commitMsg"`
|
||||
Author string `json:"author"`
|
||||
File string `json:"file"`
|
||||
Branch string `json:"branch"`
|
||||
Repo string `json:"repo"`
|
||||
Line string `json:"line"`
|
||||
Commit string `json:"commit"`
|
||||
Offender string `json:"offender"`
|
||||
Type string `json:"reason"`
|
||||
Message string `json:"commitMsg"`
|
||||
Author string `json:"author"`
|
||||
File string `json:"file"`
|
||||
Repo string `json:"repo"`
|
||||
Date time.Time `json:"date"`
|
||||
}
|
||||
|
||||
// RepoDescriptor contains a src-d git repository and other data about the repo
|
||||
@@ -72,7 +72,6 @@ type Options struct {
|
||||
GitLabOrg string `long:"gitlab-org" description:"Organization url to audit"`
|
||||
*/
|
||||
|
||||
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"`
|
||||
Depth int `long:"depth" description:"maximum commit depth"`
|
||||
|
||||
@@ -81,14 +80,13 @@ type Options struct {
|
||||
OwnerPath string `long:"owner-path" description:"Path to owner directory (repos discovered)"`
|
||||
|
||||
// Process options
|
||||
MaxGoRoutines int `long:"max-go" description:"Maximum number of concurrent go-routines gitleaks spawns"`
|
||||
Disk bool `long:"disk" description:"Clones repo(s) to disk"`
|
||||
AuditAllRefs bool `long:"all-refs" description:"run audit on all refs"`
|
||||
SingleSearch string `long:"single-search" description:"single regular expression to search for"`
|
||||
ConfigPath string `long:"config" description:"path to gitleaks config"`
|
||||
SSHKey string `long:"ssh-key" description:"path to ssh key"`
|
||||
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)"`
|
||||
Threads int `long:"threads" description:"Maximum number of threads gitleaks spawns"`
|
||||
Disk bool `long:"disk" description:"Clones repo(s) to disk"`
|
||||
SingleSearch string `long:"single-search" description:"single regular expression to search for"`
|
||||
ConfigPath string `long:"config" description:"path to gitleaks config"`
|
||||
SSHKey string `long:"ssh-key" description:"path to ssh key"`
|
||||
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"`
|
||||
|
||||
// Output options
|
||||
@@ -107,11 +105,10 @@ type Config struct {
|
||||
Regex string
|
||||
}
|
||||
Whitelist struct {
|
||||
Files []string
|
||||
Regexes []string
|
||||
Commits []string
|
||||
Branches []string
|
||||
Repos []string
|
||||
Files []string
|
||||
Regexes []string
|
||||
Commits []string
|
||||
Repos []string
|
||||
}
|
||||
Misc struct {
|
||||
Entropy []string
|
||||
@@ -122,12 +119,12 @@ type gitDiff struct {
|
||||
content string
|
||||
commit *object.Commit
|
||||
filePath string
|
||||
branchName string
|
||||
repoName string
|
||||
githubCommit *github.RepositoryCommit
|
||||
sha string
|
||||
message string
|
||||
author string
|
||||
date time.Time
|
||||
}
|
||||
|
||||
type entropyRange struct {
|
||||
@@ -136,7 +133,7 @@ type entropyRange struct {
|
||||
}
|
||||
|
||||
const defaultGithubURL = "https://api.github.com/"
|
||||
const version = "1.16.1"
|
||||
const version = "1.19.3"
|
||||
const errExit = 2
|
||||
const leakExit = 1
|
||||
const defaultConfig = `
|
||||
@@ -145,53 +142,47 @@ const defaultConfig = `
|
||||
# is set, gitleaks will load configurations from that path. If option --config-path is set, gitleaks will load
|
||||
# configurations from that path. Gitleaks does not whitelist anything by default.
|
||||
|
||||
|
||||
title = "gitleaks config"
|
||||
# add regexes to the regex table
|
||||
[[regexes]]
|
||||
description = "AWS"
|
||||
regex = '''AKIA[0-9A-Z]{16}'''
|
||||
[[regexes]]
|
||||
description = "RKCS8"
|
||||
description = "PKCS8"
|
||||
regex = '''-----BEGIN PRIVATE KEY-----'''
|
||||
[[regexes]]
|
||||
description = "RSA"
|
||||
regex = '''-----BEGIN RSA PRIVATE KEY-----'''
|
||||
[[regexes]]
|
||||
description = "Github"
|
||||
regex = '''(?i)github.*['\"][0-9a-zA-Z]{35,40}['\"]'''
|
||||
[[regexes]]
|
||||
description = "SSH"
|
||||
regex = '''-----BEGIN OPENSSH PRIVATE KEY-----'''
|
||||
[[regexes]]
|
||||
description = "PGP"
|
||||
regex = '''-----BEGIN PGP PRIVATE KEY BLOCK-----'''
|
||||
[[regexes]]
|
||||
description = "Facebook"
|
||||
regex = '''(?i)facebook.*['\"][0-9a-f]{32}['\"]'''
|
||||
regex = '''(?i)facebook(.{0,4})?['\"][0-9a-f]{32}['\"]'''
|
||||
[[regexes]]
|
||||
description = "Twitter"
|
||||
regex = '''(?i)twitter.*['\"][0-9a-zA-Z]{35,44}['\"]'''
|
||||
regex = '''(?i)twitter(.{0,4})?['\"][0-9a-zA-Z]{35,44}['\"]'''
|
||||
[[regexes]]
|
||||
description = "Github"
|
||||
regex = '''(?i)github(.{0,4})?['\"][0-9a-zA-Z]{35,40}['\"]'''
|
||||
[[regexes]]
|
||||
description = "Slack"
|
||||
regex = '''xox[baprs]-([0-9a-zA-Z]{10,48})?'''
|
||||
|
||||
[whitelist]
|
||||
#regexes = [
|
||||
# "AKAIMYFAKEAWKKEY",
|
||||
#]
|
||||
|
||||
#files = [
|
||||
# "(.*?)(jpg|gif|doc|pdf|bin)$"
|
||||
#]
|
||||
|
||||
files = [
|
||||
"(.*?)(jpg|gif|doc|pdf|bin)$"
|
||||
]
|
||||
#commits = [
|
||||
# "BADHA5H1",
|
||||
# "BADHA5H2",
|
||||
#]
|
||||
|
||||
#branches = [
|
||||
# "dev/goodrepo"
|
||||
#]
|
||||
|
||||
#repos = [
|
||||
# "mygoodrepo"
|
||||
#]
|
||||
|
||||
[misc]
|
||||
#entropy = [
|
||||
# "3.3-4.30"
|
||||
@@ -206,19 +197,21 @@ var (
|
||||
whiteListRegexes []*regexp.Regexp
|
||||
whiteListFiles []*regexp.Regexp
|
||||
whiteListCommits map[string]bool
|
||||
whiteListBranches []string
|
||||
whiteListRepos []*regexp.Regexp
|
||||
entropyRanges []entropyRange
|
||||
fileDiffRegex *regexp.Regexp
|
||||
sshAuth *ssh.PublicKeys
|
||||
dir string
|
||||
maxGo int
|
||||
threads int
|
||||
totalCommits int64
|
||||
commitMap = make(map[string]bool)
|
||||
cMutex = &sync.Mutex{}
|
||||
)
|
||||
|
||||
func init() {
|
||||
log.SetOutput(os.Stdout)
|
||||
maxGo = runtime.GOMAXPROCS(0) / 2
|
||||
// threads = runtime.GOMAXPROCS(0) / 2
|
||||
threads = 1
|
||||
regexes = make(map[string]*regexp.Regexp)
|
||||
whiteListCommits = make(map[string]bool)
|
||||
}
|
||||
@@ -339,9 +332,9 @@ func writeReport(leaks []Leak) error {
|
||||
}
|
||||
defer f.Close()
|
||||
w := csv.NewWriter(f)
|
||||
w.Write([]string{"repo", "line", "commit", "offender", "reason", "commitMsg", "author", "file", "branch"})
|
||||
w.Write([]string{"repo", "line", "commit", "offender", "reason", "commitMsg", "author", "file", "date"})
|
||||
for _, leak := range leaks {
|
||||
w.Write([]string{leak.Repo, leak.Line, leak.Commit, leak.Offender, leak.Type, leak.Message, leak.Author, leak.File, leak.Branch})
|
||||
w.Write([]string{leak.Repo, leak.Line, leak.Commit, leak.Offender, leak.Type, leak.Message, leak.Author, leak.File, leak.Date.Format(time.RFC3339)})
|
||||
}
|
||||
w.Flush()
|
||||
} else {
|
||||
@@ -405,9 +398,7 @@ func cloneRepo() (*RepoDescriptor, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
// auditGitRepo beings an audit on a git repository by checking the default HEAD branch, all branches, or
|
||||
// a single branch depending on what gitleaks is configured to do. Note when I say branch I really
|
||||
// mean reference as these branches are read only.
|
||||
// auditGitRepo beings an audit on a git repository
|
||||
func auditGitRepo(repo *RepoDescriptor) ([]Leak, error) {
|
||||
var (
|
||||
err error
|
||||
@@ -418,51 +409,24 @@ func auditGitRepo(repo *RepoDescriptor) ([]Leak, error) {
|
||||
return leaks, fmt.Errorf("skipping %s, whitelisted", repo.name)
|
||||
}
|
||||
}
|
||||
ref, err := repo.repository.Head()
|
||||
|
||||
// clear commit cache
|
||||
commitMap = make(map[string]bool)
|
||||
|
||||
refs, err := repo.repository.Storer.IterReferences()
|
||||
if err != nil {
|
||||
return leaks, err
|
||||
}
|
||||
|
||||
if opts.AuditAllRefs {
|
||||
skipBranch := false
|
||||
refs, err := repo.repository.Storer.IterReferences()
|
||||
if err != nil {
|
||||
return leaks, err
|
||||
}
|
||||
err = refs.ForEach(func(ref *plumbing.Reference) error {
|
||||
for _, b := range whiteListBranches {
|
||||
if strings.HasSuffix(string(ref.Name()), b) {
|
||||
skipBranch = true
|
||||
}
|
||||
}
|
||||
if skipBranch {
|
||||
skipBranch = false
|
||||
return nil
|
||||
}
|
||||
branchLeaks := auditGitReference(repo, ref)
|
||||
for _, leak := range branchLeaks {
|
||||
leaks = append(leaks, leak)
|
||||
}
|
||||
err = refs.ForEach(func(ref *plumbing.Reference) error {
|
||||
if ref.Name().IsTag() {
|
||||
return nil
|
||||
})
|
||||
} else {
|
||||
if opts.Branch != "" {
|
||||
foundBranch := false
|
||||
refs, _ := repo.repository.Storer.IterReferences()
|
||||
branch := strings.Split(opts.Branch, "/")[len(strings.Split(opts.Branch, "/"))-1]
|
||||
err = refs.ForEach(func(refBranch *plumbing.Reference) error {
|
||||
if strings.Split(refBranch.Name().String(), "/")[len(strings.Split(refBranch.Name().String(), "/"))-1] == branch {
|
||||
foundBranch = true
|
||||
ref = refBranch
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if foundBranch == false {
|
||||
return nil, nil
|
||||
}
|
||||
}
|
||||
leaks = auditGitReference(repo, ref)
|
||||
}
|
||||
branchLeaks := auditGitReference(repo, ref)
|
||||
for _, leak := range branchLeaks {
|
||||
leaks = append(leaks, leak)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return leaks, err
|
||||
}
|
||||
|
||||
@@ -479,13 +443,13 @@ func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak {
|
||||
semaphore chan bool
|
||||
)
|
||||
repoName = repo.name
|
||||
if opts.MaxGoRoutines != 0 {
|
||||
maxGo = opts.MaxGoRoutines
|
||||
if opts.Threads != 0 {
|
||||
threads = opts.Threads
|
||||
}
|
||||
if opts.RepoPath != "" {
|
||||
maxGo = 1
|
||||
threads = 1
|
||||
}
|
||||
semaphore = make(chan bool, maxGo)
|
||||
semaphore = make(chan bool, threads)
|
||||
|
||||
cIter, err := repo.repository.Log(&git.LogOptions{From: ref.Hash()})
|
||||
if err != nil {
|
||||
@@ -497,13 +461,72 @@ func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak {
|
||||
return errors.New("ErrStop")
|
||||
}
|
||||
commitCount = commitCount + 1
|
||||
totalCommits = totalCommits + 1
|
||||
if whiteListCommits[c.Hash.String()] {
|
||||
log.Infof("skipping commit: %s\n", c.Hash.String())
|
||||
return nil
|
||||
}
|
||||
|
||||
// commits w/o parent (root of git the git ref)
|
||||
if len(c.ParentHashes) == 0 {
|
||||
if commitMap[c.Hash.String()] {
|
||||
return nil
|
||||
}
|
||||
cMutex.Lock()
|
||||
commitMap[c.Hash.String()] = true
|
||||
cMutex.Unlock()
|
||||
|
||||
totalCommits = totalCommits + 1
|
||||
|
||||
fIter, err := c.Files()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
err = fIter.ForEach(func(f *object.File) error {
|
||||
bin, err := f.IsBinary()
|
||||
if bin || err != nil {
|
||||
return nil
|
||||
}
|
||||
for _, re := range whiteListFiles {
|
||||
if re.FindString(f.Name) != "" {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
content, err := f.Contents()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
diff := gitDiff{
|
||||
repoName: repoName,
|
||||
filePath: f.Name,
|
||||
content: content,
|
||||
sha: c.Hash.String(),
|
||||
author: c.Author.String(),
|
||||
message: strings.Replace(c.Message, "\n", " ", -1),
|
||||
date: c.Author.When,
|
||||
}
|
||||
fileLeaks := inspect(diff)
|
||||
mutex.Lock()
|
||||
leaks = append(leaks, fileLeaks...)
|
||||
mutex.Unlock()
|
||||
return nil
|
||||
})
|
||||
return nil
|
||||
}
|
||||
skipCount := false
|
||||
err = c.Parents().ForEach(func(parent *object.Commit) error {
|
||||
// check if we've seen this diff before
|
||||
if commitMap[c.Hash.String()+parent.Hash.String()] {
|
||||
return nil
|
||||
}
|
||||
cMutex.Lock()
|
||||
commitMap[c.Hash.String()+parent.Hash.String()] = true
|
||||
cMutex.Unlock()
|
||||
|
||||
if !skipCount {
|
||||
totalCommits = totalCommits + 1
|
||||
skipCount = true
|
||||
}
|
||||
|
||||
commitWg.Add(1)
|
||||
semaphore <- true
|
||||
go func(c *object.Commit, parent *object.Commit) {
|
||||
@@ -524,6 +547,9 @@ func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak {
|
||||
return
|
||||
}
|
||||
for _, f := range patch.FilePatches() {
|
||||
if f.IsBinary() {
|
||||
continue
|
||||
}
|
||||
skipFile = false
|
||||
from, to := f.Files()
|
||||
filePath = "???"
|
||||
@@ -545,13 +571,13 @@ func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak {
|
||||
for _, chunk := range chunks {
|
||||
if chunk.Type() == 1 || chunk.Type() == 2 {
|
||||
diff := gitDiff{
|
||||
branchName: string(ref.Name()),
|
||||
repoName: repoName,
|
||||
filePath: filePath,
|
||||
content: chunk.Content(),
|
||||
sha: c.Hash.String(),
|
||||
author: c.Author.String(),
|
||||
message: c.Message,
|
||||
repoName: repoName,
|
||||
filePath: filePath,
|
||||
content: chunk.Content(),
|
||||
sha: c.Hash.String(),
|
||||
author: c.Author.String(),
|
||||
message: strings.Replace(c.Message, "\n", " ", -1),
|
||||
date: c.Author.When,
|
||||
}
|
||||
chunkLeaks := inspect(diff)
|
||||
for _, leak := range chunkLeaks {
|
||||
@@ -639,9 +665,9 @@ func addLeak(leaks []Leak, line string, offender string, leakType string, diff g
|
||||
Type: leakType,
|
||||
Author: diff.author,
|
||||
File: diff.filePath,
|
||||
Branch: diff.branchName,
|
||||
Repo: diff.repoName,
|
||||
Message: diff.message,
|
||||
Date: diff.date,
|
||||
}
|
||||
if opts.Redact {
|
||||
leak.Offender = "REDACTED"
|
||||
@@ -732,6 +758,10 @@ func optsGuard() error {
|
||||
return fmt.Errorf("github user set and local owner path")
|
||||
}
|
||||
|
||||
if opts.Threads > runtime.GOMAXPROCS(0) {
|
||||
return fmt.Errorf("%d available threads", runtime.GOMAXPROCS(0))
|
||||
}
|
||||
|
||||
// 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 {
|
||||
@@ -821,7 +851,6 @@ func loadToml() error {
|
||||
regexes[regex.Description] = regexp.MustCompile(regex.Regex)
|
||||
}
|
||||
}
|
||||
whiteListBranches = config.Whitelist.Branches
|
||||
whiteListCommits = make(map[string]bool)
|
||||
for _, commit := range config.Whitelist.Commits {
|
||||
whiteListCommits[commit] = true
|
||||
|
||||
Reference in new issue
Block a user