use serde::{Deserialize, Serialize};
use super::signing::{self, PublicKey, VersionRule};
use crate::http::{HttpRequest, HttpTransport};
use crate::{Error, Result};
pub const FILE: &str = "security.json";
const MAX_BYTES: usize = 256 * 1024;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Advisory {
pub id: String,
pub affected: String,
#[serde(default)]
pub fixed: Option<String>,
pub severity: String,
pub url: String,
pub summary: String,
}
impl Advisory {
pub fn severity_rank(&self) -> u8 {
match self.severity.trim().to_ascii_lowercase().as_str() {
"critical" => 3,
"high" => 2,
"medium" => 1,
_ => 0,
}
}
pub fn affects(&self, running: &str) -> bool {
let Ok(req) = semver::VersionReq::parse(&self.affected) else {
return false;
};
let Ok(version) = semver::Version::parse(running.trim().trim_start_matches(['v', 'V']))
else {
return false;
};
let mut plain = version.clone();
plain.build = semver::BuildMetadata::EMPTY;
if req.matches(&plain) {
return true;
}
if !plain.pre.is_empty() {
plain.pre = semver::Prerelease::EMPTY;
return req.matches(&plain);
}
false
}
pub fn safe_url(&self) -> Option<&str> {
let url = self.url.trim();
(url.starts_with("https://")
&& url.len() > "https://".len()
&& !url.contains(char::is_whitespace))
.then_some(url)
}
}
pub fn affecting(advisories: &[Advisory], running: &str) -> Vec<Advisory> {
let mut hit: Vec<Advisory> = advisories
.iter()
.filter(|a| a.affects(running))
.cloned()
.collect();
hit.sort_by(|a, b| {
b.severity_rank()
.cmp(&a.severity_rank())
.then_with(|| a.id.cmp(&b.id))
});
hit
}
#[derive(Deserialize)]
struct File {
#[serde(default)]
advisories: Vec<Advisory>,
}
pub fn parse(body: &[u8]) -> Result<Vec<Advisory>> {
let file: File = serde_json::from_slice(body).map_err(|e| Error::Decode(e.to_string()))?;
Ok(file.advisories)
}
pub async fn fetch(
transport: &dyn HttpTransport,
url: &str,
running: &str,
keys: &[PublicKey],
) -> Result<Vec<Advisory>> {
let go = |_: u64, _: Option<u64>| true;
let body = transport
.download(HttpRequest::get(url.to_string()), &go)
.await?;
if body.len() > MAX_BYTES {
return Err(Error::Decode("the security notices are too large".into()));
}
let sig = transport
.download(HttpRequest::get(format!("{url}.minisig")), &go)
.await
.map_err(|e| Error::Decode(format!("the security notices have no signature: {e}")))?;
signing::verify_with(
&body,
&String::from_utf8_lossy(&sig),
FILE,
VersionRule::NotOlderThan(running),
keys,
)
.map_err(|e| Error::Decode(format!("the security notices are not trusted: {e}")))?;
parse(&body)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::http::RecordedTransport;
use gitpoulpi_testkit::signing::TestSigner;
fn advisory(id: &str, affected: &str, severity: &str) -> Advisory {
Advisory {
id: id.into(),
affected: affected.into(),
fixed: Some("1.4.3".into()),
severity: severity.into(),
url: "https://example.test/a".into(),
summary: "s".into(),
}
}
fn block<F: std::future::Future>(f: F) -> F::Output {
tokio::runtime::Builder::new_current_thread()
.build()
.unwrap()
.block_on(f)
}
#[test]
fn a_range_matches_the_versions_inside_it_only() {
let a = advisory("GPSA-2026-001", ">=1.2.0, <1.4.3", "high");
for (v, hit) in [
("1.1.9", false),
("1.2.0", true),
("1.4.2", true),
("v1.4.2", true),
("1.4.3", false),
("2.0.0", false),
("1.4.2+build5", true),
("1.3.0-rc.1", true),
("not a version", false),
] {
assert_eq!(a.affects(v), hit, "{v}");
}
assert!(!advisory("x", "garbage", "low").affects("1.0.0"));
}
#[test]
fn matching_notices_are_listed_most_severe_first() {
let list = vec![
advisory("GPSA-2026-001", "<2.0.0", "low"),
advisory("GPSA-2026-002", "<2.0.0", "critical"),
advisory("GPSA-2026-003", ">=3.0.0", "critical"),
advisory("GPSA-2026-004", "<2.0.0", "high"),
];
let ids: Vec<String> = affecting(&list, "1.0.0")
.into_iter()
.map(|a| a.id)
.collect();
assert_eq!(ids, ["GPSA-2026-002", "GPSA-2026-004", "GPSA-2026-001"]);
assert!(affecting(&list, "2.5.0").is_empty());
}
#[test]
fn only_https_links_are_offered() {
let mut a = advisory("x", "*", "low");
assert!(a.safe_url().is_some());
a.url = "javascript:alert(1)".into();
assert!(a.safe_url().is_none());
a.url = "http://example.test".into();
assert!(a.safe_url().is_none());
}
const BODY: &str = r#"{"schema":1,"advisories":[{"id":"GPSA-2026-001","affected":"<2.0.0",
"fixed":"2.0.0","severity":"high","url":"https://example.test/a","summary":"bad"}]}"#;
fn served(signer: &TestSigner, signed_for: &str) -> RecordedTransport {
RecordedTransport::new()
.on_file(
"security.json.minisig",
signer.sign(FILE, signed_for, BODY.as_bytes()).into_bytes(),
)
.on_file("security.json", BODY.as_bytes().to_vec())
}
#[test]
fn a_signed_file_is_read() {
let signer = TestSigner::new();
let keys = vec![signing::parse_key(&signer.public_key_text()).unwrap()];
let got = block(fetch(
&served(&signer, "1.5.0"),
"https://x.example/security.json",
"1.4.0",
&keys,
))
.unwrap();
assert_eq!(got.len(), 1);
assert_eq!(got[0].id, "GPSA-2026-001");
}
#[test]
fn an_unsigned_badly_signed_or_replayed_file_is_never_read() {
let signer = TestSigner::new();
let keys = vec![signing::parse_key(&signer.public_key_text()).unwrap()];
let url = "https://x.example/security.json";
let t = RecordedTransport::new().on_file("security.json", BODY.as_bytes().to_vec());
assert!(block(fetch(&t, url, "1.4.0", &keys)).is_err());
let stranger = TestSigner::new();
assert!(block(fetch(&served(&stranger, "1.5.0"), url, "1.4.0", &keys)).is_err());
assert!(block(fetch(&served(&signer, "1.3.0"), url, "1.4.0", &keys)).is_err());
let t = RecordedTransport::new()
.on_file(
"security.json.minisig",
signer.sign(FILE, "1.5.0", BODY.as_bytes()).into_bytes(),
)
.on_file("security.json", br#"{"advisories":[]}"#.to_vec());
assert!(block(fetch(&t, url, "1.4.0", &keys)).is_err());
}
}