The xz backdoor (CVE-2024-3094) took advantage of the fact that distributed "source" tarballs don't have to contain the same files as a public repository, and used bundled unit test files and an autogenerated script to hide malicious code that injected a backdoor.
Unfortunately, Cargo crates have a similar weakness. The package uploaded to crates.io can contain arbitrary files, and there's no guarantee these files come from any repository. Build scripts and proc macros are able to inject code into crates.
Crates-io crates are getting more scrutiny now (see also cargo-vet and cargo-crev), so please check your crates to ensure they're not containing any suspicious and difficult to review files. Make sure that their contents matches their public git repository, and that Cargo is able to include a valid commit hash with the upload.
Use include
in Cargo.toml
Add include = ["src/*.rs", "LICENSE", "etc"]
to your Cargo.toml
. Only package files that are necessary to build the crate. I recommend include
instead of exclude
, because it's usually easier to list needed files/directories than to catch all possible dot-files and temp files that may get dropped in the project directory.
(edited) If your tests need large data files or binary blobs, I recommend excluding such tests from the published packages. Besides adding even more bloat to downloads, now this is looking suspicious and will make reviewing crates even harder.
If you're bundling C source (in sys crates), try to minimize amount of vendored files too (but keep all the LICENSE files).
Publish from a clean git repo
Include the repository
property in the Cargo.toml
. If you've forked a crate, don't forget to point it to your forked URL.
When you run cargo publish
make sure there are no uncommitted "dirty" files, because it prevents Cargo from associating the package with a repo commit. When you publish, make sure to push this commit to the public repository, and ideally also give it a git tag (tools like cargo-release can automate that).
There is currently an ongoing effort to verify all Cargo crates against their repositories. Crates without a valid repository
link and a matching clean commit are problematic.
Keep an eye on issues
I've created a maintainer dashboard that comes with an RSS feed you can subscribe to to be notified about issues in your crates. When I finish scanning crates, I'll add warnings about untracked or mismatched files in crates.io packages.