Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow searching by CPE and provide CPE information #1149

Merged
merged 9 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ packageurl = "0.3.0"
parking_lot = "0.12"
peak_alloc = "0.2.0"
pem = "3"
petgraph = { version = "0.6.5", features = ["serde-1"] }
percent-encoding = "2.3.1"
petgraph = { version = "0.7.1", features = ["serde-1"] }
prometheus = "0.13.3"
quick-xml = "0.37.0"
rand = "0.8.5"
Expand Down
1 change: 1 addition & 0 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ log = { workspace = true }
native-tls = { workspace = true }
packageurl = { workspace = true }
pem = { workspace = true }
percent-encoding = { workspace = true }
JimFuller-RedHat marked this conversation as resolved.
Show resolved Hide resolved
postgresql_embedded = { workspace = true, features = ["blocking", "tokio"] }
regex = { workspace = true }
reqwest = { workspace = true, features = ["native-tls"] }
Expand Down
81 changes: 79 additions & 2 deletions common/src/cpe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,82 @@ use cpe::{
cpe::Cpe as _,
uri::{OwnedUri, Uri},
};
use std::fmt::{Debug, Display, Formatter};
use std::str::FromStr;
use serde::{
de::{Error, Visitor},
Deserialize, Deserializer, Serialize, Serializer,
};
use std::{
borrow::Cow,
fmt::{Debug, Display, Formatter},
str::FromStr,
};
use utoipa::{
openapi::{KnownFormat, ObjectBuilder, RefOr, Schema, SchemaFormat, Type},
PartialSchema, ToSchema,
};
use uuid::Uuid;

#[derive(Clone, Hash, Eq, PartialEq)]
pub struct Cpe {
uri: OwnedUri,
}

impl ToSchema for Cpe {
fn name() -> Cow<'static, str> {
"Cpe".into()
}
}

impl PartialSchema for Cpe {
fn schema() -> RefOr<Schema> {
ObjectBuilder::new()
.schema_type(Type::String)
.format(Some(SchemaFormat::KnownFormat(KnownFormat::Uri)))
.into()
}
}

impl Display for Cpe {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.uri, f)
}
}

impl Serialize for Cpe {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(self.to_string().as_str())
}
}

impl<'de> Deserialize<'de> for Cpe {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_str(CpeVisitor)
}
}

struct CpeVisitor;

impl Visitor<'_> for CpeVisitor {
type Value = Cpe;

fn expecting(&self, formatter: &mut Formatter) -> std::fmt::Result {
formatter.write_str("a CPE")
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: Error,
{
v.try_into().map_err(Error::custom)
}
}

const NAMESPACE: Uuid = Uuid::from_bytes([
0x1b, 0xf1, 0x2a, 0xd5, 0x0d, 0x67, 0x41, 0x18, 0xa1, 0x38, 0xb8, 0x9f, 0x19, 0x35, 0xe0, 0xa7,
]);
Expand Down Expand Up @@ -172,6 +233,22 @@ impl FromStr for Cpe {
}
}

impl TryFrom<&str> for Cpe {
type Error = <OwnedUri as FromStr>::Err;
fn try_from(value: &str) -> Result<Self, Self::Error> {
Ok(Self {
uri: OwnedUri::from_str(value)?,
})
}
}

impl TryFrom<String> for Cpe {
type Error = <OwnedUri as FromStr>::Err;
fn try_from(value: String) -> Result<Self, Self::Error> {
value.as_str().try_into()
}
}

pub trait CpeCompare: cpe::cpe::Cpe {
fn is_superset<O: CpeCompare>(&self, other: &O) -> bool {
self.compare(other).superset()
Expand Down
33 changes: 32 additions & 1 deletion common/src/purl.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use packageurl::PackageUrl;
use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS};
use serde::{
de::{Error, Visitor},
Deserialize, Deserializer, Serialize, Serializer,
Expand Down Expand Up @@ -115,6 +116,7 @@ impl FromStr for Purl {
.map_err(PurlErr::Package)
}
}

impl<'de> Deserialize<'de> for Purl {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
Expand All @@ -141,6 +143,35 @@ impl Visitor<'_> for PurlVisitor {
}
}

const QUERY_ENCODE_SET: &AsciiSet = &CONTROLS
.add(b' ') // Space must be encoded as %20 or +.
.add(b'"') // Double quote
.add(b'#') // Fragment identifier
.add(b'<') // Less than
.add(b'>') // Greater than
.add(b'[') // Left square bracket
.add(b']') // Right square bracket
.add(b'{') // Left curly brace
.add(b'}') // Right curly brace
.add(b'|') // Pipe
.add(b'\\') // Backslash
.add(b'^') // Caret
.add(b'`') // Backtick
.add(b'~') // Tilde
.add(b'@') // At sign
.add(b'!') // Exclamation mark
.add(b'$') // Dollar sign
.add(b'&') // Ampersand
.add(b'\'') // Single quote
.add(b'(') // Left parenthesis
.add(b')') // Right parenthesis
.add(b'*') // Asterisk
.add(b'+') // Plus
.add(b',') // Comma
.add(b';') // Semicolon
.add(b'=') // Equals
.add(b'%'); // Percent itself.

impl Display for Purl {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let ns = if let Some(ns) = &self.namespace {
Expand All @@ -156,7 +187,7 @@ impl Display for Purl {
"?{}",
self.qualifiers
.iter()
.map(|(k, v)| format!("{}={}", k, v))
.map(|(k, v)| format!("{}={}", k, utf8_percent_encode(v, QUERY_ENCODE_SET)))
.collect::<Vec<_>>()
.join("&")
)
Expand Down
Loading
Loading