你以为 pnpm pack 只是打个 tar 包,今天它告诉你里面还藏着一个 .env
配了多年 CI,每次 pnpm publish 都怕把 .env 打进去——今天 pnpm 12.8 终于把这步焊死了
上线前打 tar 包,本地跑 CI,.env 就在项目根目录——你真的确认它没有被 pnpm pack 一起卷进去吗?这个漏洞今天被 pnpm 12.8 彻底修了。
事故现场:你的 .env 可能一直在被发布出去
pnpm pack 和 pnpm publish 默认会把你目录下所有文件打成 tarball,包括:
# .env 通常在 .npmignore 或 .gitignore 里
# 但如果在 package.json 的 files 字段里没写清楚……
pnpm pack
# tarball 里可能包含:.env / .env.production / .env.local
2019 年的 npm 事件、2022 年大量 SDK 泄露密钥事故,根子都在这里:开发者在本地调试时往 .env 里塞了大量密钥,发布时没注意到它被一起打包进 tarball 了。
pnpm 12.8:三行配置,把 .env 锁死
pnpm 12.8(2026-09-28)发布,两条命令都加了警告检查:
pnpm pack 警告
$ pnpm pack
⚠️ WSP-003: The following files are going to be included in the tarball
and may contain sensitive data:
.env
.env.production
To exclude them, add the following to .npmignore or to the files field:
.env
.env.production
Or use the --dry-run option to inspect the tarball contents before packing.
pnpm publish 警告
$ pnpm publish
⚠️ WSP-003: Detected .env files that would be published:
.env
Add .env to .npmignore or the files field to suppress this warning.
error: WSP-003: Cannot publish with potentially sensitive files
pnpm 12.8 在发布前检查文件名是否包含 .env 字符串(大小写不敏感),发现了就直接报错,不会继续。
根因:为什么这个问题长期存在
.npmignore 和 .gitignore 是两套独立的黑名单系统,.gitignore 不等于 .npmignore。大量团队只在 .gitignore 里写了 .env,而忘了在 .npmignore 里也写一份:
# .gitignore ✅
.env
# .npmignore ❌ 漏了
pnpm 12.8 的修复思路是:不是帮你删文件,而是让这个问题在发布前就暴露出来。
怎么修
方案一:加到 .npmignore(最简单)
# .npmignore
.env
.env.*
!.env.example
方案二:用 files 字段白名单(更严格)
// package.json
{
"files": [
"dist",
"lib",
"src"
]
}
白名单比黑名单更安全——新文件默认不会被发布,除非你主动加进来。
方案三:用 pnpm-WARNINGSuppress 抑制误报
如果你的项目确实需要发布一个叫 .env.example 的示例文件:
# pnpm-workspace.yaml
warningSuppress:
wsp003: [.env.example]
背景:供应链安全在 2026 年的成熟度
pnpm 12.8 的 .env 警告只是今年供应链安全进化的一个切面:
| 时间 | 事件 | 影响 |
|---|---|---|
| 2026-09 | npm v12 默认关闭 install scripts | 运行时脚本执行 |
| 2026-09 | pnpm 12.6 autoDedupe | 依赖版本自动化 |
| 2026-09-28 | pnpm 12.8 .env 警告 | 发布前敏感文件检查 |
| 2026-09 | OIDC Trusted Publishing 多配置 | npm 发布鉴权 |
npm 生态从「先污染后治理」逐渐走向「默认安全」。
下一步
- 立刻跑一遍
pnpm pack --dry-run看看你的包里有啥 - 把 .npmignore 补全,确保 .env 系不在包里
- CI 里加上
pnpm pack --dry-run检查,作为发布门禁的一部分
评论区
0 条评论
登录后可评论。