Skip to content
Draft
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
1 change: 1 addition & 0 deletions crates/sourceorigin-standard/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "…" }`.
<!-- rules:end -->

Each rule is a variant of [`Rule`]: its name is the one `standard.toml` and
Expand Down
182 changes: 182 additions & 0 deletions crates/sourceorigin-standard/src/rules/deny_config.rs
Original file line number Diff line number Diff line change
@@ -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<Advisories>,
licenses: Option<Spanned<Licenses>>,
bans: Option<Spanned<Bans>>,
sources: Option<Spanned<Sources>>,
}

#[derive(Deserialize)]
struct Advisories {
#[serde(default)]
ignore: Vec<Spanned<toml::Value>>,
}

#[derive(Deserialize)]
struct Licenses {
#[serde(default)]
allow: Vec<toml::Value>,
}

#[derive(Deserialize)]
struct Bans {
wildcards: Option<Spanned<String>>,
}

#[derive(Deserialize)]
#[serde(rename_all = "kebab-case")]
struct Sources {
unknown_registry: Option<Spanned<String>>,
unknown_git: Option<Spanned<String>>,
}

/// A setting that must be `"deny"`.
struct Denied<'a> {
table: &'static str,
/// Where its table starts, if there is one.
table_at: Option<usize>,
key: &'static str,
value: Option<&'a Spanned<String>>,
}

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<DenyConfig> {
/// 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 = \"<why it doesn't apply, and what ends the \
ignore>\" }}`"
),
));
}
}
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(),
}
}
}
10 changes: 10 additions & 0 deletions crates/sourceorigin-standard/src/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -347,6 +354,8 @@ struct Repository {
commented: Vec<Commented>,
/// `.config/nextest.toml`, empty when missing.
nextest: TomlFile<NextestConfig>,
/// `deny.toml`.
deny: Option<TomlFile<DenyConfig>>,
}

impl Repository {
Expand All @@ -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,
Expand Down
29 changes: 29 additions & 0 deletions crates/sourceorigin-standard/src/rules/tests/deny_config.rs
Original file line number Diff line number Diff line change
@@ -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 = \"<why it doesn't apply, and what ends the \
ignore>\" }` (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);
}
1 change: 1 addition & 0 deletions crates/sourceorigin-standard/src/rules/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 6 additions & 1 deletion crates/sourceorigin-standard/src/rules/toml_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ impl<T: DeserializeOwned> TomlFile<T> {
impl<T> TomlFile<T> {
/// The line, from 1, where `spanned` starts.
pub(super) fn line<S>(&self, spanned: &Spanned<S>) -> 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)
}
}
4 changes: 2 additions & 2 deletions deny.toml
Original file line number Diff line number Diff line change
@@ -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]
Expand Down