抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >


如何配置可信发布从 CI / CD 工作流发布 npm 包

npm 令牌泄露导致的问题

长寿命或永久 npm 令牌是供应链攻击的主要载体。

npm 生态长期存在的供应链安全问题。2025 年 “沙虫” 攻击等事件显示,超 30% 的恶意包发布源于令牌泄露。

实时更新:Shai-Hulud,影响 CrowdStrike 和数百个热门软件包的历史上最危险的 NPM 漏洞

GlassWorm:第一个使用隐形代码的自传播蠕虫登陆 OpenVSX 市场

npm 将逐渐弃用之前的经典令牌和 TOTP 2FA

npm notice SECURITY NOTICE: Breaking changes starting October 13, 2025. New tokens will be limited to a maximum lifetime of 90 days, and TOTP setup will be disabled. Classic tokens will be revoked in November. Update your CI/CD workflows to avoid disruption. Learn more: https://gh.io/npm-token-changes

Important security changes to npm authentication take effect October 13,2025. New token lifetime limits (90-day max ) and TOTP 2FA restrictions become effective . Classic tokens will be revoked in November . Review changes and update your workflows now . Learn more: https://github.blog/changelog/2025-09-29-strengthening-npm-security-important-changes-to-authentication-and-token-management/ .

为了解决 npm 令牌泄露导致的问题,npm 最近的一次安全系统更新,将逐渐弃用之前的经典令牌和 TOTP 2FA。

Trusted publishing allows you to publish npm packages directly from your CI/CD workflows using OpenID Connect (OIDC) authentication, eliminating the need for long-lived npm tokens. This feature implements the trusted publishers industry standard specified by the Open Source Security Foundation (OpenSSF), joining a growing ecosystem including PyPI, RubyGems, and other major package registries in offering this security enhancement.

受信任发布允许你使用 OpenID Connect (OIDC) 身份验证直接从你的 CI / CD 工作流程中发布 npm 包,无需使用长寿命的 npm 令牌。此功能实现了 Open Source Security Foundation (OpenSSF) 指定的受信任发布者行业标准,加入了包括 PyPI、RubyGems 和其他主要包注册中心的不断增长的生态系统,提供这种安全增强功能。

注意:可信发布需要 npm CLI 版本 11.5.1 或更高版本。

笔者在写这篇文章的时候已经删除了 npm 上全部的令牌,并使用 OIDC 进行 Trusted Publisher 实现 npm 包的可信发布,以下是配置方式。

在 npmjs.com 上添加受信任的发布商

导航到 npmjs.com 上的包设置(Settings)并找到 “受信任的发布者”(Trusted Publisher)部分。在 “选择您的发布者”(Select your publisher)下,通过单击 GitHub Actions 或 GitLab CI/CD 按钮来选择您的 CI/CD 提供程序。

示例:

Publisher 选择 Github Actions

Organization or user 填写 MyGithubUserName

Repository 填写 MyRepository

Workflow filename 填写 npm-publish.yml

点击 Set up connection 完成配置

之后需要配置 Github Actions。

完整 Github Actions 工作流配置示例

其中比较关键的配置是需要给 GitHub Action 添加 id-token 权限,否则无法生成 OIDC 令牌。

其次是 npm CLI 的版本需要在 11.5.1 版本或以上。

.github/workflows/npm-publish.yml
name: npm-publish
on:
workflow_dispatch:
# release:
# types: [published]

permissions:
id-token: write # 需要给 GitHub Action 添加 id-token 权限,否则无法生成 OIDC 令牌
# packages: write
# contents: write
# issues: write
# pull-requests: write

jobs:
npm-publish:
name: npm-publish
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 24 # npm CLI 的版本需要在 11.5.1 版本或以上才支持 OIDC
registry-url: https://registry.npmjs.org
- name: Publish
run: |
npm publish
# 不再需要配置 NPM TOKEN
# env:
# NODE_AUTH_TOKEN: ${{ secrets.npm_token }}

相关文档

https://docs.npmjs.com/trusted-publishers

推荐阅读
Npm 更换源 Npm 更换源 NPM Upload NPM Upload 使用 Github Actions 部署 Hexo 使用 Github Actions 部署 Hexo 杂记片段 杂记片段 安装配置 Space 安装配置 Space Package Manager Proxy Settings Package Manager Proxy Settings

留言区

Are You A Robot?