FAQ - Help and questions

What should I check before adding a new crate to a Rust project?

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

Short answer

Check who maintains it and how it is used, what it pulls in with cargo tree, whether it has a build script or is a procedural macro, whether it uses unsafe, how recent its releases and advisories are, and whether a small amount of your own code would do instead. Every dependency runs with your program's permissions.

Checked with rustc 1.93.0, cargo 1.93.0, Windows 11

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

#1

Answer#

A dependency is code written by someone else that runs inside your process, often with access to everything your program can reach. Adding one is a trust decision. A short checklist makes that decision visible.

QuestionHow to look
What does it add?cargo tree -p <crate> after adding it, or cargo tree for the whole graph. Count new crates, not just one
Who is behind it?The repository, the owners on crates.io, how many people contribute, whether releases are regular
Is it used by others?Download counts and "used by" on crates.io are a signal of use, not of safety
Does it run code at build time?Look for a build.rs file and for proc-macro = true in its Cargo.toml (see the thread on build scripts)
Does it use unsafe?Search the source for unsafe; cargo geiger can count it. Some use is normal in low-level crates
Known problems?Search rustsec.org, then run cargo audit or cargo deny
FeaturesDisable default features you do not need: default-features = false and enable only what you use
LicenceCheck it fits your policy (cargo deny check licenses)
Do you need it?A dozen lines of your own code can be better than a dependency with fifty others

The tree of the test project in the thread on Cargo.lock has nine crates, though only two were named. That multiplication is typical, and it is why the number of new crates matters.

How to do it#

  1. Add the crate on a branch.
  2. Run cargo tree and look at what else arrived.
  3. Open the crate's repository and read its Cargo.toml, build.rs (if any) and the code that touches files, the network or processes.
  4. Run cargo audit or cargo deny check.
  5. Pin the result in Cargo.lock, and note in the pull request why the crate was chosen.
  6. Consider supply-chain tools that record review: cargo vet and cargo crev exist for sharing audits; check their documentation to see if they suit your team.

Common mistakes#

  • Adding a crate from a name seen in a tutorial without checking that it is the right one: look-alike names (typosquatting) exist on every package registry.
  • Taking the first search result of crates.io without reading what is behind it.
  • Never removing dependencies that are no longer used (cargo-udeps or cargo-machete can find them).

No. Popularity lowers some risks and raises others (it is a more attractive target). Treat it as one signal among several.

Should I copy code instead of adding a dependency?#

Only for small, well-understood pieces, and check the licence first. Copied code no longer receives fixes, so you take on that maintenance.

Sources#

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    No replies yet