To publish npm packages safely in 2026, replace long-lived npm tokens with trusted publishing: an OIDC link between npmjs.com and a specific CI workflow, which also produces provenance attestations automatically. You need npm CLI 11.5.1+, Node 22.14.0+, a GitHub-hosted (or GitLab.com / CircleCI cloud) runner, and permissions: id-token: write in your workflow. On the consuming side, npm v12 now blocks dependency install scripts by default, and a release-age cooldown plus npm ci closes most of the remaining gap.

This guide walks through the setup end to end, explains what changed after the Shai-Hulud worms, and lists the consumer-side settings worth turning on today.

What Changed After Shai-Hulud

The npm registry's security model changed more between September 2025 and mid-2026 than in the previous several years combined. The trigger was a self-replicating worm.

The two Shai-Hulud waves

In September 2025, GitHub was notified of the original Shai-Hulud attack, a worm that stole maintainer credentials and used them to republish infected versions of the victims' other packages. GitHub responded with a plan for a more secure npm supply chain (published September 22, 2025) that promised to deprecate classic tokens, shorten granular token lifetimes, move from TOTP to FIDO-based 2FA, and push maintainers toward trusted publishing.

A second wave, widely called Shai-Hulud 2.0, was identified on November 24, 2025. According to Datadog Security Labs, it injected setup_bun.js and bun_environment.js into legitimate packages and ran them from a preinstall hook. Trend Micro's analysis shows the modified manifest containing "preinstall": "node setup_bun.js". Running at preinstall rather than postinstall meant the payload executed before many scanners looked at the package, and it harvested GitHub and npm tokens from developer machines and CI to spread further.

Both waves exploited the same two weaknesses: long-lived publish tokens and install scripts that run arbitrary code on every npm install. Every change below targets one of those.

Timeline of registry and CLI changes

Date Change Source
July 2025 Trusted publishing (OIDC) generally available GitHub blog
Nov 5, 2025 Creation of new classic tokens disabled; granular token changes announced GitHub changelog
Dec 9, 2025 All existing classic tokens permanently revoked; two-hour session tokens for npm login; write-capable granular tokens capped at 90 days GitHub changelog
May 22, 2026 Staged publishing GA (npm 11.15.0+); new --allow-file, --allow-remote, --allow-directory install controls GitHub changelog
July 8, 2026 npm v12 GA: dependency install scripts, git deps and remote URL deps blocked by default GitHub changelog
Early Aug 2026 (expected) 2FA-bypass granular tokens lose sensitive account/package/org management actions same as above
~Jan 2027 (expected) 2FA-bypass tokens reduced to reading private packages and staging a publish same as above

The practical upshot: if your release pipeline still uses an NPM_TOKEN secret with "Bypass 2FA" enabled, it is on borrowed time. By around January 2027, GitHub expects such tokens to be able to stage a publish but not publish directly.

How npm Trusted Publishing Works

Trusted publishing replaces a stored secret with a short-lived identity token. When your workflow runs, GitHub Actions mints an OIDC token that says, in effect, "this is workflow publish.yml in repository acme/widget, running on a GitHub-hosted runner." npm checks that claim against the trusted publisher you configured for the package and, if it matches, accepts the publish. Nothing reusable is ever stored in your repository secrets, so there is nothing for a worm to steal and replay.

Requirements

Per the npm trusted publishers documentation:

  • npm CLI 11.5.1 or later and Node 22.14.0 or higher.
  • A supported provider: GitHub Actions (GitHub-hosted runners), GitLab CI/CD (GitLab.com shared runners), or CircleCI (cloud).
  • Self-hosted runners are not supported yet.
  • Each package can have up to 10 trusted publishers.

Provenance comes for free (with conditions)

When you publish via trusted publishing from GitHub Actions or GitLab CI/CD, npm automatically generates and publishes a provenance attestation, a signed statement linking the tarball to the exact commit and workflow run that built it. You do not need the --provenance flag. The conditions: the repository must be public and the package must be public. Provenance is not generated for private repositories (even if the package is public), and it is not generated for CircleCI.

Step-by-Step: Trusted Publishing with GitHub Actions

1. Make sure the package already exists and repository is correct

Provenance verification compares the repository field in package.json with the repository that built the package, and the provenance docs require an exact, case-sensitive match.

{
  "name": "@acme/widget",
  "version": "2.3.0",
  "repository": {
    "type": "git",
    "url": "git+https://github.com/acme/widget.git"
  },
  "publishConfig": {
    "access": "public"
  }
}

2. Configure the trusted publisher on npmjs.com

Open the package on npmjs.com, go to Settings → Trusted Publisher, choose GitHub Actions, and fill in:

Field Example Required
Organization or user acme Yes
Repository widget Yes
Workflow filename publish.yml (must include .yml/.yaml) Yes
Environment name npm-release No

Use the workflow filename only, not the full .github/workflows/ path. Setting an environment is strongly recommended because it lets you add GitHub environment protection rules (required reviewers, branch restrictions) in front of the publish job.

If you prefer the terminal, npm 11.15.0+ ships an npm trust command that configures the same thing:

npm trust github @acme/widget \
  --file publish.yml \
  --repo acme/widget \
  --env npm-release \
  --allow-publish

At least one permission flag (--allow-publish or --allow-stage-publish) is required.

3. Write the workflow

This is adapted from the example in npm's documentation, with an environment added:

name: Publish Package

on:
  push:
    tags:
      - 'v*'

permissions:
  id-token: write   # Required for OIDC
  contents: read

jobs:
  publish:
    runs-on: ubuntu-latest
    environment: npm-release
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-node@v6
        with:
          node-version: '24'
          registry-url: 'https://registry.npmjs.org'
          package-manager-cache: false  # never use caching in release builds
      - run: npm ci
      - run: npm run build --if-present
      - run: npm test
      - run: npm publish

Three details matter:

  1. id-token: write is the permission that lets the job request an OIDC token. Without it, npm falls back to looking for a token and fails.
  2. npm version. Trusted publishing needs npm 11.5.1+. Check what your Node image bundles with npm --version; if it is older, add a step that runs npm install -g npm@latest before publishing.
  3. package-manager-cache: false. npm's own example disables caching in release jobs. A poisoned cache is one of the ways a compromised PR workflow can influence a privileged release job, so keep release builds clean.

There is no NODE_AUTH_TOKEN and no NPM_TOKEN secret anywhere in this file. That is the point.

4. Lock down token-based publishing

Once the first trusted publish succeeds, go to Settings → Publishing access and select "Require two-factor authentication and disallow tokens." The npm docs note that this setting only affects traditional token authentication; trusted publishers keep working. After this, a stolen token cannot publish the package even if one is still floating around in an old CI system.

Then delete the old NPM_TOKEN repository secret and revoke the granular token behind it (npm token list / npm token revoke <id>).

5. Optional: require a human with staged publishing

Staged publishing, GA since May 2026 and requiring npm 11.15.0+, adds a second gate. The CI job runs npm stage publish instead of npm publish, which uploads the prebuilt tarball to a queue. Nothing is installable until a maintainer approves it on npmjs.com or via the CLI, and approval requires passing a 2FA challenge.

      - run: npm stage publish

You can configure the trusted publisher as stage-only (--allow-stage-publish without --allow-publish), so that even a fully compromised workflow can only queue a release, never ship one. Staging does not work for a brand-new package that does not exist on the registry yet.

Verifying provenance as a consumer

Anyone installing your package can check signatures and attestations:

npm audit signatures

The output reports how many installed packages have verified registry signatures and how many have verified attestations. A package page on npmjs.com also shows a provenance badge linking back to the source commit and workflow run.

Hardening the Consuming Side

Trusted publishing protects your packages. Most teams consume far more packages than they publish, and both Shai-Hulud waves executed on the machines of people who merely installed an infected version. These are the controls that matter for consumers.

npm v12: install scripts are now opt-in

npm v12 went GA on July 8, 2026, and it flips three defaults:

  • Dependency lifecycle scripts (preinstall, install, postinstall) no longer run unless explicitly allowed.
  • Git dependencies (direct or transitive) are not resolved unless allowed (allow-git defaults to none).
  • Remote URL dependencies such as https:// tarballs are not resolved unless allowed (allow-remote defaults to none).

The allowlist lives in the allowScripts field of your project's package.json. The npm install-scripts documentation describes the workflow:

# See which dependencies want to run install scripts but have not been reviewed
npm install-scripts ls

# Approve specific packages (pins to the installed version by default)
npm install-scripts approve esbuild sharp

# Explicitly deny one (survives a later "approve --all")
npm install-scripts deny some-telemetry-pkg

# Remove stale entries
npm install-scripts prune --dry-run

npm approve-scripts and npm deny-scripts are aliases. Approvals are pinned to a version by default (allow-scripts-pin defaults to true), so a compromised future version of an approved package does not inherit the approval. Commit the resulting package.json change so CI uses the same allowlist.

If you are still on npm 11, version 11.16.0 and later warn about scripts that v12 would block, which is the cheapest way to find out what breaks before upgrading. To turn the v12 policy into a hard failure in CI, set strict-allow-scripts=true.

Add a release-age cooldown

A cooldown means your installs simply ignore versions younger than N days. That gives maintainers, security vendors and the registry time to spot and remove a malicious release before it ever reaches your lockfile, which is exactly the window worms like Shai-Hulud depend on. If a bad version has already reached your builds, follow our compromised npm package runbook.

npm's min-release-age config takes a number of days:

# .npmrc
min-release-age=3

pnpm's equivalent is minimumReleaseAge, measured in minutes, with a per-package exclusion list for things you need to update immediately:

# pnpm-workspace.yaml
minimumReleaseAge: 4320   # 3 days
minimumReleaseAgeExclude:
  - '@acme/*-internal'
trustPolicy: no-downgrade
blockExoticSubdeps: true

The current pnpm docs list a default of 1440 minutes for minimumReleaseAge, so recent pnpm versions already apply a one-day cooldown. trustPolicy: no-downgrade makes pnpm fail if a package's trust evidence weakens compared with earlier releases (for example, a package that always shipped with provenance suddenly publishes without it). blockExoticSubdeps (default true) only lets direct dependencies use git or tarball sources.

The trade-off is real: a cooldown also delays urgent security patches. Keep the exclusion list short and review it, and let Dependabot or Renovate respect the same window. According to InfoQ's coverage of GitHub's new defaults, Dependabot version updates now wait three days before opening a PR.

Install exactly what the lockfile says

In CI, always use:

npm ci

npm ci installs exactly what package-lock.json specifies and fails if package.json and the lockfile disagree, instead of silently resolving new versions. Review lockfile diffs in PRs like code: an unexpected new transitive dependency, or a resolved URL pointing somewhere other than the registry, deserves a question.

Treat CI credentials as the real target

Shai-Hulud 2.0 harvested tokens from environment variables and the GitHub CLI. Practical mitigations:

  • Give GITHUB_TOKEN the narrowest permissions: block per job, and default the whole workflow to contents: read.
  • Never expose secrets to jobs triggered by untrusted code (pull_request_target from forks is the classic footgun).
  • Keep cloud credentials out of build jobs that run npm install; use OIDC federation to your cloud provider in a separate deploy job.
  • Pin third-party actions to a full commit SHA, not a moving tag.

Maintainer Checklist

Control Who Effort What it blocks
Trusted publishing (OIDC) Publisher ~30 min per package Token theft → malicious publish
"Disallow tokens" publishing access Publisher 1 click Leftover tokens being reused
Staged publishing (stage-only publisher) Publisher Small Fully compromised CI shipping a release
WebAuthn/passkey 2FA instead of TOTP Every maintainer Minutes Phishing of TOTP codes
npm v12 / allowScripts allowlist Consumer Hours for large apps Install-time code execution
min-release-age / minimumReleaseAge Consumer Minutes Freshly published malicious versions
npm ci + lockfile review Consumer Habit Silent dependency drift
npm audit signatures in CI Consumer Minutes Tampered tarballs, missing attestations

If you are mapping these controls onto a broader program, our overviews of DevSecOps platforms and SBOM management tools cover the tooling that sits on top, and the Dagger vs GitHub Actions comparison is useful if you are rethinking how your release pipeline is structured. For quick checks while you work, the site's developer tools are free to use.

Frequently Asked Questions

Does npm trusted publishing work with self-hosted GitHub runners?

No. The npm documentation says self-hosted runners are not currently supported, though support is planned. Trusted publishing currently requires GitHub-hosted runners, GitLab.com shared runners, or CircleCI cloud. If you must publish from self-hosted infrastructure, use a short-lived granular token with the narrowest scope and plan for the upcoming restrictions on 2FA-bypass tokens.

Do I still need the --provenance flag?

Not when you publish via trusted publishing from GitHub Actions or GitLab CI/CD: provenance is generated automatically, as long as both the repository and the package are public. The --provenance flag (or "provenance": true in publishConfig) is only needed for token-based publishing from a supported cloud CI, and it requires npm 9.5.0 or later.

What happened to npm classic tokens?

Creating new classic tokens was disabled in November 2025, and on December 9, 2025, npm permanently revoked all existing classic tokens. They cannot be recreated or recovered. npm login now issues two-hour session tokens, and granular tokens with write access have a 90-day maximum lifetime.

Will npm v12 break my install?

Possibly, if you depend on packages that compile native code or download binaries in an install script (common examples are image processing, bundlers with native binaries, and database drivers). The install still completes but the scripts are skipped. Run npm install-scripts ls, approve the packages you trust with npm install-scripts approve <pkg>, and commit the allowScripts changes to package.json. On npm 11.16.0+, you get warnings ahead of time.

Is a minimum release age worth the delay on security fixes?

For most application teams, yes, as long as you keep an exclusion list for packages where you need patches immediately. A cooldown of one to three days gives the ecosystem time to detect and pull a malicious release while delaying routine updates only slightly. Libraries that ship critical CVE fixes can be excluded or updated manually.

How many trusted publishers can a package have?

Up to 10, per the npm documentation. That covers setups like a main release workflow plus a separate canary or prerelease workflow. Existing trusted publisher connections cannot be edited in place, so delete and recreate one if a workflow file is renamed.

Conclusion

The npm ecosystem has moved from "protect your token" to "don't have a token." For publishers, the work is small: configure a trusted publisher, add id-token: write to a GitHub-hosted workflow, publish once, then disallow token-based publishing. Add staged publishing if your package is widely depended on. For consumers, upgrade to npm v12 (or turn on its warnings in 11.16.0+), maintain an explicit allowScripts allowlist, add a release-age cooldown, and use npm ci everywhere. None of these steps is exotic, and together they remove the two weaknesses that let Shai-Hulud spread: reusable publish credentials and arbitrary code running on install.