Safeguard
Security Guides•

How do I review a build.rs file for safety in ten minutes?

A quick checklist for reviewing a Rust build script: normal behavior, red flags like network calls and writes outside OUT_DIR, and what to record.

Safeguard Security Team
3 min read

Read the build script top to bottom and ask what it touches: files, environment, network and processes. A legitimate build.rs compiles bundled C code, finds system libraries or generates code into OUT_DIR. Be wary of network access, writes outside OUT_DIR, shelling out to download tools, or reading secrets from the environment.

What a build script is

The Cargo reference describes a build.rs in a package root that Cargo compiles and runs just before building the package. It talks to Cargo by printing lines that start with the cargo prefix, and it can use its own build dependencies, listed separately from normal ones. It runs with your privileges, on your machine or CI runner, at compile time.

Normal behavior

BehaviorWhy it is expected
Compiling bundled C or C++ sourceMany crates wrap native libraries
Locating system librariesEmits link instructions
Generating Rust code into OUT_DIRThe documented place for generated files
Printing rerun-if instructionsTells Cargo when to run again
Setting cfg flagsFeature detection

Red flags

Red flagWhy it matters
Network requests or downloading binariesFetches code you never reviewed
Writing outside OUT_DIRThe docs say scripts should not modify files outside that directory, and Cargo checks packaged builds do not change sources
Spawning shell commands with built stringsCommand injection and surprise tools
Reading home directory, SSH or cloud credential filesNo build needs these
Dumping all environment variablesSecrets often live there
Obfuscated or encoded stringsNo honest build script needs them
A build dependency that seems unrelatedBuild dependencies run at build time too

The ten minute routine

  1. Check size. A large script deserves more time, a tiny one may be fine.
  2. Read the build dependency list in the manifest. Look up anything unfamiliar.
  3. Search the script for process spawning, network libraries and file operations.
  4. Confirm outputs go to OUT_DIR.
  5. Look at the rerun settings. Missing ones are a quality issue, odd ones may be a hiding place.
  6. Diff against the previous release when updating. A new build script, or a changed one, in a minor release is worth a closer look.
  7. Record your conclusion and the version you reviewed.

Reduce exposure

Build in a sandbox without credentials. Prefer vendored dependencies so a build cannot fetch surprises. Consider a vetting tool that records reviews, and a policy that new build scripts need a second reader.

Limits

A ten minute read catches obvious problems, not a skilled attacker. For crates in critical paths, take longer or use an independent audit.

Quick check

Does a build script run for dev dependencies?

Any crate in the graph that has a build script runs it when it is compiled, including those pulled in for tests.

Is a build script automatically suspicious?

No. Many legitimate crates use one. What matters is what it does.

Safeguard zero-day discovery and SBOM and dependency scanning help track new risks in dependencies, and a free scan is available at gold.safeguard.sh/scan.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.