mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-12-01 21:44:23 +01:00
Use first non-empty group if secretGroup isn't set (#1459)
* feat(detect): secret is first non-empty group * test: fix errors - update capture groups - update expected data
This commit is contained in:
1 parent
b4009bfba4
commit
d3c4b9078e
4 files changed
+28
-21
No files matched your search
+23
-16
@@ -291,24 +291,31 @@ func (d *Detector) detectRule(fragment Fragment, rule config.Rule) []report.Find
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// by default if secret group is not set, we will check to see if there
|
// Set the value of |secret|, if the pattern contains at least one capture group.
|
||||||
// are any capture groups. If there are, we will use the first capture to start
|
// (The first element is the full match, hence we check >= 2.)
|
||||||
groups := rule.Regex.FindStringSubmatch(secret)
|
groups := rule.Regex.FindStringSubmatch(finding.Secret)
|
||||||
if rule.SecretGroup == 0 {
|
if len(groups) >= 2 {
|
||||||
// if len(groups) == 2 that means there is only one capture group
|
if rule.SecretGroup > 0 {
|
||||||
// the first element in groups is the full match, the second is the
|
if len(groups) <= rule.SecretGroup {
|
||||||
// first capture group
|
|
||||||
if len(groups) == 2 {
|
|
||||||
secret = groups[1]
|
|
||||||
finding.Secret = secret
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if len(groups) <= rule.SecretGroup || len(groups) == 0 {
|
|
||||||
// Config validation should prevent this
|
// Config validation should prevent this
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
secret = groups[rule.SecretGroup]
|
finding.Secret = groups[rule.SecretGroup]
|
||||||
finding.Secret = secret
|
} else {
|
||||||
|
// If |secretGroup| is not set, we will use the first suitable capture group.
|
||||||
|
if len(groups) == 2 {
|
||||||
|
// Use the only group.
|
||||||
|
finding.Secret = groups[1]
|
||||||
|
} else {
|
||||||
|
// Use the first non-empty group.
|
||||||
|
for _, s := range groups[1:] {
|
||||||
|
if len(s) > 0 {
|
||||||
|
finding.Secret = s
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if the regexTarget is defined in the allowlist "regexes" entry
|
// check if the regexTarget is defined in the allowlist "regexes" entry
|
||||||
@@ -354,7 +361,7 @@ func (d *Detector) detectRule(fragment Fragment, rule config.Rule) []report.Find
|
|||||||
// secret contains both digits and alphabetical characters.
|
// secret contains both digits and alphabetical characters.
|
||||||
// TODO: this should be replaced with stop words
|
// TODO: this should be replaced with stop words
|
||||||
if strings.HasPrefix(rule.RuleID, "generic") {
|
if strings.HasPrefix(rule.RuleID, "generic") {
|
||||||
if !containsDigit(secret) {
|
if !containsDigit(finding.Secret) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ func TestWriteSarif(t *testing.T) {
|
|||||||
}
|
}
|
||||||
want, err := os.ReadFile(test.expected)
|
want, err := os.ReadFile(test.expected)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, want, got)
|
assert.Equal(t, string(want), string(got))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Vendored
+2
-2
@@ -79,13 +79,13 @@ title = "gitleaks config"
|
|||||||
[[rules]]
|
[[rules]]
|
||||||
id = "slack"
|
id = "slack"
|
||||||
description = "Slack"
|
description = "Slack"
|
||||||
regex = '''xox[baprs]-([0-9a-zA-Z]{10,48})?'''
|
regex = '''xox[baprs]-(?:[0-9a-zA-Z]{10,48})?'''
|
||||||
tags = ["key", "Slack"]
|
tags = ["key", "Slack"]
|
||||||
|
|
||||||
[[rules]]
|
[[rules]]
|
||||||
id = "apkey"
|
id = "apkey"
|
||||||
description = "Asymmetric Private Key"
|
description = "Asymmetric Private Key"
|
||||||
regex = '''-----BEGIN ((EC|PGP|DSA|RSA|OPENSSH) )?PRIVATE KEY( BLOCK)?-----'''
|
regex = '''-----BEGIN (?:(?:EC|PGP|DSA|RSA|OPENSSH) )?PRIVATE KEY(?: BLOCK)?-----'''
|
||||||
tags = ["key", "AsymmetricPrivateKey"]
|
tags = ["key", "AsymmetricPrivateKey"]
|
||||||
|
|
||||||
[[rules]]
|
[[rules]]
|
||||||
|
|||||||
+2
-2
@@ -104,14 +104,14 @@
|
|||||||
"id": "slack",
|
"id": "slack",
|
||||||
"name": "Slack",
|
"name": "Slack",
|
||||||
"shortDescription": {
|
"shortDescription": {
|
||||||
"text": "xox[baprs]-([0-9a-zA-Z]{10,48})?"
|
"text": "xox[baprs]-(?:[0-9a-zA-Z]{10,48})?"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": "apkey",
|
"id": "apkey",
|
||||||
"name": "Asymmetric Private Key",
|
"name": "Asymmetric Private Key",
|
||||||
"shortDescription": {
|
"shortDescription": {
|
||||||
"text": "-----BEGIN ((EC|PGP|DSA|RSA|OPENSSH) )?PRIVATE KEY( BLOCK)?-----"
|
"text": "-----BEGIN (?:(?:EC|PGP|DSA|RSA|OPENSSH) )?PRIVATE KEY(?: BLOCK)?-----"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in new issue
Block a user