diff --git a/cmd/generate/config/base/config.go b/cmd/generate/config/base/config.go index c155586c..c1fe777a 100644 --- a/cmd/generate/config/base/config.go +++ b/cmd/generate/config/base/config.go @@ -11,7 +11,7 @@ import ( func CreateGlobalConfig() config.Config { return config.Config{ Title: "gitleaks config", - Allowlist: config.Allowlist{ + Allowlist: &config.Allowlist{ Description: "global allow lists", Regexes: []*regexp.Regexp{ // ----------- General placeholders ----------- diff --git a/cmd/generate/config/main.go b/cmd/generate/config/main.go index ff580846..7a654e64 100644 --- a/cmd/generate/config/main.go +++ b/cmd/generate/config/main.go @@ -1,6 +1,7 @@ package main import ( + "golang.org/x/exp/slices" "os" "text/template" @@ -18,7 +19,7 @@ const ( func main() { if len(os.Args) < 2 { - os.Stderr.WriteString("Specify path to the gitleaks.toml config\n") + _, _ = os.Stderr.WriteString("Specify path to the gitleaks.toml config\n") os.Exit(2) } gitleaksConfigPath := os.Args[1] @@ -239,9 +240,18 @@ func main() { // ensure rules have unique ids ruleLookUp := make(map[string]config.Rule, len(configRules)) for _, rule := range configRules { + if err := rule.Validate(); err != nil { + logging.Fatal().Err(err). + Str("rule-id", rule.RuleID). + Msg("Failed to validate rule") + } + sortRule(rule) + // check if rule is in ruleLookUp if _, ok := ruleLookUp[rule.RuleID]; ok { - logging.Fatal().Msgf("rule id %s is not unique", rule.RuleID) + logging.Fatal(). + Str("rule-id", rule.RuleID). + Msg("rule id is not unique") } // TODO: eventually change all the signatures to get ride of this // nasty dereferencing. @@ -257,6 +267,7 @@ func main() { if err != nil { logging.Fatal().Err(err).Msg("Failed to create rules.toml") } + defer f.Close() cfg := base.CreateGlobalConfig() cfg.Rules = ruleLookUp @@ -264,3 +275,10 @@ func main() { logging.Fatal().Err(err).Msg("could not execute template") } } + +// sortRule makes the generated config deterministic. +func sortRule(r *config.Rule) { + for _, a := range r.Allowlists { + slices.Sort(a.StopWords) + } +} diff --git a/cmd/generate/config/rules/aws.go b/cmd/generate/config/rules/aws.go index 9022ef49..44717e3a 100644 --- a/cmd/generate/config/rules/aws.go +++ b/cmd/generate/config/rules/aws.go @@ -22,7 +22,7 @@ func AWS() *config.Rule { "ABIA", // AWS STS service bearer token "ACCA", // Context-specific credential }, - Allowlists: []config.Allowlist{ + Allowlists: []*config.Allowlist{ { Regexes: []*regexp.Regexp{ regexp.MustCompile(`.+EXAMPLE$`), diff --git a/cmd/generate/config/rules/curl.go b/cmd/generate/config/rules/curl.go index 783d29d9..be7572fe 100644 --- a/cmd/generate/config/rules/curl.go +++ b/cmd/generate/config/rules/curl.go @@ -16,7 +16,7 @@ func CurlBasicAuth() *config.Rule { Regex: regexp.MustCompile(`\bcurl\b(?:.*|.*(?:[\r\n]{1,2}.*){1,5})[ \t\n\r](?:-u|--user)(?:=|[ \t]{0,5})("(:[^"]{3,}|[^:"]{3,}:|[^:"]{3,}:[^"]{3,})"|'([^:']{3,}:[^']{3,})'|((?:"[^"]{3,}"|'[^']{3,}'|[\w$@.-]+):(?:"[^"]{3,}"|'[^']{3,}'|[\w${}@.-]+)))(?:\s|\z)`), Keywords: []string{"curl"}, Entropy: 2, - Allowlists: []config.Allowlist{ + Allowlists: []*config.Allowlist{ { Regexes: []*regexp.Regexp{ regexp.MustCompile(`[^:]+:(?:change(?:it|me)|pass(?:word)?|pwd|test|token|\*+|x+)`), // common placeholder passwords @@ -109,7 +109,7 @@ func CurlHeaderAuth() *config.Rule { fmt.Sprintf(`\bcurl\b(?:.*?|.*?(?:[\r\n]{1,2}.*?){1,5})[ \t\n\r](?:-H|--header)(?:=|[ \t]{0,5})(?:"%s"|'%s')(?:\B|\s|\z)`, authPat, authPat)), Entropy: 2.75, Keywords: []string{"curl"}, - //Allowlists: []config.Allowlist{ + //Allowlists: []*config.Allowlist{ // { // Regexes: []*regexp.Regexp{}, // }, diff --git a/cmd/generate/config/rules/gcp.go b/cmd/generate/config/rules/gcp.go index 2bffe0a1..c2dbfd6a 100644 --- a/cmd/generate/config/rules/gcp.go +++ b/cmd/generate/config/rules/gcp.go @@ -34,7 +34,7 @@ func GCPAPIKey() *config.Rule { Keywords: []string{ "AIza", }, - Allowlists: []config.Allowlist{ + Allowlists: []*config.Allowlist{ { Regexes: []*regexp.Regexp{ // example keys from https://github.com/firebase/firebase-android-sdk diff --git a/cmd/generate/config/rules/generic.go b/cmd/generate/config/rules/generic.go index 9a1d2bd7..fa968cd7 100644 --- a/cmd/generate/config/rules/generic.go +++ b/cmd/generate/config/rules/generic.go @@ -36,7 +36,7 @@ func GenericCredential() *config.Rule { "token", }, Entropy: 3.5, - Allowlists: []config.Allowlist{ + Allowlists: []*config.Allowlist{ { // NOTE: this is a goofy hack to get around the fact there golang's regex engine does not support positive lookaheads. // Ideally we would want to ensure the secret contains both numbers and alphabetical characters, not just alphabetical characters. @@ -281,7 +281,7 @@ jdbc.snowflake.url=`, } func newPlausibleSecret(regex string) string { - allowList := config.Allowlist{StopWords: DefaultStopWords} + allowList := &config.Allowlist{StopWords: DefaultStopWords} // attempt to generate a random secret, // retrying until it contains at least one digit and no stop words // TODO: currently the DefaultStopWords list contains many short words, diff --git a/cmd/generate/config/rules/github.go b/cmd/generate/config/rules/github.go index f7cfc96c..58aaf43e 100644 --- a/cmd/generate/config/rules/github.go +++ b/cmd/generate/config/rules/github.go @@ -7,7 +7,7 @@ import ( "github.com/zricethezav/gitleaks/v8/regexp" ) -var githubAllowlist = []config.Allowlist{ +var githubAllowlist = []*config.Allowlist{ { Paths: []*regexp.Regexp{ // https://github.com/octokit/auth-token.js/?tab=readme-ov-file#createtokenauthtoken-options diff --git a/cmd/generate/config/rules/hashicorp_vault.go b/cmd/generate/config/rules/hashicorp_vault.go index ecc6a6b7..c3296896 100644 --- a/cmd/generate/config/rules/hashicorp_vault.go +++ b/cmd/generate/config/rules/hashicorp_vault.go @@ -15,7 +15,7 @@ func VaultServiceToken() *config.Rule { Regex: utils.GenerateUniqueTokenRegex(`(?:hvs\.[\w-]{90,120}|s\.(?i:[a-z0-9]{24}))`, false), Entropy: 3.5, Keywords: []string{"hvs.", "s."}, - Allowlists: []config.Allowlist{ + Allowlists: []*config.Allowlist{ { Regexes: []*regexp.Regexp{ // https://github.com/gitleaks/gitleaks/issues/1490#issuecomment-2334166357 diff --git a/cmd/generate/config/rules/kubernetes.go b/cmd/generate/config/rules/kubernetes.go index cb04f214..3095ba0a 100644 --- a/cmd/generate/config/rules/kubernetes.go +++ b/cmd/generate/config/rules/kubernetes.go @@ -31,7 +31,7 @@ func KubernetesSecret() *config.Rule { }, // Kubernetes secrets are usually yaml files. Path: regexp.MustCompile(`(?i)\.ya?ml$`), - Allowlists: []config.Allowlist{ + Allowlists: []*config.Allowlist{ { Regexes: []*regexp.Regexp{ // Ignore empty or placeholder values. diff --git a/cmd/generate/config/rules/nuget.go b/cmd/generate/config/rules/nuget.go index 76eaf259..6c6ae08f 100644 --- a/cmd/generate/config/rules/nuget.go +++ b/cmd/generate/config/rules/nuget.go @@ -14,7 +14,7 @@ func NugetConfigPassword() *config.Rule { Path: regexp.MustCompile(`(?i)nuget\.config$`), Keywords: []string{"