Add details to message for default location in SARIF (#1414)

* add details to message

* fix
This commit is contained in:
laurentsimon 2021-12-23 11:06:02 -08:00 committed by GitHub
parent eef99b5ce0
commit cf71c9539c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 5 deletions

View File

@ -36,7 +36,8 @@ func BinaryArtifacts(name string, dl checker.DetailLogger,
for _, f := range r.Files { for _, f := range r.Files {
dl.Warn3(&checker.LogMessage{ dl.Warn3(&checker.LogMessage{
Path: f.Path, Type: checker.FileTypeBinary, Path: f.Path, Type: checker.FileTypeBinary,
Text: "binary detected", Offset: f.Offset,
Text: "binary detected",
}) })
// We remove one point for each binary. // We remove one point for each binary.
score-- score--

View File

@ -116,7 +116,7 @@ func SecurityPolicy(c *checker.CheckRequest) (checker.SecurityPolicyData, error)
} }
// Return raw results. // Return raw results.
return checker.SecurityPolicyData{Files: files}, err return checker.SecurityPolicyData{Files: files}, nil
} }
func isSecurityRstFound(name string) bool { func isSecurityRstFound(name string) bool {

View File

@ -501,6 +501,28 @@ func createCheckIdentifiers(name string) (string, string) {
return name, fmt.Sprintf("%sID", n) return name, fmt.Sprintf("%sID", n)
} }
func filterOutDetailType(details []checker.CheckDetail, t checker.DetailType) []checker.CheckDetail {
ret := make([]checker.CheckDetail, 0)
for i := range details {
d := details[i]
if d.Type == t {
continue
}
ret = append(ret, d)
}
return ret
}
func createDefaultLocationMessage(check *checker.CheckResult) string {
details := filterOutDetailType(check.Details2, checker.DetailInfo)
s, b := detailsToString(details, zapcore.WarnLevel)
if b {
// Warning: GitHub UX needs a single `\n` to turn it into a `<br>`.
return fmt.Sprintf("%s:\n%s", check.Reason, s)
}
return check.Reason
}
// AsSARIF outputs ScorecardResult in SARIF 2.1.0 format. // AsSARIF outputs ScorecardResult in SARIF 2.1.0 format.
func (r *ScorecardResult) AsSARIF(showDetails bool, logLevel zapcore.Level, func (r *ScorecardResult) AsSARIF(showDetails bool, logLevel zapcore.Level,
writer io.Writer, checkDocs docs.Doc, policy *spol.ScorecardPolicy) error { writer io.Writer, checkDocs docs.Doc, policy *spol.ScorecardPolicy) error {
@ -576,8 +598,8 @@ func (r *ScorecardResult) AsSARIF(showDetails bool, logLevel zapcore.Level,
RuleIndex := len(run.Tool.Driver.Rules) - 1 RuleIndex := len(run.Tool.Driver.Rules) - 1
if len(locs) == 0 { if len(locs) == 0 {
locs = addDefaultLocation(locs, "no file available") locs = addDefaultLocation(locs, "no file available")
// Use the `reason` as message. msg := createDefaultLocationMessage(&check)
cr := createSARIFCheckResult(RuleIndex, sarifCheckID, check.Reason, &locs[0]) cr := createSARIFCheckResult(RuleIndex, sarifCheckID, msg, &locs[0])
run.Results = append(run.Results, cr) run.Results = append(run.Results, cr)
} else { } else {
for _, loc := range locs { for _, loc := range locs {

View File

@ -47,7 +47,7 @@
"ruleId": "CheckNameID", "ruleId": "CheckNameID",
"ruleIndex": 0, "ruleIndex": 0,
"message": { "message": {
"text": "six score reason" "text": "six score reason:\nWarn: warn message"
}, },
"locations": [ "locations": [
{ {