Skip to content

fix(release): release 校验 preflight 钉住的 master,消除会永久烧掉版号的 TOCTOU - #1157

Merged
deepcoldy merged 2 commits into
masterfrom
fix/release-pin-master-ancestry
Sep 1, 2026
Merged

deepcoldy merged 2 commits into
masterfrom
fix/release-pin-master-ancestry

Conversation

@deepcoldy

Copy link
Copy Markdown
Owner

修 #1150。

问题

binary-subpackages 把 6 个平台子包发上 npm 之后,release 才跑「stable 必须包含最新
master」那道闸。两个 job 各自 git fetch origin master 重读一次活动引用,于是同一道闸
在同一次 run 里可以给出相反结论——而子包已经发出去了,不可逆。

v3.18.10 实测(UTC):

09:53:46  preflight ancestry OK   (origin/master = 61620be7,即 tag 自己)
09:56:22  6 个子包发布成功                      ← 不可逆
09:57:47  def4275b (#1128) 落 master           ← 落在窗口内
09:58:33  release ancestry REFUSE (origin/master = def4275b)

危险窗口 4分47秒,tag 自始至终没动过。重试也救不回来:重打 tag 就改了代码,编出的二进制
哈希不同,publish-npm-if-missing.mjs 会正确拒绝用不同内容覆盖同名版本。3.18.10 就此报废,
只能跳号发 3.18.11,npm 上永久留下「只有 6 个子包、没有主包」的空洞版号。

为什么不能改成 binary-subpackages: needs: release

那是 issue 里列的方案 1,不能用。job 自己的注释写明了原因:主包对这 6 个子包声明的是
optional dependencies,而 npm 对解析不了的 optional dep 是静默 no-op——主包先发就会
装出一个没有二进制的主包,且不报错。发布顺序本身是对的,错的是「闸读了会变的东西」。

修法

preflight 把它实际校验过的那个 origin/master SHA 钉下来并 export;release 改为
校验这个钉住的 SHA,不再重读 origin/master。闸的语义不变——tag 仍必须包含「本次发布被
准入时」的 master——但结论在一次 run 内不可变。发布中途落地的 commit 属于下一次发布,
不该烧掉一个版号。

release 侧 fetch 改为按 SHA fetch(钉住的 commit 未必还是 master tip)。
钉住的值为空时 fail-closed(exit 1),不退回重读活动引用。

验证

把 release 那一步的 shell 抽出来真跑,四种情形各自因自己的理由退出:

HEAD PINNED_MASTER 结果
61620be (v3.18.10 tag) 空 exit 1 REFUSING: preflight pinned no master SHA
61620be 61620be(preflight 真钉的值) exit 0 OK: contains the master preflight admitted
61620be def4275(竞态中落地的 commit) exit 1 does not contain the master preflight admitted
2802b44 (真正落后的 commit) 61620be exit 1 does not contain... ← 守卫仍有牙

第 2 行就是被烧掉的那次:旧逻辑 REFUSE、新逻辑 PASS。
第 4 行确认这次改动没有削弱守卫:真正落后的 tag 照样拒,且它在 preflight 就会被拦下,
子包根本不会发。

反变异:把 master_sha 输出改成空串 → release 步骤 exit 1(不是静默放行),已还原。
yaml.safe_load 解析通过,10 个 job 结构不变。

Closes #1150

🤖 Generated with Claude Code

修 #1150。

## 问题

`binary-subpackages` 把 6 个平台子包发上 npm 之后,`release` 才跑「stable 必须包含最新
master」那道闸。两个 job 各自 `git fetch origin master` **重读一次活动引用**,于是同一道闸
在同一次 run 里可以给出相反结论——而子包已经发出去了,不可逆。

v3.18.10 实测(UTC):

    09:53:46  preflight ancestry OK   (origin/master = 61620be,即 tag 自己)
    09:56:22  6 个子包发布成功                      ← 不可逆
    09:57:47  def4275 (#1128) 落 master           ← 落在窗口内
    09:58:33  release ancestry REFUSE (origin/master = def4275)

危险窗口 4分47秒,tag 自始至终没动过。重试也救不回来:重打 tag 就改了代码,编出的二进制
哈希不同,`publish-npm-if-missing.mjs` 会正确拒绝用不同内容覆盖同名版本。3.18.10 就此报废,
只能跳号发 3.18.11,npm 上永久留下「只有 6 个子包、没有主包」的空洞版号。

## 为什么不能改成 `binary-subpackages: needs: release`

那是 issue 里列的方案 1,**不能用**。job 自己的注释写明了原因:主包对这 6 个子包声明的是
**optional dependencies**,而 npm 对解析不了的 optional dep 是**静默 no-op**——主包先发就会
装出一个没有二进制的主包,且不报错。发布顺序本身是对的,错的是「闸读了会变的东西」。

## 修法

`preflight` 把它**实际校验过**的那个 `origin/master` SHA 钉下来并 export;`release` 改为
校验这个钉住的 SHA,不再重读 `origin/master`。闸的语义不变——tag 仍必须包含「本次发布被
准入时」的 master——但结论在一次 run 内不可变。发布中途落地的 commit 属于下一次发布,
不该烧掉一个版号。

`release` 侧 fetch 改为**按 SHA fetch**(钉住的 commit 未必还是 master tip)。
钉住的值为空时 **fail-closed**(exit 1),不退回重读活动引用。

## 验证

把 release 那一步的 shell 抽出来真跑,四种情形各自因**自己的理由**退出:

| HEAD | PINNED_MASTER | 结果 |
|---|---|---|
| 61620be (v3.18.10 tag) | 空 | exit 1 `REFUSING: preflight pinned no master SHA` |
| 61620be | 61620be(preflight 真钉的值) | **exit 0** `OK: contains the master preflight admitted` |
| 61620be | def4275(竞态中落地的 commit) | exit 1 `does not contain the master preflight admitted` |
| 2802b44 (真正落后的 commit) | 61620be | exit 1 `does not contain...` ← **守卫仍有牙** |

第 2 行就是被烧掉的那次:旧逻辑 REFUSE、新逻辑 PASS。
第 4 行确认这次改动**没有**削弱守卫:真正落后的 tag 照样拒,且它在 preflight 就会被拦下,
子包根本不会发。

反变异:把 `master_sha` 输出改成空串 → release 步骤 exit 1(不是静默放行),已还原。
`yaml.safe_load` 解析通过,10 个 job 结构不变。
@deepcoldy

Copy link
Copy Markdown
Owner Author

Review (pi bot, automated). ⚠️ Same shared-identity caveat: this account is the PR author, so this is a comment, not an approval.

LGTM. Independently verified both questions:

Q1 (fail-closed won't block normal releases): the if-conditions are correctly paired.

  • preflight's master_ancestry step: if: steps.dist_tag.outputs.tag == 'latest' — sets master_sha only for stable releases.
  • release's ancestry check: if: steps.dist_tag.outputs.tag == 'latest' — checks PINNED_MASTER only for stable releases.
  • For canary/beta/rc: BOTH steps skip. master_sha is empty, but the check never runs. No false-positive blocking. ✅

Q2 (semantic relaxation is acceptable — and strictly more correct):

  • Old: "tag must contain the LATEST master at release time" — re-reads a moving ref, TOCTOU-prone, burned v3.18.10.
  • New: "tag must contain the master that was current when preflight ADMITTED the release" — pinned, immutable within a run.
  • The relaxation only affects commits landing in the ~5min window between preflight and release. Those commits are NOT in the tagged code (the tag predates preflight), so they can't affect the release — they belong to the next release anyway. Burning a version number to exclude them buys zero security. The old behavior was the bug, not a feature. ✅

Additional checks:

  • git fetch origin "$PINNED_MASTER" (fetch by SHA) is correct — the pinned commit was just origin/master's tip, so it's reachable.
  • Fail-closed on empty PINNED_MASTER is the right default.
  • The mutation evidence (empty master_sha → exit 1) confirms the guard has teeth.
  • The 4-scenario shell test (empty pin / matching pin / mismatched pin / genuinely stale HEAD) covers the right matrix.
  • Comments document the measured TOCTOU timeline (09:53:46 → 09:58:33 UTC) — the timezone correction is noted and correct.

The root cause analysis is also right: binary-subpackages MUST publish before the main package (optional deps are silent npm no-ops), so the publish order is correct — the bug was the guard reading a moving ref, not the order.

#1157 的修复本身没有任何自动化保护:本仓库**没有 workflow lint**,`release.yml` 改坏了
CI 一律绿,只有真的推 tag 才会发现——而那时一个版号已经烧掉了。这个测试把「发布准入决定
必须是 preflight 已经做过的那个」钉下来。

5 条断言,每条对应一种「悄悄改回旧行为、其它检查全绿」的退化:

| 断言 | 防的退化 |
|---|---|
| 子包 job 先于 release | 顺序被倒过来(它是 TOCTOU 危险的前提,且 optional dep 是 npm 静默 no-op) |
| preflight 钉住 SHA 并 export | 不再钉 ⟹ release 无从校验 |
| release 校验 `$PINNED_MASTER` 且不读 `origin/master` | 原样退回 TOCTOU |
| 空 pin 时 fail-closed | 缺 pin 变成静默放行 |
| 两侧 `if:` 条件一致 | 两边 gate 漂移 |

## 反变异(5/5 全红)

| 变异体 | 结果 |
|---|---|
| M1 release 退回 `--is-ancestor origin/master` | 1 failed ✓ |
| M2 preflight 删掉 `master_sha=$(git rev-parse …)` | 1 failed ✓ |
| M3 `if [ -z "$PINNED_MASTER" ]` 改成 `if false` | 1 failed ✓ |
| M4 preflight output 改成空串 | 1 failed ✓ |
| M5 release 加回 `git fetch origin master` | 1 failed ✓ |

还原后 5 passed。

## 实现细节

- 用 `js-yaml` 解析结构(job 依赖、step id、`if:`、`env:`),用文本匹配断言具体命令——
  结构合法但跑别的命令的文件骗不过文本匹配。
- **注释先剥掉再匹配**:本文件的守卫注释里反复出现 `origin/master` 与
  `git fetch origin master`(在解释这个 bug),不剥的话「不得重读 origin/master」这条
  会被那段警告注释本身满足。这个坑 `ci-musl-gate.test.ts` 已经踩过,沿用它的做法。

两个 runner 均 5/5(vitest + bun test),且该文件在 bun 腿的可跑集内。
@deepcoldy

Copy link
Copy Markdown
Owner Author

Re-review of dfe3cfa89 (added test file). LGTM maintained.

The test file is well-designed:

  1. stripComments is the right call — the guard comments themselves quote origin/master and git fetch origin master while explaining the bug. Without stripping, the "does not re-read" assertion would be satisfied by the warning prose. The ci-musl-gate.test.ts precedent is correctly followed.

  2. 5 assertions map 1:1 to real regressions:

    • Subpackage-before-release ordering (the TOCTOU precondition)
    • preflight pins + exports the SHA
    • release checks $PINNED_MASTER, never re-reads origin/master
    • Empty pin fails closed
    • Both gates share the same if: condition
  3. Mutation verified: adding git fetch origin master back to the release step → 1 test fails (the "never re-reads" assertion). The tests have teeth, not decoration.

  4. Both runners pass: vitest 5/5, bun test 5/5.

  5. Step names match (Guard — stable release must contain the latest master in both jobs), js-yaml is available, import.meta.dirname works.

This fills the "zero automated protection" gap the original fix had — without it, a future refactor could silently restore the TOCTOU and CI would stay green until a real tag push burned another version.

@deepcoldy
deepcoldy merged commit 7bd2e97 into master Sep 1, 2026
8 of 9 checks passed
@github-actions

github-actions Bot commented Sep 2, 2026

Copy link
Copy Markdown

🚀 Released in v3.18.12

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(release): binary-subpackages 在 release 守卫之前发布,任何被守卫拦下的发版都会永久污染该版号

1 participant