Skip to content

Commit

Permalink
fix(cpescan): bug in NvdVendorProductMatch
Browse files Browse the repository at this point in the history
  • Loading branch information
kotakanbe committed Oct 12, 2021
1 parent 4d1f7ac commit 87808a2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
5 changes: 5 additions & 0 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ diff-cpes:
@ python integration/diff_server_mode.py cpe_ids --sample_rate 0.01

diff-server-rdb:
- pkill -KILL go-cve.old
- pkill -KILL go-cve.new
integration/go-cve.old server --dbpath=$(PWD)/integration/cve.old.sqlite3 --port 1325 > /dev/null 2>&1 &
integration/go-cve.new server --dbpath=$(PWD)/integration/cve.new.sqlite3 --port 1326 > /dev/null 2>&1 &
make diff-cveid
Expand All @@ -123,6 +125,8 @@ diff-server-rdb:
pkill go-cve.new

diff-server-redis:
- pkill -KILL go-cve.old
- pkill -KILL go-cve.new
integration/go-cve.old server --dbtype redis --dbpath "redis://127.0.0.1:6379/0" --port 1325 > /dev/null 2>&1 &
integration/go-cve.new server --dbtype redis --dbpath "redis://127.0.0.1:6380/0" --port 1326 > /dev/null 2>&1 &
make diff-cveid
Expand All @@ -131,6 +135,7 @@ diff-server-redis:
pkill go-cve.new

diff-server-rdb-redis:
- pkill -KILL go-cve.new
integration/go-cve.new server --dbpath=$(PWD)/integration/cve.new.sqlite3 --port 1325 > /dev/null 2>&1 &
integration/go-cve.new server --dbtype redis --dbpath "redis://127.0.0.1:6380/0" --port 1326 > /dev/null 2>&1 &
make diff-cveid
Expand Down
10 changes: 5 additions & 5 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,6 @@ func match(specifiedURI string, cpeInNvd models.CpeBase) (isExactVerMatch, isRou
return false, false, false, nil
}

if matching.IsEqual(specified, cpeInNvdWfn) {
log.Debugf("%s equals %s", specified.String(), cpeInNvd.URI)
return true, false, false, nil
}

specifiedVer := fmt.Sprintf("%s", specified.Get(common.AttributeVersion))
switch specifiedVer {
case "NA", "ANY":
Expand All @@ -214,6 +209,11 @@ func match(specifiedURI string, cpeInNvd models.CpeBase) (isExactVerMatch, isRou
return false, false, isSuperORSubset(cpeInNvdWfn, specified), nil
}

if matching.IsEqual(specified, cpeInNvdWfn) {
log.Debugf("%s equals %s", specified.String(), cpeInNvd.URI)
return true, false, false, nil
}

ok, err := matchSemver(specifiedVer, cpeInNvd)
if err != nil {
// version range specified in cpeInNvd are not defined as semver style
Expand Down
29 changes: 28 additions & 1 deletion db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ func Test_filterCveDetailByCpeURI1(t *testing.T) {
expected: nil,
},
{
name: "NvdExactVersionMatch",
args: args{
uri: "cpe:/o:vendor:product:1.0.0",
cve: &models.CveDetail{
Expand Down Expand Up @@ -530,7 +531,7 @@ func Test_filterCveDetailByCpeURI1(t *testing.T) {
},
},
{
name: "",
name: "NvdVendorProductMatch",
args: args{
uri: "cpe:/o:vendor:product",
cve: &models.CveDetail{
Expand Down Expand Up @@ -569,6 +570,32 @@ func Test_filterCveDetailByCpeURI1(t *testing.T) {
Jvns: []models.Jvn{},
},
},
{
name: "NvdVendorProductMatch",
args: args{
uri: "cpe:/a:vendor:product",
cve: &models.CveDetail{
Nvds: []models.Nvd{
{
Cpes: []models.NvdCpe{
{CpeBase: models.CpeBase{URI: "cpe:/a:vendor:product", VersionEndExcluding: "1.0.0"}},
},
},
},
},
},
expected: &models.CveDetail{
Nvds: []models.Nvd{
{
Cpes: []models.NvdCpe{
{CpeBase: models.CpeBase{URI: "cpe:/a:vendor:product", VersionEndExcluding: "1.0.0"}},
},
DetectionMethod: models.NvdVendorProductMatch,
},
},
Jvns: []models.Jvn{},
},
},
{
args: args{
uri: "cpe:/o:vmware:esxi:7.0:-",
Expand Down
5 changes: 5 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"os"
"path/filepath"
"sort"

"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
Expand Down Expand Up @@ -83,6 +84,10 @@ func getCveByCpeName(driver db.DB) echo.HandlerFunc {
log.Errorf("%s", err)
return err
}

sort.Slice(cveDetails, func(i, j int) bool {
return cveDetails[i].CveID < cveDetails[j].CveID
})
return c.JSON(http.StatusOK, &cveDetails)
}
}
Expand Down

0 comments on commit 87808a2

Please sign in to comment.