pub struct Vector(/* private fields */);
Expand description
CVSS vector.
Notes:
- Parses CVSS v2, v3, and v4 vector strings.
- Represented internally as a
u64
(8 bytes). See “Internal Representation” below. - Metrics are sorted in specification order when iterating a
Vector
or converting aVector
to a string; the order of metrics in the original vector string is not preserved. See “Examples” below. - Optional metrics with a value of
Not Defined (X)
are skipped when iterating aVector
or converting aVector
to a string. See “Examples” below.
§Examples
Parse vector string:
// parse CVSS v2 vector string
let v2: Vector = "AV:N/AC:L/Au:N/C:C/I:C/A:C".parse()?;
// parse CVSS v3 vector string
let v3: Vector = "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H".parse()?;
// parse CVSS v4 vector string
let v4: Vector = "CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:H/VI:H/VA:H/SC:H/SI:H/SA:H".parse()?;
Iterate over vector metrics:
// parse CVSS v4 vector string
let v: Vector = "CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:H/VI:H/VA:H/SC:H/SI:H/SA:H".parse()?;
// print metrics
for m in v {
println!("metric: {m}");
}
Get metric from vector:
// parse CVSS v4 vector string
let v: Vector = "CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:H/VI:H/VA:H/SC:H/SI:H/SA:H".parse()?;
// get metric
let metric = v.get(Name::V4(v4::Name::AttackVector))?;
// check result
assert_eq!(metric, Metric::V4(v4::Metric::AttackVector(v4::AttackVector::Network)));
Get score for several vector strings:
// parse CVSS v2 vector string, get score
let v2: Vector = "AV:N/AC:L/Au:N/C:C/I:C/A:C".parse()?;
assert_eq!(Score::from(v2), Score::from(10.0));
// parse CVSS v3 vector string, get score
let v3: Vector = "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H".parse()?;
assert_eq!(Score::from(v3), Score::from(9.8));
// parse CVSS v4 vector string, get score
let v4: Vector = "CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:H/VI:H/VA:H/SC:H/SI:H/SA:H".parse()?;
assert_eq!(Score::from(v4), Score::from(10.0));
Get base score for several vector strings:
// parse CVSS v2 vector string, get base score
let v2: Vector = "AV:N/AC:L/Au:N/C:C/I:C/A:C".parse()?;
assert_eq!(v2.base_score(), Score::from(10.0));
// parse CVSS v3 vector string, get base score
let v3: Vector = "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H".parse()?;
assert_eq!(v3.base_score(), Score::from(9.8));
// parse CVSS v4 vector string, get base score
let v4: Vector = "CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:H/VI:H/VA:H/SC:H/SI:H/SA:H".parse()?;
assert_eq!(v4.base_score(), Score::from(10.0));
Show that the order of metrics within a vector string is not
preserved when parsing a vector string and then converting the
Vector
back to a string:
// vector string with first two metrics (AV and AC) swapped
let s = "CVSS:4.0/AC:L/AV:N/AT:N/PR:N/UI:N/VC:H/VI:H/VA:H/SC:H/SI:H/SA:H";
// expected result after parsing vector string above and converting
// the parsed vector back to a vector string
let exp = "CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:H/VI:H/VA:H/SC:H/SI:H/SA:H";
// parse vector string, then convert parsed vector back to vector string
let got = s.parse::<Vector>()?.to_string();
// check result
assert_eq!(got, exp);
Show that optional metrics with a value of Not Defined (X)
are
not preserved when parsing a vector string and then converting the
Vector
back to a string:
// vector string which contains an optional metric (MAV) with a
// value of `Not Defined (X)`
let s = "CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:H/VI:H/VA:H/SC:H/SI:H/SA:H/MAV:X";
// expected result after parsing vector string above and converting
// the parsed vector back to a vector string
let exp = "CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:H/VI:H/VA:H/SC:H/SI:H/SA:H";
// parse vector string, then convert parsed vector back to vector string
let got = s.parse::<Vector>()?.to_string();
// check result
assert_eq!(got, exp);
Verify that a Vector
is the same size as a u64
:
assert_eq!(size_of::<Vector>(), size_of::<u64>());
Verify that a v4::Vector
is the same size as one u64
:
assert_eq!(size_of::<v4::Vector>(), size_of::<u64>());
§Internal Representation
A Vector
is represented internally as a bit field
within a u64
, The lower 60 bits contain encoded metric
values, and the upper 4 bits contain the vector version:
A Vector
is represented internally as a bit
field within a u64
. Metric values are stored in the
lower 60 bits (bits 0..60
) and the CVSS version is stored in the
upper 4 bits (bits 60..64
):
Bit Range | Description |
---|---|
0..60 | Metric values. |
60..64 | CVSS version. |
The metric value encoding method is version-specific. See the version-specific vector representations for more information:
Implementations§
Source§impl Vector
impl Vector
Sourcepub fn get(self, name: Name) -> Result<Metric, Err>
pub fn get(self, name: Name) -> Result<Metric, Err>
Get Metric
from Vector
by Name
.
Returns Err::UnknownName
if there is a mismatch between the
version of the vector and the version of the given Name
.
§Example
Get metric from vector by name:
// parse CVSS v4 vector string
let v: Vector = "CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:H/VI:H/VA:H/SC:H/SI:H/SA:H".parse()?;
// get metric
let metric = v.get(Name::V4(v4::Name::AttackVector))?;
// check result
assert_eq!(metric, Metric::V4(v4::Metric::AttackVector(v4::AttackVector::Network)));
Example of error when there is a mismatch between the version of
the vector and the version of the given Name
:
// parse CVSS v3 vector string
let v: Vector = "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H".parse()?;
// try to get v4 metric from v3 vector
let got = v.get(Name::V4(v4::Name::AttackVector));
// check result
assert_eq!(got, Err(Err::UnknownName));
Sourcepub fn base_score(&self) -> Score
pub fn base_score(&self) -> Score
Get Vector
base score.
For CVSS v2 and CVSS v3 vectors this method returns the base score, excluding the effect of temporal and environmental metrics.
For CVSS v4 vectors this method returns the score.
§Example
Get base score for several vector strings:
// parse CVSS v2 vector string, get base score
let v2: Vector = "AV:N/AC:L/Au:N/C:C/I:C/A:C".parse()?;
assert_eq!(v2.base_score(), Score::from(10.0));
// parse CVSS v3 vector string, get base score
let v3: Vector = "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H".parse()?;
assert_eq!(v3.base_score(), Score::from(9.8));
// parse CVSS v4 vector string, get base score
let v4: Vector = "CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:H/VI:H/VA:H/SC:H/SI:H/SA:H".parse()?;
assert_eq!(v4.base_score(), Score::from(10.0));