Compare commits

...
10 Commits
Author SHA1 Message Date
Zachary Rice 3932ed6baf --no-git should report relative path to --path (#519) 2021-02-03 13:48:59 -05:00
Zachary Rice 981b636920 gitter chat 2021-02-02 17:27:24 -05:00
Kai c8e15c731a Always generate a report if --report is enabled (#508) 2021-01-26 07:59:51 -05:00
Zachary Rice 901f932a89 Add file type checking for unstaged commits (#512) 2021-01-22 11:49:21 -05:00
seb-elttamandSeb Elttam c0a9580a65 Fix dotgit regex again (#507)
* fix dotgit regex again

* Revert accidentally modified files

Co-authored-by: Seb Elttam <seb@elttam.com>
2021-01-21 15:31:21 -05:00
Paweł Kowalczyk 8be91eea5d Add exit code 1 on invalid usage, fixes #500 (#501) 2021-01-10 09:34:31 -05:00
Zachary Rice 1c6f2252b8 Update README.md 2020-12-11 20:55:32 -05:00
Zachary RiceandZach Rice 58d130c5f3 Quiet flag (#491)
* Adding a quiet option

* better description

Co-authored-by: Zach Rice <zrice@gitlab.com>
2020-12-11 09:56:09 -05:00
Zachary Rice 091ea81e68 nosec gosec (#490) 2020-12-10 22:32:32 -05:00
Zachary Rice f036dce6ab Dotgit bug (#488)
* fix allowlist bug

* Adding new repo for testing allowlisting

* adding additional constraints on the dotgit regex
2020-12-10 17:41:26 -05:00
18 changed files with 191 additions and 42 deletions

No files matched your search

+3 -1
View File
@@ -2,6 +2,7 @@
<img alt="gitleaks" src="https://raw.githubusercontent.com/zricethezav/gifs/master/gitleakslogo.png" height="70" />
<p align="center">
<a href="https://travis-ci.org/zricethezav/gitleaks"><img alt="Travis" src="https://img.shields.io/travis/zricethezav/gitleaks/master.svg?style=flat-square"></a>
<a href="https://gitter.im/gitleaks/gitleaks"><img alt="Travis" src="https://badges.gitter.im/gitleaks/gitleaks.svg?style=flat-square"></a>
</p>
</p>
@@ -19,7 +20,7 @@ Gitleaks is a SAST tool for detecting hardcoded secrets like passwords, api keys
### Installation
Written in Go, gitleaks is available in binary form for many popular platforms and OS types from the [releases page](https://github.com/zricethezav/gitleaks/releases). Alternatively, executed via Docker or it can be installed using Go directly.
Gitleaks can be installed with Homebrew, Docker, and Go. Gitleaks is also available in binary form for many popular platforms and OS types from the [releases page](https://github.com/zricethezav/gitleaks/releases).
##### MacOS
@@ -45,6 +46,7 @@ Usage:
Application Options:
-v, --verbose Show verbose output from scan
-q, --quiet Sets log level to error and only output leaks, one json object per line
-r, --repo-url= Repository URL
-p, --path= Path to directory (repo if contains .git) or file
-c, --config-path= Path to config
+6 -2
View File
@@ -4,6 +4,10 @@ import (
"regexp"
)
// used for ignoring .git directories when the --no-git flag is set
// related issue: https://github.com/zricethezav/gitleaks/issues/486
const dotGit = `/\.git/`
// AllowList is struct containing items that if encountered will allowlist
// a commit/line of code that would be considered a leak.
type AllowList struct {
@@ -45,9 +49,9 @@ func (a *AllowList) RepoAllowed(repo string) bool {
return anyRegexMatch(repo, a.Repos)
}
// IgnoreDotGit appends a `.git$` rule to ignore all .git paths. This is used for --no-git scans
// IgnoreDotGit appends a `\.git` rule to ignore all .git paths. This is used for --no-git scans
func (a *AllowList) IgnoreDotGit() error {
re, err := regexp.Compile(".git")
re, err := regexp.Compile(dotGit)
if err != nil {
return err
}
+5 -1
View File
@@ -21,6 +21,7 @@ import (
// Options stores values of command line options
type Options struct {
Verbose bool `short:"v" long:"verbose" description:"Show verbose output from scan"`
Quiet bool `short:"q" long:"quiet" description:"Sets log level to error and only output leaks, one json object per line"`
RepoURL string `short:"r" long:"repo-url" description:"Repository URL"`
Path string `short:"p" long:"path" description:"Path to directory (repo if contains .git) or file"`
ConfigPath string `short:"c" long:"config-path" description:"Path to config"`
@@ -68,7 +69,7 @@ func ParseOptions() (Options, error) {
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type != flags.ErrHelp {
parser.WriteHelp(os.Stdout)
}
os.Exit(0)
os.Exit(1)
}
if opts.Version {
@@ -83,6 +84,9 @@ func ParseOptions() (Options, error) {
if opts.Debug {
log.SetLevel(log.DebugLevel)
}
if opts.Quiet {
log.SetLevel(log.ErrorLevel)
}
return opts, nil
}
+5 -6
View File
@@ -95,9 +95,8 @@ func (cs *CommitScanner) Scan() (Report, error) {
leak.Rule = rule.Description
leak.Tags = strings.Join(rule.Tags, ", ")
if cs.opts.Verbose {
leak.Log(cs.opts.Redact)
}
leak.Log(cs.opts)
scannerReport.Leaks = append(scannerReport.Leaks, leak)
continue
}
@@ -137,9 +136,9 @@ func (cs *CommitScanner) Scan() (Report, error) {
leak.LeakURL = leak.URL()
leak.Rule = rule.Description
leak.Tags = strings.Join(rule.Tags, ", ")
if cs.opts.Verbose {
leak.Log(cs.opts.Redact)
}
leak.Log(cs.opts)
scannerReport.Leaks = append(scannerReport.Leaks, leak)
}
}
+5 -6
View File
@@ -81,9 +81,8 @@ func (fs *FilesAtCommitScanner) Scan() (Report, error) {
leak.Rule = rule.Description
leak.Tags = strings.Join(rule.Tags, ", ")
if fs.opts.Verbose {
leak.Log(fs.opts.Redact)
}
leak.Log(fs.opts)
scannerReport.Leaks = append(scannerReport.Leaks, leak)
continue
}
@@ -122,9 +121,9 @@ func (fs *FilesAtCommitScanner) Scan() (Report, error) {
leak.LeakURL = leak.URL()
leak.Rule = rule.Description
leak.Tags = strings.Join(rule.Tags, ", ")
if fs.opts.Verbose {
leak.Log(fs.opts.Redact)
}
leak.Log(fs.opts)
scannerReport.Leaks = append(scannerReport.Leaks, leak)
}
}
+16 -5
View File
@@ -6,6 +6,8 @@ import (
"strings"
"time"
"github.com/zricethezav/gitleaks/v7/options"
"github.com/go-git/go-git/v5/plumbing/object"
)
@@ -56,13 +58,22 @@ func (leak Leak) WithCommit(commit *object.Commit) Leak {
}
// Log logs a leak and redacts if necessary
func (leak Leak) Log(redact bool) {
if redact {
func (leak Leak) Log(opts options.Options) {
if !opts.Quiet && !opts.Verbose {
return
}
if opts.Redact {
leak = RedactLeak(leak)
}
var b []byte
b, _ = json.MarshalIndent(leak, "", " ")
fmt.Println(string(b))
if opts.Quiet {
var b []byte
b, _ = json.Marshal(leak)
fmt.Println(string(b))
} else {
var b []byte
b, _ = json.MarshalIndent(leak, "", " ")
fmt.Println(string(b))
}
}
// URL generates a url to the leak if leak.RepoURL is set
+21 -10
View File
@@ -73,18 +73,22 @@ func (ngs *NoGitScanner) Scan() (Report, error) {
for _, rule := range ngs.cfg.Rules {
if rule.HasFileOrPathLeakOnly(p) {
leak := NewLeak("", "Filename or path offender: "+p, defaultLineNumber)
leak.File = p
relPath, err := filepath.Rel(ngs.opts.Path, p)
if err != nil {
leak.File = p
} else {
leak.File = relPath
}
leak.Rule = rule.Description
leak.Tags = strings.Join(rule.Tags, ", ")
if ngs.opts.Verbose {
leak.Log(ngs.opts.Redact)
}
leak.Log(ngs.opts)
leaks <- leak
}
}
f, err := os.Open(p)
f, err := os.Open(p) // #nosec
if err != nil {
return err
}
@@ -116,13 +120,18 @@ func (ngs *NoGitScanner) Scan() (Report, error) {
}
leak := NewLeak(line, offender, defaultLineNumber)
leak.File = p
relPath, err := filepath.Rel(ngs.opts.Path, p)
if err != nil {
leak.File = p
} else {
leak.File = relPath
}
leak.LineNumber = lineNumber
leak.Rule = rule.Description
leak.Tags = strings.Join(rule.Tags, ", ")
if ngs.opts.Verbose {
leak.Log(ngs.opts.Redact)
}
leak.Log(ngs.opts)
leaks <- leak
}
}
@@ -131,7 +140,9 @@ func (ngs *NoGitScanner) Scan() (Report, error) {
}
go func() {
g.Wait()
if err := g.Wait(); err != nil {
log.Error(err)
}
close(leaks)
}()
+3 -1
View File
@@ -98,7 +98,9 @@ func (rs *RepoScanner) Scan() (Report, error) {
}
go func() {
g.Wait()
if err := g.Wait(); err != nil {
log.Error(err)
}
close(leaks)
}()
+9 -4
View File
@@ -27,7 +27,6 @@ func WriteReport(report Report, opts options.Options, cfg config.Config) error {
logrus.Warn("leaks found: ", len(report.Leaks))
} else {
logrus.Info("No leaks found")
return nil
}
if opts.Report == "" {
@@ -46,7 +45,7 @@ func WriteReport(report Report, opts options.Options, cfg config.Config) error {
if err != nil {
return err
}
defer file.Close()
defer rable(file.Close)
if opts.Report != "" {
switch opts.ReportFormat {
@@ -59,9 +58,15 @@ func WriteReport(report Report, opts options.Options, cfg config.Config) error {
}
case "csv":
w := csv.NewWriter(file)
_ = w.Write([]string{"repo", "line", "commit", "offender", "leakURL", "rule", "tags", "commitMsg", "author", "email", "file", "date"})
err = w.Write([]string{"repo", "line", "commit", "offender", "leakURL", "rule", "tags", "commitMsg", "author", "email", "file", "date"})
if err != nil {
return err
}
for _, leak := range report.Leaks {
w.Write([]string{leak.Repo, leak.Line, leak.Commit, leak.Offender, leak.LeakURL, leak.Rule, leak.Tags, leak.Message, leak.Author, leak.Email, leak.File, leak.Date.Format(time.RFC3339)})
err := w.Write([]string{leak.Repo, leak.Line, leak.Commit, leak.Offender, leak.LeakURL, leak.Rule, leak.Tags, leak.Message, leak.Author, leak.Email, leak.File, leak.Date.Format(time.RFC3339)})
if err != nil {
return err
}
}
w.Flush()
case "sarif":
+26 -4
View File
@@ -57,6 +57,28 @@ func (us *UnstagedScanner) Scan() (Report, error) {
if err != nil {
continue
}
// Check if file is allow listed
if us.cfg.Allowlist.FileAllowed(filepath.Base(fn)) ||
us.cfg.Allowlist.PathAllowed(fn) {
continue
}
// Check individual file path ONLY rules
for _, rule := range us.cfg.Rules {
if rule.HasFileOrPathLeakOnly(fn) {
leak := NewLeak("", "Filename or path offender: "+ fn, defaultLineNumber)
leak.Repo = us.repoName
leak.File = fn
leak.RepoURL = us.opts.RepoURL
leak.LeakURL = leak.URL()
leak.Rule = rule.Description
leak.Tags = strings.Join(rule.Tags, ", ")
leak.Log(us.opts)
scannerReport.Leaks = append(scannerReport.Leaks, leak)
continue
}
}
if _, err := io.Copy(workTreeBuf, workTreeFile); err != nil {
return scannerReport, err
}
@@ -86,7 +108,7 @@ func (us *UnstagedScanner) Scan() (Report, error) {
leak.Rule = rule.Description
leak.Tags = strings.Join(rule.Tags, ", ")
if us.opts.Verbose {
leak.Log(us.opts.Redact)
leak.Log(us.opts)
}
scannerReport.Leaks = append(scannerReport.Leaks, leak)
}
@@ -198,9 +220,9 @@ func (us *UnstagedScanner) Scan() (Report, error) {
leak.Repo = us.repoName
leak.Rule = rule.Description
leak.Tags = strings.Join(rule.Tags, ", ")
if us.opts.Verbose {
leak.Log(us.opts.Redact)
}
leak.Log(us.opts)
scannerReport.Leaks = append(scannerReport.Leaks, leak)
}
}
+8 -1
View File
@@ -178,7 +178,7 @@ func optsToCommits(opts options.Options) ([]string, error) {
if err != nil {
return []string{}, err
}
defer file.Close()
defer rable(file.Close)
scanner := bufio.NewScanner(file)
var commits []string
@@ -218,3 +218,10 @@ func extractLine(patchContent string, leak Leak, lineLookup map[string]bool) int
}
return defaultLineNumber
}
// rable is the second half of deferrable... mainly used for defer file.Close()
func rable(f func() error) {
if err := f(); err != nil {
log.Error(err)
}
}
@@ -1,4 +1,20 @@
[
{
"line": "",
"lineNumber": 1,
"offender": "Filename or path offender: ../test_data/test_repos/test_repo_10/.gitignore",
"commit": "",
"repo": "",
"repoURL": "",
"leakURL": "",
"rule": "Block dangerous filetypes",
"commitMessage": "",
"author": "",
"email": "",
"file": "../test_data/test_repos/test_repo_10/.gitignore",
"date": "0001-01-01T00:00:00Z",
"tags": "key, extensions"
},
{
"line": "",
"lineNumber": 1,
@@ -14,5 +30,53 @@
"file": "../test_data/test_repos/test_repo_10/bad.zip",
"date": "0001-01-01T00:00:00Z",
"tags": "key, extensions"
},
{
"line": "",
"lineNumber": 1,
"offender": "Filename or path offender: ../test_data/test_repos/test_repo_10/gitfile.txt",
"commit": "",
"repo": "",
"repoURL": "",
"leakURL": "",
"rule": "Block dangerous filetypes",
"commitMessage": "",
"author": "",
"email": "",
"file": "../test_data/test_repos/test_repo_10/gitfile.txt",
"date": "0001-01-01T00:00:00Z",
"tags": "key, extensions"
},
{
"line": "",
"lineNumber": 1,
"offender": "Filename or path offender: ../test_data/test_repos/test_repo_10/creds.git",
"commit": "",
"repo": "",
"repoURL": "",
"leakURL": "",
"rule": "Block dangerous filetypes",
"commitMessage": "",
"author": "",
"email": "",
"file": "../test_data/test_repos/test_repo_10/creds.git",
"date": "0001-01-01T00:00:00Z",
"tags": "key, extensions"
},
{
"line": "",
"lineNumber": 1,
"offender": "Filename or path offender: ../test_data/test_repos/test_repo_10/somedir.git/secret.key",
"commit": "",
"repo": "",
"repoURL": "",
"leakURL": "",
"rule": "Block dangerous filetypes",
"commitMessage": "",
"author": "",
"email": "",
"file": "../test_data/test_repos/test_repo_10/somedir.git/secret.key",
"date": "0001-01-01T00:00:00Z",
"tags": "key, extensions"
}
]
@@ -1,4 +1,20 @@
[
{
"line": "",
"lineNumber": 1,
"offender": "Filename or path offender: ../test_data/test_repos/test_repo_10/somedir.git/secret.key",
"commit": "",
"repo": "",
"repoURL": "",
"leakURL": "",
"rule": "Block dangerous filetypes",
"commitMessage": "",
"author": "",
"email": "",
"file": "../test_data/test_repos/test_repo_10/somedir.git/secret.key",
"date": "0001-01-01T00:00:00Z",
"tags": "key, extensions"
},
{
"line": "",
"lineNumber": 1,
+1 -1
View File
@@ -1,6 +1,6 @@
[[rules]]
description = "Block dangerous filetypes"
file = '''(.*?)(pdf|doc|docx|zip|xls|tfplan|tfstate|tfvars|vault_pass|vagrant|pyc|key|cache)$'''
file = '''(.*?)(creds.git|gitfile.txt|gitignore|pdf|doc|docx|zip|xls|tfplan|tfstate|tfvars|vault_pass|vagrant|pyc|key|cache)$'''
tags = ["key", "extensions"]
[rules.allowlist]
paths = ['''.docx''']
@@ -0,0 +1 @@
a dot git ignore file
Whitespace-only changes.
@@ -0,0 +1 @@
a git file
@@ -0,0 +1 @@
hah