-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ fully working CPE offline - now just needs tests
- Loading branch information
Showing
9 changed files
with
160 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package cpequery | ||
|
||
import ( | ||
"fmt" | ||
"github.com/vulncheck-oss/cli/pkg/cpe/cpetypes" | ||
"github.com/vulncheck-oss/cli/pkg/cpe/cpeutils" | ||
"strings" | ||
) | ||
|
||
func Query(cpe cpetypes.CPE) (string, error) { | ||
if cpe.Product == "" { | ||
return "true", nil | ||
} | ||
if cpe.IsMozilla() { | ||
return fmt.Sprintf(".products | any(. == %q)", cpe.ProductUcFirst()), nil | ||
} | ||
|
||
if cpe.Vendor == "*" && cpe.Product == "*" { | ||
return "", fmt.Errorf("need at least vendor or product specified") | ||
} | ||
|
||
// if IsParseableVersion - do NOT query for the version in the JSON | ||
return BuildCPEQuery(cpe, !cpeutils.IsParseableVersion(cpetypes.Unquote(cpe.Version))) | ||
} | ||
|
||
func addCondition(field, value string) string { | ||
if value != "*" { | ||
return fmt.Sprintf(`(.%s == %q or .%s == "*")`, field, value, field) | ||
} | ||
return "" | ||
} | ||
|
||
func BuildCPEQuery(cpe cpetypes.CPE, queryVersion bool) (string, error) { | ||
if cpe.Vendor == "*" && cpe.Product == "*" { | ||
return "", fmt.Errorf("need at least vendor or product specified") | ||
} | ||
|
||
var conditions []string | ||
|
||
fields := []struct { | ||
name string | ||
value string | ||
}{ | ||
{"vendor", cpe.Vendor}, | ||
{"product", cpe.Product}, | ||
{"update", cpe.Update}, | ||
{"edition", cpe.Edition}, | ||
{"language", cpe.Language}, | ||
{"sw_edition", cpe.SoftwareEdition}, | ||
{"target_sw", cpe.TargetSoftware}, | ||
{"target_hw", cpe.TargetHardware}, | ||
{"other", cpe.Other}, | ||
} | ||
|
||
// Add version field only if withVersion is true | ||
if queryVersion { | ||
fields = append(fields, struct{ name, value string }{"version", cpetypes.Unquote(cpe.Version)}) | ||
} | ||
|
||
for _, field := range fields { | ||
if condition := addCondition(field.name, field.value); condition != "" { | ||
conditions = append(conditions, condition) | ||
} | ||
} | ||
|
||
if len(conditions) == 0 { | ||
return "true", nil | ||
} | ||
|
||
query := strings.Join(conditions, " and ") | ||
return query, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters