Skip to content

Commit a3a6575

Browse files
committed
fix: validate secrets rules in configuration file
1 parent 7cb6828 commit a3a6575

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

internal/security/rule.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,16 @@ type Rule struct {
139139
Severity Severity
140140
Tags []string
141141
}
142+
143+
func NewRule(description string, file string, pattern string, severity string, tags []string) *Rule {
144+
rule := &Rule{
145+
Description: description,
146+
Regexp: regexp.MustCompile(pattern),
147+
Severity: Severity(severity),
148+
Tags: tags,
149+
}
150+
if file != "" {
151+
rule.File = regexp.MustCompile(file)
152+
}
153+
return rule
154+
}

internal/security/scanner_regex.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (scanner *RegexScanner) SatisfyRules(commit *object.Commit, filePath string
9090
"commit": commit.Hash.String(),
9191
"file": filePath,
9292
"rule": "security",
93-
}).Debugf("Searching for `%v`", rule.Description)
93+
}).Debugf("searching for `%v`", rule.Description)
9494
if rule.File != nil && rule.File.FindString(filePath) == "" {
9595
continue
9696
}
@@ -101,22 +101,22 @@ func (scanner *RegexScanner) SatisfyRules(commit *object.Commit, filePath string
101101
offender := content[match[0]:match[1]]
102102
groups := rule.Regexp.FindStringSubmatch(offender)
103103
names := rule.Regexp.SubexpNames()
104-
for i, group := range groups {
105-
if i != 0 && names[i] == "secret" {
104+
for index, group := range groups {
105+
if index != 0 && names[index] == "secret" {
106106
offender = group
107107
break
108108
}
109109
}
110110
if len(rule.Entropies) > 0 && !scanner.validateEntropy(groups, rule) {
111-
scanner.Logger.Debugf("Entropy not satisfied on secret %s", offender)
111+
scanner.Logger.Debugf("entropy not satisfied on secret %s", offender)
112112
continue
113113
}
114114
if scanner.checkFalsePositive(filePath, line, offender) != IsPositive {
115115
scanner.Logger.WithFields(logging.Fields{
116116
"condition": "secret",
117117
"commit": commit.Hash.String(),
118118
"rule": "security",
119-
}).Warningf("False positive secret %s", offender)
119+
}).Warningf("false positive secret %s", offender)
120120
continue
121121
}
122122
file, _ := commit.File(filePath)
@@ -188,12 +188,14 @@ func NewRegexScanner(logger logging.Interface, options *config.Options) *RegexSc
188188
if len(options.Security.Rules) > 0 {
189189
var defaultRules []Rule
190190
for _, rule := range options.Security.Rules {
191-
defaultRules = append(defaultRules, Rule{
192-
Description: rule.Description,
193-
File: regexp.MustCompile(rule.File),
194-
Regexp: regexp.MustCompile(rule.Regexp),
195-
Severity: Severity(rule.Severity),
196-
})
191+
logger.WithFields(logging.Fields{
192+
"condition": "secret",
193+
"rule": "security",
194+
}).Infof("adding secret rule %s", rule.Description)
195+
defaultRule := NewRule(rule.Description, rule.File, rule.Regexp, rule.Severity, rule.Tags)
196+
if defaultRule != nil {
197+
defaultRules = append(defaultRules, *defaultRule)
198+
}
197199
}
198200
if options.Security.MergeRules {
199201
rules = append(rules, defaultRules...)

0 commit comments

Comments
 (0)