Skip to content

fix(claude-ops): route large jq payloads via --slurpfile, not argv - #1380

Merged
kyle-sexton merged 1 commit into
mainfrom
fix/1336-fleet-state-argv-length
Jul 25, 2026
Merged

kyle-sexton merged 1 commit into
mainfrom
fix/1336-fleet-state-argv-length

Conversation

@kyle-sexton

Copy link
Copy Markdown
Member

Closes #1336

Summary

  • fleet-state.sh (claude-ops plugins skill) embedded catalog/installed/enabled JSON blobs as
    literal jq --argjson command-line arguments; a large enough marketplace catalog (confirmed
    against a real 273-plugin catalog) exceeded the platform/shell's argv-length ceiling and jq
    crashed with Argument list too long before emitting anything, silently dropping that
    marketplace from sync/audit/converge. Confirmed on Windows Git Bash/MSYS jq, but the
    underlying ceiling is a real OS ARG_MAX limit everywhere, just reached sooner there.
  • Every affected call site now writes its payload to a temp file and passes --slurpfile instead
    of --argjson, so catalog size never determines whether a marketplace can be synced. Only the
    fixed-size boolean --argjson uses (au/ci/autoUpdate) remain untouched.
  • Two follow-on defects surfaced during verification, both fixed in the same change:
    • Temp files are cleaned up via one per-run directory removed by an EXIT trap, not an array —
      each writer call runs inside a $(...) subshell, so an array append there would silently
      vanish on return.
    • A malformed source file (e.g. settings.json) used to make the affected --argjson fail loud
      immediately; --slurpfile instead tolerates a genuinely empty payload as "zero JSON values"
      and would have silently degraded the report to null fields. The writer now emits a
      deliberately-invalid sentinel for an empty payload so jq's own parser still errors at the call
      site, preserving the pre-fix fail-loud behavior.
  • Bumped claude-ops to 0.19.3 with a matching CHANGELOG.md entry.

Test plan

  • fleet-state.test.sh (31 cases, 0 failed), including two new cases added by this PR:
    • A synthetic 500-plugin catalog case (past the real 273-plugin repro) — asserts no
      Argument list too long, exit 0, and full non-truncated output (catalog/installed/missing_*
      arrays all correctly sized, a spot-checked enabled entry correct).
    • A malformed user_settings.json case — asserts the fix still fails loud (nonzero exit), not
      silently degraded.
    • A static guard asserting the remaining --argjson count stays at the fixed booleans-only
      total, so a future edit can't silently reintroduce a large-payload --argjson call site.
  • Reproduced the actual pre-fix crash: extracted the pre-fix script from HEAD via git archive
    and ran it against the same synthetic 500-plugin fixture — confirmed Argument list too long
    before this fix, confirmed clean success after.
  • Verified the EXIT-trap temp-directory cleanup empirically (temp-dir count identical before and
    after a normal run — no leaked files).
  • shellcheck clean on fleet-state.sh.

Related

N/A

🤖 Generated with Claude Code

…of argv

fleet-state.sh embedded catalog/installed/enabled JSON blobs as literal
`jq --argjson` command-line arguments. For a marketplace catalog large
enough (confirmed against a real 273-plugin catalog), the serialized JSON
exceeded the platform/shell's argv-length ceiling and jq crashed with
"Argument list too long" before emitting anything, silently dropping that
marketplace from sync/audit/converge. Confirmed on Windows Git Bash/MSYS
jq, but the underlying ceiling is real everywhere.

Every affected call site now writes its payload to a temp file and passes
--slurpfile instead of --argjson, so catalog size never determines whether
jq can run. Two follow-on defects surfaced during verification and are
fixed in the same change: temp files are cleaned up via a single per-run
directory (a subshell-safe pattern, since each writer call runs inside a
$(...) subshell) rather than an array that would silently fail to track
them, and an empty payload (from a prior read of malformed source JSON)
writes a deliberately-invalid sentinel so jq's own parser still fails loud
at the call site, preserving the pre-fix fail-loud behavior instead of
silently degrading the report to null fields.

Closes #1336

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@claude

claude Bot commented Jul 25, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @kyle-sexton's task in 4m 21s —— View job


Code Review

  • Gather context and understand the request
  • Read changed files
  • Review fleet-state.sh for correctness, security, and shell best practices
  • Review fleet-state.test.sh for test quality and coverage
  • Review plugin.json and CHANGELOG.md

Overall: well-implemented fix — correct root cause, clean solution, thorough tests. A few minor observations below, none blocking.


fleet-state.sh — core change

Root cause diagnosis and approach are correct. Routing large JSON payloads through mktemp files and --slurpfile instead of passing them as argv is the right portable fix. Process substitution (<(...)) is explicitly ruled out in the comment for good reason — native-Windows jq does not reliably read /dev/fd-style paths produced by Git Bash.

EXIT trap cleanup is correct. The single-directory-per-run approach elegantly sidesteps the subshell problem: $(jq_slurp_tmpfile ...) runs the function in a subshell, so an array append there would be silently discarded. Deleting the entire _FLEET_STATE_TMPDIR on EXIT is both subshell-safe and exit-path-complete (normal, exit, set -u error).

fleet-state.sh L134–138

Empty-sentinel design preserves fail-loud behavior. With --argjson name "", jq immediately rejects an empty value. --slurpfile treats an empty file as "zero JSON values" (yielding null) rather than an error, so a naive conversion would silently degrade the report. Writing FLEET_STATE_INVALID_EMPTY_PAYLOAD forces jq's own parser to error at the consumer call site. This is the right mechanism and the test case at the end of the test file confirms it.

fleet-state.sh L140–158

--slurpfile array-wrap semantics are applied consistently. Every converted call site correctly accesses $name[0] where it previously read $name, and the final output assembly dereferences all seven slurpfile bindings. Verified at: L470–476

Static guard count is correct. My independent count of non-comment --argjson occurrences comes to 7: three --argjson ci (case-insensitive OS boolean), three --argjson au (error-path autoUpdate booleans in emit_marketplace), and one --argjson autoUpdate in the final assembly. All are fixed-size booleans, none catalog-sized. This matches the assert_eq "7" in the test.

Minor observations (not blocking)

jq_slurp_tmpfile mktemp failure mode. If mktemp fails (disk full, exhausted inodes), the function exits return 1 without printing anything, so the $(jq_slurp_tmpfile ...) substitution captures "". The caller then passes --slurpfile name "" to jq, which emits a file-not-found error instead of the clear ERROR: could not create a temp directory... message. This is an extreme edge case and the failure is still loud — just less diagnostic. If the author wants to address it, the fix would be to have jq_slurp_tmpfile print an explicit sentinel path-like token that is always non-empty, or to handle mktemp failure at the call sites.

fleet-state.sh L142 — not worth blocking the PR over.

Repeated writes of identical values. $explicit_false_ids is passed to jq_slurp_tmpfile twice (for missing_from_install and missing_from_user_install), and similarly several other values are repeated at the final assembly. Each call creates a fresh temp file with identical content. This is a minor inefficiency (two small writes instead of one), not a correctness issue. Acceptable given the fix's scope.


fleet-state.test.sh — new test cases

All three new cases are well-constructed:

Large-catalog case (500 plugins): Uses realistic-length IDs to match real-world payload sizes. Asserts not just exit 0 but also semantic correctness — correct counts for catalog, installed, all three missing_from_* arrays, and a spot-checked enabled value. This is stronger than a smoke test. The comment explaining WHY 500 was chosen (past the 273-plugin repro) and WHY the assertions are shaped the way they are is good test documentation.

Malformed user_settings.json case: Correctly locks in pre-existing fail-loud behavior. The comment explaining the --slurpfile silent-degradation risk (would yield null without the sentinel) is the key insight and is captured clearly.

Static guard: Using grep -v '^\s*#' "$SCRIPT" | grep -c -- '--argjson' to count non-comment occurrences is a solid structural assertion. The || true at the end correctly handles grep's exit code 1 when count is 0. One note: the magic constant 7 will fail as a false positive if a new legitimate fixed-boolean --argjson is added, but that's intentional friction (the comment explains this) and the right trade-off for a regression guard.

Coverage gap (minor): The --all mode has its own jq_slurp_tmpfile call at the result accumulation step (L536–538) that isn't exercised by the new large-catalog case (which uses --marketplace). This shares the same emit_marketplace code path, so the risk is low — but a --all variant of the large-catalog test would cover the final accumulation loop as well. Not a blocker, just an observation.


plugin.json / CHANGELOG.md

Version bump 0.19.2 → 0.19.3 is correctly scoped as a patch (bug fix only). The CHANGELOG entry is detailed, accurate, and cross-references the issue number. Both look good.


Summary: The fix is correct and safe to merge. The empty-sentinel guard, EXIT trap cleanup design, and static regression guard are all well-reasoned decisions. The two minor points above (mktemp failure diagnostics and --all coverage) are low-priority follow-ups at the author's discretion.

@claude

claude Bot commented Jul 25, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @kyle-sexton's task in 2m 23s —— View job


Security Review — PR #1380

  • Gather context from PR description and changed files
  • Read fleet-state.sh (primary change — temp file handling, jq invocations)
  • Read fleet-state.test.sh (new test cases)
  • Analyze for injection surfaces, temp file races, logic flaws, unsafe shell patterns
  • Post security findings

No security issues found.

I reviewed all four changed files (fleet-state.sh, fleet-state.test.sh, plugin.json, CHANGELOG.md) against the security surfaces relevant to this change. Here is the reasoning for each area checked:

Temp file handling (primary change surface)

The temp directory is created with mktemp -d, which produces a directory with mode 700 — only the owning user can write into it. Files created inside via mktemp "$_FLEET_STATE_TMPDIR/payload.XXXXXX" (line 142) inherit the same ownership constraints. There is no TOCTOU or symlink-attack window: the directory is exclusively owned from creation, and mktemp produces uniquely named files atomically.

Exit-trap cleanup

trap 'rm -rf "$_FLEET_STATE_TMPDIR"' EXIT (line 138) uses single quotes so the variable expands at trap-fire time, not at set time — correct. The trap is only registered after mktemp -d succeeds (the || { exit 2 } on the prior lines ensures the script never reaches the trap registration on failure). set -uo pipefail is active throughout, so if _FLEET_STATE_TMPDIR were somehow unset, bash's -u flag would generate an "unbound variable" error rather than silently expanding to an empty string and running rm -rf / or rm -rf "".

Command / argument injection

printf '%s' "$1" >"$f" (line 155) writes the JSON payload without interpreting it. The file path passed to jq via --slurpfile is always a mktemp-generated path (alphanumeric random suffix under a controlled directory) — no caller-controlled characters reach the jq argument list. The --argjson uses retained in the diff (autoUpdate, au, ci) pass only the literals true or false, confirmed by the static count guard in the test.

Data sensitivity in temp files

The data routed through temp files is enabledPlugins maps (plugin-ID-to-boolean), catalog metadata, and install records — no tokens, no credentials, no PII. Temp file mode 600 is appropriate for this content.

Sentinel for empty payloads

The FLEET_STATE_INVALID_EMPTY_PAYLOAD sentinel (line 153) is not a security control but a correctness one; it preserves pre-existing fail-loud behavior when an upstream jq read on a malformed source file produces empty output. There is no way for user-supplied content to bypass or spoof this path (the [[ -z "$1" ]] check happens before writing anything).

_FLEET_STATE_TMPDIR environment poisoning

The variable is set unconditionally on line 134, overwriting any ambient environment value of the same name before the trap is registered. An attacker-controlled pre-existing _FLEET_STATE_TMPDIR in the environment has no effect.


@kyle-sexton
kyle-sexton merged commit abdd511 into main Jul 25, 2026
1 check passed
@kyle-sexton
kyle-sexton deleted the fix/1336-fleet-state-argv-length branch July 25, 2026 20:48
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.

claude-ops fleet-state.sh crashes on large-catalog marketplaces (Windows jq arg-length limit)

1 participant