use super::date::{YEAR_MAX, YEAR_MIN};
use crate::error::{Error, ErrorKind};
use serde::{de::Error as DeError, Deserialize, Deserializer, Serialize, Serializer};
use std::{
fmt::{self, Display},
str::FromStr,
};
pub const PLACEHOLDER: &str = "RUSTSEC-0000-0000";
#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct Id {
kind: Kind,
year: Option<u32>,
string: String,
}
impl Id {
pub fn as_str(&self) -> &str {
self.string.as_ref()
}
pub fn kind(&self) -> Kind {
self.kind
}
pub fn is_placeholder(&self) -> bool {
self.string == PLACEHOLDER
}
pub fn is_rustsec(&self) -> bool {
self.kind == Kind::RUSTSEC
}
pub fn is_cve(&self) -> bool {
self.kind == Kind::CVE
}
pub fn is_ghsa(&self) -> bool {
self.kind == Kind::GHSA
}
pub fn is_other(&self) -> bool {
self.kind == Kind::Other
}
pub fn year(&self) -> Option<u32> {
self.year
}
pub fn url(&self) -> Option<String> {
match self.kind {
Kind::RUSTSEC => {
if self.is_placeholder() {
None
} else {
Some(format!("https://rustsec.org/advisories/{}", &self.string))
}
}
Kind::CVE => Some(format!(
"https://cve.mitre.org/cgi-bin/cvename.cgi?name={}",
&self.string
)),
Kind::TALOS => Some(format!(
"https://www.talosintelligence.com/reports/{}",
&self.string
)),
_ => None,
}
}
}
impl AsRef<str> for Id {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl Default for Id {
fn default() -> Id {
Id {
kind: Kind::RUSTSEC,
year: None,
string: PLACEHOLDER.into(),
}
}
}
impl Display for Id {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.string.fmt(f)
}
}
impl FromStr for Id {
type Err = Error;
fn from_str(advisory_id: &str) -> Result<Self, Error> {
if advisory_id == PLACEHOLDER {
return Ok(Id::default());
}
let kind = Kind::detect(advisory_id);
let year = match kind {
Kind::RUSTSEC | Kind::CVE | Kind::TALOS => Some(parse_year(advisory_id)?),
_ => None,
};
Ok(Self {
kind,
year,
string: advisory_id.into(),
})
}
}
impl Into<String> for Id {
fn into(self) -> String {
self.string
}
}
impl Serialize for Id {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(&self.string)
}
}
impl<'de> Deserialize<'de> for Id {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
Self::from_str(&String::deserialize(deserializer)?)
.map_err(|e| D::Error::custom(format!("{}", e)))
}
}
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub enum Kind {
RUSTSEC,
CVE,
GHSA,
TALOS,
Other,
}
impl Kind {
pub fn detect(string: &str) -> Self {
if string.starts_with("RUSTSEC-") {
Kind::RUSTSEC
} else if string.starts_with("CVE-") {
Kind::CVE
} else if string.starts_with("TALOS-") {
Kind::TALOS
} else if string.starts_with("GHSA-") {
Kind::GHSA
} else {
Kind::Other
}
}
}
fn parse_year(advisory_id: &str) -> Result<u32, Error> {
let mut parts = advisory_id.split('-');
parts.next().unwrap();
let year = match parts.next().unwrap().parse::<u32>() {
Ok(n) => match n {
YEAR_MIN..=YEAR_MAX => n,
_ => fail!(
ErrorKind::Parse,
"out-of-range year in advisory ID: {}",
advisory_id
),
},
_ => fail!(
ErrorKind::Parse,
"malformed year in advisory ID: {}",
advisory_id
),
};
if let Some(num) = parts.next() {
if num.parse::<u32>().is_err() {
fail!(ErrorKind::Parse, "malformed advisory ID: {}", advisory_id);
}
} else {
fail!(ErrorKind::Parse, "incomplete advisory ID: {}", advisory_id);
}
if parts.next().is_some() {
fail!(ErrorKind::Parse, "malformed advisory ID: {}", advisory_id);
}
Ok(year)
}
#[cfg(test)]
mod tests {
use super::{Id, Kind, PLACEHOLDER};
const EXAMPLE_RUSTSEC_ID: &str = "RUSTSEC-2018-0001";
const EXAMPLE_CVE_ID: &str = "CVE-2017-1000168";
const EXAMPLE_GHSA_ID: &str = "GHSA-4mmc-49vf-jmcp";
const EXAMPLE_TALOS_ID: &str = "TALOS-2017-0468";
const EXAMPLE_UNKNOWN_ID: &str = "Anonymous-42";
#[test]
fn rustsec_id_test() {
let rustsec_id = EXAMPLE_RUSTSEC_ID.parse::<Id>().unwrap();
assert!(rustsec_id.is_rustsec());
assert_eq!(rustsec_id.year().unwrap(), 2018);
assert_eq!(
rustsec_id.url().unwrap(),
"https://rustsec.org/advisories/RUSTSEC-2018-0001"
);
}
#[test]
fn rustsec_0000_0000_test() {
let rustsec_id = PLACEHOLDER.parse::<Id>().unwrap();
assert!(rustsec_id.is_rustsec());
assert!(rustsec_id.year().is_none());
assert!(rustsec_id.url().is_none());
}
#[test]
fn cve_id_test() {
let cve_id = EXAMPLE_CVE_ID.parse::<Id>().unwrap();
assert!(cve_id.is_cve());
assert_eq!(cve_id.year().unwrap(), 2017);
assert_eq!(
cve_id.url().unwrap(),
"https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2017-1000168"
);
}
#[test]
fn ghsa_id_test() {
let ghsa_id = EXAMPLE_GHSA_ID.parse::<Id>().unwrap();
assert!(ghsa_id.is_ghsa());
assert!(ghsa_id.year().is_none());
assert!(ghsa_id.url().is_none());
}
#[test]
fn talos_id_test() {
let talos_id = EXAMPLE_TALOS_ID.parse::<Id>().unwrap();
assert_eq!(talos_id.kind(), Kind::TALOS);
assert_eq!(talos_id.year().unwrap(), 2017);
assert_eq!(
talos_id.url().unwrap(),
"https://www.talosintelligence.com/reports/TALOS-2017-0468"
);
}
#[test]
fn other_id_test() {
let other_id = EXAMPLE_UNKNOWN_ID.parse::<Id>().unwrap();
assert!(other_id.is_other());
assert!(other_id.year().is_none());
assert!(other_id.url().is_none());
}
}