Skip to content

Commit

Permalink
Merge pull request #220 from macedogm/add-aliases-flag
Browse files Browse the repository at this point in the history
Add support to vulnerability aliases
  • Loading branch information
puerco authored Jul 11, 2024
2 parents 9bbf7cd + 8a9459b commit 4067f8e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
32 changes: 31 additions & 1 deletion internal/cmd/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"errors"
"fmt"
"os"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -64,6 +65,7 @@ type vexStatementOptions struct {
Justification string
ImpactStatement string
Vulnerability string
Aliases []string
ActionStatement string
Products []string
Subcomponents []string
Expand Down Expand Up @@ -107,6 +109,22 @@ func (so *vexStatementOptions) Validate() error {
return fmt.Errorf("--impact-statement can be set only when status is \"not_affected\" (status was %q)", so.Status)
}

if len(so.Aliases) > 0 {
previousLen := len(so.Aliases)
slices.Sort(so.Aliases)
so.Aliases = slices.Compact(so.Aliases)

if previousLen != len(so.Aliases) {
return errors.New("repeated aliases found")
}

for _, sa := range so.Aliases {
if sa == so.Vulnerability {
return errors.New("an alias cannot be the same as the vulnerability ID")
}
}
}

return nil
}

Expand All @@ -119,6 +137,13 @@ func (so *vexStatementOptions) AddFlags(cmd *cobra.Command) {
"vulnerability to add to the statement (eg CVE-2023-12345)",
)

cmd.PersistentFlags().StringSliceVar(
&so.Aliases,
"aliases",
[]string{},
"list of aliases under which the vulnerability may be known (eg GO-2023-12345, GHSA-a1a1-b2b2-c3c3)",
)

cmd.PersistentFlags().StringSliceVarP(
&so.Products,
productLongFlag,
Expand Down Expand Up @@ -179,7 +204,8 @@ func (so *vexStatementOptions) ToStatement() vex.Statement {

s := vex.Statement{
Vulnerability: vex.Vulnerability{
Name: vex.VulnerabilityID(so.Vulnerability),
Name: vex.VulnerabilityID(so.Vulnerability),
Aliases: []vex.VulnerabilityID{},
},
Timestamp: &t,
LastUpdated: nil,
Expand All @@ -204,6 +230,10 @@ func (so *vexStatementOptions) ToStatement() vex.Statement {
})
}

for _, sa := range so.Aliases {
s.Vulnerability.Aliases = append(s.Vulnerability.Aliases, vex.VulnerabilityID(sa))
}

for _, sc := range so.Subcomponents {
s.Products[0].Subcomponents = append(s.Products[0].Subcomponents, vex.Subcomponent{
Component: vex.Component{ID: sc},
Expand Down
16 changes: 16 additions & 0 deletions internal/cmd/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,22 @@ func TestVexStatementOptionsValidate(t *testing.T) {
Vulnerability: "CVE-2014-12345678",
}, true,
},
"repeated aliases found": {
vexStatementOptions{
Status: string(vex.StatusUnderInvestigation),
Products: []string{"pkg:golang/fmt"},
Vulnerability: "CVE-2014-12345678",
Aliases: []string{"CVE-2014-1234", "CVE-2014-1234"},
}, true,
},
"repeated alias and vulnerability ID": {
vexStatementOptions{
Status: string(vex.StatusUnderInvestigation),
Products: []string{"pkg:golang/fmt"},
Vulnerability: "CVE-2014-12345678",
Aliases: []string{"CVE-2014-1234", "CVE-2014-12345678"},
}, true,
},
"ok": {
vexStatementOptions{
Status: string(vex.StatusUnderInvestigation),
Expand Down

0 comments on commit 4067f8e

Please sign in to comment.