Skip to content

Commit

Permalink
fix: match cpe pattern substrings
Browse files Browse the repository at this point in the history
RHINENG-10943
  • Loading branch information
psegedy committed Jun 27, 2024
1 parent 6620430 commit 22b0118
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion vmaas/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ func (l *ParsedCpe) match(r *ParsedCpe) bool {
if l != nil && r == nil {
return false
}
if l != nil && r != nil && *l != *r {
if l != nil && r != nil && !strings.HasPrefix(*r, *l) {
return false
}
return true
Expand Down
48 changes: 48 additions & 0 deletions vmaas/vulnerabilities_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package vmaas

import (
"fmt"
"sort"
"testing"

Expand Down Expand Up @@ -109,3 +110,50 @@ func TestCSAF(t *testing.T) {
// CVEs from `fixed2` product, `fixed1` is not an update (kernel-1.1-1 to kernel-1.1-1)
assert.Equal(t, []string{"CVE-5"}, manualCves)
}

func TestCPEMatch(t *testing.T) {
type cpeTest struct {
pattern CpeLabel
repoCpe CpeLabel
expected bool
}
el := CpeLabel("cpe:/o:redhat:enterprise_linux")
el8 := CpeLabel("cpe:/o:redhat:enterprise_linux:8")
el9 := CpeLabel("cpe:/o:redhat:enterprise_linux:9")
el9Baseos := CpeLabel("cpe:/o:redhat:enterprise_linux:9::baseos")
el9BaseosA := CpeLabel("cpe:/a:redhat:enterprise_linux:9::baseos")
eus := CpeLabel("cpe:/o:redhat:rhel_eus")
sat6 := CpeLabel("cpe:/a:redhat:satellite:6")
sat610el7 := CpeLabel("cpe:/a:redhat:satellite:6.10::el7")

cpeTests := []cpeTest{
{el, el8, true},
{el, el9, true},
{el, el9Baseos, true},
{el, el9BaseosA, true},
{el, eus, false},
{el8, el, false},
{el8, el9, false},
{el8, el9Baseos, false},
{el8, el9BaseosA, false},
{el8, el8, true},
{el9, el9Baseos, true},
{el9, el9BaseosA, true},
{el9, el, false},
{el9, el8, false},
{el9Baseos, el, false},
{el9Baseos, el9, false},
{el9BaseosA, el, false},
{el9BaseosA, el8, false},
{el9BaseosA, el9, false},
{el9BaseosA, el9Baseos, false},
{sat6, sat610el7, true},
{sat610el7, sat6, false},
}
for _, test := range cpeTests {
t.Run(fmt.Sprint(test), func(t *testing.T) {
res := cpeMatch(test.pattern, test.repoCpe)
assert.Equal(t, test.expected, res)
})
}
}

0 comments on commit 22b0118

Please sign in to comment.