sarif.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. package report
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "github.com/zricethezav/gitleaks/v8/config"
  7. )
  8. func writeSarif(cfg config.Config, findings []Finding, w io.WriteCloser) error {
  9. sarif := Sarif{
  10. Schema: "https://json.schemastore.org/sarif-2.1.0.json",
  11. Version: "2.1.0",
  12. Runs: getRuns(cfg, findings),
  13. }
  14. encoder := json.NewEncoder(w)
  15. encoder.SetIndent("", " ")
  16. return encoder.Encode(sarif)
  17. }
  18. func getRuns(cfg config.Config, findings []Finding) []Runs {
  19. return []Runs{
  20. {
  21. Tool: getTool(cfg),
  22. Results: getResults(findings),
  23. },
  24. }
  25. }
  26. func getTool(cfg config.Config) Tool {
  27. tool := Tool{
  28. Driver: Driver{
  29. Name: driver,
  30. SemanticVersion: version,
  31. InformationUri: "https://github.com/gitleaks/gitleaks",
  32. Rules: getRules(cfg),
  33. },
  34. }
  35. // if this tool has no rules, ensure that it is represented as [] instead of null/nil
  36. if hasEmptyRules(tool) {
  37. tool.Driver.Rules = make([]Rules, 0)
  38. }
  39. return tool
  40. }
  41. func hasEmptyRules(tool Tool) bool {
  42. return len(tool.Driver.Rules) == 0
  43. }
  44. func getRules(cfg config.Config) []Rules {
  45. // TODO for _, rule := range cfg.Rules {
  46. var rules []Rules
  47. for _, rule := range cfg.OrderedRules() {
  48. shortDescription := ShortDescription{
  49. Text: rule.Description,
  50. }
  51. if rule.Regex != nil {
  52. shortDescription = ShortDescription{
  53. Text: rule.Regex.String(),
  54. }
  55. } else if rule.Path != nil {
  56. shortDescription = ShortDescription{
  57. Text: rule.Path.String(),
  58. }
  59. }
  60. rules = append(rules, Rules{
  61. ID: rule.RuleID,
  62. Name: rule.Description,
  63. Description: shortDescription,
  64. })
  65. }
  66. return rules
  67. }
  68. func messageText(f Finding) string {
  69. if f.Commit == "" {
  70. return fmt.Sprintf("%s has detected secret for file %s.", f.RuleID, f.File)
  71. }
  72. return fmt.Sprintf("%s has detected secret for file %s at commit %s.", f.RuleID, f.File, f.Commit)
  73. }
  74. func getResults(findings []Finding) []Results {
  75. results := []Results{}
  76. for _, f := range findings {
  77. r := Results{
  78. Message: Message{
  79. Text: messageText(f),
  80. },
  81. RuleId: f.RuleID,
  82. Locations: getLocation(f),
  83. // This information goes in partial fingerprings until revision
  84. // data can be added somewhere else
  85. PartialFingerPrints: PartialFingerPrints{
  86. CommitSha: f.Commit,
  87. Email: f.Email,
  88. CommitMessage: f.Message,
  89. Date: f.Date,
  90. Author: f.Author,
  91. },
  92. }
  93. results = append(results, r)
  94. }
  95. return results
  96. }
  97. func getLocation(f Finding) []Locations {
  98. uri := f.File
  99. if f.SymlinkFile != "" {
  100. uri = f.SymlinkFile
  101. }
  102. return []Locations{
  103. {
  104. PhysicalLocation: PhysicalLocation{
  105. ArtifactLocation: ArtifactLocation{
  106. URI: uri,
  107. },
  108. Region: Region{
  109. StartLine: f.StartLine,
  110. EndLine: f.EndLine,
  111. StartColumn: f.StartColumn,
  112. EndColumn: f.EndColumn,
  113. Snippet: Snippet{
  114. Text: f.Secret,
  115. },
  116. },
  117. },
  118. },
  119. }
  120. }
  121. type PartialFingerPrints struct {
  122. CommitSha string `json:"commitSha"`
  123. Email string `json:"email"`
  124. Author string `json:"author"`
  125. Date string `json:"date"`
  126. CommitMessage string `json:"commitMessage"`
  127. }
  128. type Sarif struct {
  129. Schema string `json:"$schema"`
  130. Version string `json:"version"`
  131. Runs []Runs `json:"runs"`
  132. }
  133. type ShortDescription struct {
  134. Text string `json:"text"`
  135. }
  136. type FullDescription struct {
  137. Text string `json:"text"`
  138. }
  139. type Rules struct {
  140. ID string `json:"id"`
  141. Name string `json:"name"`
  142. Description ShortDescription `json:"shortDescription"`
  143. }
  144. type Driver struct {
  145. Name string `json:"name"`
  146. SemanticVersion string `json:"semanticVersion"`
  147. InformationUri string `json:"informationUri"`
  148. Rules []Rules `json:"rules"`
  149. }
  150. type Tool struct {
  151. Driver Driver `json:"driver"`
  152. }
  153. type Message struct {
  154. Text string `json:"text"`
  155. }
  156. type ArtifactLocation struct {
  157. URI string `json:"uri"`
  158. }
  159. type Region struct {
  160. StartLine int `json:"startLine"`
  161. StartColumn int `json:"startColumn"`
  162. EndLine int `json:"endLine"`
  163. EndColumn int `json:"endColumn"`
  164. Snippet Snippet `json:"snippet"`
  165. }
  166. type Snippet struct {
  167. Text string `json:"text"`
  168. }
  169. type PhysicalLocation struct {
  170. ArtifactLocation ArtifactLocation `json:"artifactLocation"`
  171. Region Region `json:"region"`
  172. }
  173. type Locations struct {
  174. PhysicalLocation PhysicalLocation `json:"physicalLocation"`
  175. }
  176. type Results struct {
  177. Message Message `json:"message"`
  178. RuleId string `json:"ruleId"`
  179. Locations []Locations `json:"locations"`
  180. PartialFingerPrints `json:"partialFingerprints"`
  181. }
  182. type Runs struct {
  183. Tool Tool `json:"tool"`
  184. Results []Results `json:"results"`
  185. }