Compare commits

...
5 Commits
Author SHA1 Message Date
Zach Rice 6f6ebd454f fix off by one line number for --no-git 2021-12-08 15:04:32 -06:00
Zachary Rice ce42947cae fix deduplication issue caused by clobbered findings (#742)
* fix deduplication issue caused by clobbered findings

* fix index

* remove indexing, slow is better than wrong
2021-12-07 19:11:37 -06:00
Zach Rice 84e285e4ce ignore gitleaks.toml by default 2021-12-06 10:43:21 -06:00
Ben Randall 928c6a6043 Update pre-commit step to run gitleaks checks (#729)
* Fix pre-commit config.

* Debug output

* No debug and use redacted.

* Typo

* Add back staged.

* Update .pre-commit-hooks.yaml

* Update .pre-commit-hooks.yaml
2021-12-03 16:09:05 -06:00
Florian Greinacher 106897fdf0 fix: format dates in log in a portable way (#735)
The output of time.String() depends on the runtime environment and should only be used for debugging.

This commit ensures that a well-defined UTC time is written to the report.
2021-12-03 09:34:57 -06:00
19 changed files with 102 additions and 71 deletions

No files matched your search

+6
View File
@@ -9,6 +9,12 @@ builds:
- windows
goarch:
- amd64
- "386"
- arm
- arm64
goarm:
- "6"
- "7"
ldflags:
- -s -w -X=github.com/zricethezav/gitleaks/v8/cmd.Version={{.Version}}
archives:
+1 -1
View File
@@ -1,6 +1,6 @@
- id: gitleaks
name: Detect hardcoded secrets
description: Detect hardcoded secrets using Gitleaks
entry: gitleaks
entry: gitleaks protect --verbose --redact --staged
language: golang
pass_filenames: false
+1 -1
View File
@@ -31,7 +31,7 @@ func runDetect(cmd *cobra.Command, args []string) {
initConfig()
var (
vc config.ViperConfig
findings []*report.Finding
findings []report.Finding
err error
)
+12 -4
View File
@@ -273,14 +273,19 @@ description = "Dropbox API secret/key"
regex = '''(?i)(dropbox[a-z0-9_ .\-,]{0,25})(=|>|:=|\|\|:|<=|=>|:).{0,5}['\"]([a-z0-9]{15})['\"]'''
[[rules]]
id = "dropbox-sl-api-token"
id = "dropbox--api-key"
description = "Dropbox API secret/key"
regex = '''(?i)(dropbox[a-z0-9_ .\-,]{0,25})(=|>|:=|\|\|:|<=|=>|:).{0,5}['\"]([a-z0-9]{15})['\"]'''
[[rules]]
id = "dropbox-short-lived-api-token"
description = "Dropbox short lived API token"
regex = '''(?i)(dropbox[a-z0-9_ .\-,]{0,25})(=|>|:=|\|\|:|<=|=>|:).{0,5}['\"](sl\.[a-z0-9\-=_]{135})['\"]'''
[[rules]]
id = "dropbox-ll-api-token"
id = "dropbox-long-lived-api-token"
description = "Dropbox long lived API token"
regex = '''(?i)(dropbox)(.{0,20})['\"](?i)[a-z0-9]{11}(AAAAAAAAAA)[a-z0-9-_=]{43}['\"]'''
regex = '''(?i)(dropbox[a-z0-9_ .\-,]{0,25})(=|>|:=|\|\|:|<=|=>|:).{0,5}['\"][a-z0-9]{11}(AAAAAAAAAA)[a-z0-9\-_=]{43}['\"]'''
[[rules]]
id = "duffel-api-token"
@@ -538,4 +543,7 @@ secretGroup = 4
[allowlist]
description = "global allow lists"
regexes = ['''219-09-9999''', '''078-05-1120''', '''(9[0-9]{2}|666)-\d{2}-\d{4}''']
paths = ['''(.*?)(jpg|gif|doc|pdf|bin|svg|socket)$''']
paths = [
'''gitleaks.toml''',
'''(.*?)(jpg|gif|doc|pdf|bin|svg|socket)$'''
]
+1
View File
@@ -7,6 +7,7 @@ import (
"strings"
"github.com/rs/zerolog/log"
"github.com/zricethezav/gitleaks/v8/config"
"github.com/zricethezav/gitleaks/v8/report"
)
+7 -4
View File
@@ -15,9 +15,9 @@ import (
// FromFiles opens the directory or file specified in source and checks each file against the rules
// from the configuration. If any secrets are found, they are added to the list of findings.
func FromFiles(source string, cfg config.Config, outputOptions Options) ([]*report.Finding, error) {
func FromFiles(source string, cfg config.Config, outputOptions Options) ([]report.Finding, error) {
var (
findings []*report.Finding
findings []report.Finding
mu sync.Mutex
)
g, _ := errgroup.WithContext(context.Background())
@@ -51,7 +51,10 @@ func FromFiles(source string, cfg config.Config, outputOptions Options) ([]*repo
}
fis := DetectFindings(cfg, b, p, "")
for _, fi := range fis {
fi.File = p
// need to add 1 since line counting starts at 1
fi.StartLine++
fi.EndLine++
if outputOptions.Redact {
fi.Redact()
}
@@ -59,7 +62,7 @@ func FromFiles(source string, cfg config.Config, outputOptions Options) ([]*repo
printFinding(fi)
}
mu.Lock()
findings = append(findings, &fi)
findings = append(findings, fi)
mu.Unlock()
}
return nil
+7 -7
View File
@@ -17,16 +17,16 @@ func TestFromFiles(t *testing.T) {
cfgName string
opts Options
source string
expectedFindings []*report.Finding
expectedFindings []report.Finding
}{
{
source: filepath.Join(repoBasePath, "nogit"),
cfgName: "simple",
expectedFindings: []*report.Finding{
expectedFindings: []report.Finding{
{
Description: "AWS Access Key",
StartLine: 19,
EndLine: 19,
StartLine: 20,
EndLine: 20,
StartColumn: 16,
EndColumn: 35,
Match: "AKIALALEMEL33243OLIA",
@@ -40,11 +40,11 @@ func TestFromFiles(t *testing.T) {
{
source: filepath.Join(repoBasePath, "nogit", "main.go"),
cfgName: "simple",
expectedFindings: []*report.Finding{
expectedFindings: []report.Finding{
{
Description: "AWS Access Key",
StartLine: 19,
EndLine: 19,
StartLine: 20,
EndLine: 20,
StartColumn: 16,
EndColumn: 35,
Match: "AKIALALEMEL33243OLIA",
+5 -4
View File
@@ -3,6 +3,7 @@ package detect
import (
"strings"
"sync"
"time"
"github.com/gitleaks/go-gitdiff/gitdiff"
"github.com/rs/zerolog/log"
@@ -14,8 +15,8 @@ import (
// FromGit accepts a gitdiff.File channel (structure output from `git log -p`) and a configuration
// struct. Files from the gitdiff.File channel are then checked against each rule in the configuration to
// check for secrets. If any secrets are found, they are added to the list of findings.
func FromGit(files <-chan *gitdiff.File, cfg config.Config, outputOptions Options) []*report.Finding {
var findings []*report.Finding
func FromGit(files <-chan *gitdiff.File, cfg config.Config, outputOptions Options) []report.Finding {
var findings []report.Finding
mu := sync.Mutex{}
wg := sync.WaitGroup{}
commitMap := make(map[string]bool)
@@ -70,7 +71,7 @@ func FromGit(files <-chan *gitdiff.File, cfg config.Config, outputOptions Option
fi.Author = f.PatchHeader.Author.Name
fi.Email = f.PatchHeader.Author.Email
}
fi.Date = f.PatchHeader.AuthorDate.String()
fi.Date = f.PatchHeader.AuthorDate.UTC().Format(time.RFC3339)
}
if outputOptions.Redact {
@@ -81,7 +82,7 @@ func FromGit(files <-chan *gitdiff.File, cfg config.Config, outputOptions Option
printFinding(fi)
}
mu.Lock()
findings = append(findings, &fi)
findings = append(findings, fi)
mu.Unlock()
}
+28 -26
View File
@@ -26,13 +26,13 @@ func TestFromGit(t *testing.T) {
source string
logOpts string
expected string
expectedFindings []*report.Finding
expectedFindings []report.Finding
}{
{
source: filepath.Join(repoBasePath, "small"),
expected: filepath.Join(expectPath, "git", "small.txt"),
cfgName: "simple",
expectedFindings: []*report.Finding{
expectedFindings: []report.Finding{
{
Description: "AWS Access Key",
StartLine: 20,
@@ -40,14 +40,15 @@ func TestFromGit(t *testing.T) {
StartColumn: 19,
EndColumn: 38,
Secret: "AKIALALEMEL33243OLIA",
Match: "AKIALALEMEL33243OLIA",
File: "main.go",
// Line: "\tawsToken := \"AKIALALEMEL33243OLIA\"",
Commit: "1b6da43b82b22e4eaa10bcf8ee591e91abbfc587",
Author: "Zachary Rice",
Email: "zricer@protonmail.com",
Message: "Accidentally add a secret",
RuleID: "aws-access-key",
Tags: []string{"key", "AWS"},
Date: "2021-11-02T23:37:53Z",
Commit: "1b6da43b82b22e4eaa10bcf8ee591e91abbfc587",
Author: "Zachary Rice",
Email: "zricer@protonmail.com",
Message: "Accidentally add a secret",
RuleID: "aws-access-key",
Tags: []string{"key", "AWS"},
},
{
Description: "AWS Access Key",
@@ -56,14 +57,15 @@ func TestFromGit(t *testing.T) {
StartColumn: 17,
EndColumn: 36,
Secret: "AKIALALEMEL33243OLIA",
Match: "AKIALALEMEL33243OLIA",
File: "foo/foo.go",
// Line: "\taws_token := \"AKIALALEMEL33243OLIA\"",
Commit: "491504d5a31946ce75e22554cc34203d8e5ff3ca",
Author: "Zach Rice",
Email: "zricer@protonmail.com",
Message: "adding foo package with secret",
RuleID: "aws-access-key",
Tags: []string{"key", "AWS"},
Date: "2021-11-02T23:48:06Z",
Commit: "491504d5a31946ce75e22554cc34203d8e5ff3ca",
Author: "Zach Rice",
Email: "zricer@protonmail.com",
Message: "adding foo package with secret",
RuleID: "aws-access-key",
Tags: []string{"key", "AWS"},
},
},
},
@@ -72,7 +74,7 @@ func TestFromGit(t *testing.T) {
expected: filepath.Join(expectPath, "git", "small-branch-foo.txt"),
logOpts: "--all foo...",
cfgName: "simple",
expectedFindings: []*report.Finding{
expectedFindings: []report.Finding{
{
Description: "AWS Access Key",
StartLine: 9,
@@ -80,14 +82,15 @@ func TestFromGit(t *testing.T) {
StartColumn: 17,
EndColumn: 36,
Secret: "AKIALALEMEL33243OLIA",
// Line: "\taws_token := \"AKIALALEMEL33243OLIA\"",
File: "foo/foo.go",
Commit: "491504d5a31946ce75e22554cc34203d8e5ff3ca",
Author: "Zach Rice",
Email: "zricer@protonmail.com",
Message: "adding foo package with secret",
RuleID: "aws-access-key",
Tags: []string{"key", "AWS"},
Match: "AKIALALEMEL33243OLIA",
Date: "2021-11-02T23:48:06Z",
File: "foo/foo.go",
Commit: "491504d5a31946ce75e22554cc34203d8e5ff3ca",
Author: "Zach Rice",
Email: "zricer@protonmail.com",
Message: "adding foo package with secret",
RuleID: "aws-access-key",
Tags: []string{"key", "AWS"},
},
},
},
@@ -120,7 +123,6 @@ func TestFromGit(t *testing.T) {
findings := FromGit(files, cfg, tt.opts)
for _, f := range findings {
f.Match = "" // remove lines cause copying and pasting them has some wack formatting
f.Date = ""
}
assert.ElementsMatch(t, tt.expectedFindings, findings)
}
+1 -1
View File
@@ -7,7 +7,7 @@ import (
)
// writeCsv writes the list of findings to a writeCloser.
func writeCsv(f []*Finding, w io.WriteCloser) error {
func writeCsv(f []Finding, w io.WriteCloser) error {
if len(f) == 0 {
return nil
}
+3 -3
View File
@@ -9,7 +9,7 @@ import (
func TestWriteCSV(t *testing.T) {
tests := []struct {
findings []*Finding
findings []Finding
testReportName string
expected string
wantEmpty bool
@@ -17,7 +17,7 @@ func TestWriteCSV(t *testing.T) {
{
testReportName: "simple",
expected: filepath.Join(expectPath, "report", "csv_simple.csv"),
findings: []*Finding{
findings: []Finding{
{
RuleID: "test-rule",
Match: "line containing secret",
@@ -39,7 +39,7 @@ func TestWriteCSV(t *testing.T) {
wantEmpty: true,
testReportName: "empty",
expected: filepath.Join(expectPath, "report", "this_should_not_exist.csv"),
findings: []*Finding{}},
findings: []Finding{}},
}
for _, test := range tests {
+11 -1
View File
@@ -1,6 +1,9 @@
package report
import "strings"
import (
"strconv"
"strings"
)
// Finding contains information about strings that
// have been captured by a tree-sitter query.
@@ -40,3 +43,10 @@ func (f *Finding) Redact() {
f.Match = strings.Replace(f.Match, f.Secret, "REDACTED", -1)
f.Secret = "REDACT"
}
func (f *Finding) Hash() string {
return f.Secret + f.Commit +
strconv.Itoa(f.EndLine) +
strconv.Itoa(f.StartLine)
}
+1 -1
View File
@@ -5,7 +5,7 @@ import (
"io"
)
func writeJson(findings []*Finding, w io.WriteCloser) error {
func writeJson(findings []Finding, w io.WriteCloser) error {
if len(findings) == 0 {
return nil
}
+3 -3
View File
@@ -9,7 +9,7 @@ import (
func TestWriteJSON(t *testing.T) {
tests := []struct {
findings []*Finding
findings []Finding
testReportName string
expected string
wantEmpty bool
@@ -17,7 +17,7 @@ func TestWriteJSON(t *testing.T) {
{
testReportName: "simple",
expected: filepath.Join(expectPath, "report", "json_simple.json"),
findings: []*Finding{
findings: []Finding{
{
Description: "",
@@ -42,7 +42,7 @@ func TestWriteJSON(t *testing.T) {
wantEmpty: true,
testReportName: "empty",
expected: filepath.Join(expectPath, "report", "this_should_not_exist.json"),
findings: []*Finding{}},
findings: []Finding{}},
}
for _, test := range tests {
+1 -1
View File
@@ -13,7 +13,7 @@ const (
CWE_DESCRIPTION = "Use of Hard-coded Credentials"
)
func Write(findings []*Finding, cfg config.Config, ext string, reportPath string) error {
func Write(findings []Finding, cfg config.Config, ext string, reportPath string) error {
if len(findings) == 0 {
return nil
}
+7 -7
View File
@@ -16,13 +16,13 @@ const (
func TestReport(t *testing.T) {
tests := []struct {
findings []*Finding
findings []Finding
ext string
wantEmpty bool
}{
{
ext: "json",
findings: []*Finding{
findings: []Finding{
{
RuleID: "test-rule",
},
@@ -30,7 +30,7 @@ func TestReport(t *testing.T) {
},
{
ext: ".json",
findings: []*Finding{
findings: []Finding{
{
RuleID: "test-rule",
},
@@ -38,7 +38,7 @@ func TestReport(t *testing.T) {
},
{
ext: ".jsonj",
findings: []*Finding{
findings: []Finding{
{
RuleID: "test-rule",
},
@@ -47,7 +47,7 @@ func TestReport(t *testing.T) {
},
{
ext: ".csv",
findings: []*Finding{
findings: []Finding{
{
RuleID: "test-rule",
},
@@ -55,7 +55,7 @@ func TestReport(t *testing.T) {
},
{
ext: "csv",
findings: []*Finding{
findings: []Finding{
{
RuleID: "test-rule",
},
@@ -63,7 +63,7 @@ func TestReport(t *testing.T) {
},
{
ext: "CSV",
findings: []*Finding{
findings: []Finding{
{
RuleID: "test-rule",
},
+5 -5
View File
@@ -8,7 +8,7 @@ import (
"github.com/zricethezav/gitleaks/v8/config"
)
func writeSarif(cfg config.Config, findings []*Finding, w io.WriteCloser) error {
func writeSarif(cfg config.Config, findings []Finding, w io.WriteCloser) error {
sarif := Sarif{
Schema: "https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0-rtm.5.json",
Version: "2.1.0",
@@ -20,7 +20,7 @@ func writeSarif(cfg config.Config, findings []*Finding, w io.WriteCloser) error
return encoder.Encode(sarif)
}
func getRuns(cfg config.Config, findings []*Finding) []Runs {
func getRuns(cfg config.Config, findings []Finding) []Runs {
return []Runs{
{
Tool: getTool(cfg),
@@ -54,7 +54,7 @@ func getRules(cfg config.Config) []Rules {
return rules
}
func messageText(f *Finding) string {
func messageText(f Finding) string {
if f.Commit == "" {
return fmt.Sprintf("%s has detected secret for file %s.", f.RuleID, f.File)
}
@@ -63,7 +63,7 @@ func messageText(f *Finding) string {
}
func getResults(findings []*Finding) []Results {
func getResults(findings []Finding) []Results {
var results []Results
for _, f := range findings {
r := Results{
@@ -87,7 +87,7 @@ func getResults(findings []*Finding) []Results {
return results
}
func getLocation(f *Finding) []Locations {
func getLocation(f Finding) []Locations {
return []Locations{
{
PhysicalLocation: PhysicalLocation{
+2 -2
View File
@@ -14,7 +14,7 @@ const configPath = "../testdata/config/"
func TestWriteSarif(t *testing.T) {
tests := []struct {
findings []*Finding
findings []Finding
testReportName string
expected string
wantEmpty bool
@@ -24,7 +24,7 @@ func TestWriteSarif(t *testing.T) {
cfgName: "simple",
testReportName: "simple",
expected: filepath.Join(expectPath, "report", "sarif_simple.sarif"),
findings: []*Finding{
findings: []Finding{
{
Description: "",
Binary file not shown.