Generate comprehensive secret samples (#1484)

* feat(config): comprehensive samples

* refactor(config): use new generate func

* chore(config): comment unsupported cases

* feat(config): enable '?=' case

* feat(config): enable '+=' case

* feat(config): enable escaped json case

* feat(config): enable '<' (xml) case

* feat(config): enable multiline yaml case

* chore(config): add comment case

* feat(config): add php cases

* feat(config): add java case

* chore(config): address review comments

* fix(config): prevent matching '=='

* feat(config): add URL cases

* chore(config): revert regex changes
To be tackled in separate PRs.
This commit is contained in:
Richard Gomez authored and GitHub committed 2024-10-30 09:34:21 -05:00
1 parent d1d9054ef0
commit c11adc91ad
110 files changed
+436 -621

No files matched your search

+74
View File
@@ -21,6 +21,7 @@ const (
identifierSuffix = `)(?:[ \t\w.-]{0,20})(?:[\s|']|[\s|"]){0,3}`
// commonly used assignment operators or function call
//language=regexp
operator = `(?:=|>|:{1,3}=|\|\|:|<=|=>|:|\?=)`
// boundaries for the secret
@@ -69,3 +70,76 @@ func GenerateUniqueTokenRegex(secretRegex string, isCaseInsensitive bool) *regex
func GenerateSampleSecret(identifier string, secret string) string {
return fmt.Sprintf("%s_api_token = \"%s\"", identifier, secret)
}
// See: https://github.com/gitleaks/gitleaks/issues/1222
func GenerateSampleSecrets(identifier string, secret string) []string {
samples := map[string]string{
// Configuration
// INI
"ini - quoted1": "{i}Token=\"{s}\"",
"ini - quoted2": "{i}Token = \"{s}\"",
"ini - unquoted1": "{i}Token={s}",
"ini - unquoted2": "{i}Token = {s}",
// JSON
"json - string": "{\n \"{i}_token\": \"{s}\"\n}",
//TODO: "json - escaped string": "\\{\n \\\"{i}_token\\\": \\\"{s}\\\"\n\\}",
//TODO: "json - string key/value": "{\n \"name\": \"{i}_token\",\n \"value\": \"{s}\"\n}",
// XML
//TODO: "xml - element": "<{i}Token>{s}</{i}Token>",
"xml - element multiline": "<{i}Token>\n {s}\n</{i}Token>",
//TODO: "xml - attribute": "<entry name=\"{i}Token\" value=\"{s}\" />",
//TODO: "xml - key/value elements": "<entry>\n <name=\"{i}Token\" />\n <value=\"{s}\" />\n</entry>",
// YAML
"yaml - singleline - unquoted": "{i}_token: {s}",
"yaml - singleline - single quote": "{i}_token: '{s}'",
"yaml - singleline - double quote": "{i}_token: \"{s}\"",
//TODO: "yaml - multiline - literal": "{i}_token: |\n {s}",
//TODO: "yaml - multiline - folding": "{i}_token: >\n {s}",
//"": "",
// Programming Languages
"C#": `string {i}Token = "{s}";`,
"go - normal": `var {i}Token string = "{s}"`,
"go - short": `{i}Token := "{s}"`,
"go - backticks": "{i}Token := `{s}`",
"java": "String {i}Token = \"{s}\";",
//TODO: "java - escaped quotes": `config.put("sasl.jaas.config", "org.apache.kafka.common.security.plain.PlainLoginModule required username=\"JDOE35\" {i}Token=\"{s}\""`,
//TODO:"kotlin - type": "var {i}Token: string = \"{s}\"",
"kotlin - notype": "var {i}Token = \"{s}\"",
"php - string concat": `${i}Token .= "{s}"`,
//TODO: "php - null coalesce": `${i}Token ??= "{s}"`,
"python - single quote": "{i}Token = '{s}'",
"python - double quote": `{i}Token = "{s}"`,
//"": "",
// Miscellaneous
//TODO: "url - basic auth": `https://{i}:{s}@example.com/`,
//TODO: "url - query parameter": "https://example.com?{i}Token={s}&fooBar=baz",
//TODO: "comment - slash": "//{s} is the password",
//TODO: "comment - slash multiline": "/*{s} is the password",
//TODO: "comment - hashtag": "#{s} is the password",
//TODO: "comment - semicolon": ";{s} is the password",
//TODO: "csv - unquoted": `{i}Token,{s},`,
"logstash": " \"{i}Token\" => \"{s}\"",
//TODO: "sql - tabular": "|{s}|",
//TODO: "sql": "",
// Makefile
// See: https://github.com/gitleaks/gitleaks/pull/1191
"make - recursive assignment": "{i}_TOKEN = \"{s}\"",
"make - simple assignment": "{i}_TOKEN := \"{s}\"",
"make - shell assignment": "{i}_TOKEN ::= \"{s}\"",
"make - evaluated shell assignment": "{i}_TOKEN :::= \"{s}\"",
"make - conditional assignment": "{i}_TOKEN ?= \"{s}\"",
//TODO: "make - append": "{i}_TOKEN += \"{s}\"",
//"": "",
}
replacer := strings.NewReplacer("{i}", identifier, "{s}", secret)
cases := make([]string, 0, len(samples))
for _, v := range samples {
cases = append(cases, replacer.Replace(v))
}
return cases
}
+1 -1
View File
@@ -16,7 +16,7 @@ func Validate(rule config.Rule, truePositives []string, falsePositives []string)
r := &rule
d := createSingleRuleDetector(r)
for _, tp := range truePositives {
if len(d.DetectString(tp)) != 1 {
if len(d.DetectString(tp)) < 1 {
log.Fatal().
Str("rule", r.RuleID).
Str("value", tp).