惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
C
Cisco Blogs
Help Net Security
Help Net Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Scott Helme
Scott Helme
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
M
MIT News - Artificial intelligence
L
LINUX DO - 热门话题
I
InfoQ
GbyAI
GbyAI
NISL@THU
NISL@THU
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Engineering at Meta
Engineering at Meta
H
Hackread – Cybersecurity News, Data Breaches, AI and More
TaoSecurity Blog
TaoSecurity Blog
Simon Willison's Weblog
Simon Willison's Weblog
A
About on SuperTechFans
Spread Privacy
Spread Privacy
月光博客
月光博客
W
WeLiveSecurity
AWS News Blog
AWS News Blog
云风的 BLOG
云风的 BLOG
有赞技术团队
有赞技术团队
Security Latest
Security Latest
人人都是产品经理
人人都是产品经理
PCI Perspectives
PCI Perspectives
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Microsoft Azure Blog
Microsoft Azure Blog
Hugging Face - Blog
Hugging Face - Blog
S
SegmentFault 最新的问题
T
Troy Hunt's Blog
Martin Fowler
Martin Fowler
The Hacker News
The Hacker News
T
Tor Project blog
C
CERT Recently Published Vulnerability Notes
Apple Machine Learning Research
Apple Machine Learning Research
Stack Overflow Blog
Stack Overflow Blog
K
Kaspersky official blog
Cloudbric
Cloudbric
H
Help Net Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tailwind CSS Blog
D
DataBreaches.Net
Security Archives - TechRepublic
Security Archives - TechRepublic
T
Tenable Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
博客园 - Franky
L
LINUX DO - 最新话题
MyScale Blog
MyScale Blog

沈唁志

Linux 服务器实现 Word 转图片方案 2025|在代码之外,我学会了旅行、牵手和拥抱 使用 GitHub Actions 自动同步 Docker 镜像到 CNB 怎么申请开具中国税收居民身份证明? HMAC 签名编码的坑:Go 和 PHP 的不同处理方式 Bitwarden Secrets Manager:简化 DevOps 的机密管理 MySQL 字符集与大小写敏感性解析 在命令行中输出带颜色的日志 PHP 中生成带毫秒的时间戳
使用 Trusted Publishing 提升 npm 包发布安全性
沈唁 · 2026-04-21 · via 沈唁志

在现代前端生态中,npm 包发布流程的安全性越来越重要。

传统的发布方式通常依赖长期有效的 npm token,一旦泄露,攻击者就可以直接发布恶意版本,带来严重的供应链风险。为了解决这一问题,npm 在 2025 年 7 月份推出了 Trusted Publishing(可信发布) 机制。

Trusted Publishing 是 npm 已正式推出基于 OpenID Connect (OIDC) 的可信发布功能。该功能允许使用 OpenID Connect (OIDC) 进行身份验证,直接从 CI/CD 工作流安全地发布 npm 包,从而减少管理长期令牌的需求。

借助可信发布,现在可以

  • 无长期 token:不需要在仓库中保存 npm token
  • 消除令牌安全风险:无需在 CI/CD 环境中存储、轮换或意外暴露 npm token。
  • 短期凭证:每次发布时动态签发,自动过期
  • 绑定 CI 身份,建立加密信任:发布权限与 GitHub 仓库/工作流强绑定,每次发布都使用短期、特定于工作流的凭据进行身份验证,这些凭据无法被泄露或重复使用。
  • 降低泄露风险:即使 CI 配置泄露,也难以滥用
  • 自动获取来源证明:使用可信发布时,npm CLI 默认发布来源证明。--provenance不再需要该标志。

传统发布方式的问题

在 Trusted Publishing 出现之前,常见流程如下:

  1. 在 npm 上生成一个 Access Token
  2. 将 token 存入 GitHub Secrets(如 NPM_TOKEN)
  3. 在 CI 中执行 npm publish 进行发布

问题在于 token 通常是长期有效,一旦泄露,攻击者可以进行发布恶意版本等操作。

现在 npm 限制生成 token 最长有效期为 90 天,还使用这种方式会经常需要轮换 token。

Trusted Publishing 的工作原理

Trusted Publishing 会在 npm 和你的 CI/CD 提供商之间建立信任关系

当你为你的包配置可信发布者后,npm 将只接受来自你已授权的特定工作流的发布。

Trusted Publishing 基于以下流程:

  1. GitHub Actions 在运行时向 npm 发起身份请求(OIDC)
  2. npm 验证该请求是否来自可信仓库
  3. 如果匹配配置,签发一个短期访问令牌
  4. CI 使用该令牌执行 npm publish

整个过程:

  • 无需手动管理 token
  • 每次发布都是一次“临时授权”

设置 Trusted Publishing

我也是在发布 docsify-footnotes时发现 token 不可用了,此处以它为例:

1. npm 侧配置

进入 npm 包的 Settings页面,就可以看到Trusted Publisher,并且需要选择publisher,支持GitHub ActionsGitLab CI/CDCircleCI

我的仓库在 GitHub,所以选择GitHub Actions,需要配置Organization or userRepositoryWorkflow filename,这三个都是必填项。

GitHub 用户名或组织名称 Organization or usersy-records
仓库名称 Repositorydocsify-footnotes
工作流文件名 Workflow filenamepublish.yml

点击Set up connection保存即可

2. GitHub Actions 配置

保存之后需要修改对应的 action yml 文件,需要将所需的 OIDC 权限添加到你的工作流中:

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

关键要求是id-token: write需要获得相应的权限,才能让 GitHub Actions 生成 OIDC 令牌,这是一个完整例子:

name: Publish Package

on:
  push:
    tags:
      - 'v*'

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

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

      - uses: actions/setup-node@v6
        with:
          node-version: '24'
          registry-url: 'https://registry.npmjs.org'
      - run: npm ci
      - run: npm run build --if-present
      - run: npm test
      - run: npm publish

因为我的几个 npm 仓库都采用的 复用 actions,所以只需要修改加上permissions和移除写入 npm token 就可以使用了,见 commit

发布成功后的邮件通知也会增加说明 GitHub Actions 的信息。

Hi sy-records!

A new version of the package @sy-records/docsify-footnotes (2.3.0) was published at 2026-04-21T02:48:22.069Z from GitHub Actions: https://github.com/sy-records/docsify-footnotes/actions/runs/24701393194/attempts/1 (triggered via a "push" event on git ref "refs/tags/v2.3.0").
The shasum of this package is 8136f65cb7d651d4b3671290b7c16da4cac71b24.

If you have questions or security concerns, you can contact us at https://www.npmjs.com/support.

Thanks,

The npm team.

需要注意的是 Trusted Publishing 需要 npm CLI 版本 11.5.1 或更高版本以及 Node 版本 22.14.0 或更高版本。

Trusted Publishing 是 npm 在供应链安全方向的重要升级,建议所有开源项目逐步迁移。