FAQ - Help and questions

What does cargo deny check, and how do I configure it?

#security#cargo-deny#supply-chain#licencespublished 0 replies0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail

Short answer

cargo-deny applies four checks from a deny.toml file: advisories (RustSec), licenses (an allow-list), bans (crates or duplicate versions you refuse) and sources (approved registries and Git hosts). Run it in CI beside cargo audit. A new project must set its allowed licences before it passes.

Checked with cargo-deny 0.20.2, rustc 1.93.0, Windows 11

Frequently asked question, written and answered by the Ruststudio team. Replies are open: corrections are welcome.

#1

Answer#

cargo audit answers one question: "does a dependency have a known advisory?". cargo deny is a policy tool that asks four:

CheckQuestion
advisoriesAre there vulnerabilities, unmaintained crates or yanked versions (the RustSec database)?
licensesIs every dependency under a licence you allow?
bansIs a crate that you refuse in the graph, or are there several versions of the same crate?
sourcesDoes 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:

deny.tomltoml
[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:

text
advisories ok, bans ok, licenses ok, sources ok

When 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#

  1. Install with cargo install cargo-deny --locked.
  2. Run cargo deny init, then set licenses.allow to the licences your legal policy accepts.
  3. Set sources so only crates.io (and any Git host you name) is allowed. That stops a pasted Git dependency from slipping in.
  4. Use bans to forbid crates you have decided against, and to see duplicate versions of one crate (multiple-versions = "warn").
  5. Run cargo deny check in CI. Use cargo deny check advisories as 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.

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#

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    No replies yet