mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-30 13:14:22 +01:00
Make config generation utils public (#1480)
* chore(generate): make utils public * chore(generate): change validate log messages * chore(generate): split functions into files * chore(config): add warning for public funcs
This commit is contained in:
1 parent
3be7faa1ff
commit
bd81872eee
114 files changed
+862
-713
No files matched your search
@@ -0,0 +1,71 @@
|
||||
// == WARNING ==
|
||||
// These functions are used to generate GitLeak's default config.
|
||||
// You are free to use these in your own project, HOWEVER, no API stability is guaranteed.
|
||||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
// case insensitive prefix
|
||||
caseInsensitive = `(?i)`
|
||||
|
||||
// identifier prefix (just an ignore group)
|
||||
identifierCaseInsensitivePrefix = `(?i:`
|
||||
identifierCaseInsensitiveSuffix = `)`
|
||||
identifierPrefix = `(?:`
|
||||
identifierSuffix = `)(?:[0-9a-z\-_\t .]{0,20})(?:[\s|']|[\s|"]){0,3}`
|
||||
|
||||
// commonly used assignment operators or function call
|
||||
operator = `(?:=|>|:{1,3}=|\|\|:|<=|=>|:|\?=)`
|
||||
|
||||
// boundaries for the secret
|
||||
// \x60 = `
|
||||
secretPrefixUnique = `\b(`
|
||||
secretPrefix = `(?:'|\"|\s|=|\x60){0,5}(`
|
||||
secretSuffix = `)(?:['|\"|\n|\r|\s|\x60|;]|$)`
|
||||
)
|
||||
|
||||
func GenerateSemiGenericRegex(identifiers []string, secretRegex string, isCaseInsensitive bool) *regexp.Regexp {
|
||||
var sb strings.Builder
|
||||
// The identifiers should always be case-insensitive.
|
||||
// This is inelegant but prevents an extraneous `(?i:)` from being added to the pattern; it could be removed.
|
||||
if isCaseInsensitive {
|
||||
sb.WriteString(caseInsensitive)
|
||||
writeIdentifiers(&sb, identifiers)
|
||||
} else {
|
||||
sb.WriteString(identifierCaseInsensitivePrefix)
|
||||
writeIdentifiers(&sb, identifiers)
|
||||
sb.WriteString(identifierCaseInsensitiveSuffix)
|
||||
}
|
||||
sb.WriteString(operator)
|
||||
sb.WriteString(secretPrefix)
|
||||
sb.WriteString(secretRegex)
|
||||
sb.WriteString(secretSuffix)
|
||||
return regexp.MustCompile(sb.String())
|
||||
}
|
||||
|
||||
func writeIdentifiers(sb *strings.Builder, identifiers []string) {
|
||||
sb.WriteString(identifierPrefix)
|
||||
sb.WriteString(strings.Join(identifiers, "|"))
|
||||
sb.WriteString(identifierSuffix)
|
||||
}
|
||||
|
||||
func GenerateUniqueTokenRegex(secretRegex string, isCaseInsensitive bool) *regexp.Regexp {
|
||||
var sb strings.Builder
|
||||
if isCaseInsensitive {
|
||||
sb.WriteString(caseInsensitive)
|
||||
}
|
||||
sb.WriteString(secretPrefixUnique)
|
||||
sb.WriteString(secretRegex)
|
||||
sb.WriteString(secretSuffix)
|
||||
return regexp.MustCompile(sb.String())
|
||||
}
|
||||
|
||||
func GenerateSampleSecret(identifier string, secret string) string {
|
||||
return fmt.Sprintf("%s_api_token = \"%s\"", identifier, secret)
|
||||
}
|
||||
Reference in new issue
Block a user