@memnexus-ai/mx-agent-cli 0.1.233 → 0.1.235

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -22,6 +22,15 @@
22
22
  # `git rebase origin/main`, and settings.json also hard-denies `git merge *` at
23
23
  # the permission layer, which fires before this hook).
24
24
  #
25
+ # CAVEAT on that second layer (security review, #4569 — do not read it as full
26
+ # cover): the settings.json pattern `Bash(git merge *)` requires an ARGUMENT, and
27
+ # this hook's merge rule matches `merge\s`, which also requires one. A bare
28
+ # `git merge` with no argument therefore passes BOTH layers. With an upstream
29
+ # configured it merges that upstream. It is left open deliberately: tightening
30
+ # to `merge(\s|$)` would newly block prose ending on the verb, and bare
31
+ # `git merge` is a shape essentially nobody types. Stated here so no reader
32
+ # concludes the permission layer closes what this rule does not.
33
+ #
25
34
  # Requires environment variables set by `worktree start`:
26
35
  # - CLAUDE_WORKTREE_PATH: Absolute path to worktree (e.g., /workspace/.worktrees/foo)
27
36
  # - CLAUDE_WORKTREE_NAME: Worktree name (e.g., foo)
@@ -31,11 +40,24 @@ set -euo pipefail
31
40
  # Read the tool input from stdin (JSON format)
32
41
  INPUT=$(cat)
33
42
 
34
- # Extract the command - handle both direct command and tool_input formats
43
+ # Extract the command - handle both direct command and tool_input formats.
44
+ #
45
+ # EXIT-CODE NOTE (security review, #4569 — the previous comment was imprecise).
46
+ # There are two distinct "can't parse" paths and they do NOT exit the same way:
47
+ # * Well-formed JSON with no command field (`{}`, empty stdin) -> jq succeeds
48
+ # and prints nothing -> the guard below exits 0.
49
+ # * MALFORMED JSON (`not json`) -> jq itself fails. There is no `|| true` here
50
+ # (unlike git-mutation-guard.sh:61), so under `set -e` the assignment aborts
51
+ # the script with jq's exit code, which is 5.
52
+ # Both are non-blocking: Claude Code blocks only on exit 2, so exit 5 still lets
53
+ # the command run, just noisily. The behaviour is left as-is rather than
54
+ # "fixed" to 0, because a malformed payload is a harness bug worth surfacing,
55
+ # not something to swallow silently. Both paths are pinned by tests so this is a
56
+ # decision rather than an accident.
35
57
  COMMAND=$(echo "$INPUT" | jq -r '.tool_input.command // .command // empty' 2>/dev/null)
36
58
 
37
59
  if [[ -z "$COMMAND" ]]; then
38
- # Can't parse command, allow it (fail open)
60
+ # No command field in a well-formed payload — nothing to inspect, allow.
39
61
  exit 0
40
62
  fi
41
63
 
@@ -89,16 +111,70 @@ strip_heredocs() {
89
111
 
90
112
  SCAN=$(printf '%s' "$COMMAND" | strip_heredocs)
91
113
 
114
+ # ============================================================
115
+ # Segment anchors + wrapper-prefix run (backport from git-mutation-guard)
116
+ # ============================================================
117
+ # BYPASS HARDENING (#4569, Part A): both rules below anchor their verb to a
118
+ # "command-segment start". A bare anchor is defeated by anything that can
119
+ # legally sit between the separator and the verb, so both rules now use the same
120
+ # prefix machinery (WRAPPRE) that git-mutation-guard.sh's GITSEG already uses
121
+ # (git-mutation-guard.sh line ~300, introduced by af94a2d / #3934 and hardened
122
+ # by #3912). WRAPPRE is a mechanical backport — its alternatives are copied from
123
+ # GITSEG character for character, not newly invented.
124
+ #
125
+ # THE TWO RULES DO NOT SHARE AN ANCHOR, deliberately (security review, #4569).
126
+ # An earlier revision gave both rules one shared anchor, which silently handed
127
+ # the cd rule a `$` alternative it never had. `$` was measured to kill ZERO
128
+ # tests — `$(cd …)` is already caught by the `(` alternative — while blocking
129
+ # the universal shell-transcript convention
130
+ # `mx memories create --content "shell transcript: $ cd /etc"`, which was
131
+ # allowed before. A character that catches nothing and costs real work does not
132
+ # belong in the anchor, so the cd rule keeps its own.
133
+ #
134
+ # CDANCHOR — identical to git-mutation-guard.sh's GITSEG anchor: start-of-line
135
+ # (grep is line-oriented, so embedded newlines are segment starts too), a
136
+ # separator `; & | ( {`, a backtick, or a `then`/`do`/`else` shell keyword.
137
+ # No `$`.
138
+ #
139
+ # GITANCHOR — CDANCHOR plus `$`. The `$` is NOT from GITSEG; it is PRE-EXISTING
140
+ # behaviour of this hook's git rule (the old `SEG`, added for SHOULD FIX 1) and
141
+ # is retained only so this change moves no git verdict. `{` and the
142
+ # then/do/else keywords are new to the git rule and ARE from GITSEG; dropping
143
+ # `{` alone kills 1 test, dropping the backtick alone kills 2.
144
+ #
145
+ # Prose like "planned a git merge" or "first cd /etc" has a plain space before
146
+ # the verb, matches neither anchor, and stays allowed.
147
+ #
148
+ # WRAPPRE — an optional run of things a shell allows between the segment start
149
+ # and the actual command: leading redirections (`2>/dev/null `, `>out `),
150
+ # inline env-var assignments (`FOO=1 `, and repeats), and launcher/wrapper words
151
+ # that exec their argv (`sudo`, `env`, `nice`, `nohup`, `time`, `timeout`,
152
+ # `ionice`, `stdbuf`, `command`, `xargs`, `setsid`, `doas`, `nsenter`, with an
153
+ # optional numeric arg like `timeout 5`). Without this run, `FOO=1 cd /etc`,
154
+ # `env cd /etc`, `2>/dev/null cd /etc` and `sudo git -C <path> checkout main`
155
+ # all slipped past the anchor (verified bypasses, #4569). `echo` is deliberately
156
+ # NOT a wrapper word, so `echo "; cd /etc"` gains no new match from this.
157
+ #
158
+ # KNOWN GAP, declined for this change and inherited from GITSEG (SF-4/SF-5,
159
+ # security review #4569): a wrapper word carrying its OWN options escapes
160
+ # (`sudo -u root git checkout main`, `env -i cd /etc`,
161
+ # `timeout --preserve-status 5 …`), as do the shell builtins `exec`, `builtin`
162
+ # and `pushd` (`builtin cd /etc`). The identical gap exists in
163
+ # git-mutation-guard.sh, so closing it properly means changing both hooks with a
164
+ # false-positive corpus for each. Filed as follow-up, not bolted on here.
165
+ CDANCHOR='(^|[;&|({`]|\s(then|do|else)\s)\s*'
166
+ GITANCHOR='(^|[;&|({`$]|\s(then|do|else)\s)\s*'
167
+ WRAPPRE='([0-9]*[<>][^[:space:]]*\s+|[A-Za-z_][A-Za-z0-9_]*=[^[:space:];&|]*\s+|(sudo|env|nice|nohup|time|timeout|ionice|stdbuf|command|xargs|setsid|doas|nsenter)\s+([0-9]+\s+)?)*'
168
+
92
169
  # ============================================================
93
170
  # Rule 1: Block cd to paths outside worktree
94
171
  # ============================================================
95
172
  # FALSE-POSITIVE HARDENING (#3909): a `cd` only counts as real navigation when
96
- # it STARTS a command segment (CDSEG: start-of-line, or after a separator
97
- # `; & | ( {`, or a `then`/`do`/`else` shell keyword) — the same segment-anchor
98
- # approach as git-mutation-guard.sh's GITSEG (line ~148). Applied to the
99
- # already-heredoc-stripped SCAN. In ADDITION, an absolute target that is not a
100
- # directory ON THE LOCAL FILESYSTEM is ignored: a cd to a locally-nonexistent
101
- # path is harmless (it would just fail), and this is what removes the
173
+ # it STARTS a command segment (CDSEG: CDANCHOR + the optional WRAPPRE run
174
+ # above) — the same segment-anchor approach as git-mutation-guard.sh's GITSEG.
175
+ # Applied to the already-heredoc-stripped SCAN. In ADDITION, an absolute target
176
+ # that is not a directory ON THE LOCAL FILESYSTEM is ignored: a cd to a
177
+ # locally-nonexistent path is harmless (it would just fail), and this removes the
102
178
  # remote-payload false-positive class — the `cd` runs on a REMOTE host, not
103
179
  # here. Examples that must NOT block:
104
180
  # az vm run-command ... --scripts 'bash -c "... ; cd /home/azureuser/app ; ..."'
@@ -107,12 +183,56 @@ SCAN=$(printf '%s' "$COMMAND" | strip_heredocs)
107
183
  # Relative paths already fail open this way (empty RESOLVED below); this makes
108
184
  # absolute-path handling consistent. A REAL `cd /etc && ...` at a segment start
109
185
  # that resolves to an existing local dir outside the worktree still blocks.
110
- CDSEG='(^|[;&|({]|\s(then|do|else)\s)\s*'
186
+ #
187
+ # ACCEPTED RESIDUAL, deliberate — the backtick alternative in CDANCHOR (security
188
+ # review, #4569): keeping the backtick is worth 2 real detections
189
+ # (`` `cd /etc && ls` `` as a genuine command substitution), but a backtick is
190
+ # also markdown's code-span delimiter, so prose that quotes a command in a code
191
+ # span now blocks:
192
+ # gh pr create --body 'the command `cd /etc && ls` is blocked'
193
+ # The workaround is `--body-file`, which is already the documented answer for
194
+ # this hook family's prose false positives (KI-130-GUARD-QUOTESTRIP-DRIFT).
195
+ # Pinned by a test — see the "accepted residual" assertions in
196
+ # worktree-guard.test.sh. The durable fix is backporting
197
+ # git-mutation-guard.sh's `strip_sink_quotes` (#3938), which worktree-guard
198
+ # never received; filed as follow-up, out of scope here.
199
+ CDSEG="${CDANCHOR}${WRAPPRE}"
111
200
 
112
201
  if echo "$SCAN" | grep -qE "${CDSEG}cd\s+"; then
113
202
  # Extract cd target(s) that appear at a command-segment start only. The
114
203
  # grep match may carry a leading separator/keyword prefix; strip everything
115
204
  # up to and including the anchored `cd ` token, then strip surrounding quotes.
205
+ #
206
+ # ACCEPTED RESIDUAL, deliberate — the capture stops at `; & | < >` and NOT at
207
+ # `)` (SF-6 was proposed and REVERTED, #4569). Excluding `)` would make a bare
208
+ # subshell `(cd /etc)` block, because the target would come out as `/etc`
209
+ # instead of `/etc)` and pass the `-d` existence test below. It is not
210
+ # excluded, because that is the SAME trade already decided the other way for
211
+ # the checkout-main trailing boundary ~140 lines down: CDANCHOR treats a
212
+ # literal `(` as a subshell start regardless of quoting, so making `)` a
213
+ # terminator turns any parenthetical in prose into a command. Taking opposite
214
+ # sides of one trade in one file is how a guard becomes impossible to reason
215
+ # about, so both sides now match.
216
+ #
217
+ # What excluding `)` would have cost, measured — all previously allowed:
218
+ # git commit -m 'fix: close the (cd /etc) subshell hole'
219
+ # gh pr create --body "the bypass (cd /workspace) is now closed"
220
+ # echo "(cd /workspace/cli)"
221
+ # Note the first and third: `--body-file`, the workaround for this hook's
222
+ # other prose residuals, does NOT exist for `git commit -m` or `echo`.
223
+ #
224
+ # What it would have bought: exactly one shape — a bare `(cd /etc)` that
225
+ # changes directory and does nothing else, which is inert on its own. Anything
226
+ # with a second command still blocks, because `;` `&` `|` terminate the
227
+ # capture: `(cd /etc && ls)`, `(cd /etc; ls)` and `{ cd /etc; }` are all
228
+ # caught. Pinned by tests on both sides — the bare form asserted allowed as a
229
+ # declared residual, the compound forms asserted blocked so it stays bounded.
230
+ #
231
+ # `}` is likewise not excluded. Doing so would truncate `cd "${WT}/cli"` to
232
+ # `${WT`, which resolves empty and is skipped — a FAIL-OPEN, not a false
233
+ # positive. It is unnecessary regardless: a brace group must terminate on `;`,
234
+ # a newline or `&` before its closing `}`, so all brace-group forms already
235
+ # block on that separator.
116
236
  CD_TARGETS=$(echo "$SCAN" | grep -oE "${CDSEG}cd\s+[^;&|<>]+" \
117
237
  | sed -E 's/^.*\bcd[[:space:]]+//' \
118
238
  | tr -d '"'"'"'`' || true)
@@ -186,15 +306,98 @@ fi
186
306
  # (Belt-and-suspenders with settings.json deny rules)
187
307
  # ============================================================
188
308
 
189
- # Segment anchor: a command starts at ^, or right after a separator. Besides
190
- # ; & | (which cover ; && || and pipe/background), include ( ` and $ so a git op
191
- # inside a subshell `(git ...)`, backticks `` `git ...` ``, or command
192
- # substitution `$(git ...)` is still caught (SHOULD FIX 1). Prose like
193
- # "planned a git merge" has a plain space before "git" and stays allowed.
194
- SEG='(^|[;&|(`$])\s*'
309
+ # Segment anchor: GITANCHOR (defined above) — ^ or right after a separator.
310
+ # Besides ; & | (which cover ; && || and pipe/background), it includes ( { ` and
311
+ # $ so a git op inside a subshell `(git ...)`, a brace group `{ git ...; }`,
312
+ # backticks `` `git ...` ``, or command substitution `$(git ...)` is still
313
+ # caught (SHOULD FIX 1). Prose like "planned a git merge" has a plain space
314
+ # before "git" and stays allowed.
315
+ #
316
+ # GIT GLOBAL OPTIONS between `git` and the verb are consumed by GOPT (#4569,
317
+ # backported verbatim from git-mutation-guard.sh's GITSEG, #3934). git accepts
318
+ # global options before the subcommand — `-C <path>`, `-c <name>=<value>`,
319
+ # `--git-dir[=| ]<p>`, `--work-tree[=| ]<p>`, `--namespace`, `--super-prefix`,
320
+ # `--no-pager`, and other single/double-dash forms with or without values.
321
+ # Without GOPT, `git -C <other-worktree> checkout main` and
322
+ # `git --git-dir=… --work-tree=… <verb>` bypassed all four rules below entirely
323
+ # (verified, #4569) — which is exactly the isolation escape the rules exist to
324
+ # stop. `-C`/`-c`/the enumerated value-taking long options consume their
325
+ # following value; a generic `--long`/`--long=val`/`-x` handles the no-value
326
+ # forms. git rejects the glued/`=` short forms (`-C/path`, `-C=path`,
327
+ # `-cfoo=1` — all "unknown option"), so requiring `\s+` after `-C`/`-c` is
328
+ # complete, not a gap. A `-C` path containing spaces inside quotes is NOT
329
+ # matched (the value alternatives are whitespace-delimited); no worktree path in
330
+ # this repo has spaces, and widening to quote-aware value parsing is the
331
+ # false-positive risk this hook family has already regressed on twice.
332
+ #
333
+ # SCOPE (Red Team ruling, #4569): this hardening applies to the four existing
334
+ # rules only — switch-to-main, branch creation, merge, push-to-main. It does NOT
335
+ # make worktree-guard inspect arbitrary write-capable commands (`cp`, `mv`,
336
+ # `rm`, `tee`, `sed -i`, `rsync`, …) for out-of-worktree absolute paths; that
337
+ # set cannot be enumerated and was explicitly descoped.
338
+ GOPT='(\s+(-C\s+[^[:space:];&|]+|-c\s+[^[:space:];&|]+|--(git-dir|work-tree|namespace|super-prefix)(=[^[:space:];&|]*|\s+[^[:space:];&|]+)|--[A-Za-z][A-Za-z0-9-]*=[^[:space:];&|]*|--[A-Za-z][A-Za-z0-9-]*|-[A-Za-z]))*\s+'
339
+ SEG="${GITANCHOR}${WRAPPRE}git${GOPT}"
195
340
 
196
- # Block checkout to main
197
- if echo "$SCAN" | grep -qE "${SEG}git\s+(checkout|switch)\s+main(\s|$)"; then
341
+ # Block checkout to main.
342
+ # TRAILING BOUNDARY (#4569, found by re-running the repro against the prefix
343
+ # fix — NOT part of the GITSEG backport): the boundary after `main` used to be
344
+ # `(\s|$)`, which meant a branch name followed directly by a SEPARATOR rather
345
+ # than a space was not a match — `git checkout main; npm test`,
346
+ # `git checkout main&&ls`, `{ git checkout main; }` and
347
+ # `git checkout main>/dev/null` were all allowed, while
348
+ # `git checkout main && ls` was blocked. Hardening only the prefix would have
349
+ # left the most obvious of those escapes available for the cost of one
350
+ # character, which is why the boundary is widened to the separator set as well.
351
+ # It does NOT make the rule airtight, and no claim here should be read that way:
352
+ # `git checkout "main"` (quoted branch name) still escapes, because the
353
+ # character after `checkout ` is a quote and the pattern wants `main` there.
354
+ # That is a known residual of a guardrail, not a boundary.
355
+ #
356
+ # The set is deliberately narrow: whitespace, end-of-string, the three
357
+ # separators that can legally follow a bare word with no space — `;` `&` `|` —
358
+ # and the two redirection operators `<` `>` (SF-2, #4569:
359
+ # `git checkout main>/dev/null`, `git switch main>/tmp/o` and
360
+ # `git checkout main>&2` are all working commands that switch to main, and a
361
+ # sentence almost never puts `>` straight after the bare word "main").
362
+ # Two characters were tried and REMOVED, each for a measured false positive:
363
+ #
364
+ # `)` and `}` (QA finding, Major, #4569). GITANCHOR treats a literal `(` as a
365
+ # subshell start regardless of quoting — pre-existing, shared with the merge
366
+ # and push rules, accepted there. Adding `)` as a terminator combined with
367
+ # that to make any quoted sentence closing a parenthetical right after the
368
+ # word "main" read as a real command: `gh pr create --body "Before this
369
+ # change, running (git checkout main) from another worktree succeeded
370
+ # silently."` blocked. That is the sentence someone writes to DESCRIBE this
371
+ # fix, so the changelog author would have been the first to hit it.
372
+ #
373
+ # Any non-branch-name character (i.e. including quotes). That blocks prose
374
+ # ENDING on the verb, e.g.
375
+ # `mx memories create --content "...; git checkout main"`.
376
+ #
377
+ # ACCEPTED RESIDUAL, deliberate: a genuine subshell `(git checkout main)` is
378
+ # therefore ALLOWED. Nobody wraps a checkout in a subshell for any real reason,
379
+ # whereas writing `(git checkout main)` in prose is routine. This guard is a
380
+ # guardrail against accidents, not a security boundary (PO ruling 2026-07-06,
381
+ # bypassable by design), so trading a near-never real shape for a near-certain
382
+ # false positive is the correct side of that trade and matches this hook
383
+ # family's documented failure mode (#3909). Pinned by a test — see the
384
+ # "accepted residual" assertions in worktree-guard.test.sh.
385
+ #
386
+ # ACCEPTED RESIDUAL, deliberate — the `;` in the set (SF-1, #4569). Prose that
387
+ # documents a git SEQUENCE with `;` separators newly blocks where it did not
388
+ # before, e.g.
389
+ # gh issue comment --body "we saw { git checkout main; } slip through"
390
+ # mx memories create --content "steps: cd repo; git checkout main; npm test"
391
+ # That is again the shape of a sentence describing this very fix. It is kept
392
+ # because `;` closes a real bypass that costs one character to use, and the
393
+ # workaround is `--body-file` — the same documented answer as for the backtick
394
+ # residual above. Pinned by tests so the trade stays visible.
395
+ #
396
+ # `{ git checkout main; }` still blocks: the character after `main` there is
397
+ # `;`, not `}`. A longer branch name that merely starts with "main"
398
+ # (`maintenance`, `main-fix`, `main/topic`) still does not match, because
399
+ # neither a letter, `-` nor `/` is in the set.
400
+ if echo "$SCAN" | grep -qE "${SEG}(checkout|switch)\s+main([[:space:];&|<>]|$)"; then
198
401
  {
199
402
  echo ""
200
403
  echo "BLOCKED: Cannot switch to main branch"
@@ -208,7 +411,7 @@ if echo "$SCAN" | grep -qE "${SEG}git\s+(checkout|switch)\s+main(\s|$)"; then
208
411
  fi
209
412
 
210
413
  # Block checkout -b (new branch creation)
211
- if echo "$SCAN" | grep -qE "${SEG}git\s+(checkout\s+-b|switch\s+(-c|--create))"; then
414
+ if echo "$SCAN" | grep -qE "${SEG}(checkout\s+-b|switch\s+(-c|--create))"; then
212
415
  {
213
416
  echo ""
214
417
  echo "BLOCKED: Cannot create new branches"
@@ -226,7 +429,7 @@ fi
226
429
  # `git rebase origin/main`, not merge. The block-report on 2026-07-06 was that
227
430
  # the reason was invisible (message went to stdout); routing to stderr fixes the
228
431
  # "blocked with no explanation" symptom.
229
- if echo "$SCAN" | grep -qE "${SEG}git\s+merge\s"; then
432
+ if echo "$SCAN" | grep -qE "${SEG}merge\s"; then
230
433
  {
231
434
  echo ""
232
435
  echo "BLOCKED: Cannot merge branches directly"
@@ -240,7 +443,7 @@ if echo "$SCAN" | grep -qE "${SEG}git\s+merge\s"; then
240
443
  fi
241
444
 
242
445
  # Block push to main
243
- if echo "$SCAN" | grep -qE "${SEG}git\s+push\s+.*\s*origin\s+main"; then
446
+ if echo "$SCAN" | grep -qE "${SEG}push\s+.*\s*origin\s+main"; then
244
447
  {
245
448
  echo ""
246
449
  echo "BLOCKED: Cannot push to main"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@memnexus-ai/mx-agent-cli",
3
- "version": "0.1.233",
3
+ "version": "0.1.235",
4
4
  "description": "CLI for creating and managing AI agent teams",
5
5
  "type": "module",
6
6
  "bin": {