Skip to Content
Course content

242: cargo-audit for Security Scanning

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Here is a situation I've run into more times than I'd like to admit: you've built a tool, it passes all your tests, and the logic is sound. You're feeling great. But beneath the surface, in your Cargo.lock file, you're carrying a "time bomb"—a dependency with a known security vulnerability (CVE) that could allow an attacker to crash your server or leak memory.

# Cargo.toml
[dependencies]
# Imagine we are using an older version of a crate for compatibility
# that has a known vulnerability in its parsing logic.
some-parsing-crate = "0.1.0" 
tokio = { version = "1.0", features = ["full"] }

If you run cargo check or cargo test, everything looks perfect. The compiler doesn't care if a crate has a security flaw; it only cares if the code is valid Rust. This is why "it works on my machine" is a dangerous phrase when it comes to security.

The invisible vulnerability in Cargo.lock

The problem is that your dependency tree is deep. You might not even be using some-parsing-crate directly; it could be a dependency of a dependency. Since Cargo.lock pins exact versions, you stay on that vulnerable version indefinitely unless you manually update or run cargo update. But even then, how do you know which versions are actually dangerous?

I used to manually check security mailing lists, which is a great way to waste your entire Tuesday. Instead, we use cargo-audit. This tool scans your Cargo.lock and compares it against the RustSec Advisory Database—a community-curated list of crates with reported vulnerabilities.

Automating the hunt with cargo-audit

First, you need to install the tool. It's not part of the standard Rust toolchain, so you'll grab it via cargo:

cargo install cargo-audit

Now, let's see what happens when we run it on our project:

$ cargo audit
Scanning Cargo.lock for vulnerabilities...
Found 1 vulnerability!

Crate: some-parsing-crate
Version: 0.1.0
Advisory: RUSTSEC-2023-XXXX
Title: Buffer overflow in parser
URL: https://rustsec.org/advisories/RUSTSEC-2023-XXXX
Keyword: [denial-of-service]
Fixed-in: 0.1.1

The output is explicit. It tells us exactly which crate is the problem, why it's a problem (Buffer overflow), and most importantly, which version fixes it (0.1.1). I usually check the URL provided in the output to understand if the vulnerability actually affects the way I'm using the crate, but in most cases, the rule is simple: update immediately.

To fix this, we update our dependencies. If we pinned the version in Cargo.toml, we update it there. If it's a transitive dependency, we can try:

cargo update -p some-parsing-crate

Running cargo audit again should now return a clean bill of health: Scanning Cargo.lock for vulnerabilities... All crates are up to date!

I highly recommend adding this to your CI/CD pipeline. There is nothing quite as humbling as having your build fail because a crate you haven't touched in six months suddenly got a security advisory. It's better to find out during a PR check than during a post-mortem meeting.




📋 Practical Task

Securing a Legacy Dependency Tree

In this exercise, you will simulate a security audit on a project with a known vulnerable dependency.

  • Create a new Rust project called security-scan-demo.
  • Add the following dependency to your Cargo.toml: # Use an old version of a crate known for vulnerabilities, such as an old version of `yum` or `nom` if available, or simply use a crate you know has a CVE history. For this exercise, use: scroll = "0.9.0" (which has known historical vulnerabilities).
  • Install cargo-audit using cargo install cargo-audit.
  • Run cargo audit and identify the specific RUSTSEC advisory ID associated with the vulnerability.
  • Update the scroll crate to the latest patched version in your Cargo.toml.
  • Run cargo audit again to verify that the vulnerability has been resolved.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.