polycvss
Rust library to parse and score CVSS vector strings.
Features:
- CVSS v2, CVSS v3, and CVSS v4 support.
- Version-agnostic parsing and scoring API.
- Memory efficient: Vectors are 8 bytes. Scores and severities are 1 byte.
- No dependencies by default except the standard library.
- Optional serde integration via the
serdebuild feature. - Extensive tests: Tested against thousands of vectors and scores from the NVD CVSS calculators.
Links:
Here is an example tool which parses the first command-line argument as a CVSS vector string, then prints the score and severity:
use ;
Here is the example tool output for a CVSS v2 vector string, a CVSS v3 vector string, and a CVSS v4 vector string:
# test with cvss v2 vector string
# test with cvss v3 vector string
# test with cvss v4 vector string
This example tool is included in the Git repository as
src/bin/cvss-score.rs.
Examples
Parse vector strings:
// 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?;
Get vector score:
// 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 score
let score = from;
// check result
assert_eq!;
Compare scores:
let a = from; // first score
let b = from; // second score
assert!; // compare scores
Get score severity:
let severity = from;
assert_eq!;
Compare severities:
let a = Low; // first severity
let b = High; // second severity
assert!; // compare severities
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?;
// check result
assert_eq!;
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
Convert a version-agnostic vector to a version-specific vector to access version-specific behavior:
// parse 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?;
// convert version-agnosic vector to a v4 vector
let v = from;
// get nomenclature
assert_eq!;
Install
polycvss package page on crates.io
Run cargo add polycvss to add polycvss as a dependency to an
exiting Rust project:
Run cargo install polycvss to install the example cvss-score tool:
# install cvss-score in cargo bin dir (e.g. `~/.cargo/bin`)
Build
Run cargo build to create a debug build of the example tool in
target/debug:
Run cargo build --release to create a release build of the example
tool in target/release:
You can also build the example cvss-score tool in a container using
Podman or Docker like this:
To build a static binary of the example cvss-score tool in a container:
Documentation
polycvss API documentation on docs.rs
Run cargo doc to build the API documentation locally in
target/doc/polycvss/:
Run cargo doc --lib build the library documentation and exclude the
example tool documentation:
# remove generated docs
# (needed to clean up stale artifacts)
# generate library-only docs
Tests
Use cargo test to run the test suite:
; ; ; ; ;
Use cargo clippy to run the linter:
)
The test suite includes a large number of scored CVSS vector string test cases. The test cases were generated using cvss-calcs.
The generated test cases can be found in src/v3.rs,
src/v3.rs, and src/v4.rs.