Safeguard
Security Guides•

Where does Cargo store your crates.io token and how do I protect it?

cargo login writes your token to credentials.toml. Where it lives, safer CI options with environment variables, revoking, and what to do after a leak.

Safeguard Security Team
3 min read

cargo login stores your crates.io token in credentials.toml inside CARGO_HOME, by default $HOME/.cargo. Treat the file as a secret. In CI, pass the token through an environment variable from your secret store, and revoke the token at once if it leaks.

Where tokens live

ItemDetail
File$CARGO_HOME/credentials.toml, written by cargo login and cleaned by cargo logout
Default location$HOME/.cargo on Unix, %USERPROFILE%\.cargo on Windows
crates.io token sectionThe registry table with a token key
Other registriesA named table per registry, each with its own token
Environment variable for crates.ioCARGO_REGISTRY_TOKEN
Environment for othersCARGO_REGISTRIES_NAME_TOKEN, with the name in uppercase

The configuration reference says a registry token should appear only in the credentials file, not in the normal config file that people commit.

Why it matters

A publish token lets whoever holds it release new versions of your crates. Every project that depends on you then pulls what they publish. That makes a token an entry point into your users' builds.

Protecting it on a laptop

  1. Create the token on the crates.io settings page and copy it once. It is not shown again.
  2. Run the login command and paste it.
  3. Restrict file permissions on the credentials file so only your account can read it.
  4. Keep CARGO_HOME out of any backup or sync folder that others can see.
  5. Run cargo logout on machines you no longer use for publishing.

Protecting it in CI

  • Store the token in your secret manager, and inject it as the environment variable only in the publish job.
  • Do not echo environment variables in logs.
  • Limit which branches or tags can reach the publish job.
  • Avoid putting a token in a repository config file, a Dockerfile or a build argument.
  • Prefer short lived credentials where your setup offers them. See crates.io trusted publishing for that route.

After a leak

  1. Revoke the token on the settings page immediately.
  2. Create a new one and replace it where used.
  3. Review recent versions and owners of your crates for anything you did not publish.
  4. Check CI logs and image layers where the token may have been printed.
  5. If a malicious version was published, yank it and notify users. Remember yanking does not delete code.

Try it yourself

Click through it at your own pace, or open it full screen. Look at how ongoing monitoring surfaces a change you did not make.

Quick check

Can I put the token in Cargo.toml?

No. Never put secrets in a manifest. Use the credentials file or an environment variable.

Does revoking a token undo versions it published?

No. It stops future publishes. You still need to yank bad versions and tell users.

Safeguard compliance and evidence automation can help record how publishing credentials are managed, 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.