Skip to content

Promote npm trusted publisher #201

@sxzz

Description

@sxzz

Motivation

npm Trusted Publishing is now generally available, allowing package owners to publish npm packages via CI without manually generating npm tokens. This method greatly reduces the risk of token leaks, which has been a recurring issue in the past. For more details, see:

Current Status

I've added a provenance tab to node-modules-inspector, where you can view the provenance status of all my packages: https://pkg.sxzz.dev/grid/provenance

Some packages have provenance enabled but are not yet using Trusted Publishing, including:

Migration

Note

This proposal applies only to packages already published via CI. If your package does not yet use CI for publishing, you will need to set up CI workflows before migrating to Trusted Publishing.

Migrating is straightforward for packages already published via CI:

  1. Connect your GitHub repository to your existing npm package at https://www.npmjs.com/package/<your-package>/access
  2. Upgrade npm to the latest version (11.5.1+) if you're using npm or pnpm.
  3. Safely remove npm tokens
  4. [optional] Disallow tokens and trusted publishing will still work
Image

For reference, see this example commit:

  - name: Publish to NPM
-   run: npm publish
+   run: npm i -g npm@latest && npm publish # or pnpm publish
-   env:
-     NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
-     NPM_CONFIG_PROVENANCE: true

I also wrote a reusable workflow: https://github.com/sxzz/workflows/blob/main/examples/release.yml

Automation

Anthony created a tool that lets you open the settings pages for all your monorepo packages at once.

I’ve also developed a userscript that automatically fills in repository information on the trusted publisher form (using the required repository field from package.json) and enables 2FA by default. You can install it using Tampermonkey or Violentmonkey. Thanks to @antfu for the inspiration!

Tip

If you maintain a large monorepo, connecting all packages can be time-consuming, and the user experience is currently lacking. I tried to write a script to automate repository connections, but was blocked by Cloudflare verification. Hopefully, npm will offer this feature via API in the future.

Read more

Migrated

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions