Answer#
cargo audit answers one question: "does a dependency have a known advisory?". cargo deny is a policy tool that asks four:
| Check | Question |
|---|---|
advisories | Are there vulnerabilities, unmaintained crates or yanked versions (the RustSec database)? |
licenses | Is every dependency under a licence you allow? |
bans | Is a crate that you refuse in the graph, or are there several versions of the same crate? |
sources | Does every crate come from a registry or repository you approved? |
Start with cargo deny init, which writes a commented deny.toml, then edit it. This is a small file that works for a project whose dependencies are MIT or Apache-2.0:
[advisories]
version = 2
[licenses]
allow = ["MIT", "Apache-2.0"]
[bans]
multiple-versions = "warn"
wildcards = "deny"
[sources]
unknown-registry = "deny"
unknown-git = "deny"Then run the checks. cargo deny check prints one line per area when everything passes:
advisories ok, bans ok, licenses ok, sources okWhen a licence is not on the allow-list, the check fails and names the crate and the licence. That is what happened with a freshly created deny.toml in the test project: the default list is empty, so the first run rejected the crates until the allow-list was filled in. The project's own Cargo.toml also needs a license field, or the check reports the project as unlicensed.
How to do it#
- Install with
cargo install cargo-deny --locked. - Run
cargo deny init, then setlicenses.allowto the licences your legal policy accepts. - Set
sourcesso only crates.io (and any Git host you name) is allowed. That stops a pasted Git dependency from slipping in. - Use
bansto forbid crates you have decided against, and to see duplicate versions of one crate (multiple-versions = "warn"). - Run
cargo deny checkin CI. Usecargo deny check advisoriesas a separate, scheduled job, because advisories change without code changes.
Common mistakes#
- Copying a long allow-list without reading it. Each licence is a decision.
- Turning every warning into an ignore entry. See the thread on handling advisories.
- Running it only on developer machines.
Related questions#
Do I need both cargo audit and cargo deny?#
cargo deny check advisories reads the same database as cargo audit, so it can replace it. Many teams use cargo deny alone for all four areas.
Where do I run it in CI?#
As a separate step after the build, with cargo deny check. Run the advisories check on a schedule too, since new advisories appear without code changes.
Sources#
- cargo-deny book: https://embarkstudios.github.io/cargo-deny/
- cargo-deny, checks: https://embarkstudios.github.io/cargo-deny/checks/index.html
- SPDX licence list: https://spdx.org/licenses/