Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add vex.EffectiveStatement deprecate StatementFromID #30

Merged
merged 1 commit into from
Jun 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions pkg/vex/vex.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,39 @@ func (vexDoc *VEX) ToJSON(w io.Writer) error {
return nil
}

// EffectiveStatement returns the latest VEX statement for a given product and
// vulnerability, that is the statement that contains the latest data about
// impact to a given product.
func (vexDoc *VEX) EffectiveStatement(product, vulnID string) (s *Statement) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if it'd be simpler to start by calling SortStatements to do the time sort.

And then you can go latest-to-oldest, doing the "does this statement refer to the given product?" check, and return the statement the first time the answer is "yes". WDYT?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, changed 👍

statements := vexDoc.Statements
var t time.Time
if vexDoc.Timestamp != nil {
t = *vexDoc.Timestamp
}

SortStatements(statements, t)

for i := len(statements) - 1; i >= 0; i-- {
if statements[i].Vulnerability != vulnID {
continue
}
for _, p := range statements[i].Products {
if p == product {
return &statements[i]
}
}
}
return nil
}

// StatementFromID returns a statement for a given vulnerability if there is one.
//
// Deprecated: vex.StatementFromID is deprecated and will be removed in an upcoming version
func (vexDoc *VEX) StatementFromID(id string) *Statement {
for _, statement := range vexDoc.Statements { //nolint:gocritic // turning off for rule rangeValCopy
if statement.Vulnerability == id {
logrus.Infof("VEX doc contains statement for CVE %s", id)
return &statement
logrus.Warn("vex.StatementFromID is deprecated and will be removed in an upcoming version")
puerco marked this conversation as resolved.
Show resolved Hide resolved
for i := range vexDoc.Statements {
if vexDoc.Statements[i].Vulnerability == id && len(vexDoc.Statements[i].Products) > 0 {
return vexDoc.EffectiveStatement(vexDoc.Statements[i].Products[0], id)
}
}
return nil
Expand Down
88 changes: 88 additions & 0 deletions pkg/vex/vex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,94 @@ func TestLoadCSAF(t *testing.T) {
require.Equal(t, vexDoc.Metadata.ID, "2022-EVD-UC-01-NA-001")
}

func TestEffectiveStatement(t *testing.T) {
date1 := time.Date(2023, 4, 17, 20, 34, 58, 0, time.UTC)
date2 := time.Date(2023, 4, 18, 20, 34, 58, 0, time.UTC)
for _, tc := range []struct {
vexDoc *VEX
vulnID string
product string
shouldNil bool
expectedDate *time.Time
expectedStatus Status
}{
{
// Single statement
vexDoc: &VEX{
Statements: []Statement{
{
Vulnerability: "CVE-2014-123456",
Timestamp: &date1,
Products: []string{"pkg://deb@1.0"},
Status: StatusNotAffected,
},
},
},
vulnID: "CVE-2014-123456",
product: "pkg://deb@1.0",
shouldNil: false,
expectedDate: &date1,
expectedStatus: StatusNotAffected,
},
{
// Two consecutive statemente
vexDoc: &VEX{
Statements: []Statement{
{
Vulnerability: "CVE-2014-123456",
Timestamp: &date1,
Products: []string{"pkg://deb@1.0"},
Status: StatusUnderInvestigation,
},
{
Vulnerability: "CVE-2014-123456",
Timestamp: &date2,
Products: []string{"pkg://deb@1.0"},
Status: StatusNotAffected,
},
},
},
vulnID: "CVE-2014-123456",
product: "pkg://deb@1.0",
shouldNil: false,
expectedDate: &date2,
expectedStatus: StatusNotAffected,
},
{
// Different products
vexDoc: &VEX{
Statements: []Statement{
{
Vulnerability: "CVE-2014-123456",
Timestamp: &date1,
Products: []string{"pkg://deb@1.0"},
Status: StatusUnderInvestigation,
},
{
Vulnerability: "CVE-2014-123456",
Timestamp: &date2,
Products: []string{"pkg://deb@2.0"},
Status: StatusNotAffected,
},
},
},
vulnID: "CVE-2014-123456",
product: "pkg://deb@1.0",
shouldNil: false,
expectedDate: &date1,
expectedStatus: StatusUnderInvestigation,
},
} {
s := tc.vexDoc.EffectiveStatement(tc.product, tc.vulnID)
if tc.shouldNil {
require.Nil(t, s)
} else {
require.Equal(t, tc.expectedDate, s.Timestamp)
require.Equal(t, tc.expectedStatus, s.Status)
}
}
}

func genTestDoc(t *testing.T) VEX {
ts, err := time.Parse(time.RFC3339, "2022-12-22T16:36:43-05:00")
require.NoError(t, err)
Expand Down