A single leaked publishing token for RubyGems, NuGet or crates.io lets an attacker ship malicious code to everyone who installs the package, under the maintainer's name. These tokens are long-lived by default, often have full account scope, and live in CI secrets, developer home directories and old configuration files. The 2025 and 2026 package worms showed how fast stolen publishing credentials are turned into new malicious releases. This guide covers how each registry handles identity and ownership, the token scopes and expiry options available, trusted publishing as the replacement for stored keys, how leaked tokens are detected by GitHub, Gitleaks and TruffleHog, and a hardening baseline for maintainers and platform teams. npm is covered separately in the npm Tokens Security Guide and PyPI in the PyPI API Tokens and Trusted Publishing Security Guide.
Key takeaways
- Publishing tokens are signing keys in all but name. Whoever holds one can push a new version that downstream builds will trust and install automatically.
- All three registries now offer trusted publishing. RubyGems, NuGet.org and crates.io can exchange a CI provider's OIDC token for a short-lived publish credential, so no long-lived key has to be stored in CI.
- Where a token must exist, scope it. Restrict it to push only, to named packages or a name pattern, and give it an expiry; delete the RubyGems legacy full-access key.
- Detection coverage is uneven. GitHub secret scanning covers all three token types with push protection, Gitleaks has a RubyGems rule but none for NuGet API keys or crates.io tokens, and TruffleHog verifies RubyGems and NuGet keys but has no crates.io detector.
- Trusted publishing moves the risk, it does not remove it. A compromised repository or release workflow can still publish; protect the workflow with environments, reviewers and tag rules.
How identity and access work in RubyGems, NuGet and crates.io
Each registry ties packages to owner accounts, and owners authenticate API calls with tokens. The details differ in ways that matter for scoping and incident response.
| Aspect | RubyGems.org | NuGet.org | crates.io |
|---|---|---|---|
| Account sign-in | RubyGems account; MFA by TOTP or WebAuthn (passkeys, security keys), no SMS | Microsoft account linked to a NuGet.org profile; individual or organisation owners | GitHub OAuth login |
| Package ownership | Gem owners added with the add_owner operation | Package owners: users or organisations | Named owners (full rights) and GitHub team owners (publish and yank only, cannot change owners) |
| Token storage on clients | ~/.gem/credentials after gem signin | Passed to dotnet nuget push --api-key or stored in NuGet configuration | ~/.cargo/credentials.toml after cargo login, or CARGO_REGISTRY_TOKEN |
| Token prefix | rubygems_ followed by 48 hex characters | No documented vendor prefix | cio |
| Viewable after creation | No (stored with one-way encryption) | No (copy once, regenerate to see again) | No |
| Trusted publishing | GitHub Actions, rubygems/release-gem@v1 | GitHub Actions (NuGet/login@v1) and GitLab | GitHub Actions (rust-lang/crates-io-auth-action@v1), GitLab in public beta |
Owner accounts are the root of trust. Enforce MFA on every owner account, prefer organisation ownership on NuGet.org so packages survive staff changes, and keep at least two owners on critical packages so a single compromised or departed person cannot strand or hijack them. On crates.io, ownership follows GitHub identity, so GitHub account security is crates.io security.
Admin roles and privileged access
None of these registries has a rich role model; the privileged actions are owner changes, yanking and publishing new package names. Treat any credential that can perform them as privileged.
- RubyGems owner changes: the
add_ownerandremove_ownerscopes let a token change who controls a gem. An attacker who adds themselves as an owner keeps access after the original token is revoked, so check owner lists during incident response. - RubyGems MFA enforcement: set the account MFA level to "UI and API", which requires an OTP for sign-in, push, yank and owner changes. Add
rubygems_mfa_required = "true"to the gemspec metadata so every later version requires owners to have MFA enabled. - crates.io owners: named owners can add and remove other owners; GitHub team owners cannot. Prefer a small set of named owners plus a team for routine publishing, and remember that a team owner depends on the GitHub organisation's membership controls.
- NuGet.org organisations: organisation admins manage members and package ownership. Keep admins few, and note that a trusted publishing policy owned by an organisation becomes inactive if the creator leaves the organisation.
- Yank is not deletion: on crates.io, yanking stops new lockfiles selecting a version but existing lockfiles still use it, and Cargo's documentation is explicit that yanking does not remove an accidentally published secret. Rotate the secret instead.
Integrations, API tokens and service accounts
Token scopes and expiry
| Control | RubyGems.org | NuGet.org | crates.io |
|---|---|---|---|
| Publish-only scope | push_rubygem | Push only new package versions, or push new packages and versions | publish-update (existing crates) or publish-new |
| Other scopes | index_rubygems, yank_rubygem, add_owner, remove_owner, access_webhooks, show_dashboard (exclusive) | Unlist | yank, change-owners, trusted-publishing |
| Package restriction | One gem per key for push, yank and owner scopes; invalid if ownership is removed | Glob pattern such as fabrikam.service.*, which also matches new package IDs | Crate names, with a trailing * wildcard for prefixes |
| Expiry | Optional, at least 5 minutes ahead, cannot be changed later | Every key has an expiry; banner warning and email 10 days before | Optional expiry |
| Per-key MFA | Yes, OTP required for push, yank and owner changes on that key | No | No |
| Legacy risk | Legacy key migrated to all scopes: delete it | Older unscoped keys: replace with scoped keys | Older tokens created before scopes existed: replace |
A scope glob is wider than it looks. A NuGet key scoped to contoso.* can publish a brand new package called Contoso.Anything, which is exactly what an attacker wants for a convincing typosquat under a trusted prefix. Scope CI keys to "push only new package versions" of named packages wherever possible.
Trusted publishing
Trusted publishing replaces a stored key with an exchange: the CI job requests an OIDC token from GitHub or GitLab, the registry validates it against a policy naming the repository, workflow file and optional environment, and returns a short-lived credential.
- RubyGems: the job needs
id-token: write; the token can push only to gems that trust the publisher. A pending trusted publisher allows the very first release of a new gem without any API key. - NuGet.org: the temporary API key is valid for 1 hour and each OIDC token can be exchanged only once. Policies for private repositories start in a 7-day pending state and are then locked to the GitHub repository and owner IDs, which prevents a deleted repository being recreated under the same name to inherit the policy.
- crates.io: tokens expire after 30 minutes. The first publish of a crate still needs an API token, so create a short-expiry
publish-newtoken for that one release and delete it afterwards.
The OIDC subject is now the identity that matters, so protect it: use a dedicated release workflow triggered only by protected tags, a GitHub environment with required reviewers, and pinned third-party actions. In the Miasma and Hades Worms 2026, attackers pushed unreviewed commits and the projects' own OIDC publishing workflows released trojanised packages with valid provenance. See the GitHub Actions OIDC Federation Hardening Guide and the npm Trusted Publishing and Provenance Guide for the same pattern on npm.
Logging and detection
Detecting leaked tokens
| Scanner | RubyGems API key | NuGet API key | crates.io token |
|---|---|---|---|
| GitHub secret scanning | rubygems_api_key: partner programme, push protection, validity check | nuget_api_key: partner programme, push protection, validity check on GitHub.com (not GHES) | cratesio_api_token: partner programme, push protection, no validity check |
| Gitleaks default rules | rubygems-api-token | No API key rule; nuget-config-password catches clear-text passwords in NuGet.config | No rule |
| TruffleHog | Detector with live verification against the RubyGems API | Detector keyed on the word "nuget" with live verification | No detector |
Coverage as checked on 27 October 2026 against the GitHub pattern data, the Gitleaks default configuration and the TruffleHog detector set. The gaps matter: a crates.io token in a private GitLab repository will pass a default Gitleaks scan. Add custom rules for the cio prefix and for NuGet keys near a nuget or --api-key keyword, and test them against your own revoked tokens. See the Secret Scanner Rule Coverage Comparison Guide and the Git Secret Scanning Guide.
Through the GitHub partner programme, tokens found in public repositories are reported to the registry, which can revoke them. That protects you only for public GitHub content: package archives, container images, CI logs and other forges need your own scanning. Published packages themselves are a common leak path; the PyPI Secrets Exposure 2023 found thousands of secrets inside released Python packages.
Monitoring publishing activity
- Release notifications: crates.io recommends monitoring publishing through its email notifications; enable the equivalent owner notifications on RubyGems and NuGet.org and route them to a shared mailbox or ticket queue, not only to one maintainer.
- Version diffing: compare each published version with the tagged source; a release with no matching tag or CI run is a red flag.
- Owner changes: alert on new owners or removed owners on critical packages.
- CI logs: check that publish keys are masked and that no job prints the environment.
Metrics
- Share of critical packages published only through trusted publishing.
- Long-lived publish tokens in existence, and the share with an expiry and package restriction.
- Owner accounts with phishing-resistant MFA, and packages with at least two owners.
- Mean time from leak detection to token revocation.
- Releases without a matching signed tag or CI run (target: zero).
Hardening baseline
- Inventory: list every package your organisation publishes on each registry, its owners and every token or trusted publisher that can publish it. Search CI secrets,
~/.gem/credentials,~/.cargo/credentials.tomland NuGet configuration on build hosts. - Secure owner accounts: phishing-resistant MFA (WebAuthn on RubyGems, strong MFA on the Microsoft and GitHub accounts behind NuGet.org and crates.io); RubyGems MFA level "UI and API";
rubygems_mfa_requiredin gemspecs. - Pilot trusted publishing: move one low-risk package to trusted publishing with a protected release environment; confirm a tag-triggered release works end to end.
- Migrate and revoke: roll trusted publishing out to the remaining packages, then delete the stored tokens. Keep the old token for one release cycle only if rollback is needed, with a short expiry.
- Scope what remains: any token that must stay gets push-only scope, a package restriction and an expiry, and lives in a vault or CI secret store, not in a developer's home directory.
- Detect: enable secret scanning with push protection, add custom rules for the gaps above, and scan built packages before publishing.
- Rehearse: run a leak drill: revoke, check owners, yank or unlist affected versions, and publish a clean version.
For a single maintainer, steps 2, 3 and 6 deliver most of the value in an afternoon. For an organisation with hundreds of packages, ownership through registry organisations or GitHub teams, centrally managed release workflows and an inventory of trusted publisher policies matter more. See the Package Registry Security Comparison Guide.
Responding to a leaked publishing token
- Revoke first: delete or regenerate the token (on NuGet.org, regenerating stops the old key immediately and keeps its scope). Revoke every token held by the same account if the source of the leak is unknown.
- Check ownership: look for owners, organisation members or trusted publisher policies added since the leak window and remove them.
- Review releases: compare every version published in the window with tagged source; yank (RubyGems, crates.io) or unlist (NuGet.org) anything unexplained and publish a clean version.
- Tell consumers: publish an advisory with affected versions so downstream teams can pin or roll back. See the Malicious Open Source Package Response Playbook.
- Close the source: find where the token lived (CI variable, laptop, published archive) and replace it with trusted publishing.
Framework mapping
| Control | NIST SP 800-53 Rev. 5 | NIST CSF 2.0 | Threat addressed (ATT&CK) |
|---|---|---|---|
| Scoped, expiring tokens; trusted publishing | IA-5, AC-6 | PR.AA-01, PR.AA-05 | T1552.001 Credentials In Files |
| Owner MFA and ownership reviews | IA-2, AC-2 | PR.AA-05 | T1078 Valid Accounts |
| Release integrity and provenance | SR-4, SI-7 | PR.PS-06 | T1195.002 Compromise Software Supply Chain |
| Leak scanning and publish monitoring | AU-6, AU-12 | DE.CM-09 | T1195.001 Compromise Software Dependencies and Development Tools |
Common misconfigurations
- RubyGems legacy key still present: it carries every scope, including owner changes. Delete it, run
gem signouton every host that used it and sign in again with a scoped key. - Broad NuGet globs: a
*or company-prefix glob lets a stolen CI key publish new packages under your name. - Key on the command line:
dotnet nuget push --api-keywith a literal key ends up in shell history and CI logs; pass it from a masked secret. - Trusted publisher on a loose workflow: a policy that trusts any workflow run on any branch lets a pull request or compromised contributor publish. Name the workflow file, require an environment and trigger only on protected tags.
- Yanking instead of rotating: yanking a crate version that contained a token leaves the token valid and the source still downloadable by existing lockfiles.
- Assuming every publisher is trustworthy: the GemStuffer RubyGems Campaign 2026 showed mass-created accounts obtaining working API keys and flooding the registry, a reminder that consumers also need package allowlists and a proxy. See the Private Package Repository and Proxy Guide.
Practitioner checklist
- Inventory every published package, owner, token and trusted publisher across RubyGems, NuGet.org and crates.io.
- Require phishing-resistant MFA on all owner accounts and keep at least two owners on critical packages.
- Set RubyGems MFA to "UI and API" and add
rubygems_mfa_requiredto gemspecs. - Move CI publishing to trusted publishing with a named workflow, protected environment and tag-only trigger.
- Delete the RubyGems legacy key and any unscoped or non-expiring tokens.
- Scope remaining tokens to push only, named packages and a short expiry.
- Enable GitHub push protection and add custom scanner rules for crates.io and NuGet keys where your tools need them added.
- Route release and owner-change notifications to a monitored queue.
- Write and rehearse a token leak runbook: revoke, check owners, yank or unlist, republish.
Standards and references
- RubyGems Guides: API key scopes
- RubyGems Guides: Trusted Publishing
- RubyGems Guides: Setting up multi-factor authentication
- Microsoft Learn: Scoped API keys for NuGet.org
- Microsoft Learn: NuGet.org Trusted Publishing
- crates.io: Trusted Publishing
- The Cargo Book: Publishing on crates.io
- GitHub Docs: Supported secret scanning patterns
Related NHI Mgmt Group resources: npm Tokens Security Guide · PyPI API Tokens and Trusted Publishing Security Guide · Maven Central and Gradle Supply Chain Security Guide · Malicious Open Source Package Response Playbook