🧬 Cvss3x
pkg/cvss/cvss3x.go · Main type · Embeds three segments + version
This page documents the
Cvss3xtype definition and its serialization interface implementations as written inpkg/cvss/cvss3x.go. For the higher-level overview of thepkg/cvsspackage (parsing, scoring, building), see /sdk/cvss.
Synopsis
Cvss3x is the main CVSS 3.x value type. It embeds the three metric segments — Cvss3xBase (mandatory), Cvss3xTemporal and Cvss3xEnvironmental (both optional) — plus a major/minor version pair. String() renders the canonical CVSS:3.1/AV:.../... form, and the four Marshal*/Unmarshal* methods integrate it with encoding/json, encoding/xml, mapstructure and database drivers.
cv := cvss.NewCvss3x() // base allocated, temporal/env nil
cv.MajorVersion, cv.MinorVersion = 3, 1
// populate via parser/builder in practice
fmt.Println(cv.String()) // CVSS:3.1/...
fmt.Println(cv.Check()) // <nil> when validStructure
API Reference
Cvss3x struct
type Cvss3x struct {
*Cvss3xBase
*Cvss3xTemporal
*Cvss3xEnvironmental
MajorVersion int
MinorVersion int
}The embedded pointers give Cvss3x direct access to every metric field and method of the three segments. MajorVersion/MinorVersion form the 3.0 / 3.1 prefix.
NewCvss3x
func NewCvss3x() *Cvss3xReturns a *Cvss3x with Cvss3xBase allocated (empty) and Cvss3xTemporal / Cvss3xEnvironmental left nil. The version fields are zero-valued — callers (or the parser) set them afterwards.
Check
func (x *Cvss3x) Check() errorValidates the whole vector in order:
- nil receiver →
fmt.Errorf("Cvss3x is nil") MajorVersion != 3→ error (only major version 3 is supported)MinorVersionnot in{0, 1}→ error (only 3.0 and 3.1)Cvss3xBasenil → error; otherwisex.Cvss3xBase.Check()(all eight base metrics must be set)- If
Cvss3xTemporalis non-nil,x.Cvss3xTemporal.Check() - If
Cvss3xEnvironmentalis non-nil,x.Cvss3xEnvironmental.Check()
The temporal and environmental segments are optional; when present, only their set fields are validated.
String
func (x *Cvss3x) String() stringBuilds the canonical vector string: CVSS:<Major>.<Minor> followed by /-prefixed segment outputs from Cvss3xBase.String(), Cvss3xTemporal.String() and Cvss3xEnvironmental.String() (each only when the segment is non-nil and non-empty). Example: CVSS:3.1/AV:L/AC:L/PR:L/UI:N/S:U/C:N/I:H/A:H.
JSON serialization
func (x *Cvss3x) MarshalJSON() ([]byte, error)
func (x *Cvss3x) UnmarshalJSON(data []byte) errorMarshalJSON implements json.Marshaler: a nil receiver yields null, otherwise the vector string is emitted as a JSON string ("CVSS:3.1/...").
UnmarshalJSON implements json.Unmarshaler: null and "" are no-ops; any other string is parsed via the internal fromVectorString (the same parser used by pkg/parser). A parse failure is wrapped as failed to unmarshal Cvss3x: %w.
Text serialization
func (x *Cvss3x) MarshalText() ([]byte, error)
func (x *Cvss3x) UnmarshalText(data []byte) errorMarshalText implements encoding.TextMarshaler: nil → nil, nil, otherwise the raw bytes of x.String(). UnmarshalText implements encoding.TextUnmarshaler: empty input is a no-op, otherwise it parses via fromVectorString (wrapped error: failed to unmarshal Cvss3x from text: %w).
The text pair is what makes Cvss3x usable as a map key, an encoding/xml attribute/element, a mapstructure target, or a database/sql scan value — anywhere the encoding.Text* interfaces are consulted.
Example
package main
import (
"encoding/json"
"fmt"
"github.com/scagogogo/cvss-skills/pkg/cvss"
"github.com/scagogogo/cvss-skills/pkg/parser"
)
func main() {
// Parse a vector string into a Cvss3x.
cv, err := parser.ParseString("CVSS:3.1/AV:L/AC:L/PR:L/UI:N/S:U/C:N/I:H/A:H")
if err != nil {
panic(err)
}
fmt.Println(cv.String()) // CVSS:3.1/AV:L/AC:L/PR:L/UI:N/S:U/C:N/I:H/A:H
fmt.Println(cv.Check()) // <nil>
// JSON round-trip via MarshalJSON / UnmarshalJSON.
b, _ := json.Marshal(cv)
fmt.Println(string(b)) // "CVSS:3.1/AV:L/AC:L/PR:L/UI:N/S:U/C:N/I:H/A:H"
var cv2 cvss.Cvss3x
if err := json.Unmarshal(b, &cv2); err != nil {
panic(err)
}
fmt.Println(cv2.String()) // same vector
// Text round-trip via MarshalText / UnmarshalText (works with xml/sql/mapstructure).
txt, _ := cv.MarshalText()
var cv3 cvss.Cvss3x
_ = cv3.UnmarshalText(txt)
fmt.Println(cv3.String())
}Related
- /sdk/cvss — package-level overview (this page is the type-level complement)
- /sdk/cvss3x-base · /sdk/cvss3x-temporal · /sdk/cvss3x-environmental — the three embedded segments
- /sdk/json — JSON serialization helpers built on these methods
- /sdk/parser — the
fromVectorStringparser used byUnmarshalJSON/UnmarshalText