Documentation
¶
Overview ¶
Package versions provides SemVer-aware version parsing, comparison, range evaluation, and CVE 5.1 affected/unaffected status determination.
MIRRORED — this package is kept byte-identical between vdb-api/internal/versions and cli/internal/versions. The shared test corpus lives in corpus_test.go. Update both copies together.
Index ¶
- func BuildRangeList(entries []VersionEntry, defaultStatus string) []string
- func BuildRangeStrings(entries []VersionEntry, defaultStatus string) (versionRange, unaffectedVersions string)
- func Compare(a, b Version) int
- func DeriveFixedVersions(entries []VersionEntry) []string
- func EqualExact(a, b Version, p PseudoPolicy) bool
- func EvaluateStatus(installed string, entries []VersionEntry, defaultStatus string, opt Options) (Status, Evidence)
- func IsWildcardRange(s string) bool
- func Normalize(s string) string
- type Constraint
- type Evidence
- type Op
- type Options
- type PseudoPolicy
- type Range
- type RangeSet
- type Status
- type Version
- type VersionChange
- type VersionEntry
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildRangeList ¶
func BuildRangeList(entries []VersionEntry, defaultStatus string) []string
BuildRangeList renders entries into per-OR-group canonical strings (one element per affected range) for consumers that cannot split "||".
func BuildRangeStrings ¶
func BuildRangeStrings(entries []VersionEntry, defaultStatus string) (versionRange, unaffectedVersions string)
BuildRangeStrings renders entries into canonical range expressions for the affected set and the unaffected set, each OR-joined with " || ". Range entries include their introduced lower bound: {version: "1.0.0", lessThan: "2.0.0"} renders ">= 1.0.0 < 2.0.0". Junk entries are skipped. When defaultStatus is affected and no affected entries rendered, the affected expression is "*".
func Compare ¶
Compare returns -1, 0, or 1 ordering a against b per SemVer 2.0 precedence (extended with Extra segments for 4-part schemes). Build metadata is ignored. A Wildcard version compares equal to anything.
func DeriveFixedVersions ¶
func DeriveFixedVersions(entries []VersionEntry) []string
DeriveFixedVersions extracts fix-version candidates from version entries, unifying the per-handler interpretations: an unaffected entry's version, or a range's lessThan / lessThanOrEqual boundary. Order-preserving, deduplicated. Values are passed through verbatim — git shas, letter versions ("1.0.2k"), and epoch versions ("1:2.4.5-1") are legitimate fix identifiers in some ecosystems, so no SemVer parse gate is applied.
func EqualExact ¶
func EqualExact(a, b Version, p PseudoPolicy) bool
EqualExact reports whether a and b match for "="/"!=" purposes. Under PseudoBaseEqual a pseudo/build-suffixed version additionally equals its bare base release (in either direction). Wildcards equal anything; a segment wildcard ("8.x") prefix-matches any version sharing the segments before it ("8.x" equals 8.2.0 but not 9.0.0).
func EvaluateStatus ¶
func EvaluateStatus(installed string, entries []VersionEntry, defaultStatus string, opt Options) (Status, Evidence)
EvaluateStatus determines whether installed is affected, unaffected, or unknown given CVE 5.1 version entries and a default status.
Precedence (first hit wins):
- exact entry, status unaffected, exact match → unaffected
- exact entry, status affected, exact match → affected
- range entry, status unaffected, contains → unaffected
- range entry, status affected, contains → affected
- defaultStatus affected/unaffected → that status
- → unknown
An entry's Changes array (in-range status steps) overrides the entry status with the status of the latest change at or below installed. Junk entries (unparseable versions/bounds) are skipped, never fatal.
func IsWildcardRange ¶
IsWildcardRange reports whether a range expression means "all versions". Mirrors (and supersedes) the historical CLI heuristics.
Types ¶
type Constraint ¶
Constraint is a single operator + version comparison.
type Evidence ¶
type Evidence struct {
// MatchKind is one of: exact-unaffected, exact-affected,
// range-unaffected, range-affected, default, unparseable-version, none.
MatchKind string
// RangeString is the canonical rendering of the deciding entry.
RangeString string
}
Evidence explains why EvaluateStatus reached its verdict.
type Options ¶
type Options struct {
Ecosystem string
PseudoPolicy *PseudoPolicy
}
Options threads ecosystem context (and an optional explicit policy override) through evaluation call sites.
type PseudoPolicy ¶
type PseudoPolicy int
PseudoPolicy controls whether a pseudo/build-suffixed version (e.g. "5.3.2-0.20260526213025-e8e5b83ca9a5") is considered equal to its base release ("5.3.2") for exact-match (= / !=) purposes. Relational operators always use true SemVer ordering regardless of policy.
const ( // PseudoBaseEqual treats the suffix as a build artifact: the pseudo // version equals its base release for exact matches. Default for // ecosystems other than Go. PseudoBaseEqual PseudoPolicy = iota // PseudoStrict follows Go module semantics: a pseudo-version sorts // strictly before its base release and never equals it. Default for // the go/golang ecosystem. PseudoStrict )
func ResolvePseudoPolicy ¶
func ResolvePseudoPolicy(opt Options) PseudoPolicy
ResolvePseudoPolicy returns the effective policy: an explicit override wins; otherwise the go/golang ecosystem gets PseudoStrict and everything else gets PseudoBaseEqual.
type Range ¶
type Range struct {
Constraints []Constraint
}
Range is a set of constraints ANDed together.
type RangeSet ¶
RangeSet is a set of ranges ORed together.
func ParseRange ¶
ParseRange parses a version range expression into a RangeSet.
Grammar (canonical, as emitted by the VDB API):
RangeSet := Range ( " || " Range )* OR Range := Constraint ( " " Constraint )* AND Constraint := Op " "? Version | Version bare Version ⇒ "=" Op := "=" | "==" | "<" | "<=" | ">" | ">=" | "!="
Additionally tolerated (never emitted): unicode operators (≥ ≤ ≠), v/V/npm: version prefixes, interval notation "[a, b)" / "(a, b]", and comma lists with disambiguation — items carrying operators make commas AND separators (">= 2.0.0, < 2.3.1"); all-bare items make commas an OR exact list ("1.14.1, 0.30.4" ⇒ "= 1.14.1 || = 0.30.4"). "*" matches all versions.
type Status ¶
type Status string
Status is the canonical three-state outcome of a version evaluation.
func NormalizeStatus ¶
NormalizeStatus maps the status vocabulary seen across CVE 5.1, MSRC, and relationship-type data onto the canonical three-state Status.
type Version ¶
type Version struct {
Major, Minor, Patch uint64
Extra []uint64 // 4th+ numeric segments (maven/.NET style), compared after Patch
Prerelease string
PrereleaseIDs []string // dot-split identifiers for SemVer 2.0 precedence
Build string // build metadata after "+", ignored in comparison
IsPseudo bool // timestamp+hash suffix detected (Go pseudo-version or distro rebuild)
PseudoBase string // e.g. "5.3.2"
PseudoTimestamp string // e.g. "20260526213025"
Wildcard bool // "*" or "x" — equals anything
SegWildcard bool // a SEGMENT is a wildcard ("8.x", "1.2.*") — prefix-matches for =/!=
SegWildcardAt int // index of the first wildcard segment (0=major)
Original string
}
Version is a parsed, comparison-ready version.
type VersionChange ¶
VersionChange is a CVE 5.1 in-range status step: at version At and above (within the entry's range) the status becomes Status.
type VersionEntry ¶
type VersionEntry struct {
Version string
Status Status
VersionType string
LessThan *string
LessThanOrEqual *string
Changes []VersionChange
}
VersionEntry mirrors a CVE 5.1 affected.versions[] element (one row of CVEAffectedVersion): an exact version or a range with an "introduced" lower bound (Version) and an exclusive (LessThan) or inclusive (LessThanOrEqual) upper bound.