use std::time::Duration;
use anyhow::{Context, Result};
use reqwest::header::{HeaderMap, HeaderValue, ACCEPT, CONTENT_TYPE, USER_AGENT};
use reqwest::{Client as HttpClient, StatusCode};
use serde::{Deserialize, Serialize};
use thiserror::Error;
use time::OffsetDateTime;
use crate::storage::Cache;
pub const OSV_API_BASE: &str = "https://api.osv.dev";
const TTL_OSV_QUERY: Duration = Duration::from_secs(6 * 3600);
#[derive(Debug, Error)]
pub enum OsvError {
#[error("osv returned {status}: {body}")]
Other { status: u16, body: String },
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PackageCoords {
pub name: String,
pub ecosystem: String,
pub version: String,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct OsvAdvisory {
pub id: String,
#[serde(default)]
pub summary: String,
#[serde(with = "time::serde::iso8601")]
pub modified: OffsetDateTime,
#[serde(default, with = "time::serde::iso8601::option")]
pub withdrawn: Option<OffsetDateTime>,
#[serde(default)]
pub severity: Vec<Severity>,
#[serde(default)]
pub affected: Vec<Affected>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Severity {
#[serde(rename = "type")]
pub kind: String,
pub score: String,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Affected {
#[serde(default)]
pub package: Option<PackageRef>,
#[serde(default)]
pub versions: Vec<String>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct PackageRef {
pub name: String,
pub ecosystem: String,
}
#[derive(Debug, Default, Deserialize)]
struct QueryResponse {
#[serde(default)]
vulns: Vec<OsvAdvisory>,
}
#[derive(Debug, Clone)]
pub struct Client {
http: HttpClient,
base_url: String,
cache: Cache,
}
impl Client {
#[must_use]
pub fn new(http: HttpClient, cache: Cache) -> Self {
Self {
http,
base_url: OSV_API_BASE.to_string(),
cache,
}
}
#[must_use]
pub fn with_base_url(mut self, url: impl Into<String>) -> Self {
self.base_url = url.into();
self
}
pub async fn query(&self, coords: &PackageCoords) -> Result<Vec<OsvAdvisory>> {
let cache_key = format!(
"osv:{}:{}:{}",
coords.ecosystem, coords.name, coords.version
);
let body = serde_json::json!({
"package": {
"name": coords.name,
"ecosystem": coords.ecosystem,
},
"version": coords.version,
});
let raw = self
.fetch_post_json(&cache_key, "/v1/query", &body, TTL_OSV_QUERY)
.await?;
let parsed: QueryResponse =
serde_json::from_slice(&raw).context("parse OSV /v1/query response")?;
let mut vulns: Vec<OsvAdvisory> = parsed
.vulns
.into_iter()
.filter(|v| v.withdrawn.is_none())
.collect();
vulns.sort_by(|a, b| a.id.cmp(&b.id));
Ok(vulns)
}
async fn fetch_post_json(
&self,
cache_key: &str,
path: &str,
body: &serde_json::Value,
ttl: Duration,
) -> Result<Vec<u8>> {
if let Some(entry) = self.cache.get(cache_key)? {
if !entry.is_stale() {
return Ok(entry.body.clone());
}
}
let url = format!("{}{}", self.base_url, path);
let mut headers = HeaderMap::new();
headers.insert(USER_AGENT, HeaderValue::from_static("repo-trust"));
headers.insert(ACCEPT, HeaderValue::from_static("application/json"));
headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
let resp = self
.http
.post(&url)
.headers(headers)
.json(body)
.send()
.await
.with_context(|| format!("POST {url}"))?;
match resp.status() {
StatusCode::OK => {
let body = resp.bytes().await?;
self.cache.put(cache_key, None, &body, ttl)?;
Ok(body.to_vec())
},
s => {
let body = resp.text().await.unwrap_or_default();
Err(OsvError::Other {
status: s.as_u16(),
body,
}
.into())
},
}
}
}