sarif.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package scan
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/zricethezav/gitleaks/v7/config"
  6. )
  7. //Sarif ...
  8. type Sarif struct {
  9. Schema string `json:"$schema"`
  10. Version string `json:"version"`
  11. Runs []Runs `json:"runs"`
  12. }
  13. //ShortDescription ...
  14. type ShortDescription struct {
  15. Text string `json:"text"`
  16. }
  17. //FullDescription ...
  18. type FullDescription struct {
  19. Text string `json:"text"`
  20. }
  21. //Rules ...
  22. type Rules struct {
  23. ID string `json:"id"`
  24. Name string `json:"name"`
  25. }
  26. //Driver ...
  27. type Driver struct {
  28. Name string `json:"name"`
  29. SemanticVersion string `json:"semanticVersion"`
  30. Rules []Rules `json:"rules"`
  31. }
  32. //Tool ...
  33. type Tool struct {
  34. Driver Driver `json:"driver"`
  35. }
  36. //Message ...
  37. type Message struct {
  38. Text string `json:"text"`
  39. }
  40. //ArtifactLocation ...
  41. type ArtifactLocation struct {
  42. URI string `json:"uri"`
  43. }
  44. //Region ...
  45. type Region struct {
  46. StartLine int `json:"startLine"`
  47. Snippet Snippet `json:"snippet"`
  48. }
  49. //Snippet ...
  50. type Snippet struct {
  51. Text string `json:"text"`
  52. }
  53. //PhysicalLocation ...
  54. type PhysicalLocation struct {
  55. ArtifactLocation ArtifactLocation `json:"artifactLocation"`
  56. Region Region `json:"region"`
  57. }
  58. //Locations ...
  59. type Locations struct {
  60. PhysicalLocation PhysicalLocation `json:"physicalLocation"`
  61. }
  62. //Results ...
  63. type Results struct {
  64. Message Message `json:"message"`
  65. RuleId string `json:"ruleId"`
  66. Properties ResultProperties `json:"properties"`
  67. Locations []Locations `json:"locations"`
  68. }
  69. //ResultProperties ...
  70. type ResultProperties struct {
  71. Commit string `json:"commit"`
  72. Offender string `json:"offender"`
  73. Date time.Time `json:"date"`
  74. Author string `json:"author"`
  75. Email string `json:"email"`
  76. CommitMessage string `json:"commitMessage"`
  77. Repo string `json:"repo"`
  78. }
  79. //Runs ...
  80. type Runs struct {
  81. Tool Tool `json:"tool"`
  82. Results []Results `json:"results"`
  83. }
  84. func configToRules(cfg config.Config) []Rules {
  85. var rules []Rules
  86. for _, rule := range cfg.Rules {
  87. rules = append(rules, Rules{
  88. ID: rule.Description,
  89. Name: rule.Description,
  90. })
  91. }
  92. return rules
  93. }
  94. func leaksToResults(leaks []Leak) []Results {
  95. results := make([]Results, 0)
  96. for _, leak := range leaks {
  97. results = append(results, Results{
  98. Message: Message{
  99. Text: fmt.Sprintf("%s secret detected", leak.Rule),
  100. },
  101. RuleId: leak.Rule,
  102. Properties: ResultProperties{
  103. Commit: leak.Commit,
  104. Offender: leak.Offender,
  105. Date: leak.Date,
  106. Author: leak.Author,
  107. Email: leak.Email,
  108. CommitMessage: leak.Message,
  109. Repo: leak.Repo,
  110. },
  111. Locations: leakToLocation(leak),
  112. })
  113. }
  114. return results
  115. }
  116. func leakToLocation(leak Leak) []Locations {
  117. uri := leak.File
  118. if leak.LeakURL != "" {
  119. uri = leak.LeakURL
  120. }
  121. return []Locations{
  122. {
  123. PhysicalLocation: PhysicalLocation{
  124. ArtifactLocation: ArtifactLocation{
  125. URI: uri,
  126. },
  127. Region: Region{
  128. StartLine: leak.LineNumber,
  129. Snippet: Snippet{
  130. Text: leak.Line,
  131. },
  132. },
  133. },
  134. },
  135. }
  136. }