From a44caf25ebc17162fa5c647baf3afedb73f55d75 Mon Sep 17 00:00:00 2001 From: piquel Date: Sun, 22 Nov 2026 21:32:33 +0100 Subject: [PATCH] feat: deny-config denies unknown sources and wildcards, and needs a reason for each ignored advisory --- crates/sourceorigin-standard/README.md | 1 + .../src/rules/deny_config.rs | 182 ++++++++++++++++++ crates/sourceorigin-standard/src/rules/mod.rs | 10 + .../src/rules/tests/deny_config.rs | 29 +++ .../src/rules/tests/mod.rs | 1 + .../src/rules/toml_file.rs | 7 +- deny.toml | 4 +- 7 files changed, 231 insertions(+), 3 deletions(-) create mode 100644 crates/sourceorigin-standard/src/rules/deny_config.rs create mode 100644 crates/sourceorigin-standard/src/rules/tests/deny_config.rs diff --git a/crates/sourceorigin-standard/README.md b/crates/sourceorigin-standard/README.md index 8accb85..f456da3 100644 --- a/crates/sourceorigin-standard/README.md +++ b/crates/sourceorigin-standard/README.md @@ -47,6 +47,7 @@ A repository adopts the standard in four steps: - `clippy-config`: `clippy.toml` sets `allow-expect-in-tests`, `allow-panic-in-tests` and `allow-dbg-in-tests` to `true`, and never `allow-unwrap-in-tests`. - `nextest-config`: `.config/nextest.toml`'s default profile sets a `slow-timeout`, every `slow-timeout` in it has a `terminate-after`, and no profile or override sets `retries`. - `workspace-dependencies`: Every dependency of a workspace crate, of every kind and target, is inherited with `workspace = true` from the root's `[workspace.dependencies]`, where each `path` dependency also has a `version`. +- `deny-config`: `deny.toml` sets `unknown-registry` and `unknown-git` under `[sources]`, and `wildcards` under `[bans]`, to `"deny"`, allows at least one license, and gives every ignored advisory a `reason`: `{ id = "…", reason = "…" }`. Each rule is a variant of [`Rule`]: its name is the one `standard.toml` and diff --git a/crates/sourceorigin-standard/src/rules/deny_config.rs b/crates/sourceorigin-standard/src/rules/deny_config.rs new file mode 100644 index 0000000..8096ac5 --- /dev/null +++ b/crates/sourceorigin-standard/src/rules/deny_config.rs @@ -0,0 +1,182 @@ +//! `deny-config`: cargo-deny checks where crates come from, what they +//! require, and their licenses, and says why it ignores an advisory. + +use serde::Deserialize; +use toml::Spanned; + +use super::fix::{Checked, Fix}; +use super::toml_file::TomlFile; +use super::{Finding, Repository}; + +/// cargo-deny's configuration, from the repository's root. +pub(super) const PATH: &str = "deny.toml"; + +/// `deny.toml`, reduced to what the rule reads. +#[derive(Deserialize)] +pub(super) struct DenyConfig { + advisories: Option, + licenses: Option>, + bans: Option>, + sources: Option>, +} + +#[derive(Deserialize)] +struct Advisories { + #[serde(default)] + ignore: Vec>, +} + +#[derive(Deserialize)] +struct Licenses { + #[serde(default)] + allow: Vec, +} + +#[derive(Deserialize)] +struct Bans { + wildcards: Option>, +} + +#[derive(Deserialize)] +#[serde(rename_all = "kebab-case")] +struct Sources { + unknown_registry: Option>, + unknown_git: Option>, +} + +/// A setting that must be `"deny"`. +struct Denied<'a> { + table: &'static str, + /// Where its table starts, if there is one. + table_at: Option, + key: &'static str, + value: Option<&'a Spanned>, +} + +impl DenyConfig { + /// The settings that must be `"deny"`. + fn denied(&self) -> [Denied<'_>; 3] { + let sources = self.sources.as_ref(); + let bans = self.bans.as_ref(); + let sources_at = sources.map(|sources| sources.span().start); + [ + Denied { + table: "sources", + table_at: sources_at, + key: "unknown-registry", + value: sources.and_then(|sources| sources.get_ref().unknown_registry.as_ref()), + }, + Denied { + table: "sources", + table_at: sources_at, + key: "unknown-git", + value: sources.and_then(|sources| sources.get_ref().unknown_git.as_ref()), + }, + Denied { + table: "bans", + table_at: bans.map(|bans| bans.span().start), + key: "wildcards", + value: bans.and_then(|bans| bans.get_ref().wildcards.as_ref()), + }, + ] + } +} + +impl TomlFile { + /// Each setting that must be `"deny"` and isn't, fixed by setting it, + /// an empty license allowlist, and each ignored advisory without a + /// `reason`. + fn checked(&self) -> Checked { + let mut checked = Checked::default(); + let finding = |line, message: String| Finding { + path: PATH.into(), + line, + message, + }; + for Denied { + table, + table_at, + key, + value, + } in self.value.denied() + { + let setting = format!("{key} = \"deny\""); + let line = match value { + Some(value) if value.get_ref() == "deny" => continue, + Some(value) => self.line(value), + None => table_at.map_or(1, |offset| self.line_at(offset)), + }; + checked.findings.push(finding( + line, + format!("`[{table}]` doesn't deny `{key}`: set `{setting}`"), + )); + checked.fixes.push(Fix::new( + PATH, + format!("set `{setting}`"), + move |document| { + if document.get(table).is_none() { + document[table] = toml_edit::table(); + } + let item = &mut document[table][key]; + if item.is_none() { + *item = toml_edit::value("deny"); + } else { + Fix::set(item, "deny"); + } + }, + )); + } + let licenses = self.value.licenses.as_ref(); + if licenses.is_none_or(|licenses| licenses.get_ref().allow.is_empty()) { + checked.findings.push(finding( + licenses.map_or(1, |licenses| self.line(licenses)), + "`[licenses]` allows no license: list the licenses the repository accepts in \ + `allow`" + .to_owned(), + )); + } + let ignored = self + .value + .advisories + .iter() + .flat_map(|advisories| &advisories.ignore); + for advisory in ignored { + let reason = advisory + .get_ref() + .get("reason") + .and_then(toml::Value::as_str); + if reason.is_none_or(|reason| reason.trim().is_empty()) { + let id = advisory + .get_ref() + .get("id") + .or(Some(advisory.get_ref())) + .and_then(toml::Value::as_str) + .unwrap_or_default(); + checked.findings.push(finding( + self.line(advisory), + format!( + "the ignored advisory `{id}` gives no reason: write it as \ + `{{ id = \"{id}\", reason = \"\" }}`" + ), + )); + } + } + checked + } +} + +impl Repository { + /// What `deny.toml` lacks of the standard, or a missing `deny.toml`. + pub(super) fn deny_config(&self) -> Checked { + match &self.deny { + Some(deny) => deny.checked(), + None => vec![Finding { + path: PATH.into(), + line: 1, + message: format!("no {PATH}: configure cargo-deny there, which `just audit` runs"), + }] + .into(), + } + } +} diff --git a/crates/sourceorigin-standard/src/rules/mod.rs b/crates/sourceorigin-standard/src/rules/mod.rs index 5cf4e82..3eac1c3 100644 --- a/crates/sourceorigin-standard/src/rules/mod.rs +++ b/crates/sourceorigin-standard/src/rules/mod.rs @@ -11,6 +11,7 @@ mod cargo_locked; mod clippy_config; mod comments; mod config_comments; +mod deny_config; mod fix; mod generated_lints; mod layout; @@ -41,6 +42,7 @@ use crate::workspace::Workspace; use agent_files::AgentFiles; use budget::Budget; use config_comments::Commented; +use deny_config::DenyConfig; use fix::Checked; pub use fix::Fix; use layout::CargoConfig; @@ -174,6 +176,11 @@ rules! { /// `[workspace.dependencies]`, where each `path` dependency also has a /// `version`. WorkspaceDependencies = "workspace-dependencies" => workspace_dependencies, + /// `deny.toml` sets `unknown-registry` and `unknown-git` under + /// `[sources]`, and `wildcards` under `[bans]`, to `"deny"`, allows at + /// least one license, and gives every ignored advisory a `reason`: + /// `{ id = "…", reason = "…" }`. + DenyConfig = "deny-config" => deny_config, } /// Why the rules couldn't be checked. @@ -347,6 +354,8 @@ struct Repository { commented: Vec, /// `.config/nextest.toml`, empty when missing. nextest: TomlFile, + /// `deny.toml`. + deny: Option>, } impl Repository { @@ -371,6 +380,7 @@ impl Repository { scripts: TextFile::load_all(root, &cargo_locked::FILES)?, agent_files: AgentFiles::load(root)?, nextest: TomlFile::load_or_empty(root, nextest_config::PATH)?, + deny: TomlFile::load(root, deny_config::PATH)?, metadata, sources, unparsed, diff --git a/crates/sourceorigin-standard/src/rules/tests/deny_config.rs b/crates/sourceorigin-standard/src/rules/tests/deny_config.rs new file mode 100644 index 0000000..6ca8ac0 --- /dev/null +++ b/crates/sourceorigin-standard/src/rules/tests/deny_config.rs @@ -0,0 +1,29 @@ +use super::{Fixture, INHERITS}; +use crate::rules::Rule; + +#[test] +fn cargo_deny_denies_unknown_sources_and_wildcards_and_explains_ignores() { + let fixture = Fixture::new(); + fixture.add_crate("core", INHERITS).write( + "deny.toml", + "[advisories]\nignore = [\n \"RUSTSEC-2026-0001\",\n \ + { id = \"RUSTSEC-2026-0002\", reason = \"unreachable\" },\n]\n\n[licenses]\nallow = []\n\n\ + [bans]\nwildcards = \"warn\"\n\n[sources]\nunknown-registry = \"deny\"\n", + ); + assert_eq!( + fixture.violations(Rule::DenyConfig), + [ + "deny.toml:3: the ignored advisory `RUSTSEC-2026-0001` gives no reason: write it as \ + `{ id = \"RUSTSEC-2026-0001\", reason = \"\" }` (deny-config)", + "deny.toml:7: `[licenses]` allows no license: list the licenses the repository \ + accepts in `allow` (deny-config)", + "deny.toml:11: `[bans]` doesn't deny `wildcards`: set `wildcards = \"deny\"` \ + (deny-config)", + "deny.toml:13: `[sources]` doesn't deny `unknown-git`: set `unknown-git = \"deny\"` \ + (deny-config)", + ] + ); + fixture.fix_rule(Rule::DenyConfig); + assert_eq!(fixture.violations(Rule::DenyConfig).len(), 2); +} diff --git a/crates/sourceorigin-standard/src/rules/tests/mod.rs b/crates/sourceorigin-standard/src/rules/tests/mod.rs index 4f2f5ff..00a9786 100644 --- a/crates/sourceorigin-standard/src/rules/tests/mod.rs +++ b/crates/sourceorigin-standard/src/rules/tests/mod.rs @@ -7,6 +7,7 @@ mod cargo_locked; mod clippy_config; mod comments; mod config_comments; +mod deny_config; mod fix; mod framework; mod generated_lints; diff --git a/crates/sourceorigin-standard/src/rules/toml_file.rs b/crates/sourceorigin-standard/src/rules/toml_file.rs index 7dd5b68..1e5ced6 100644 --- a/crates/sourceorigin-standard/src/rules/toml_file.rs +++ b/crates/sourceorigin-standard/src/rules/toml_file.rs @@ -64,6 +64,11 @@ impl TomlFile { impl TomlFile { /// The line, from 1, where `spanned` starts. pub(super) fn line(&self, spanned: &Spanned) -> usize { - line_of(&self.text, spanned.span().start) + self.line_at(spanned.span().start) + } + + /// The line, from 1, holding byte `offset`. + pub(super) fn line_at(&self, offset: usize) -> usize { + line_of(&self.text, offset) } } diff --git a/deny.toml b/deny.toml index 29cdeb9..c1e280b 100644 --- a/deny.toml +++ b/deny.toml @@ -1,7 +1,7 @@ [advisories] version = 2 -# Each ignored advisory gets a comment saying why it doesn't apply and what -# ends the ignore. +# Each ignored advisory is `{ id = "…", reason = "…" }`, the reason saying +# why it doesn't apply and what ends the ignore. ignore = [] [licenses]