Repository navigation
Stop putting the release token and version in a shell word - #1730
HailLucifer666 wants to merge 4 commits into
Conversation
THE BUG. Both release scripts built a curl command by interpolating
process.env token and argv[0] into a string and ran it with execSync, so:
- the token sat in the command line of a child process, readable by any
local process for as long as curl ran;
- versionType comes straight from argv and was placed inside a single-quoted
shell word. A `'` in it closed the quote and the rest of the argument was
executed by the shell.
Reproduced before the change: `x'; printf INJECTED > f #` created the file
under a POSIX shell, and an unrelated process listing found exactly one
process exposing the token. After the change the same query finds zero.
THE FIX. The request is built as data — token only in the Authorization
header, body via JSON.stringify — and sent with fetch instead of a shell. Both
triggerWorkflow functions take an injectable fetch so the tests can see what
reaches the network. Each file is guarded so importing it no longer starts a
release (import.meta.main / require.main).
Also fixed, because it is the same function's failure path: success was
decided by whether the response body contained "workflow_dispatch", so a 401
or a 500 printed "🎉 Release workflow triggered!" and the maintainer believed
a release was running. The status code is now the answer.
SCOPE. Missing-token handling is unchanged (still exits 1, deliberately
untested — see the test header). Release workflow files and the private repo
untouched.
Verified: cli 9/9 and sdk 8/8 targeted tests; five mutations run and recorded
in cli/src/__tests__/release-script.test.ts.
Co-authored with free-pi (GLM)
Probe (b) — rebuilding the body by interpolation instead of JSON.stringify — stayed GREEN against the first version of these tests, so the guard could not see the exact defect it was written for. The reason, measured: for `x'; rm -rf ~ #` the interpolated string and the JSON.stringify output are byte-identical, because JSON never needed to escape a single quote. The shell payload proves the value survives the trip; it cannot prove the serialiser is doing anything. Added the input that does: a versionType containing `"`, where interpolation produces a different (and invalid) document. Probe (b) now fails on that test and nothing else, and the mutation record in the test header says so. Co-authored with free-pi (GLM)
The missing-token branch was recorded as deliberately untested because error() calls process.exit and a test cannot survive that. Spying on exit is the way out, so it is tested now: the refusal happens, names CODEBUFF_GITHUB_TOKEN, and — the part that matters — sends nothing. A refusal that still dispatches is the bug wearing a fix's clothes. Probe (f) in the test header breaks the branch to prove the test can see it. Removed `?? process.env.GITHUB_TOKEN` from both: I added it, nothing routes through it, and checkGitHubToken has always required CODEBUFF_GITHUB_TOKEN. A second accepted source is surface with no caller (GATES E). Co-authored with free-pi (GLM)
THE BUG.
Three files dispatch a GitHub release workflow. Two were fixed in this branch
(`cli/scripts/release.ts`, `sdk/scripts/release.js`). The third,
`freebuff/cli/release.ts`, was not: it still built a `curl` command line and
handed it to `execSync`, with the token and the request payload interpolated
into a single-quoted shell word.
WHY THIS ONE IS WORSE THAN THE OTHER TWO.
`payload` is `JSON.stringify({ref:'main',inputs:{version_type, checkout_ref}})`
and JSON never needs to escape a single quote, so the payload arrives in the
shell word with its quotes intact. `versionType` was already an argv value in
the other copies; this script adds `--ref`, a second argv value, into the same
word. A `'` in either one closes the quote and the rest executes. The token in
that command line was also readable from the process list for as long as curl
ran.
THE FIX.
The same shape the other two copies now use: `buildDispatch()` returns
`{url, init}` and `triggerWorkflow()` awaits `fetch`. No shell parses anything,
so a hostile `--ref` is a string in a JSON body. The success test is the status
code (204) rather than "does the response text contain workflow_dispatch" — a
401 used to print a celebration. `process.env.GITHUB_TOKEN = token` is gone; it
existed only so the interpolated curl command could read it. The entrypoint is
guarded with `import.meta.main` so the file can be imported by a test.
PROOF.
`cli/src/__tests__/release/freebuff-release-script.test.ts` — 10 tests, each
mutation recorded in its header and each red for the stated reason before the
commit:
(a) token moved into the URL -> "token only in a header" red
(b) body built by interpolation -> the quote case and the --ref
case red
(c) checkoutRef dropped from the inputs -> the --ref case red
Together with the two existing release-script suites: 31 pass / 0 fail.
SCOPE.
This is a fix for the copy the earlier sweep missed, not a refactor of the
three into one shared module. They dispatch different workflows with different
inputs and live in three packages; collapsing them is a separate change with
its own review. Nothing about which workflow runs, or its inputs, changed.
Co-authored-by: pi <noreply@pi.dev>
|
This is a genuine fix for a real problem: The bonus fix (checking The The test suites are thorough — they pin the exact defects (injection string surviving in a shell word vs. JSON data, token placement, status-code-based success) and include a documented mutation-testing process, which is unusually rigorous for a script this size. The commentary explaining why the first version of a test couldn't distinguish JSON-encoding from interpolation for single-quote-only payloads is a nice catch, showing real care rather than AI-generated padding. Minor notes for the maintainer doing the port: the workflow URLs/workflow names ( Worth porting as-is, modulo matching it against whatever the private repo's actual release script layout looks like now. |
|
One addition since your review: the third copy of the release trigger, It is the same defect class and slightly worse than the other two: the payload carries |
Issue
Three files dispatch a GitHub release workflow by interpolating
CODEBUFF_GITHUB_TOKENand argvvalues into a curl command string, then running it with
execSync:cli/scripts/release.ts,sdk/scripts/release.js, andfreebuff/cli/release.ts. Two problems, both reproduced on 2026-11-26before any edit:
curl runs. Measured: with the old construction, an unrelated process listing found exactly one
process exposing a freshly generated token. With the request-object form below, the same query
found zero — the token stays in the script's own memory.
versionTypeis shell data, not JSON data. It comes straight fromargvand sits inside asingle-quoted shell word, so a
'in it closes the word. Reproduced withversionType = "x'; printf INJECTED > repro-injected.txt #"under a POSIX shell (the platform arelease is cut from): the file was created.
The third copy is worse than the other two: its payload also carries
--ref, a second argv value,into the same single-quoted word. A
'in either value closes the quote.Third defect, same function, found while reading the failure path: success was decided by whether the
response body contained
workflow_dispatch. A 401 (Bad credentials) or a 500 does not, so thescript printed
🎉 Release workflow triggered!and the maintainer believed a release was running.freebuff/cli/release.tswas added in a second push (bd2b540d) after the first two files werealready under review. The original sweep missed it because it lives under
freebuff/cli/, notcli/scripts/orsdk/scripts/. Saying so rather than amending the earlier history.Fix
The request is built as data — token only in the
Authorizationheader, body viaJSON.stringify—and sent with
fetch.triggerWorkflowtakes an injectable fetch so a test can see exactly whatwould reach the network. The status code decides success:
204only. All three files are guarded(
import.meta.main/require.main === module) so importing them no longer fires a release — withoutthat, a test import with no token called
process.exit(1)from inside the import.The
process.env.GITHUB_TOKEN = tokenwrite is gone: its only reader was the curl string.Verification
Sixteen mutations recorded across the three headers (cli 6, sdk 5,
freebuff/cli5), each applied,diffed to confirm it applied, watched go red, then reverted. One is worth flagging: rebuilding the
body by interpolation instead of
JSON.stringifystayed green against the first version of thesetests. For
x'; rm -rf ~ #the two are byte-identical, because JSON never needed to escape a singlequote. That is why the tests also carry a value containing
"; that input is the one that sees themutation. The third copy adds
checkoutRefto that list: dropping it from the inputs turns "the refreaches the body" red.
Also run as scripts, not just as modules: all three reach
main()and exit with the missing-tokenmessage, so the guard does not silently disable the release.
Out of scope
process.exit(1)), and is tested by spying onexit: therefusal happens, names
CODEBUFF_GITHUB_TOKEN, and sends nothing.execSyncsite interpolatesprocess.env(checked all 71); the three files herewere the only ones putting a credential in a shell string.
different inputs and live in three packages. Collapsing them is its own change with its own review.