Answer#
Downloading and compiling a crate is not passive. Two mechanisms run arbitrary code during the build:
- A build script (
build.rs) is a normal Rust program that Cargo builds and runs before compiling the crate. It exists for good reasons (compiling C code, generating bindings), but it can read files, use the network and start programs, with the same permissions as the user who typedcargo build. - A procedural macro is a crate that the compiler loads and runs while it expands your code.
The demonstration is harmless: a project whose build.rs only prints messages through Cargo's cargo::warning channel.
fn main() {
// A build script is an ordinary program. It runs on your machine, with your permissions, during `cargo build`.
let name = std::env::var("CARGO_PKG_NAME").unwrap();
println!("cargo::warning=build.rs is running for package {name}");
println!("cargo::warning=it can read your environment: PATH is set = {}", std::env::var_os("PATH").is_some());
}Running cargo build on it:
Compiling bs v0.1.0 (C:\...\bs)
warning: [email protected]: build.rs is running for package bs
warning: [email protected]: it can read your environment: PATH is set = true
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.63sThe messages appear while the project is built, before any program of yours is run. A malicious build script could do far more than print.
What to do about it#
- Treat every new dependency that has a
build.rsor is a proc-macro crate as code you are running; review it as described in the thread on reviewing a new dependency. - Do not build untrusted projects on a machine that holds secrets. Use a container, a virtual machine or a throw-away account, with no access to your SSH keys or tokens.
- Keep CI runners free of long-lived credentials, and give builds no network when the dependencies are vendored or cached (
cargo vendor,--offlineand--locked). - Pin versions with
Cargo.lockso a new release of a dependency does not run new code without review. - Be aware that the same applies to an IDE: opening a project can run
cargo check, which runs build scripts and macros. Open unknown projects in a safe environment first.
Common mistakes#
- Believing that
cargo checkis "just checking": it still runs build scripts and procedural macros. - Reviewing the library code but not
build.rs. - Giving the build environment production credentials "for convenience".
Related questions#
Can I list the crates in my tree that have a build script?#
Cargo has no single option for it. In the JSON from cargo metadata, a package with a build script has a target whose kind is custom-build (checked on the test project above). You can also look for build.rs in the sources that cargo vendor copies into one folder.
Does vendoring remove the risk?#
It removes surprise updates and the network at build time. The vendored code still runs when you build it, so it still needs review.
Sources#
- The Cargo Book, "Build Scripts": https://doc.rust-lang.org/cargo/reference/build-scripts.html
- The Rust Reference, "Procedural Macros": https://doc.rust-lang.org/reference/procedural-macros.html
- The Cargo Book,
cargo vendor: https://doc.rust-lang.org/cargo/commands/cargo-vendor.html