Skip to content

Commit 8a07fca

Browse files
committed
fix: add github security advisory problems to legacy vuln info
This maps GHSA problems back onto legacy JSON output.
1 parent 6e95d7d commit 8a07fca

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

internal/legacy/transform/transform.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ func ProcessProblemForVuln(
5353
return processCveProblem(vuln, prob)
5454
case string(testapi.Cwe):
5555
return processCweProblem(vuln, prob)
56+
case string(testapi.Ghsa):
57+
return processGhsaProblem(vuln, prob)
5658
case string(testapi.SnykLicense):
5759
return processSnykLicenseProblem(vuln, prob, logger)
5860
default:
@@ -386,6 +388,19 @@ func processCweProblem(v *definitions.Vulnerability, prob *testapi.Problem) erro
386388
return nil
387389
}
388390

391+
func processGhsaProblem(v *definitions.Vulnerability, prob *testapi.Problem) error {
392+
ensureVulnHasIdentifiers(v)
393+
ghsa, err := prob.AsGithubSecurityAdvisoryProblem()
394+
if err != nil {
395+
return fmt.Errorf("converting problem to github security advisory: %w", err)
396+
}
397+
if v.Identifiers.GHSA == nil {
398+
v.Identifiers.GHSA = &[]string{}
399+
}
400+
*v.Identifiers.GHSA = append(*v.Identifiers.GHSA, ghsa.Id)
401+
return nil
402+
}
403+
389404
// processSnykLicenseProblem processes a Snyk license problem by extracting its data and populating the vulnerability.
390405
func processSnykLicenseProblem(v *definitions.Vulnerability, prob *testapi.Problem, logger *zerolog.Logger) error {
391406
license, err := prob.AsSnykLicenseProblem()

internal/legacy/transform/transform_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ func TestProcessProblemForVuln_Identifiers(t *testing.T) {
2525
err = cweProblem.FromCweProblem(testapi.CweProblem{Id: "cwe-problem-id", Source: testapi.Cwe})
2626
require.NoError(t, err)
2727

28+
ghsaProblem := &testapi.Problem{}
29+
err = ghsaProblem.FromGithubSecurityAdvisoryProblem(testapi.GithubSecurityAdvisoryProblem{Id: "ghsa-problem-id", Source: testapi.Ghsa})
30+
require.NoError(t, err)
31+
2832
// Setup invalid problem
2933
malformedProblem := &testapi.Problem{} // empty union
3034

@@ -59,6 +63,18 @@ func TestProcessProblemForVuln_Identifiers(t *testing.T) {
5963
require.Len(t, v.Identifiers.CVE, 0)
6064
},
6165
},
66+
{
67+
name: "should add GHSA identifier to empty vulnerability",
68+
vuln: &definitions.Vulnerability{},
69+
problem: ghsaProblem,
70+
assertFunc: func(t *testing.T, v *definitions.Vulnerability) {
71+
t.Helper()
72+
require.NotNil(t, v.Identifiers)
73+
require.Len(t, *v.Identifiers.GHSA, 1)
74+
assert.Equal(t, "ghsa-problem-id", (*v.Identifiers.GHSA)[0])
75+
require.Len(t, v.Identifiers.CVE, 0)
76+
},
77+
},
6278
{
6379
name: "should append CWE identifier to existing CVE",
6480
vuln: &definitions.Vulnerability{

0 commit comments

Comments
 (0)