generate.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // == WARNING ==
  2. // These functions are used to generate GitLeak's default config.
  3. // You are free to use these in your own project, HOWEVER, no API stability is guaranteed.
  4. package utils
  5. import (
  6. "fmt"
  7. "strings"
  8. regexp "github.com/wasilibs/go-re2"
  9. )
  10. const (
  11. // case insensitive prefix
  12. caseInsensitive = `(?i)`
  13. // identifier prefix (just an ignore group)
  14. identifierCaseInsensitivePrefix = `[\w.-]{0,50}?(?i:`
  15. identifierCaseInsensitiveSuffix = `)`
  16. identifierPrefix = `[\w.-]{0,50}?(?:`
  17. identifierSuffix = `)(?:[ \t\w.-]{0,20})(?:[\s|']|[\s|"]){0,3}`
  18. // commonly used assignment operators or function call
  19. //language=regexp
  20. operator = `(?:=|>|:{1,3}=|\|\|:|<=|=>|:|\?=)`
  21. // boundaries for the secret
  22. // \x60 = `
  23. secretPrefixUnique = `\b(`
  24. secretPrefix = `(?:'|\"|\s|=|\x60){0,5}(`
  25. secretSuffix = `)(?:['|\"|\n|\r|\s|\x60|;]|$)`
  26. )
  27. func GenerateSemiGenericRegex(identifiers []string, secretRegex string, isCaseInsensitive bool) *regexp.Regexp {
  28. var sb strings.Builder
  29. // The identifiers should always be case-insensitive.
  30. // This is inelegant but prevents an extraneous `(?i:)` from being added to the pattern; it could be removed.
  31. if isCaseInsensitive {
  32. sb.WriteString(caseInsensitive)
  33. writeIdentifiers(&sb, identifiers)
  34. } else {
  35. sb.WriteString(identifierCaseInsensitivePrefix)
  36. writeIdentifiers(&sb, identifiers)
  37. sb.WriteString(identifierCaseInsensitiveSuffix)
  38. }
  39. sb.WriteString(operator)
  40. sb.WriteString(secretPrefix)
  41. sb.WriteString(secretRegex)
  42. sb.WriteString(secretSuffix)
  43. return regexp.MustCompile(sb.String())
  44. }
  45. func MergeRegexps(regexps ...*regexp.Regexp) *regexp.Regexp {
  46. patterns := make([]string, len(regexps))
  47. for i, r := range regexps {
  48. patterns[i] = r.String()
  49. }
  50. return regexp.MustCompile(strings.Join(patterns, "|"))
  51. }
  52. func writeIdentifiers(sb *strings.Builder, identifiers []string) {
  53. sb.WriteString(identifierPrefix)
  54. sb.WriteString(strings.Join(identifiers, "|"))
  55. sb.WriteString(identifierSuffix)
  56. }
  57. func GenerateUniqueTokenRegex(secretRegex string, isCaseInsensitive bool) *regexp.Regexp {
  58. var sb strings.Builder
  59. if isCaseInsensitive {
  60. sb.WriteString(caseInsensitive)
  61. }
  62. sb.WriteString(secretPrefixUnique)
  63. sb.WriteString(secretRegex)
  64. sb.WriteString(secretSuffix)
  65. return regexp.MustCompile(sb.String())
  66. }
  67. func GenerateSampleSecret(identifier string, secret string) string {
  68. return fmt.Sprintf("%s_api_token = \"%s\"", identifier, secret)
  69. }
  70. // See: https://github.com/gitleaks/gitleaks/issues/1222
  71. func GenerateSampleSecrets(identifier string, secret string) []string {
  72. samples := map[string]string{
  73. // Configuration
  74. // INI
  75. "ini - quoted1": "{i}Token=\"{s}\"",
  76. "ini - quoted2": "{i}Token = \"{s}\"",
  77. "ini - unquoted1": "{i}Token={s}",
  78. "ini - unquoted2": "{i}Token = {s}",
  79. // JSON
  80. "json - string": "{\n \"{i}_token\": \"{s}\"\n}",
  81. // TODO: "json - escaped string": "\\{\n \\\"{i}_token\\\": \\\"{s}\\\"\n\\}",
  82. // TODO: "json - string key/value": "{\n \"name\": \"{i}_token\",\n \"value\": \"{s}\"\n}",
  83. // XML
  84. // TODO: "xml - element": "<{i}Token>{s}</{i}Token>",
  85. "xml - element multiline": "<{i}Token>\n {s}\n</{i}Token>",
  86. // TODO: "xml - attribute": "<entry name=\"{i}Token\" value=\"{s}\" />",
  87. // TODO: "xml - key/value elements": "<entry>\n <name=\"{i}Token\" />\n <value=\"{s}\" />\n</entry>",
  88. // YAML
  89. "yaml - singleline - unquoted": "{i}_token: {s}",
  90. "yaml - singleline - single quote": "{i}_token: '{s}'",
  91. "yaml - singleline - double quote": "{i}_token: \"{s}\"",
  92. // TODO: "yaml - multiline - literal": "{i}_token: |\n {s}",
  93. // TODO: "yaml - multiline - folding": "{i}_token: >\n {s}",
  94. // "": "",
  95. // Programming Languages
  96. "C#": `string {i}Token = "{s}";`,
  97. "go - normal": `var {i}Token string = "{s}"`,
  98. "go - short": `{i}Token := "{s}"`,
  99. "go - backticks": "{i}Token := `{s}`",
  100. "java": "String {i}Token = \"{s}\";",
  101. // TODO: "java - escaped quotes": `config.put("sasl.jaas.config", "org.apache.kafka.common.security.plain.PlainLoginModule required username=\"JDOE35\" {i}Token=\"{s}\""`,
  102. // TODO:"kotlin - type": "var {i}Token: string = \"{s}\"",
  103. "kotlin - notype": "var {i}Token = \"{s}\"",
  104. "php - string concat": `${i}Token .= "{s}"`,
  105. // TODO: "php - null coalesce": `${i}Token ??= "{s}"`,
  106. "python - single quote": "{i}Token = '{s}'",
  107. "python - double quote": `{i}Token = "{s}"`,
  108. // "": "",
  109. // Miscellaneous
  110. // TODO: "url - basic auth": `https://{i}:{s}@example.com/`,
  111. // TODO: "url - query parameter": "https://example.com?{i}Token={s}&fooBar=baz",
  112. // TODO: "comment - slash": "//{s} is the password",
  113. // TODO: "comment - slash multiline": "/*{s} is the password",
  114. // TODO: "comment - hashtag": "#{s} is the password",
  115. // TODO: "comment - semicolon": ";{s} is the password",
  116. // TODO: "csv - unquoted": `{i}Token,{s},`,
  117. "logstash": " \"{i}Token\" => \"{s}\"",
  118. // TODO: "sql - tabular": "|{s}|",
  119. // TODO: "sql": "",
  120. // Makefile
  121. // See: https://github.com/gitleaks/gitleaks/pull/1191
  122. "make - recursive assignment": "{i}_TOKEN = \"{s}\"",
  123. "make - simple assignment": "{i}_TOKEN := \"{s}\"",
  124. "make - shell assignment": "{i}_TOKEN ::= \"{s}\"",
  125. "make - evaluated shell assignment": "{i}_TOKEN :::= \"{s}\"",
  126. "make - conditional assignment": "{i}_TOKEN ?= \"{s}\"",
  127. // TODO: "make - append": "{i}_TOKEN += \"{s}\"",
  128. // "": "",
  129. }
  130. replacer := strings.NewReplacer("{i}", identifier, "{s}", secret)
  131. cases := make([]string, 0, len(samples))
  132. for _, v := range samples {
  133. cases = append(cases, replacer.Replace(v))
  134. }
  135. return cases
  136. }