sarif.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. Properties ResultProperties `json:"properties"`
  66. Locations []Locations `json:"locations"`
  67. }
  68. //ResultProperties ...
  69. type ResultProperties struct {
  70. Commit string `json:"commit"`
  71. Offender string `json:"offender"`
  72. Date time.Time `json:"date"`
  73. Author string `json:"author"`
  74. Email string `json:"email"`
  75. CommitMessage string `json:"commitMessage"`
  76. Repo string `json:"repo"`
  77. }
  78. //Runs ...
  79. type Runs struct {
  80. Tool Tool `json:"tool"`
  81. Results []Results `json:"results"`
  82. }
  83. func configToRules(cfg config.Config) []Rules {
  84. var rules []Rules
  85. for _, rule := range cfg.Rules {
  86. rules = append(rules, Rules{
  87. ID: rule.Description,
  88. Name: rule.Description,
  89. })
  90. }
  91. return rules
  92. }
  93. func leaksToResults(leaks []Leak) []Results {
  94. results := make([]Results, 0)
  95. for _, leak := range leaks {
  96. results = append(results, Results{
  97. Message: Message{
  98. Text: fmt.Sprintf("%s secret detected", leak.Rule),
  99. },
  100. Properties: ResultProperties{
  101. Commit: leak.Commit,
  102. Offender: leak.Offender,
  103. Date: leak.Date,
  104. Author: leak.Author,
  105. Email: leak.Email,
  106. CommitMessage: leak.Message,
  107. Repo: leak.Repo,
  108. },
  109. Locations: leakToLocation(leak),
  110. })
  111. }
  112. return results
  113. }
  114. func leakToLocation(leak Leak) []Locations {
  115. uri := leak.File
  116. if leak.LeakURL != "" {
  117. uri = leak.LeakURL
  118. }
  119. return []Locations{
  120. {
  121. PhysicalLocation: PhysicalLocation{
  122. ArtifactLocation: ArtifactLocation{
  123. URI: uri,
  124. },
  125. Region: Region{
  126. StartLine: leak.LineNumber,
  127. Snippet: Snippet{
  128. Text: leak.Line,
  129. },
  130. },
  131. },
  132. },
  133. }
  134. }