AI觉醒星球
Awakening is here
Knowledge File / 全球热点解读
2026-06-10 5 浏览 公开

趋势解读:Upcoming breaking changes for npm v12,提升开发者接入体验

npm v12 引入安全相关的默认变更,要求开发者显式授权脚本、Git依赖和远程URL依赖,预计2026年7月发布。

SOURCE / 全球热点解读 MIN / 4 ACCESS / 公开 POST / 2026-06-10 04:04:10

原贴

查看原文
作者:Allison 来源站点:github.blog 原贴时间:

原文

Our next npm major version, v12, introduces security-related default changes to npm install . All these changes are available behind warnings in npm today on 11.16.0 or newer, so you can prepare before the upgrade. v12 is estimated to release in July 2026. Each change turns an npm install behavior that runs automatically today into one you explicitly opt into: allowScripts defaults to off: npm install will no longer execute preinstall , install , or postinstall scripts from dependencies unless they are explicitly allowed in your project. This includes native node-gyp builds (i.e., a package with a binding.gyp and no explicit install script still gets blocked, because npm runs an implicit node-gyp rebuild for it). prepare scripts from git, file, and link dependencies are blocked the same way. To see what would be blocked, run npm approve-scripts --allow-scripts-pending . Then allow the packages you trust with npm approve-scripts and block the rest with npm deny-scripts . The resulting allowlist is written to package.json and should be committed. If your install routine runs scripts, you can observe warnings in npm 11.16.0+. --allow-git defaults to none : npm install will no longer resolve Git dependencies (direct or transitive) unless explicitly allowed via --allow-git . This closes a code-execution path where a Git dependency’s .npmrc could override the Git executable, even with --ignore-scripts . This change was previously announced on 2026-02-18 and is available in npm 11.10.0+. --allow-git defaults to none : npm install will no longer resolve Git dependencies (direct or transitive) unless explicitly allowed via --allow-git . This closes a code-execution path where a Git dependency’s .npmrc could override the Git executable, even with --ignore-scripts . This change was previously announced on 2026-02-18 and is available in npm 11.10.0+. --allow-remote defaults to none : npm install will no longer resolve dependencies from remote URLs, such as https tarballs (direct or transitive), unless explicitly allowed via --allow-remote . This flag is available in npm 11.15.0+. The related --allow-file and --allow-directory flags are not changing their defaults in v12. --allow-remote defaults to none : npm install will no longer resolve dependencies from remote URLs, such as https tarballs (direct or transitive), unless explicitly allowed via --allow-remote . This flag is available in npm 11.15.0+. The related --allow-file and --allow-directory flags are not changing their defaults in v12. Upgrade to npm 11.16.0 or later, run your normal install, and review the warnings. Use npm approve-scripts --allow-scripts-pending to see which packages have scripts, approve the ones you trust, and commit the updated package.json . After that, only the scripts you approved keep running once you upgrade. Anything you leave unapproved will stop. More details are available in our docs at npm approve-scripts , npm deny-scripts , and allow-scripts config (for npx and global installs). Please share your comments and questions in our community discussion .

中文翻译

我们的下一个 npm 主要版本 v12 对 npm install 引入了与安全相关的默认更改。所有这些更改都在今天的 11.16.0 或更高版本的 npm 警告后面可用,因此您可以在升级之前做好准备。 v12 预计将于 2026 年 7 月发布。每项更改都会将今天自动运行的 npm install 行为转变为您明确选择的行为:allowScripts 默认为关闭:npm install 将不再执行依赖项中的 preinstall 、 install 或 postinstall 脚本,除非您的项目中明确允许它们。这包括本机 Node-gyp 构建(即,带有 Binding.gyp 且没有显式安装脚本的包仍然会被阻止,因为 npm 为其运行隐式 Node-gyp 重建)。来自 git、文件和链接依赖项的准备脚本也会以同样的方式被阻止。要查看将阻止哪些内容,请运行 npmapprove-scripts --allow-scripts-pending 。然后使用 npmapprove-scripts 允许您信任的软件包,并使用 npmdeny-scripts 阻止其余的软件包。生成的允许列表将写入 package.json 并应提交。如果您的安装例程运行脚本,您可以在 npm 11.16.0+ 中观察到警告。 --allow-git 默认为 none :npm install 将不再解析 Git 依赖项(直接或传递),除非通过 --allow-git 明确允许。这会关闭一个代码执行路径,其中 Git 依赖项的 .npmrc 可以覆盖 Git 可执行文件,即使使用 --ignore-scripts 也是如此。此更改之前已于 2026 年 2 月 18 日宣布,并在 npm 11.10.0+ 中提供。 --allow-remote 默认为 none :npm install 将不再解析依赖项来自远程 URL 的依赖项,例如 https tarball(直接或传递),除非通过 --allow-remote 明确允许。该标志在 npm 11.15.0+ 中可用。相关的 --allow-file 和 --allow-directory 标志在 v12 中不会更改其默认值。升级到 npm 11.16.0 或更高版本,运行正常安装,然后查看警告。使用 npmapprove-scripts --allow-scripts-pending 查看哪些包有脚本,批准您信任的包,并提交更新的 package.json 。此后,只有您批准的脚本在升级后才会继续运行。任何未经批准的事情都会停止。更多详细信息可在我们的文档中找到,网址为 npmapprove-scripts 、 npmdeny-scripts 和allow-scriptsconfig(适用于 npx 和全局安装)。请在我们的社区讨论中分享您的意见和问题。

核心信息

npm v12 引入安全相关的默认变更,要求开发者显式授权脚本、Git依赖和远程URL依赖,预计2026年7月发布。

  • npm v12 默认关闭脚本执行,需显式授权
  • Git 和远程依赖默认不解析,防止代码执行
  • 提前可用 npm 11.16+ 预览警告
  • 通过 approve-scripts 管理信任列表
  • 预计2026年7月发布,需尽早准备

详细解读

信号解读:npm v12 的 breaking changes 是 Node.js 生态向“安全默认”迈出的重要一步。核心变化是 npm install 不再自动执行依赖脚本、解析 Git 和远程 URL 依赖,需要开发者显式授权。这标志着 npm 从“默认信任”转向“默认拒绝”,类似苹果 iOS 的权限管理思路。

为什么重要:过去供应链攻击常利用自动脚本执行(如恶意 postinstall 脚本)或 Git 依赖的 .npmrc 覆盖来植入恶意代码。此次变更直接堵住了这两类高危漏洞,对大型企业和开源项目来说,能显著降低安全风险。但也会增加开发者的配置负担,尤其是依赖较多脚本的 Legacy 项目。

对谁有价值:前端/Node.js 开发者、DevOps 团队、安全工程师、使用 npm 的企业。开发者需提前在 npm 11.16+ 上测试并建立信任列表;企业可借此机会优化依赖审批流程;安全团队可将此作为基线策略。

行动建议:立即升级到 npm 11.16+,运行 npm approve-scripts --allow-scripts-pending 生成当前脚本依赖的白名单,然后逐一审查并提交。对于 Git/远程依赖,使用 --allow-git--allow-remote 逐步允许信任源。注意:如果项目有大量自动脚本,迁移需要几个月,不要等到 2026 年 7 月。

风险与限制:部分合法脚本(如构建工具)可能被误杀,导致 CI/CD 失败。另外,对于非开发者用户(如设计师使用 Node 工具),配置复杂度上升。此外,npm 生态中仍有大量包依赖脚本,短期内可能引发兼容性问题。

信息差价值

信息差价值:官方提前一年多预告,而大多数开发者可能直到临近升级才关注。提前理解这些变动的开发者可以率先优化自己的依赖管理流程,避免临时抓瞎,抢占工程效率和安全合规的先机。

业务启发:对使用 npm 的业务团队,尤其是大型 Monorepo 或微服务架构,应建立依赖脚本的审计和许可机制。可借鉴 npm 的 approve-scripts 思想,在 CI 中集成脚本白名单检查,将安全左移至开发阶段。同时,这对工具链供应商(如 CI/CD 平台、私有 npm 仓库)也是一个机会,可推出自动化审批服务。

可沉淀动作:1. 创建内部文档,列出全部已信任的脚本包并定期复审。2. 在团队内推行“先预览、后升级”的流程:在 npm 11.16+ 上运行 npm approve-scripts 并 git diff 观察变化。3. 对于 Git 和远程依赖,考虑将它们迁移到私有 npm 镜像或打包成本地文件,以降低依赖数量。

参考来源

上一篇 IBM CEO:AI不一定导致员工减少 下一篇 Mythos 5 智能体因资源互相杀戮