A payments team's first `cargo deny check` rejects an `MPL-2.0` crate and fails on an unmaintained advisory that `cargo audit` only warned about; why, and how do you tune deny.toml?
answer
- four checks, one config file
- an allow-list, not a deny-list
- unmaintained scope defaults to all
- exceptions, skip and ignore with reasons
basics
~20 scargo-deny's licenses check rejects any license not in [licenses] allow, and its advisories check errors on unmaintained advisories for every crate by default, where cargo-audit only warns. Add a per-crate license exception and a reasoned ignore, or narrow unmaintained.
solid answer
~50 s`cargo deny check` runs four checks configured in `deny.toml`: advisories, licenses, bans and sources. The **licenses** check is an allow-list: a crate whose license expression is not satisfied by `[licenses] allow` (or a per-crate `exceptions` entry) is rejected with "license is not explicitly allowed", so `MPL-2.0` fails until you allow it globally or for that crate. The **advisories** check reads the same RustSec database as cargo-audit, but a matching advisory that is not in `ignore` and falls within its scope is an error, and `unmaintained` defaults to scope `"all"`; set it to `"workspace"` to fail only for crates your own crates depend on directly, or ignore the ID with a `reason`. **Bans** reports duplicate versions as a warning by default (`multiple-versions = "warn"`) and denies crates you list; **sources** warns on unknown registries and git repositories.
go deeper
Recall that cargo deny check runs four checks from deny.toml: advisories, licenses, bans and sources.
Explain the license allow-list with per-crate exceptions, and that multiple-versions only warns until you set it to deny.
Diagnose the cargo-audit versus cargo-deny difference: unmaintained advisories error with scope all, and fix it with a scope change or a reasoned ignore.
Decide which licenses the organisation accepts, who signs off exceptions, and whether duplicates or unknown git sources should fail builds or only report.
## cargo-deny in one paragraph **cargo-deny** (crate `cargo-deny`, current release 0.20.2) is a subcommand that lints a project's *dependency graph* against a policy written in **`deny.toml`**. `cargo deny init` writes a template, and `cargo deny check` runs all four checks, or one at a time with `cargo deny check licenses`, `bans`, `advisories` or `sources`. The config file is found by walking up from the manifest directory, trying `deny.toml`, `.deny.toml` and `.cargo/deny.toml` in each directory. Every finding has a **lint level** (`deny` fails the check, `warn` reports it, `allow` stays quiet). ## Why the `MPL-2.0` crate is rejected The licenses check is an **allow-list**. For each crate it reads the SPDX license expression from the crate's `license` field (or, failing that, detects it from license files with a `confidence-threshold` that defaults to `0.8`). The expression passes only if the licenses in `[licenses] allow` satisfy it; otherwise cargo-deny reports the reason "license is not explicitly allowed" and the check fails. An `MPL-2.0` crate fails simply because nobody listed `MPL-2.0`. The template's `allow` list is empty, so a project has to state every license it accepts. Your options, from narrowest to widest: 1. **A per-crate exception**: `exceptions = [{ allow = ["MPL-2.0"], crate = "the-crate" }]` accepts that license for that crate only, which is usually what legal review signs off on. 2. **A global allowance**: add `"MPL-2.0"` to `allow` if the policy accepts it everywhere. 3. **A clarification**: `[[licenses.clarify]]` supplies the expression by hand for a crate whose metadata is missing or wrong, pinned to the hash of its license file so it lapses if the file changes. The opposite drift is caught too: an allowed license no crate uses is reported, at `unused-allowed-license = "warn"` by default. ## Why the unmaintained advisory fails here but not in cargo-audit Both tools read the RustSec advisory database, but they disagree on defaults: | Advisory type | cargo-audit 0.22 default | cargo-deny 0.20 default | |---|---|---| | Vulnerability | exit 1 | error | | Unmaintained | warning | error, scope `"all"` | | Unsound | warning | error, scope `"workspace"` | | Yanked version | warning | `yanked = "warn"` | In cargo-deny, an advisory that matches a locked version and is not in `ignore` is raised at the **deny** level, subject to the scopes described next. The `unmaintained` and `unsound` keys take a **scope** (`all`, `workspace`, `transitive`, `none`) that decides which crates may raise them: with `"workspace"`, only a crate that a workspace member depends on directly. So the unmaintained advisory that cargo-audit printed as a warning is an error in cargo-deny unless you: - set `unmaintained = "workspace"`, so transitive crates you do not choose do not block you, or - add `{ id = "RUSTSEC-...", reason = "..." }` to `[advisories] ignore`. Ignored advisories still print a note, and an ignore entry that no longer matches anything is reported (`unused-ignored-advisory`, warn by default), so stale exceptions surface. ## The other two checks **Bans** governs which crates may appear: - `multiple-versions` (default `"warn"`) reports two versions of one crate in the graph; set it to `"deny"` to fail, and use `skip` or `skip-tree` for duplicates you accept, each with a reason. - `deny = [{ crate = "openssl", reason = "rustls only" }]` bans a crate outright; `wrappers` allows it only as a direct dependency of named crates. - `wildcards` (default `"allow"`) can deny `*` version requirements. **Sources** checks where crates come from: `unknown-registry` and `unknown-git` default to `"warn"`, `allow-registry` defaults to the crates.io index, and `allow-git` lists trusted repositories, which matters for a payments service that must not build from an arbitrary fork. ## Scoping the graph `[graph]` decides which dependency graph is checked: `targets` restricts it to the target triples you ship (so a Windows-only crate does not fail a Linux service), and `all-features` or `features` decide which optional dependencies count. A check that runs with features different from the build can approve a graph you never ship. ## Tuning in practice - Start with `cargo deny init`, then make the first run green with explicit, commented exceptions rather than broad allowances. - Give every `ignore`, `skip` and `exceptions` entry a `reason`, so review sees why. - Keep `deny.toml` at the workspace root so one policy covers every member crate.
- Why might `cargo deny check bans` pass while the release binary still contains two versions of `syn`?`multiple-versions` defaults to `"warn"`, so duplicates are reported but the check does not fail. Set it to `"deny"` to make duplicates fatal, and list accepted ones under `skip` or `skip-tree` with a reason. Whether the duplicate can be removed is a question for `cargo tree -d` and the resolver.
- How do you stop a Windows-only dependency from failing the licenses check of a Linux-only service?List the triples you actually ship under `[graph] targets` in `deny.toml`. cargo-deny then prunes crates reachable only through dependencies for other targets, so their licenses, advisories and duplicates stop counting.
- cargo-deny accepts any license unless it is listed as denied
- cargo-deny's advisories check behaves exactly like cargo audit's defaults
- Duplicate crate versions fail cargo deny check bans by default
- Ignoring an advisory in deny.toml hides it from the output entirely
- cargo-deny compiles the project to find which dependencies are used