sarif.go 4.5 KB

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