npm 的可信发布软件包
See Details
目录
可信发布允许你使用 OpenID Connect (OIDC) 身份验证直接从 CI/CD 工作流发布 npm 包,从而无需使用长期有效的 npm 令牌。此功能实现了开源安全基金会 (OpenSSF) 指定的 可信发布者行业标准,加入了包括 PyPI、RubyGems 和其他主要软件包注册中心在内的不断发展的生态系统,从而提供了此安全增强功能。
¥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.
可信发布的工作原理
¥How trusted publishing works
可信发布使用 OIDC 在 npm 和你的 CI/CD 提供商之间建立信任关系。当你为你的软件包配置可信发布者时,除了传统的身份验证方法(例如 npm 令牌和手动发布)之外,npm 还会接受来自你授权的特定工作流的发布。npm CLI 会自动检测 OIDC 环境,并在回退到传统令牌之前使用它们进行身份验证。
¥Trusted publishing creates a trust relationship between npm and your CI/CD provider using OIDC. When you configure a trusted publisher for your package, npm will accept publishes from the specific workflow you've authorized, in addition to traditional authentication methods like npm tokens and manual publishes. The npm CLI automatically detects OIDC environments and uses them for authentication before falling back to traditional tokens.
这种方法消除了与长期写入令牌相关的安全风险,这些令牌可能会被盗用、在日志中意外暴露或需要手动轮换。相反,每次发布都会使用特定于你的工作流程的短期加密签名令牌,这些令牌无法提取或重复使用。
¥This approach eliminates the security risks associated with long-lived write tokens, which can be compromised, accidentally exposed in logs, or require manual rotation. Instead, each publish uses short-lived, cryptographically-signed tokens that are specific to your workflow and cannot be extracted or reused.
支持的 CI/CD 提供商
¥Supported CI/CD providers
受信任的发布目前支持:
¥Trusted publishing currently supports:
-
GitHub Actions(GitHub 托管的运行器)
¥GitHub Actions (GitHub-hosted runners)
-
GitLab CI/CD 管道(GitLab.com 共享运行器)
¥GitLab CI/CD Pipelines (GitLab.com shared runners)
目前不支持自托管运行器,但计划在未来版本中支持。
¥Self-hosted runners are not currently supported but are planned for future releases.
配置可信发布
¥Configuring trusted publishing
步骤 1:在 npmjs.com 上添加可信发布者
¥Step 1: Add a trusted publisher on npmjs.com
导航到 npmjs.com 上的软件包设置并找到 "受信任的发布者" 部分。在 "选择你的发布者" 下,点击 GitHub Actions 或 GitLab CI/CD 按钮选择你的 CI/CD 提供商。
¥Navigate to your package settings on npmjs.com and find the "Trusted Publisher" section. Under "Select your publisher", choose your CI/CD provider by clicking either the GitHub Actions or GitLab CI/CD button.
对于 GitHub Actions
¥For GitHub Actions
配置以下字段:
¥Configure the following fields:
-
组织或用户(必填):你的 GitHub 用户名或组织名称
¥Organization or user (required): Your GitHub username or organization name
-
仓库(必填):你的仓库名称
¥Repository (required): Your repository name
-
工作流文件名(必填):工作流程的文件名(例如,
publish.yml)¥Workflow filename (required): The filename of your workflow (e.g.,
publish.yml)-
仅输入文件名,无需完整路径
¥Enter only the filename, not the full path
-
必须包含
.yml或.yaml扩展名¥Must include the
.ymlor.yamlextension -
工作流程文件必须存在于存储库的
.github/workflows/中¥The workflow file must exist in
.github/workflows/in your repository
-
-
环境名称(可选):如果使用 GitHub 环境 进行部署保护
¥Environment name (optional): If using GitHub environments for deployment protection
对于 GitLab CI/CD
¥For GitLab CI/CD
配置以下字段:
¥Configure the following fields:
-
命名空间(必填):你的 GitLab 用户名或群组名称
¥Namespace (required): Your GitLab username or group name
-
项目名称(必填):你的项目名称
¥Project name (required): Your project name
-
顶层 CI 文件路径(必需):CI 文件的路径(例如,
.gitlab-ci.yml)¥Top-level CI file path (required): The path to your CI file (e.g.,
.gitlab-ci.yml)-
必须包含
.yml扩展名¥Must include the
.ymlextension
-
-
环境名称(可选):如果使用 GitLab 环境
¥Environment name (optional): If using GitLab environments
注意:每个包一次只能配置一个可信发布者。
¥Note: Each package can only have one trusted publisher configured at a time.
步骤 2:配置你的 CI/CD 工作流程
¥Step 2: Configure your CI/CD workflow
GitHub Actions 配置
¥GitHub Actions configuration
将所需的 OIDC 权限添加到你的工作流。完整示例:
¥Add the required OIDC permissions to your workflow. Here's a complete example:
name: Publish Packageon:push:tags:- 'v*'permissions:id-token: write # Required for OIDCcontents: readjobs:publish:runs-on: ubuntu-lateststeps:- uses: actions/checkout@v4- uses: actions/setup-node@v4with:node-version: '20'registry-url: 'https://registry.npmjs.org'# Ensure npm 11.5.1 or later is installed- name: Update npmrun: npm install -g npm@latest- run: npm ci- run: npm run build --if-present- run: npm test- run: npm publish
关键要求是 id-token: write 权限,它允许 GitHub Actions 生成 OIDC 令牌。在 GitHub 的 OIDC 文档 中了解更多信息。
¥The critical requirement is the id-token: write permission, which allows GitHub Actions to generate OIDC tokens. Learn more in GitHub's OIDC documentation.
GitLab CI/CD 配置
¥GitLab CI/CD configuration
在你的管道中配置 OIDC ID 令牌:
¥Configure the OIDC ID token in your pipeline:
stages:- test- build- publishvariables:NODE_VERSION: '20'test:stage: testimage: node:${NODE_VERSION}script:- npm ci- npm testpublish:stage: publishimage: node:${NODE_VERSION}id_tokens:NPM_ID_TOKEN:aud: "npm:registry.npmjs.org"SIGSTORE_ID_TOKEN:aud: sigstorescript:# Ensure npm 11.5.1 or later is installed- npm install -g npm@latest- npm ci- npm run build --if-present- npm publishonly:- tags
id_tokens 配置指示 GitLab 为 npm 生成 OIDC 令牌。在 GitLab 的 OIDC 文档 中了解更多信息。
¥The id_tokens configuration tells GitLab to generate an OIDC token for npm. Learn more in GitLab's OIDC documentation.
管理可信发布者配置
¥Managing trusted publisher configurations
你可以随时通过 npmjs.com 上的软件包设置修改或删除可信发布者配置。每个包一次只能连接一个可信发布者,但可以根据需要编辑或删除此连接。要更改提供商(例如,从 GitHub Actions 切换到 GitLab CI/CD),只需编辑现有配置并选择新的提供商即可。此更改将立即生效,并用于未来的发布。要完全删除受信任的发布并返回基于令牌的身份验证,请从软件包设置中删除受信任的发布者配置。
¥You can modify or remove your trusted publisher configuration at any time through your package settings on npmjs.com. Each package can only have one trusted publisher connection at a time, but this connection can be edited or deleted as needed. To change providers (for example, switching from GitHub Actions to GitLab CI/CD), simply edit your existing configuration and select the new provider. The change takes effect immediately for future publishes. To remove trusted publishing entirely and return to token-based authentication, delete the trusted publisher configuration from your package settings.
推荐:使用受信任的发布者时限制令牌访问
¥Recommended: Restrict token access when using trusted publishers
配置好软件包的受信任发布者后,我们强烈建议限制传统的基于令牌的发布访问权限,以增强安全性。
¥Once you've configured trusted publishers for your package, we strongly recommend restricting traditional token-based publishing access for enhanced security.
如何配置最高安全级别
¥How to configure maximum security
-
启用受信任的发布者后,导航至软件包的“设置”→“发布访问权限”
¥After enabling trusted publishers, navigate to your package's Settings → Publishing access
-
选择 "启用双重身份验证并禁用令牌"
¥Select "Require two-factor authentication and disallow tokens"
-
点击“更新包设置”保存更改
¥Save your changes by clicking on Update Package Settings
为什么选择此版本事项
¥Why this matters
受信任的发布者使用在 CI/CD 工作流程中按需生成的短期、范围限定的凭据,从而无需长期令牌。通过使用受信任的发布者限制传统令牌访问,你可以降低与凭据管理相关的潜在安全风险。
¥Trusted publishers use short-lived, scoped credentials that are generated on-demand during your CI/CD workflow, eliminating the need for long-lived tokens. By restricting traditional token access while using trusted publishers, you reduce potential security risks associated with credential management.
注意:"禁止使用令牌" 设置仅影响传统的令牌身份验证。你的受信任发布者将继续正常工作,因为它们使用 OIDC 令牌。
¥Note: The "disallow tokens" setting only affects traditional token authentication. Your trusted publishers will continue to work normally, as they use OIDC tokens.
迁移技巧
¥Migration tip
如果你正在从基于令牌的发布方式过渡:
¥If you're transitioning from token-based publishing:
-
请先设置受信任的发布者并验证其是否正常工作
¥Set up trusted publishers first and verify they work
-
然后按照上述说明限制令牌访问权限。
¥Then restrict token access as described above
-
不再需要的 撤销任何现有的自动化令牌
¥Revoke any existing automation tokens that are no longer needed
这可确保平稳过渡,不会中断你的发布流程。
¥This ensures a smooth transition without disrupting your release process.
自动溯源生成
¥Automatic provenance generation
当你使用可信发布功能进行发布时,npm 会自动为你的软件包生成并发布 出处证明。此操作默认发生 - 你无需在发布命令中添加 --provenance 标志。
¥When you publish using trusted publishing, npm automatically generates and publishes provenance attestations for your package. This happens by default—you don't need to add the --provenance flag to your publish command.
Provenance 提供软件包构建位置和方式的加密证明,允许用户验证其真实性。此自动生成仅在满足以下所有条件时才适用:
¥Provenance provides cryptographic proof of where and how your package was built, allowing users to verify its authenticity. This automatic generation only applies when all of these conditions are met:
-
通过可信发布 (OIDC) 发布
¥Publishing via trusted publishing (OIDC)
-
从公共仓库发布
¥Publishing from a public repository
-
发布公共软件包
¥Publishing a public package
注意:即使发布公共软件包,Provenance 生成也是 不支持私有仓库。
¥Note: Provenance generation is not supported for private repositories, even when publishing public packages.
禁用溯源生成
¥Disabling provenance generation
虽然我们强烈建议保持 provenance 启用状态,但你也可以根据需要禁用它。通过以下任一方式将 provenance 选项设置为 false:
¥While we strongly recommend keeping provenance enabled, you can disable it if needed. Set the provenance option to false in any of these ways:
使用环境变量:
¥Using environment variable:
NPM_CONFIG_PROVENANCE=false npm publish
在你的 .npmrc 文件中:
¥In your .npmrc file:
provenance=false
在你的 package.json 中:
¥In your package.json:
{"publishConfig": {"provenance": false}}