Answer#
The RustSec project keeps a public database of security advisories for crates published on crates.io. cargo audit downloads that database and checks the versions pinned in your Cargo.lock against it. It tells you what is affected, how serious it is, and which version fixes it.
cargo install cargo-audit --locked
cargo auditA project with no known problems prints a summary and exits with status 0:
Fetching advisory database from `https://github.com/RustSec/advisory-db.git`
Loaded 1617 security advisories (from C:\Users\...\.cargo\advisory-db)
Updating crates.io index
Scanning Cargo.lock for vulnerabilities (16 crate dependencies)(The number of advisories in the database changes constantly; the number here is what it held when this thread was checked.)
To show what a finding looks like, the next run is on a small throw-away project that pins time = "=0.1.45", a version with a real advisory (RUSTSEC-2020-0071). The output is shortened:
Scanning Cargo.lock for vulnerabilities (7 crate dependencies)
Crate: time
Version: 0.1.45
Title: Potential segfault in the time crate
Date: 2020-11-18
ID: RUSTSEC-2020-0071
URL: https://rustsec.org/advisories/RUSTSEC-2020-0071
Severity: 6.2 (medium)
Solution: Upgrade to >=0.2.23
error: 1 vulnerability found!cargo audit exits with a non-zero status when it finds a vulnerability, so a CI job fails by itself. The advisory text on rustsec.org says that the problem affects non-Windows targets; read the advisory, not just the title, to judge how it applies to you.
How to do it#
- Commit
Cargo.lockfor applications (see the thread onCargo.lock), because the audit reads it. - Run
cargo auditin CI on every push and on a daily or weekly schedule: an unchanged project can become vulnerable overnight. - Fix by updating:
cargo update -p <crate>for a compatible release, or raise the version inCargo.tomlfor a breaking one. - If a crate is unmaintained or yanked,
cargo auditreports it as a warning. Plan a replacement. - If you cannot fix an advisory yet, record the reason; see the thread on handling advisories you cannot fix.
Common mistakes#
- Auditing only before a release.
- Treating "no vulnerabilities found" as "safe". The database lists what has been reported.
- Ignoring warnings. Unmaintained crates often become vulnerabilities later.
Related questions#
Does it scan my own code?#
No. It checks dependencies by name and version. Use code review, tests, Clippy and fuzzing for your own code.
Where does the database come from?#
It is the RustSec advisory-db repository on GitHub, maintained by the Rust Secure Code working group and contributors.
Sources#
- RustSec: https://rustsec.org/
cargo-auditREADME: https://github.com/rustsec/rustsec/tree/main/cargo-audit- Advisory RUSTSEC-2020-0071: https://rustsec.org/advisories/RUSTSEC-2020-0071.html