Join our Newsletter — 33% off our NHI Course
Home› Guides› RubyGems, NuGet and Crates.io Publishing Tokens Guide: Scope,…
Guide Secrets Management

RubyGems, NuGet and Crates.io Publishing Tokens Guide: Scope, Detect and Replace Publish Keys

PDF for newsletter subscribers · free to read online

← All guides
By NHI Mgmt Group, edited by Lalit Choda Updated 8 November 2026 13 min read
On this page

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.

AspectRubyGems.orgNuGet.orgcrates.io
Account sign-inRubyGems account; MFA by TOTP or WebAuthn (passkeys, security keys), no SMSMicrosoft account linked to a NuGet.org profile; individual or organisation ownersGitHub OAuth login
Package ownershipGem owners added with the add_owner operationPackage owners: users or organisationsNamed owners (full rights) and GitHub team owners (publish and yank only, cannot change owners)
Token storage on clients~/.gem/credentials after gem signinPassed to dotnet nuget push --api-key or stored in NuGet configuration~/.cargo/credentials.toml after cargo login, or CARGO_REGISTRY_TOKEN
Token prefixrubygems_ followed by 48 hex charactersNo documented vendor prefixcio
Viewable after creationNo (stored with one-way encryption)No (copy once, regenerate to see again)No
Trusted publishingGitHub Actions, rubygems/release-gem@v1GitHub Actions (NuGet/login@v1) and GitLabGitHub 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_owner and remove_owner scopes 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

ControlRubyGems.orgNuGet.orgcrates.io
Publish-only scopepush_rubygemPush only new package versions, or push new packages and versionspublish-update (existing crates) or publish-new
Other scopesindex_rubygems, yank_rubygem, add_owner, remove_owner, access_webhooks, show_dashboard (exclusive)Unlistyank, change-owners, trusted-publishing
Package restrictionOne gem per key for push, yank and owner scopes; invalid if ownership is removedGlob pattern such as fabrikam.service.*, which also matches new package IDsCrate names, with a trailing * wildcard for prefixes
ExpiryOptional, at least 5 minutes ahead, cannot be changed laterEvery key has an expiry; banner warning and email 10 days beforeOptional expiry
Per-key MFAYes, OTP required for push, yank and owner changes on that keyNoNo
Legacy riskLegacy key migrated to all scopes: delete itOlder unscoped keys: replace with scoped keysOlder 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-new token 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

ScannerRubyGems API keyNuGet API keycrates.io token
GitHub secret scanningrubygems_api_key: partner programme, push protection, validity checknuget_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 rulesrubygems-api-tokenNo API key rule; nuget-config-password catches clear-text passwords in NuGet.configNo rule
TruffleHogDetector with live verification against the RubyGems APIDetector keyed on the word "nuget" with live verificationNo 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

  1. 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.toml and NuGet configuration on build hosts.
  2. 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_required in gemspecs.
  3. 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.
  4. 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.
  5. 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.
  6. Detect: enable secret scanning with push protection, add custom rules for the gaps above, and scan built packages before publishing.
  7. 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

  1. 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.
  2. Check ownership: look for owners, organisation members or trusted publisher policies added since the leak window and remove them.
  3. 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.
  4. Tell consumers: publish an advisory with affected versions so downstream teams can pin or roll back. See the Malicious Open Source Package Response Playbook.
  5. Close the source: find where the token lived (CI variable, laptop, published archive) and replace it with trusted publishing.

Framework mapping

ControlNIST SP 800-53 Rev. 5NIST CSF 2.0Threat addressed (ATT&CK)
Scoped, expiring tokens; trusted publishingIA-5, AC-6PR.AA-01, PR.AA-05T1552.001 Credentials In Files
Owner MFA and ownership reviewsIA-2, AC-2PR.AA-05T1078 Valid Accounts
Release integrity and provenanceSR-4, SI-7PR.PS-06T1195.002 Compromise Software Supply Chain
Leak scanning and publish monitoringAU-6, AU-12DE.CM-09T1195.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 signout on 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-key with 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_required to 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

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

PDF for subscribers

Take this guide with you

Every section, table and checklist in one PDF, with links to the sources and related guides. Subscribe to our weekly NHI & AI Identity Journal and we’ll email you a download link.

Free weekly newsletter

Subscribe to the NHI & AI Identity Journal

The latest on NHI and Agentic AI security – articles, research, breaches, news and events every week.

Bonus 33% off our NHI Course when you subscribe.

NHIMG Editorial Note
Researched and written by the NHI Mgmt Group editorial team using AI tools, under the editorial direction of Lalit Choda. Facts are drawn from the sources listed on this page and checked by our automated quality process. Last updated 8 November 2026. Spotted an error? Use our contact page.
NHI Mgmt Group, the independent authority on Non-Human Identity, IAM and Agentic AI security. nhimg.org

PDF Downloads for Newsletter Subscribers

Get the guide

Enter your details and we’ll email you a download link.

The full guide is free to read online. The PDF is a thank you for subscribing.

Fill in all fields marked * and tick the PDF box to continue.