Skip to content

🛡️ CVE

The cve module provides a CVEReference value type modeling a single vulnerability (CVE ID, description, dates, CVSS score, severity, references, affected CPEs, free-form metadata), along with constructors and mutation methods, plus a set of package-level utilities for parsing, validating, grouping, sorting, deduplicating, and querying lists of CVE references.

Type: CVEReference

go
type CVEReference struct {
    CVEID            string                   // CVE identifier, e.g. "CVE-2021-44228"
    Description      string                   // vulnerability description
    PublishedDate    time.Time                // publication date
    LastModifiedDate time.Time                // last modification date
    CVSSScore        float64                  // CVSS score 0.0-10.0
    Severity         string                   // Low / Medium / High / Critical
    References       []string                 // reference URLs
    AffectedCPEs     []string                 // affected CPE URIs (2.2 or 2.3)
    Metadata         map[string]interface{}   // extra metadata
}

🆕 NewCVEReference

go
func NewCVEReference(cveID string) *CVEReference

Creates a CVEReference with the CVE ID normalized via cve.Format. References and AffectedCPEs are initialized to empty slices, Metadata to an empty map, and both dates to the current time.

ParameterTypeDescription
cveIDstringCVE identifier, e.g. "CVE-2021-44228"
ReturnTypeDescription
#1*CVEReferenceInitialized CVE reference
go
cve := cpeskills.NewCVEReference("CVE-2021-44228")
cve.Description = "Log4j remote code execution"
cve.SetSeverity(10.0)

➕ AddAffectedCPE

go
func (cve *CVEReference) AddAffectedCPE(cpeURI string)

Adds an affected CPE URI. Duplicate URIs are ignored. LastModifiedDate is updated to the current time.

ParameterTypeDescription
cpeURIstringCPE URI, 2.2 (cpe:/) or 2.3 (cpe:2.3:) format
go
cve.AddAffectedCPE("cpe:2.3:a:apache:log4j:2.0:*:*:*:*:*:*:*")

➖ RemoveAffectedCPE

go
func (cve *CVEReference) RemoveAffectedCPE(cpeURI string) bool

Removes the first occurrence of the given CPE URI. Returns whether a removal happened. LastModifiedDate is updated on success.

ParameterTypeDescription
cpeURIstringCPE URI to remove
ReturnTypeDescription
#1booltrue if the URI was present and removed
go
removed := cve.RemoveAffectedCPE("cpe:2.3:a:apache:log4j:2.0:*:*:*:*:*:*:*")

🔗 AddReference

go
func (cve *CVEReference) AddReference(reference string)

Appends a reference URL, skipping duplicates. LastModifiedDate is updated.

ParameterTypeDescription
referencestringReference URL
go
cve.AddReference("https://nvd.nist.gov/vuln/detail/CVE-2021-44228")

🎚️ SetSeverity

go
func (cve *CVEReference) SetSeverity(cvssScore float64)

Sets CVSSScore and derives Severity from the score: >=9.0 Critical, >=7.0 High, >=4.0 Medium, otherwise Low.

ParameterTypeDescription
cvssScorefloat64CVSS score in 0.0–10.0
go
cve.SetSeverity(9.8)
fmt.Println(cve.Severity) // Critical

🏷️ SetMetadata

go
func (cve *CVEReference) SetMetadata(key string, value interface{})

Stores or overwrites a metadata entry under key. LastModifiedDate is updated.

ParameterTypeDescription
keystringMetadata key
valueinterface{}Metadata value
go
cve.SetMetadata("cwe", "CWE-502")

🔎 GetMetadata

go
func (cve *CVEReference) GetMetadata(key string) (interface{}, bool)

Returns the metadata value for key and whether the key existed.

ParameterTypeDescription
keystringMetadata key
ReturnTypeDescription
#1interface{}The stored value (zero value if absent)
#2boolWhether the key was present
go
val, ok := cve.GetMetadata("cwe")

🗑️ RemoveMetadata

go
func (cve *CVEReference) RemoveMetadata(key string) bool

Removes a metadata entry, returning whether it existed. LastModifiedDate is updated on success.

ParameterTypeDescription
keystringMetadata key
ReturnTypeDescription
#1booltrue if the key was present and removed
go
cve.RemoveMetadata("cwe")

🔎 QueryByCVE

go
func QueryByCVE(cves []*CVEReference, cveID string) []*CPE

Searches cves for the one whose CVEID matches (after normalization) and returns its AffectedCPEs parsed into *CPE objects.

ParameterTypeDescription
cves[]*CVEReferenceThe CVE list to search
cveIDstringCVE identifier to find
ReturnTypeDescription
#1[]*CPECPEs affected by the matched CVE; empty if not found
go
cpes := cpeskills.QueryByCVE(cveList, "CVE-2021-44228")

ℹ️ GetCVEInfo

go
func GetCVEInfo(cves []*CVEReference, cveID string) *CVEReference

Returns the CVEReference in cves whose CVEID matches (after normalization), or nil if none.

ParameterTypeDescription
cves[]*CVEReferenceThe CVE list to search
cveIDstringCVE identifier
ReturnTypeDescription
#1*CVEReferenceThe matched CVE, nil if not found
go
ref := cpeskills.GetCVEInfo(cveList, "CVE-2021-44228")

📝 ExtractCVEsFromText

go
func ExtractCVEsFromText(text string) []string

Scans text for CVE-ID patterns (CVE-YYYY-NNNNN+) and returns them in order of appearance, preserving duplicates.

ParameterTypeDescription
textstringArbitrary text
ReturnTypeDescription
#1[]stringAll CVE IDs found
go
ids := cpeskills.ExtractCVEsFromText("Fixed CVE-2021-44228 and CVE-2021-45046.")

📅 GroupCVEsByYear

go
func GroupCVEsByYear(cveIDs []string) map[string][]string

Groups CVE IDs by their year component (the YYYY in CVE-YYYY-NNNNN).

ParameterTypeDescription
cveIDs[]stringCVE IDs
ReturnTypeDescription
#1map[string][]stringYear -> list of CVE IDs
go
byYear := cpeskills.GroupCVEsByYear([]string{"CVE-2021-44228", "CVE-2020-1472"})

↕️ SortCVEs

go
func SortCVEs(cveIDs []string) []string

Returns a new slice of CVE IDs sorted chronologically by year, then by numeric sequence within the year. The input is not modified.

ParameterTypeDescription
cveIDs[]stringCVE IDs
ReturnTypeDescription
#1[]stringSorted CVE IDs
go
sorted := cpeskills.SortCVEs([]string{"CVE-2021-44228", "CVE-2014-0160", "CVE-2021-45046"})

🧹 RemoveDuplicateCVEs

go
func RemoveDuplicateCVEs(cveIDs []string) []string

Returns the input with duplicates removed, preserving first-occurrence order.

ParameterTypeDescription
cveIDs[]stringCVE IDs, possibly with duplicates
ReturnTypeDescription
#1[]stringDeduplicated CVE IDs
go
unique := cpeskills.RemoveDuplicateCVEs([]string{"CVE-2021-44228", "CVE-2021-44228"})

🕒 GetRecentCVEs

go
func GetRecentCVEs(cveIDs []string, years int) []string

Returns CVE IDs whose year is within the last years years (relative to the most recent year present, or current time).

ParameterTypeDescription
cveIDs[]stringCVE IDs
yearsintNumber of recent years to keep
ReturnTypeDescription
#1[]stringCVE IDs from recent years
go
recent := cpeskills.GetRecentCVEs(cveIDs, 3)

✅ ValidateCVE

go
func ValidateCVE(cveID string) bool

Returns whether cveID matches the standard CVE format CVE-YYYY-NNNNN (with at least four digits).

ParameterTypeDescription
cveIDstringString to validate
ReturnTypeDescription
#1booltrue if the string is a valid CVE ID
go
cpeskills.ValidateCVE("CVE-2021-44228") // true
cpeskills.ValidateCVE("CVE-21-1")        // false

🔎 QueryByProduct

go
func QueryByProduct(cves []*CVEReference, vendor, product string, version string) []*CVEReference

Returns the CVE references in cves that affect the given vendor/product (and optionally version) by matching each CVE's AffectedCPEs.

ParameterTypeDescription
cves[]*CVEReferenceThe CVE list to filter
vendorstringCPE vendor to match
productstringCPE product to match
versionstringCPE version; pass "" to ignore version
ReturnTypeDescription
#1[]*CVEReferenceMatching CVE references
go
hits := cpeskills.QueryByProduct(cveList, "apache", "log4j", "2.0")

🧭 CVE Operations Overview

Released under the MIT License.