Skip to content

Stop putting the release token and version in a shell word - #1730

Open
HailLucifer666 wants to merge 4 commits into
CodebuffAI:mainfrom
HailLucifer666:fix/release-workflow-dispatch
Open

HailLucifer666 wants to merge 4 commits into
CodebuffAI:mainfrom
HailLucifer666:fix/release-workflow-dispatch

Conversation

@HailLucifer666

@HailLucifer666 HailLucifer666 commented Nov 25, 2026 •

Copy link
Copy Markdown

Issue

Three files dispatch a GitHub release workflow by interpolating CODEBUFF_GITHUB_TOKEN and argv
values into a curl command string, then running it with execSync: cli/scripts/release.ts,
sdk/scripts/release.js, and freebuff/cli/release.ts. Two problems, both reproduced on 2026-11-26
before any edit:

  1. The token is in the command line. Anything that can list processes reads the credential while
    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.
  2. versionType is shell data, not JSON data. It comes straight from argv and sits inside a
    single-quoted shell word, so a ' in it closes the word. Reproduced with
    versionType = "x'; printf INJECTED > repro-injected.txt #" under a POSIX shell (the platform a
    release 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 the
script printed 🎉 Release workflow triggered! and the maintainer believed a release was running.

freebuff/cli/release.ts was added in a second push (bd2b540d) after the first two files were
already under review. The original sweep missed it because it lives under freebuff/cli/, not
cli/scripts/ or sdk/scripts/. Saying so rather than amending the earlier history.

Fix

The request is built as data — token only in the Authorization header, body via JSON.stringify —
and sent with fetch. triggerWorkflow takes an injectable fetch so a test can see exactly what
would reach the network. The status code decides success: 204 only. All three files are guarded
(import.meta.main / require.main === module) so importing them no longer fires a release — without
that, a test import with no token called process.exit(1) from inside the import.

The process.env.GITHUB_TOKEN = token write is gone: its only reader was the curl string.

Verification

cli/src/__tests__/release-script.test.ts                   11 pass 0 fail
sdk/src/__tests__/release-script.test.ts                   10 pass 0 fail
cli/src/__tests__/release/freebuff-release-script.test.ts  10 pass 0 fail
                                                           31 pass 0 fail
cli typecheck                              13 errors, identical set on main, none in these files
sdk build                                  exit 0
grep -n 'execSync(' <all three release scripts>   no matches (the word itself survives only
                                                 in comments describing the old code)

Sixteen mutations recorded across the three headers (cli 6, sdk 5, freebuff/cli 5), each applied,
diffed to confirm it applied, watched go red, then reverted. One is worth flagging: rebuilding the
body by interpolation instead of JSON.stringify stayed green against the first version of these
tests. For x'; rm -rf ~ # the two are byte-identical, because JSON never needed to escape a single
quote. That is why the tests also carry a value containing "; that input is the one that sees the
mutation. The third copy adds checkoutRef to that list: dropping it from the inputs turns "the ref
reaches the body" red.

Also run as scripts, not just as modules: all three reach main() and exit with the missing-token
message, so the guard does not silently disable the release.

Out of scope

  • Missing-token handling is unchanged (process.exit(1)), and is tested by spying on exit: the
    refusal happens, names CODEBUFF_GITHUB_TOKEN, and sends nothing.
  • No other non-test execSync site interpolates process.env (checked all 71); the three files here
    were the only ones putting a credential in a shell string.
  • The three copies are not refactored into one shared module: they dispatch different workflows with
    different inputs and live in three packages. Collapsing them is its own change with its own review.
  • The release workflow YAMLs and the private repo are untouched.

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)
HailLucifer666 and others added 2 commits November 26, 2026 05:26
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>
@codebuff-team

Copy link
Copy Markdown
Contributor

This is a genuine fix for a real problem: cli/scripts/release.ts and sdk/scripts/release.js built a curl command via string interpolation, which (a) exposed CODEBUFF_GITHUB_TOKEN in the process list for the duration of the curl call, and (b) let an unsanitized versionType argv value break out of a single-quoted shell word. The fix — replacing execSync+curl with fetch and a JSON.stringify-built body, with the token only in the Authorization header — is the right approach and removes an entire class of injection risk rather than patching around it.

The bonus fix (checking response.status === 204 instead of string-matching workflow_dispatch in the body) is correct and catches a real false-positive-success bug: a 401/500 previously printed a celebratory message.

The import.meta.main / require.main === module guards are a sensible and minimal addition to make the files testable without side effects.

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 (cli-release-prod.yml, sdk-release.yml) are hardcoded as before, so no regression there. The PR is large by line count but almost entirely test code for a two-file production change, which is appropriate for a security-relevant script with no prior coverage.

Worth porting as-is, modulo matching it against whatever the private repo's actual release script layout looks like now.

@codebuff-team codebuff-team added bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree labels Nov 26, 2026
@HailLucifer666

Copy link
Copy Markdown
Author

One addition since your review: the third copy of the release trigger, freebuff/cli/release.ts, was still building the curl command line. It was missed by the original sweep because it lives under freebuff/cli/, not cli/scripts/ or sdk/scripts/. Fixed in bd2b540d with its own suite at cli/src/__tests__/release/freebuff-release-script.test.ts (10 tests, 5 recorded mutations).

It is the same defect class and slightly worse than the other two: the payload carries --ref as a second argv value into the same single-quoted word. All three suites now pass together: 11 + 10 + 10 = 31, grep execSync in the three files returns nothing. The PR body is updated to match.

This branch has not been deployed

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

Labels

bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants