|
| 1 | +package format |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "regexp" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/xmirrorsecurity/opensca-cli/v3/cmd/detail" |
| 11 | +) |
| 12 | + |
| 13 | +type sarifReport struct { |
| 14 | + Version string `json:"version"` |
| 15 | + Schema string `json:"$schema"` |
| 16 | + Runs []sarifRun `json:"runs"` |
| 17 | +} |
| 18 | + |
| 19 | +type sarifRun struct { |
| 20 | + Tool struct { |
| 21 | + Driver struct { |
| 22 | + Name string `json:"name"` |
| 23 | + Version string `json:"version"` |
| 24 | + InformationUri string `json:"informationUri"` |
| 25 | + Rules []sarifRule `json:"rules"` |
| 26 | + } `json:"driver"` |
| 27 | + } `json:"tool"` |
| 28 | + Results []sarifResult `json:"results"` |
| 29 | +} |
| 30 | + |
| 31 | +type sarifRule struct { |
| 32 | + Id string `json:"id"` |
| 33 | + Name string `json:"name"` |
| 34 | + ShortDescription sarifRuleShortDescription `json:"shortDescription"` |
| 35 | + FullDescription sarifRuleFullDescription `json:"fullDescription"` |
| 36 | + Help sarifRuleHelp `json:"help"` |
| 37 | + Properties sarifRuleProperties `json:"properties"` |
| 38 | +} |
| 39 | + |
| 40 | +type sarifRuleShortDescription struct { |
| 41 | + Text string `json:"text"` |
| 42 | +} |
| 43 | + |
| 44 | +type sarifRuleFullDescription struct { |
| 45 | + Text string `json:"text"` |
| 46 | +} |
| 47 | + |
| 48 | +type sarifRuleHelp struct { |
| 49 | + Text string `json:"text"` |
| 50 | + Markdown string `json:"markdown"` |
| 51 | +} |
| 52 | + |
| 53 | +type sarifRuleProperties struct { |
| 54 | + Tags []string `json:"tags"` |
| 55 | +} |
| 56 | + |
| 57 | +type sarifResult struct { |
| 58 | + RuleId string `json:"ruleId"` |
| 59 | + Level string `json:"level"` |
| 60 | + Message struct { |
| 61 | + Text string `json:"text"` |
| 62 | + } `json:"message"` |
| 63 | + Locations []sarifLocation `json:"locations"` |
| 64 | +} |
| 65 | + |
| 66 | +type sarifLocation struct { |
| 67 | + PhysicalLocation struct { |
| 68 | + ArtifactLocation struct { |
| 69 | + Uri string `json:"uri"` |
| 70 | + Index int `json:"index,omitempty"` |
| 71 | + } `json:"artifactLocation"` |
| 72 | + Region struct { |
| 73 | + StartColumn int `json:"startColumn"` |
| 74 | + EndColumn int `json:"endColumn"` |
| 75 | + StartLine int `json:"startLine"` |
| 76 | + EndLine int `json:"endLine"` |
| 77 | + } `json:"region"` |
| 78 | + } `json:"physicalLocation"` |
| 79 | +} |
| 80 | + |
| 81 | +func Sarif(report Report, out string) { |
| 82 | + |
| 83 | + s := sarifReport{ |
| 84 | + Version: "2.1.0", |
| 85 | + Schema: "https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0.json", |
| 86 | + } |
| 87 | + |
| 88 | + run := sarifRun{} |
| 89 | + run.Tool.Driver.Name = "opensca-cli" |
| 90 | + run.Tool.Driver.Version = strings.TrimLeft(report.TaskInfo.ToolVersion, "vV") |
| 91 | + run.Tool.Driver.InformationUri = "https://opensca.xmirror.cn" |
| 92 | + |
| 93 | + vulnInfos := map[string]*detail.VulnInfo{} |
| 94 | + |
| 95 | + report.ForEach(func(n *detail.DepDetailGraph) bool { |
| 96 | + for _, vuln := range n.Vulnerabilities { |
| 97 | + |
| 98 | + if vuln.Id == "" { |
| 99 | + continue |
| 100 | + } |
| 101 | + |
| 102 | + vulnInfos[vuln.Id] = &detail.VulnInfo{Vuln: vuln, Language: n.Language} |
| 103 | + |
| 104 | + result := sarifResult{ |
| 105 | + RuleId: vuln.Id, |
| 106 | + Level: "warning", |
| 107 | + } |
| 108 | + result.Message.Text = fmt.Sprintf("引入的组件 %s 中存在 %s", n.Dep.Key()[:strings.LastIndex(n.Dep.Key(), ":")], vuln.Name) |
| 109 | + for i, path := range n.Paths { |
| 110 | + if truncIndex := strings.Index(path, "["); truncIndex > 0 { |
| 111 | + path = strings.Trim(path[:truncIndex], `\/`) |
| 112 | + } |
| 113 | + location := sarifLocation{} |
| 114 | + location.PhysicalLocation.ArtifactLocation.Uri = path |
| 115 | + location.PhysicalLocation.ArtifactLocation.Index = i |
| 116 | + location.PhysicalLocation.Region.StartColumn = 1 |
| 117 | + location.PhysicalLocation.Region.EndColumn = 1 |
| 118 | + location.PhysicalLocation.Region.StartLine = 1 |
| 119 | + location.PhysicalLocation.Region.EndLine = 1 |
| 120 | + result.Locations = append(result.Locations, location) |
| 121 | + } |
| 122 | + |
| 123 | + run.Results = append(run.Results, result) |
| 124 | + } |
| 125 | + return true |
| 126 | + }) |
| 127 | + |
| 128 | + for _, vuln := range vulnInfos { |
| 129 | + run.Tool.Driver.Rules = append(run.Tool.Driver.Rules, sarifRule{ |
| 130 | + Id: vuln.Id, |
| 131 | + Name: vuln.Name, |
| 132 | + ShortDescription: sarifRuleShortDescription{Text: vuln.Name}, |
| 133 | + FullDescription: sarifRuleFullDescription{Text: vuln.Description}, |
| 134 | + Help: sarifRuleHelp{Markdown: formatDesc(vuln)}, |
| 135 | + Properties: sarifRuleProperties{Tags: formatTags(vuln)}, |
| 136 | + }) |
| 137 | + } |
| 138 | + |
| 139 | + s.Runs = []sarifRun{run} |
| 140 | + outWrite(out, func(w io.Writer) error { |
| 141 | + return json.NewEncoder(w).Encode(s) |
| 142 | + }) |
| 143 | +} |
| 144 | + |
| 145 | +func formatDesc(v *detail.VulnInfo) string { |
| 146 | + table := []struct { |
| 147 | + fmt string |
| 148 | + val string |
| 149 | + }{ |
| 150 | + {"| id | %s |", v.Id}, |
| 151 | + {"| --- | --- |", ""}, |
| 152 | + {"| cve | %s |", v.Cve}, |
| 153 | + {"| cnnvd | %s |", v.Cnnvd}, |
| 154 | + {"| cnvd | %s |", v.Cnvd}, |
| 155 | + {"| cwe | %s |", v.Cwe}, |
| 156 | + {"| level | %s |", v.SecurityLevel()}, |
| 157 | + {"| desc | %s |", sanitizeString(v.Description)}, |
| 158 | + {"| suggestion | %s |", sanitizeString(v.Suggestion)}, |
| 159 | + } |
| 160 | + var lines []string |
| 161 | + for _, line := range table { |
| 162 | + if strings.Contains(line.fmt, "%s") && line.val == "" { |
| 163 | + continue |
| 164 | + } |
| 165 | + if line.val == "" { |
| 166 | + lines = append(lines, line.fmt) |
| 167 | + } else { |
| 168 | + lines = append(lines, fmt.Sprintf(line.fmt, line.val)) |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + return strings.Join(lines, "\n") |
| 173 | +} |
| 174 | + |
| 175 | +func sanitizeString(s string) string { |
| 176 | + re := regexp.MustCompile("<[^>]*>") |
| 177 | + s = re.ReplaceAllString(s, "") |
| 178 | + |
| 179 | + s = strings.ReplaceAll(s, "\r", "") |
| 180 | + s = strings.ReplaceAll(s, "\n", "") |
| 181 | + |
| 182 | + return s |
| 183 | +} |
| 184 | + |
| 185 | +func formatTags(v *detail.VulnInfo) []string { |
| 186 | + tags := []string{"security", "Use-Vulnerable-and-Outdated-Components", v.Cve, v.Cwe, v.AttackType, v.Language} |
| 187 | + for i := 0; i < len(tags); { |
| 188 | + if tags[i] == "" { |
| 189 | + tags = append(tags[:i], tags[i+1:]...) |
| 190 | + } else { |
| 191 | + i++ |
| 192 | + } |
| 193 | + } |
| 194 | + return tags |
| 195 | +} |
0 commit comments