Skip to content

Commit 2e4a959

Browse files
committed
add violation context and attribs to simple-json and sarif
1 parent f9060e5 commit 2e4a959

File tree

9 files changed

+212
-57
lines changed

9 files changed

+212
-57
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ require (
112112
)
113113

114114
// attiasas:add_repo_context_scan_graph
115-
replace github.com/jfrog/jfrog-client-go => github.com/attiasas/jfrog-client-go v0.0.0-20241202080241-23f62508099d
115+
replace github.com/jfrog/jfrog-client-go => github.com/attiasas/jfrog-client-go v0.0.0-20241202121042-ba0c6c74db7a
116116

117117
// replace github.com/jfrog/jfrog-cli-core/v2 => github.com/jfrog/jfrog-cli-core/v2 dev
118118

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuW
2222
github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkEghdlcqw7yxLeM89kiTRPUo=
2323
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
2424
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
25-
github.com/attiasas/jfrog-client-go v0.0.0-20241202080241-23f62508099d h1:fiImxqhkajkfANzZHJ4vE9cT99t8oRfhZlb15ndioGg=
26-
github.com/attiasas/jfrog-client-go v0.0.0-20241202080241-23f62508099d/go.mod h1:1a7bmQHkRmPEza9wva2+WVrYzrGbosrMymq57kyG5gU=
25+
github.com/attiasas/jfrog-client-go v0.0.0-20241202121042-ba0c6c74db7a h1:47orWZJqdB4YIiqnYd0ysEjvqXiwy3eadwKkHo6s1qg=
26+
github.com/attiasas/jfrog-client-go v0.0.0-20241202121042-ba0c6c74db7a/go.mod h1:1a7bmQHkRmPEza9wva2+WVrYzrGbosrMymq57kyG5gU=
2727
github.com/beevik/etree v1.4.0 h1:oz1UedHRepuY3p4N5OjE0nK1WLCqtzHf25bxplKOHLs=
2828
github.com/beevik/etree v1.4.0/go.mod h1:cyWiXwGoasx60gHvtnEh5x8+uIjUVnjWqBvEnhnqKDA=
2929
github.com/bradleyjkemp/cupaloy/v2 v2.8.0 h1:any4BmKE+jGIaMpnU8YgH/I2LPiLBufr6oMMlVBbn9M=

utils/formats/sarifutils/sarifutils.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,61 @@ import (
1010
"github.com/owenrumney/go-sarif/v2/sarif"
1111
)
1212

13+
const (
14+
WatchSarifPropertyKey = "watch"
15+
PoliciesSarifPropertyKey = "policies"
16+
JasIssueIdSarifPropertyKey = "issueId"
17+
CWEPropertyKey = "CWE"
18+
)
19+
20+
// Specific JFrog Sarif Utils
21+
22+
func GetResultWatches(result *sarif.Result) (watches string) {
23+
if watchesProperty, ok := result.Properties[WatchSarifPropertyKey]; ok {
24+
if watchesValue, ok := watchesProperty.(string); ok {
25+
return watchesValue
26+
}
27+
}
28+
return
29+
}
30+
31+
func GetResultPolicies(result *sarif.Result) (policies []string) {
32+
if policiesProperty, ok := result.Properties[PoliciesSarifPropertyKey]; ok {
33+
if policiesValue, ok := policiesProperty.(string); ok {
34+
split := strings.Split(policiesValue, ",")
35+
for _, policy := range split {
36+
policies = append(policies, strings.TrimSpace(policy))
37+
}
38+
return
39+
}
40+
}
41+
return
42+
}
43+
44+
func GetResultIssueId(result *sarif.Result) (issueId string) {
45+
if issueIdProperty, ok := result.Properties[JasIssueIdSarifPropertyKey]; ok {
46+
if issueIdValue, ok := issueIdProperty.(string); ok {
47+
return issueIdValue
48+
}
49+
}
50+
return
51+
}
52+
53+
func GetRuleCWE(rule *sarif.ReportingDescriptor) (cwe string) {
54+
if rule == nil || rule.DefaultConfiguration == nil || rule.DefaultConfiguration.Parameters == nil || rule.DefaultConfiguration.Parameters.Properties == nil {
55+
// No CWE property
56+
return
57+
}
58+
if cweProperty, ok := rule.DefaultConfiguration.Parameters.Properties[CWEPropertyKey]; ok {
59+
if cweValue, ok := cweProperty.(string); ok {
60+
return cweValue
61+
}
62+
}
63+
return
64+
}
65+
66+
// General Sarif Utils
67+
1368
func NewReport() (*sarif.Report, error) {
1469
report, err := sarif.New(sarif.Version210)
1570
if err != nil {

utils/formats/simplejsonapi.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ type SimpleJsonResults struct {
2626
MultiScanId string `json:"multiScanId,omitempty"`
2727
}
2828

29+
type ViolationContext struct {
30+
Watch string `json:"watch,omitempty"`
31+
Policies []string `json:"policies,omitempty"`
32+
}
33+
2934
type SeverityDetails struct {
3035
Severity string `json:"severity"`
3136
SeverityNumValue int `json:"-"` // For sorting
@@ -42,12 +47,12 @@ type ImpactedDependencyDetails struct {
4247
// Used for vulnerabilities and security violations
4348
type VulnerabilityOrViolationRow struct {
4449
ImpactedDependencyDetails
50+
ViolationContext
4551
Summary string `json:"summary"`
4652
Applicable string `json:"applicable"`
4753
FixedVersions []string `json:"fixedVersions"`
4854
Cves []CveRow `json:"cves"`
4955
IssueId string `json:"issueId"`
50-
Watch string `json:"watch,omitempty"`
5156
References []string `json:"references"`
5257
ImpactPaths [][]ComponentRow `json:"impactPaths"`
5358
JfrogResearchInformation *JfrogResearchInformation `json:"jfrogResearchInformation"`
@@ -56,7 +61,7 @@ type VulnerabilityOrViolationRow struct {
5661

5762
type LicenseViolationRow struct {
5863
LicenseRow
59-
Watch string `json:"watch,omitempty"`
64+
ViolationContext
6065
}
6166

6267
type LicenseRow struct {
@@ -68,6 +73,7 @@ type LicenseRow struct {
6873

6974
type OperationalRiskViolationRow struct {
7075
ImpactedDependencyDetails
76+
ViolationContext
7177
RiskReason string `json:"riskReason"`
7278
IsEol string `json:"isEndOfLife"`
7379
EolMessage string `json:"endOfLifeMessage"`
@@ -80,7 +86,11 @@ type OperationalRiskViolationRow struct {
8086

8187
type SourceCodeRow struct {
8288
SeverityDetails
89+
ViolationContext
8390
Location
91+
RuleId string `json:"ruleId"`
92+
IssueId string `json:"issueId"`
93+
CWE string `json:"cwe,omitempty"`
8494
Finding string `json:"finding,omitempty"`
8595
Fingerprint string `json:"fingerprint,omitempty"`
8696
Applicability *Applicability `json:"applicability,omitempty"`

utils/results/common.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ func ApplyHandlerToJasIssues(runs []*sarif.Run, entitledForJas bool, handler Par
8888
return nil
8989
}
9090

91-
// PrepareScaVulnerabilities allows to iterate over the provided SCA security vulnerabilities and call the provided handler for each impacted component/package with a vulnerability to process it.
92-
func PrepareScaVulnerabilities(target ScanTarget, vulnerabilities []services.Vulnerability, entitledForJas bool, applicabilityRuns []*sarif.Run, handler ParseScaVulnerabilityFunc) error {
91+
// ApplyHandlerToScaVulnerabilities allows to iterate over the provided SCA security vulnerabilities and call the provided handler for each impacted component/package with a vulnerability to process it.
92+
func ApplyHandlerToScaVulnerabilities(target ScanTarget, vulnerabilities []services.Vulnerability, entitledForJas bool, applicabilityRuns []*sarif.Run, handler ParseScaVulnerabilityFunc) error {
9393
if handler == nil {
9494
return nil
9595
}
@@ -116,8 +116,8 @@ func PrepareScaVulnerabilities(target ScanTarget, vulnerabilities []services.Vul
116116
return nil
117117
}
118118

119-
// PrepareScaViolations allows to iterate over the provided SCA violations and call the provided handler for each impacted component/package with a violation to process it.
120-
func PrepareScaViolations(target ScanTarget, violations []services.Violation, entitledForJas bool, applicabilityRuns []*sarif.Run, securityHandler ParseScaViolationFunc, licenseHandler ParseScaViolationFunc, operationalRiskHandler ParseScaViolationFunc) (watches []string, failBuild bool, err error) {
119+
// ApplyHandlerToScaViolations allows to iterate over the provided SCA violations and call the provided handler for each impacted component/package with a violation to process it.
120+
func ApplyHandlerToScaViolations(target ScanTarget, violations []services.Violation, entitledForJas bool, applicabilityRuns []*sarif.Run, securityHandler ParseScaViolationFunc, licenseHandler ParseScaViolationFunc, operationalRiskHandler ParseScaViolationFunc) (watches []string, failBuild bool, err error) {
121121
if securityHandler == nil && licenseHandler == nil && operationalRiskHandler == nil {
122122
return
123123
}
@@ -195,8 +195,8 @@ func PrepareScaViolations(target ScanTarget, violations []services.Violation, en
195195
return
196196
}
197197

198-
// PrepareLicenses allows to iterate over the provided licenses and call the provided handler for each component/package with a license to process it.
199-
func PrepareLicenses(target ScanTarget, licenses []services.License, handler ParseLicensesFunc) error {
198+
// ApplyHandlerToLicenses allows to iterate over the provided licenses and call the provided handler for each component/package with a license to process it.
199+
func ApplyHandlerToLicenses(target ScanTarget, licenses []services.License, handler ParseLicensesFunc) error {
200200
if handler == nil {
201201
return nil
202202
}
@@ -627,3 +627,11 @@ func getFinalApplicabilityStatus(applicabilityStatuses []jasutils.ApplicabilityS
627627

628628
return jasutils.NotApplicable
629629
}
630+
631+
func ConvertPolicesToString(policies []services.Policy) []string {
632+
var policiesStr []string
633+
for _, policy := range policies {
634+
policiesStr = append(policiesStr, policy.Policy)
635+
}
636+
return policiesStr
637+
}

0 commit comments

Comments
 (0)