mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-29 04:34:46 +01:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2acc34dfdf | ||
|
|
ede68fd95d | ||
|
|
6c60c3dde3 |
No files matched your search
@@ -40,6 +40,7 @@ type Rule struct {
|
|||||||
Regex *regexp.Regexp
|
Regex *regexp.Regexp
|
||||||
File *regexp.Regexp
|
File *regexp.Regexp
|
||||||
Path *regexp.Regexp
|
Path *regexp.Regexp
|
||||||
|
ReportGroup int
|
||||||
Tags []string
|
Tags []string
|
||||||
AllowList AllowList
|
AllowList AllowList
|
||||||
Entropies []Entropy
|
Entropies []Entropy
|
||||||
@@ -73,6 +74,7 @@ type TomlLoader struct {
|
|||||||
Regex string
|
Regex string
|
||||||
File string
|
File string
|
||||||
Path string
|
Path string
|
||||||
|
ReportGroup int
|
||||||
Tags []string
|
Tags []string
|
||||||
Entropies []struct {
|
Entropies []struct {
|
||||||
Min string
|
Min string
|
||||||
@@ -198,6 +200,7 @@ func (tomlLoader TomlLoader) Parse() (Config, error) {
|
|||||||
Regex: re,
|
Regex: re,
|
||||||
File: fileNameRe,
|
File: fileNameRe,
|
||||||
Path: filePathRe,
|
Path: filePathRe,
|
||||||
|
ReportGroup: rule.ReportGroup,
|
||||||
Tags: rule.Tags,
|
Tags: rule.Tags,
|
||||||
AllowList: allowList,
|
AllowList: allowList,
|
||||||
Entropies: entropies,
|
Entropies: entropies,
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package config
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -129,3 +131,83 @@ func TestParse(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestParseFields will test that fields are properly parsed from a config. As fields are added, then please
|
||||||
|
// add tests here.
|
||||||
|
func TestParseFields(t *testing.T) {
|
||||||
|
tomlConfig := `
|
||||||
|
[[rules]]
|
||||||
|
description = "Some Groups without a reportGroup"
|
||||||
|
regex = '(.)(.)'
|
||||||
|
|
||||||
|
[[rules]]
|
||||||
|
description = "Some Groups"
|
||||||
|
regex = '(.)(.)'
|
||||||
|
reportGroup = 1
|
||||||
|
`
|
||||||
|
configPath, err := writeTestConfig(tomlConfig)
|
||||||
|
defer os.Remove(configPath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
config, err := NewConfig(options.Options{Config: configPath})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Couldn't parse config: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
expectedRuleFields := []struct {
|
||||||
|
Description string
|
||||||
|
ReportGroup int
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
Description: "Some Groups without a reportGroup",
|
||||||
|
ReportGroup: 0,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Description: "Some Groups",
|
||||||
|
ReportGroup: 1,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(config.Rules) != len(expectedRuleFields) {
|
||||||
|
t.Fatalf("expected %v rules", len(expectedRuleFields))
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, expected := range expectedRuleFields {
|
||||||
|
rule, err := findRuleByDescription(config.Rules, expected.Description)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if rule.ReportGroup != expected.ReportGroup {
|
||||||
|
t.Errorf("expected the rule with description '%v' to have a ReportGroup of %v", expected.Description, expected.ReportGroup)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func findRuleByDescription(rules []Rule, description string) (*Rule, error) {
|
||||||
|
for _, rule := range rules {
|
||||||
|
if rule.Description == description {
|
||||||
|
return &rule, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, fmt.Errorf("Couldn't find rule with the description: %s", description)
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeTestConfig(toml string) (string, error) {
|
||||||
|
tmpfile, err := ioutil.TempFile("", "testConfig")
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("Couldn't create test config got: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := tmpfile.Write([]byte(toml)); err != nil {
|
||||||
|
return "", fmt.Errorf("Couldn't create test config got: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := tmpfile.Close(); err != nil {
|
||||||
|
return "", fmt.Errorf("Couldn't create test config got: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return tmpfile.Name(), nil
|
||||||
|
}
|
||||||
+24
-5
@@ -20,11 +20,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
diffAddPrefix = "+"
|
diffAddPrefix = "+"
|
||||||
diffAddFilePrefix = "+++ b"
|
diffAddFilePrefix = "+++ b"
|
||||||
diffAddFilePrefixSlash = "+++ b/"
|
diffAddFilePrefixSlash = "+++ b/"
|
||||||
diffLineSignature = " @@"
|
diffLineSignature = " @@"
|
||||||
defaultLineNumber = -1
|
diffLineSignaturePrefix = "@@ "
|
||||||
|
defaultLineNumber = -1
|
||||||
)
|
)
|
||||||
|
|
||||||
// CheckRules accepts bundle and checks each rule defined in the config against the bundle's content.
|
// CheckRules accepts bundle and checks each rule defined in the config against the bundle's content.
|
||||||
@@ -120,6 +121,11 @@ func (repo *Repo) CheckRules(bundle *Bundle) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 0 is a match for the full regex pattern
|
||||||
|
if 0 < rule.ReportGroup && rule.ReportGroup < len(groups) {
|
||||||
|
offender = groups[rule.ReportGroup]
|
||||||
|
}
|
||||||
|
|
||||||
leak := manager.Leak{
|
leak := manager.Leak{
|
||||||
LineNumber: defaultLineNumber,
|
LineNumber: defaultLineNumber,
|
||||||
Line: line,
|
Line: line,
|
||||||
@@ -238,6 +244,19 @@ func extractAndInjectLineNumber(leak *manager.Leak, bundle *Bundle, repo *Repo)
|
|||||||
leak.LineNumber = potentialLine
|
leak.LineNumber = potentialLine
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
} else if strings.HasPrefix(txt, diffLineSignaturePrefix) && currStartDiffLine != 0 {
|
||||||
|
// This logic is used for when there are multiple leaks of the same offender within the same patch
|
||||||
|
i := strings.Index(txt, diffAddPrefix)
|
||||||
|
pairs := strings.Split(strings.Split(txt[i+1:], diffLineSignature)[0], ",")
|
||||||
|
currStartDiffLine, err = strconv.Atoi(pairs[0])
|
||||||
|
if err != nil {
|
||||||
|
log.Debug(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
currLine = 0
|
||||||
|
if !strings.HasSuffix(txt, diffLineSignature) {
|
||||||
|
currLine = -1
|
||||||
|
}
|
||||||
}
|
}
|
||||||
currLine++
|
currLine++
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -144,6 +144,17 @@ func TestScan(t *testing.T) {
|
|||||||
},
|
},
|
||||||
wantEmpty: true,
|
wantEmpty: true,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
description: "test local repo three leaks dev branch with reportGroup set",
|
||||||
|
opts: options.Options{
|
||||||
|
RepoPath: "../test_data/test_repos/test_repo_3",
|
||||||
|
Report: "../test_data/test_local_repo_three_leaks_with_report_groups.json.got",
|
||||||
|
Config: "../test_data/test_configs/aws_key_with_report_groups.toml",
|
||||||
|
Branch: "dev",
|
||||||
|
ReportFormat: "json",
|
||||||
|
},
|
||||||
|
wantPath: "../test_data/test_local_repo_three_leaks_with_report_groups.json",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
description: "test local repo three leaks dev branch",
|
description: "test local repo three leaks dev branch",
|
||||||
opts: options.Options{
|
opts: options.Options{
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
[[rules]]
|
||||||
|
description = "AWS Secret Key"
|
||||||
|
regex = '''(?i)aws(.{0,20})?(?-i)['\"][0-9a-zA-Z\/+]{40}['\"]'''
|
||||||
|
tags = ["key", "AWS"]
|
||||||
|
|
||||||
|
[[rules]]
|
||||||
|
description = "AWS Manager ID"
|
||||||
|
regex = '''AWS secret: ("?)((A3T[A-Z0-9]|AKIA|AGPA|AIDA|AROA|AIPA|ANPA|ANVA|ASIA)[A-Z0-9]{16})("?)'''
|
||||||
|
tags = ["key", "AWS"]
|
||||||
|
reportGroup = 2
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"line": "AWS secret: \"AKIALALEMEL33243OLIAE\"",
|
||||||
|
"lineNumber": 6,
|
||||||
|
"offender": "AKIALALEMEL33243OLIA",
|
||||||
|
"commit": "deea550dd6c7acaf0e59432600593533984a2125",
|
||||||
|
"repo": "test_repo_3",
|
||||||
|
"rule": "AWS Manager ID",
|
||||||
|
"commitMessage": "dev branch\n",
|
||||||
|
"author": "zach rice",
|
||||||
|
"email": "zricer@protonmail.com",
|
||||||
|
"file": "secrets.md",
|
||||||
|
"date": "2019-10-25T13:35:03-04:00",
|
||||||
|
"tags": "key, AWS",
|
||||||
|
"operation": "addition"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"line": "Here's an AWS secret: \"AKIALALEMEL33243OLIAE\"",
|
||||||
|
"lineNumber": 5,
|
||||||
|
"offender": "AKIALALEMEL33243OLIA",
|
||||||
|
"commit": "17471a5fda722a9e423f1a0d3f0d267ea009d41c",
|
||||||
|
"repo": "test_repo_3",
|
||||||
|
"rule": "AWS Manager ID",
|
||||||
|
"commitMessage": "wait this is actually adding an aws secret\n",
|
||||||
|
"author": "zach rice",
|
||||||
|
"email": "zricer@protonmail.com",
|
||||||
|
"file": "secrets.md",
|
||||||
|
"date": "2019-10-25T13:01:27-04:00",
|
||||||
|
"tags": "key, AWS",
|
||||||
|
"operation": "addition"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"line": "Here's an AWS secret: AKIALALEMEL33243OLIAE",
|
||||||
|
"lineNumber": 3,
|
||||||
|
"offender": "AKIALALEMEL33243OLIA",
|
||||||
|
"commit": "b10b3e2cb320a8c211fda94c4567299d37de7776",
|
||||||
|
"repo": "test_repo_3",
|
||||||
|
"rule": "AWS Manager ID",
|
||||||
|
"commitMessage": "adding aws key\n",
|
||||||
|
"author": "zach rice",
|
||||||
|
"email": "zricer@protonmail.com",
|
||||||
|
"file": "secrets.md",
|
||||||
|
"date": "2019-10-25T12:58:39-04:00",
|
||||||
|
"tags": "key, AWS",
|
||||||
|
"operation": "addition"
|
||||||
|
}
|
||||||
|
]
|
||||||
Reference in new issue
Block a user