config.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package config
  2. import (
  3. "fmt"
  4. "path"
  5. "regexp"
  6. "strconv"
  7. "github.com/zricethezav/gitleaks/v4/options"
  8. "github.com/BurntSushi/toml"
  9. )
  10. // Whitelist is struct containing items that if encountered will whitelist
  11. // a commit/line of code that would be considered a leak.
  12. type Whitelist struct {
  13. Description string
  14. Regex *regexp.Regexp
  15. File *regexp.Regexp
  16. Path *regexp.Regexp
  17. }
  18. // Entropy represents an entropy range
  19. type Entropy struct {
  20. Min float64
  21. Max float64
  22. Group int
  23. }
  24. // Rule is a struct that contains information that is loaded from a gitleaks config.
  25. // This struct is used in the Config struct as an array of Rules and is iterated
  26. // over during an audit. Each rule will be checked. If a regex match is found AND
  27. // that match is not whitelisted (globally or locally), then a leak will be appended
  28. // to the final audit report.
  29. type Rule struct {
  30. Description string
  31. Regex *regexp.Regexp
  32. FileNameRegex *regexp.Regexp
  33. FilePathRegex *regexp.Regexp
  34. Tags []string
  35. Whitelist []Whitelist
  36. Entropies []Entropy
  37. }
  38. // Config is a composite struct of Rules and Whitelists
  39. // Each Rule contains a description, regular expression, tags, and whitelists if available
  40. type Config struct {
  41. Rules []Rule
  42. Whitelist struct {
  43. Description string
  44. Commits []string
  45. Files []*regexp.Regexp
  46. Paths []*regexp.Regexp
  47. }
  48. }
  49. // TomlLoader gets loaded with the values from a gitleaks toml config
  50. // see the config in config/defaults.go for an example. TomlLoader is used
  51. // to generate Config values (compiling regexes, etc).
  52. type TomlLoader struct {
  53. Whitelist struct {
  54. Description string
  55. Commits []string
  56. Files []string
  57. Paths []string
  58. }
  59. Rules []struct {
  60. Description string
  61. Regex string
  62. FileNameRegex string
  63. FilePathRegex string
  64. Tags []string
  65. Entropies []struct {
  66. Min string
  67. Max string
  68. Group string
  69. }
  70. Whitelist []struct {
  71. Description string
  72. Regex string
  73. File string
  74. Path string
  75. }
  76. }
  77. }
  78. // NewConfig will create a new config struct which contains
  79. // rules on how gitleaks will proceed with its audit.
  80. // If no options are passed via cli then NewConfig will return
  81. // a default config which can be seen in config.go
  82. func NewConfig(options options.Options) (Config, error) {
  83. var cfg Config
  84. tomlLoader := TomlLoader{}
  85. var err error
  86. if options.Config != "" {
  87. _, err = toml.DecodeFile(options.Config, &tomlLoader)
  88. // append a whitelist rule for whitelisting the config
  89. tomlLoader.Whitelist.Files = append(tomlLoader.Whitelist.Files, path.Base(options.Config))
  90. } else {
  91. _, err = toml.Decode(DefaultConfig, &tomlLoader)
  92. }
  93. if err != nil {
  94. return cfg, err
  95. }
  96. cfg, err = tomlLoader.Parse()
  97. if err != nil {
  98. return cfg, err
  99. }
  100. return cfg, nil
  101. }
  102. // Parse will parse the values set in a TomlLoader and use those values
  103. // to create compiled regular expressions and rules used in audits
  104. func (tomlLoader TomlLoader) Parse() (Config, error) {
  105. var cfg Config
  106. for _, rule := range tomlLoader.Rules {
  107. re, err := regexp.Compile(rule.Regex)
  108. if err != nil {
  109. return cfg, fmt.Errorf("problem loading config: %v", err)
  110. }
  111. fileNameRe, err := regexp.Compile(rule.FileNameRegex)
  112. if err != nil {
  113. return cfg, fmt.Errorf("problem loading config: %v", err)
  114. }
  115. filePathRe, err := regexp.Compile(rule.FilePathRegex)
  116. if err != nil {
  117. return cfg, fmt.Errorf("problem loading config: %v", err)
  118. }
  119. // rule specific whitelists
  120. var whitelists []Whitelist
  121. for _, wl := range rule.Whitelist {
  122. wlRe, err := regexp.Compile(wl.Regex)
  123. if err != nil {
  124. return cfg, fmt.Errorf("problem loading config: %v", err)
  125. }
  126. wlFileNameRe, err := regexp.Compile(wl.File)
  127. if err != nil {
  128. return cfg, fmt.Errorf("problem loading config: %v", err)
  129. }
  130. wlFilePathRe, err := regexp.Compile(wl.Path)
  131. if err != nil {
  132. return cfg, fmt.Errorf("problem loading config: %v", err)
  133. }
  134. whitelists = append(whitelists, Whitelist{
  135. Description: wl.Description,
  136. File: wlFileNameRe,
  137. Path: wlFilePathRe,
  138. Regex: wlRe,
  139. })
  140. }
  141. var entropies []Entropy
  142. for _, e := range rule.Entropies {
  143. min, err := strconv.ParseFloat(e.Min, 64)
  144. if err != nil {
  145. return cfg, err
  146. }
  147. max, err := strconv.ParseFloat(e.Max, 64)
  148. if err != nil {
  149. return cfg, err
  150. }
  151. if e.Group == "" {
  152. e.Group = "0"
  153. }
  154. group, err := strconv.ParseInt(e.Group, 10, 64)
  155. if err != nil {
  156. return cfg, err
  157. } else if int(group) >= len(re.SubexpNames()) {
  158. return cfg, fmt.Errorf("problem loading config: group cannot be higher than number of groups in regexp")
  159. } else if group < 0 {
  160. return cfg, fmt.Errorf("problem loading config: group cannot be lower than 0")
  161. } else if min > 8.0 || min < 0.0 || max > 8.0 || max < 0.0 {
  162. return cfg, fmt.Errorf("problem loading config: invalid entropy ranges, must be within 0.0-8.0")
  163. } else if min > max {
  164. return cfg, fmt.Errorf("problem loading config: entropy Min value cannot be higher than Max value")
  165. }
  166. entropies = append(entropies, Entropy{Min: min, Max: max, Group: int(group)})
  167. }
  168. cfg.Rules = append(cfg.Rules, Rule{
  169. Description: rule.Description,
  170. Regex: re,
  171. FileNameRegex: fileNameRe,
  172. FilePathRegex: filePathRe,
  173. Tags: rule.Tags,
  174. Whitelist: whitelists,
  175. Entropies: entropies,
  176. })
  177. }
  178. // global file name whitelists
  179. for _, wlFileName := range tomlLoader.Whitelist.Files {
  180. re, err := regexp.Compile(wlFileName)
  181. if err != nil {
  182. return cfg, fmt.Errorf("problem loading config: %v", err)
  183. }
  184. cfg.Whitelist.Files = append(cfg.Whitelist.Files, re)
  185. }
  186. // global file path whitelists
  187. for _, wlFilePath := range tomlLoader.Whitelist.Paths {
  188. re, err := regexp.Compile(wlFilePath)
  189. if err != nil {
  190. return cfg, fmt.Errorf("problem loading config: %v", err)
  191. }
  192. cfg.Whitelist.Paths = append(cfg.Whitelist.Paths, re)
  193. }
  194. cfg.Whitelist.Commits = tomlLoader.Whitelist.Commits
  195. cfg.Whitelist.Description = tomlLoader.Whitelist.Description
  196. return cfg, nil
  197. }