Skip to content

Commit

Permalink
feat: configuration for inferring NVD fix versions
Browse files Browse the repository at this point in the history
Signed-off-by: Weston Steimel <[email protected]>
  • Loading branch information
westonsteimel committed Oct 16, 2024
1 parent 0cd597b commit 6dcb320
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 8 deletions.
7 changes: 4 additions & 3 deletions cmd/grype-db/cli/options/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ type Build struct {

func DefaultBuild() Build {
return Build{
DBLocation: DefaultDBLocation(),
SkipValidation: false,
SchemaVersion: process.DefaultSchemaVersion,
DBLocation: DefaultDBLocation(),
SkipValidation: false,
SchemaVersion: process.DefaultSchemaVersion,
InferNVDFixVersions: true,
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/process/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func getProcessors(cfg BuildConfig) ([]data.Processor, error) {
case grypeDBv4.SchemaVersion:
return v4.Processors(), nil
case grypeDBv5.SchemaVersion:
return v5.Processors(v5.NewConfig(v5.WithOSCPEsIncluded(cfg.IncludeOSCPEs))), nil
return v5.Processors(v5.NewConfig(v5.WithOSCPEsIncluded(cfg.IncludeOSCPEs), v5.WithInferNVDFixVersions(cfg.InferNVDFixVersions))), nil
default:
return nil, fmt.Errorf("unable to create processor: unsupported schema version: %+v", cfg.SchemaVersion)
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/process/v5/processors.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ func WithOSCPEsIncluded(included bool) Option {
}
}

func WithInferNVDFixVersions(infer bool) Option {
return func(cfg *Config) {
cfg.NVD.InferNVDFixVersions = infer
}
}

func NewConfig(options ...Option) Config {
var cfg Config
for _, option := range options {
Expand Down
13 changes: 10 additions & 3 deletions pkg/process/v5/transformers/nvd/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import (
)

type Config struct {
IncludeOSCPE bool
IncludeOSCPE bool
InferNVDFixVersions bool
}

func Transformer(cfg Config) data.NVDTransformer {
Expand Down Expand Up @@ -82,7 +83,7 @@ func transform(cfg Config, vulnerability unmarshal.NVDVulnerability) ([]data.Ent
PackageName: grypeNamespace.Resolver().Normalize(p.Product),
Namespace: entryNamespace,
CPEs: orderedCPEs,
Fix: getFix(matches),
Fix: getFix(matches, cfg.InferNVDFixVersions),
})
}

Expand Down Expand Up @@ -118,7 +119,13 @@ func getVersionFormat(name string, cpes []string) version.Format {
return version.UnknownFormat
}

func getFix(matches []nvd.CpeMatch) grypeDB.Fix {
func getFix(matches []nvd.CpeMatch, inferNVDFixVersions bool) grypeDB.Fix {
if !inferNVDFixVersions {
return grypeDB.Fix{
State: grypeDB.UnknownFixState,
}
}

possiblyFixed := strset.New()
knownAffected := strset.New()
unspecifiedSet := strset.New("*", "-", "*")
Expand Down
10 changes: 9 additions & 1 deletion pkg/process/v5/transformers/nvd/transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1058,8 +1058,16 @@ func TestGetFix(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fix := getFix(tt.matches)
fix := getFix(tt.matches, true)
assert.Equal(t, tt.expected, fix)
})

t.Run(tt.name+" don't infer NVD fixes", func(t *testing.T) {
fix := getFix(tt.matches, false)
assert.Equal(t, grypeDB.Fix{
Versions: nil,
State: "unknown",
}, fix)
})
}
}

0 comments on commit 6dcb320

Please sign in to comment.