feat(rule): validate & sort rule when generating (#1817)

This commit is contained in:
Richard Gomez authored and GitHub committed 2025-04-12 10:09:19 -05:00
1 parent 107a41827b
commit cfdeb0d7e0
18 files changed
+145 -115

No files matched your search

+22 -4
View File
@@ -38,9 +38,6 @@ type Allowlist struct {
// Paths is a slice of path regular expressions that are allowed to be ignored.
Paths []*regexp.Regexp
// Regexes is slice of content regular expressions that are allowed to be ignored.
Regexes []*regexp.Regexp
// Can be `match` or `line`.
//
// If `match` the _Regexes_ will be tested against the match of the _Rule.Regex_.
@@ -50,15 +47,21 @@ type Allowlist struct {
// If RegexTarget is empty, it will be tested against the found secret.
RegexTarget string
// Regexes is slice of content regular expressions that are allowed to be ignored.
Regexes []*regexp.Regexp
// StopWords is a slice of stop words that are allowed to be ignored.
// This targets the _secret_, not the content of the regex match like the
// Regexes slice.
StopWords []string
// validated is an internal flag to track whether `Validate()` has been called.
validated bool
}
// CommitAllowed returns true if the commit is allowed to be ignored.
func (a *Allowlist) CommitAllowed(c string) (bool, string) {
if c == "" {
if a == nil || c == "" {
return false, ""
}
@@ -72,15 +75,25 @@ func (a *Allowlist) CommitAllowed(c string) (bool, string) {
// PathAllowed returns true if the path is allowed to be ignored.
func (a *Allowlist) PathAllowed(path string) bool {
if a == nil || path == "" {
return false
}
return anyRegexMatch(path, a.Paths)
}
// RegexAllowed returns true if the regex is allowed to be ignored.
func (a *Allowlist) RegexAllowed(secret string) bool {
if a == nil || secret == "" {
return false
}
return anyRegexMatch(secret, a.Regexes)
}
func (a *Allowlist) ContainsStopWord(s string) (bool, string) {
if a == nil || s == "" {
return false, ""
}
s = strings.ToLower(s)
for _, stopWord := range a.StopWords {
if strings.Contains(s, strings.ToLower(stopWord)) {
@@ -91,6 +104,10 @@ func (a *Allowlist) ContainsStopWord(s string) (bool, string) {
}
func (a *Allowlist) Validate() error {
if a.validated {
return nil
}
// Disallow empty allowlists.
if len(a.Commits) == 0 &&
len(a.Paths) == 0 &&
@@ -116,5 +133,6 @@ func (a *Allowlist) Validate() error {
a.StopWords = maps.Keys(uniqueStopwords)
}
a.validated = true
return nil
}
+1
View File
@@ -148,6 +148,7 @@ func TestValidate(t *testing.T) {
opts = cmp.Options{
cmp.Comparer(regexComparer),
cmpopts.SortSlices(arrayComparer),
cmpopts.IgnoreUnexported(Allowlist{}),
}
)
if diff := cmp.Diff(tt.input, tt.expected, opts); diff != "" {
+6 -8
View File
@@ -67,7 +67,7 @@ type Config struct {
Path string
Description string
Rules map[string]Rule
Allowlist Allowlist
Allowlist *Allowlist
Keywords map[string]struct{}
// used to keep sarif results consistent
@@ -164,17 +164,15 @@ func (vc *ViperConfig) Translate() (Config, error) {
allowlistPaths = append(allowlistPaths, regexp.MustCompile(a))
}
allowlist := Allowlist{
allowlist := &Allowlist{
Description: a.Description,
MatchCondition: condition,
Commits: a.Commits,
Paths: allowlistPaths,
RegexTarget: a.RegexTarget,
Regexes: allowlistRegexes,
Paths: allowlistPaths,
Commits: a.Commits,
StopWords: a.StopWords,
}
if err := allowlist.Validate(); err != nil {
return Config{}, fmt.Errorf("%s: %w", cr.RuleID, err)
}
cr.Allowlists = append(cr.Allowlists, allowlist)
}
orderedRules = append(orderedRules, cr.RuleID)
@@ -192,7 +190,7 @@ func (vc *ViperConfig) Translate() (Config, error) {
Description: vc.Description,
Extend: vc.Extend,
Rules: rulesMap,
Allowlist: Allowlist{
Allowlist: &Allowlist{
RegexTarget: vc.Allowlist.RegexTarget,
Regexes: allowlistRegexes,
Paths: allowlistPaths,
+13 -8
View File
@@ -2,6 +2,7 @@ package config
import (
"fmt"
"github.com/google/go-cmp/cmp/cmpopts"
"testing"
"github.com/google/go-cmp/cmp"
@@ -33,7 +34,7 @@ func TestTranslate(t *testing.T) {
Regex: regexp.MustCompile(`example\d+`),
Tags: []string{},
Keywords: []string{},
Allowlists: []Allowlist{
Allowlists: []*Allowlist{
{
MatchCondition: AllowlistMatchOr,
Regexes: []*regexp.Regexp{regexp.MustCompile("123")},
@@ -66,7 +67,7 @@ func TestTranslate(t *testing.T) {
Regex: regexp.MustCompile("(?:A3T[A-Z0-9]|AKIA|ASIA|ABIA|ACCA)[A-Z0-9]{16}"),
Keywords: []string{},
Tags: []string{"key", "AWS"},
Allowlists: []Allowlist{
Allowlists: []*Allowlist{
{
MatchCondition: AllowlistMatchOr,
Regexes: []*regexp.Regexp{regexp.MustCompile("AKIALALEMEL33243OLIA")},
@@ -84,7 +85,7 @@ func TestTranslate(t *testing.T) {
Regex: regexp.MustCompile("(?:A3T[A-Z0-9]|AKIA|ASIA|ABIA|ACCA)[A-Z0-9]{16}"),
Keywords: []string{},
Tags: []string{"key", "AWS"},
Allowlists: []Allowlist{
Allowlists: []*Allowlist{
{
MatchCondition: AllowlistMatchOr,
Commits: []string{"allowthiscommit"},
@@ -102,7 +103,7 @@ func TestTranslate(t *testing.T) {
Regex: regexp.MustCompile("(?:A3T[A-Z0-9]|AKIA|ASIA|ABIA|ACCA)[A-Z0-9]{16}"),
Keywords: []string{},
Tags: []string{"key", "AWS"},
Allowlists: []Allowlist{
Allowlists: []*Allowlist{
{
MatchCondition: AllowlistMatchOr,
Paths: []*regexp.Regexp{regexp.MustCompile(".go")},
@@ -178,7 +179,7 @@ func TestTranslate(t *testing.T) {
Regex: regexp.MustCompile(`(?i)aws_(.{0,20})?=?.[\'\"0-9a-zA-Z\/+]{40}`),
Keywords: []string{},
Tags: []string{"key", "AWS"},
Allowlists: []Allowlist{
Allowlists: []*Allowlist{
{
MatchCondition: AllowlistMatchOr,
StopWords: []string{"fake"},
@@ -206,7 +207,7 @@ func TestTranslate(t *testing.T) {
Regex: regexp.MustCompile(`(?i)aws_(.{0,20})?=?.[\'\"0-9a-zA-Z\/+]{40}`),
Keywords: []string{},
Tags: []string{"key", "AWS"},
Allowlists: []Allowlist{
Allowlists: []*Allowlist{
{
MatchCondition: AllowlistMatchOr,
StopWords: []string{"fake"},
@@ -234,8 +235,9 @@ func TestTranslate(t *testing.T) {
Regex: regexp.MustCompile(`(?i)aws_(.{0,20})?=?.[\'\"0-9a-zA-Z\/+]{40}`),
Keywords: []string{},
Tags: []string{"key", "AWS"},
Allowlists: []Allowlist{
Allowlists: []*Allowlist{
{
Description: "False positive. Keys used for colors match the rule, and should be excluded.",
MatchCondition: AllowlistMatchOr,
Paths: []*regexp.Regexp{regexp.MustCompile(`something.py`)},
},
@@ -401,7 +403,10 @@ func TestTranslate(t *testing.T) {
}
return x.String() == y.String()
}
opts := cmp.Options{cmp.Comparer(regexComparer)}
opts := cmp.Options{
cmp.Comparer(regexComparer),
cmpopts.IgnoreUnexported(Rule{}, Allowlist{}),
}
if diff := cmp.Diff(tt.cfg.Rules, cfg.Rules, opts); diff != "" {
t.Errorf("%s diff: (-want +got)\n%s", tt.cfgName, diff)
}
+5 -36
View File
@@ -595,6 +595,8 @@ regexes = [
]
stopwords = [
"000000",
"6fe4476ee5a1832882e326b506d14126",
"_ec2_",
"aaaaaa",
"about",
"abstract",
@@ -610,9 +612,9 @@ stopwords = [
"activity",
"adapter",
"add-",
"add-on",
"add.",
"add_",
"add-on",
"addon",
"addres",
"admin",
@@ -655,10 +657,7 @@ stopwords = [
"any_",
"apache",
"app-",
"app-",
"app.",
"app.",
"app_",
"app_",
"apple",
"arch",
@@ -682,7 +681,6 @@ stopwords = [
"aura",
"auth",
"author",
"author",
"authorize",
"auto",
"automated",
@@ -803,7 +801,6 @@ stopwords = [
"cli.",
"cli_",
"client",
"client",
"clojure",
"clone",
"closure",
@@ -838,7 +835,6 @@ stopwords = [
"concept",
"conf",
"config",
"config",
"connect",
"connector",
"console",
@@ -897,7 +893,6 @@ stopwords = [
"dead",
"debian",
"debug",
"debug",
"debugger",
"deck",
"define",
@@ -929,10 +924,7 @@ stopwords = [
"dns-",
"dns_",
"doc-",
"doc-",
"doc.",
"doc.",
"doc_",
"doc_",
"docker",
"docpad",
@@ -946,8 +938,8 @@ stopwords = [
"dom.",
"dom_",
"domain",
"done",
"don't",
"done",
"dot-",
"dot.",
"dot_",
@@ -967,7 +959,6 @@ stopwords = [
"dsl_",
"dynamic",
"easy",
"_ec2_",
"ecdsa",
"eclipse",
"edit",
@@ -997,7 +988,6 @@ stopwords = [
"event",
"evented",
"example",
"example",
"exchange",
"exercise",
"experiment",
@@ -1245,10 +1235,7 @@ stopwords = [
"koan",
"kohana",
"lab-",
"lab-",
"lab.",
"lab.",
"lab_",
"lab_",
"lambda",
"lamp",
@@ -1333,10 +1320,7 @@ stopwords = [
"manifest",
"manual",
"map-",
"map-",
"map.",
"map.",
"map_",
"map_",
"mapper",
"mapping",
@@ -1422,10 +1406,7 @@ stopwords = [
"nette",
"network",
"new-",
"new-",
"new.",
"new.",
"new_",
"new_",
"next",
"nginx",
@@ -1494,7 +1475,6 @@ stopwords = [
"package",
"packet",
"page",
"page",
"panel",
"paper",
"paperclip",
@@ -1611,8 +1591,8 @@ stopwords = [
"readme",
"ready",
"real",
"reality",
"real-time",
"reality",
"realtime",
"recipe",
"recorder",
@@ -1678,7 +1658,6 @@ stopwords = [
"rvm_",
"salt",
"sample",
"sample",
"sandbox",
"sas-",
"sas.",
@@ -1729,7 +1708,6 @@ stopwords = [
"set.",
"set_",
"setting",
"setting",
"setup",
"sha1",
"sha2",
@@ -1852,19 +1830,12 @@ stopwords = [
"synopsi",
"syntax",
"system",
"system",
"tab-",
"tab-",
"tab.",
"tab.",
"tab_",
"tab_",
"table",
"tag-",
"tag-",
"tag.",
"tag.",
"tag_",
"tag_",
"talk",
"target",
@@ -2003,7 +1974,6 @@ stopwords = [
"website",
"websocket",
"welcome",
"welcome",
"what",
"what'",
"when",
@@ -2070,7 +2040,6 @@ stopwords = [
"zsh-",
"zsh.",
"zsh_",
"6fe4476ee5a1832882e326b506d14126",
]
[[rules.allowlists]]
regexTarget = "line"
+20 -2
View File
@@ -41,11 +41,18 @@ type Rule struct {
Keywords []string
// Allowlists allows a rule to be ignored for specific commits, paths, regexes, and/or stopwords.
Allowlists []Allowlist
Allowlists []*Allowlist
// validated is an internal flag to track whether `Validate()` has been called.
validated bool
}
// Validate guards against common misconfigurations.
func (r Rule) Validate() error {
func (r *Rule) Validate() error {
if r.validated {
return nil
}
// Ensure |id| is present.
if strings.TrimSpace(r.RuleID) == "" {
// Try to provide helpful context, since |id| is empty.
@@ -70,5 +77,16 @@ func (r Rule) Validate() error {
return fmt.Errorf("%s: invalid regex secret group %d, max regex secret group %d", r.RuleID, r.SecretGroup, r.Regex.NumSubexp())
}
for _, allowlist := range r.Allowlists {
// This will probably never happen.
if allowlist == nil {
continue
}
if err := allowlist.Validate(); err != nil {
return fmt.Errorf("%s: %w", r.RuleID, err)
}
}
r.validated = true
return nil
}