fix(rotate-secret): 修复 tr/head 管道 SIGPIPE 导致 rotate-secret.sh 静默退出 - #14
Open
Davidasx wants to merge 1 commit into
Open
fix(rotate-secret): 修复 tr/head 管道 SIGPIPE 导致 rotate-secret.sh 静默退出#14Davidasx wants to merge 1 commit into
Davidasx wants to merge 1 commit into
Conversation
`tr -dc 'A-Za-z0-9' </dev/urandom | head -c "$LENGTH"` 中,head 读满 指定字节后即关闭管道退出,而 tr 面对 /dev/urandom 的无限流仍在继续写入, 因而被 SIGPIPE 终止,退出码 141。脚本开头设置了 set -o pipefail,整条管道 的退出码取自失败的 tr,再叠加 set -e,脚本便在第一条 echo 之前就终止。 表现为运行 `bash scripts/rotate-secret.sh` 没有任何输出直接退出, 退出码 141,secret 也不会被更新。 改为先用 head 从 /dev/urandom 读取有限字节再交给 tr 过滤,使 tr 的输入 自然结束、不再写入已关闭的管道;外层循环累积直到满足长度要求,以补偿 过滤非字母数字字符造成的损耗。 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
该 PR 修复了 scripts/rotate-secret.sh 在 set -euo pipefail 下因 tr | head 管道触发 SIGPIPE(退出码 141)而导致脚本在输出任何提示前静默退出的问题,从而确保 npm run rotate-secret:sh 能稳定生成并更新 Cloudflare Worker Secret。
Changes:
- 将随机串生成逻辑从
tr ... </dev/urandom | head -c ...改为先head截断输入、再tr过滤的方式,避免SIGPIPE。 - 通过循环累积过滤结果,保证最终输出长度满足
--length指定值,并在最后截取到目标长度。
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题
运行
npm run rotate-secret:sh(即bash scripts/rotate-secret.sh)时,脚本没有任何输出就直接退出,secret 不会被更新:退出码 141 = 128 + 13,即进程被
SIGPIPE终止。原因
问题在
scripts/rotate-secret.sh生成随机串这一行:RAND_RAW="$(LC_ALL=C tr -dc 'A-Za-z0-9' </dev/urandom | head -c "$LENGTH")"head -c "$LENGTH"读满指定字节数后立即关闭管道并退出;tr面对/dev/urandom这个无限流仍在继续写入,于是被SIGPIPE杀掉,退出码 141;set -euo pipefail中,pipefail使整条管道的退出码取自失败的tr(141),-e随即终止整个脚本。由于这一行位于第一条
echo之前,用户看到的现象就是「什么都没输出直接退出」,很难定位。修复
先用
head从/dev/urandom读取有限字节,再交给tr过滤 —— 这样tr的输入自然结束,不会再写入已关闭的管道。因为过滤会丢弃非字母数字字符,外层用循环累积直到长度满足要求,再截取到目标长度:验证
修复后单独验证生成逻辑(在
set -euo pipefail下):长度正确、字符集仍为 URL-safe 的
[A-Za-z0-9],退出码 0。-l/--length自定义长度的行为不变。备注
tr与head的这一竞争在 GNU coreutils 下几乎必然触发。wrangler secret put调用、结尾提示)均未改动。scripts/rotate-secret.ps1(Windows 版)为独立实现,不受此问题影响,本 PR 未涉及。🤖 Generated with Claude Code