yadflow 4.9.0 → 4.11.0
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.
- package/CHANGELOG.md +184 -0
- package/README.md +15 -5
- package/bin/commands.mjs +11 -1
- package/bin/yad.mjs +203 -14
- package/cli/agent-guard-worker.mjs +13 -0
- package/cli/agent-guard.mjs +1797 -73
- package/cli/agent-rules.mjs +64 -5
- package/cli/argv-flags.mjs +2 -2
- package/cli/brain/vendor/LICENSE-graphology-library.txt +21 -0
- package/cli/brain/vendor/LICENSE-graphology.txt +21 -0
- package/cli/brain/vendor/LICENSE-sigma.txt +12 -0
- package/cli/brain/vendor/graphology-library.min.js +2 -0
- package/cli/brain/vendor/graphology.umd.min.js +2 -0
- package/cli/brain/vendor/sigma.min.js +1369 -0
- package/cli/brain/view/app.js +382 -0
- package/cli/brain/view/index.html +63 -0
- package/cli/brain/view/live.css +228 -0
- package/cli/brain/view/live.html +110 -0
- package/cli/brain/view/live.js +884 -0
- package/cli/brain/view/style.css +141 -0
- package/cli/brain-about.mjs +206 -0
- package/cli/brain-activity.mjs +285 -0
- package/cli/brain-skills.mjs +118 -0
- package/cli/brain-vendor.mjs +71 -0
- package/cli/brain-view.mjs +344 -0
- package/cli/brain.mjs +810 -0
- package/cli/ci-check.mjs +48 -4
- package/cli/codeowners-command.mjs +72 -6
- package/cli/commit.mjs +17 -3
- package/cli/docs.mjs +2 -0
- package/cli/doctor.mjs +151 -25
- package/cli/epic-state.mjs +186 -4
- package/cli/epic.mjs +26 -10
- package/cli/errors.mjs +2 -1
- package/cli/find-product.mjs +69 -15
- package/cli/fold.mjs +31 -6
- package/cli/foundation-publish.mjs +256 -0
- package/cli/gate.mjs +29 -3
- package/cli/git-read.mjs +38 -0
- package/cli/guard-stream.mjs +409 -0
- package/cli/guide.mjs +3 -1
- package/cli/hook.mjs +102 -9
- package/cli/issue-links.mjs +61 -0
- package/cli/issues-act.mjs +371 -0
- package/cli/issues-write.mjs +441 -0
- package/cli/issues.mjs +513 -0
- package/cli/lib.mjs +37 -2
- package/cli/lifecycle.mjs +7 -0
- package/cli/local-page.mjs +138 -0
- package/cli/machine.mjs +1 -1
- package/cli/manifest.mjs +59 -3
- package/cli/map/view/app.js +712 -0
- package/cli/map/view/index.html +50 -0
- package/cli/map/view/style.css +142 -0
- package/cli/map-view.mjs +186 -0
- package/cli/map.mjs +637 -0
- package/cli/members.mjs +9 -15
- package/cli/next.mjs +1 -1
- package/cli/openpr.mjs +26 -6
- package/cli/plan.mjs +127 -0
- package/cli/platform.mjs +159 -4
- package/cli/product-index.mjs +1 -1
- package/cli/protection.mjs +53 -3
- package/cli/reconcile.mjs +9 -3
- package/cli/report.mjs +9 -3
- package/cli/reports-dir.mjs +391 -0
- package/cli/reports.mjs +65 -0
- package/cli/run-checks.mjs +15 -4
- package/cli/scan.mjs +314 -0
- package/cli/setup.mjs +42 -8
- package/cli/standup.mjs +120 -78
- package/cli/status.mjs +6 -5
- package/cli/story-frames.mjs +17 -6
- package/cli/test-agents-file.mjs +985 -12
- package/cli/test-brain-activity.mjs +753 -0
- package/cli/test-brain-view.mjs +381 -0
- package/cli/test-brain.mjs +795 -0
- package/cli/test-build.mjs +25 -0
- package/cli/test-ci-check.mjs +469 -10
- package/cli/test-guide.mjs +218 -0
- package/cli/test-machine.mjs +59 -0
- package/cli/test-map.mjs +615 -0
- package/cli/test-node.mjs +15 -0
- package/cli/test-reports.mjs +382 -0
- package/cli/test-run-checks.mjs +21 -0
- package/cli/test-setup-agent.mjs +14 -0
- package/cli/test-story-frames.mjs +74 -4
- package/cli/test-worktree.mjs +24 -3
- package/cli/toolbox-connect.mjs +259 -0
- package/cli/toolbox.mjs +187 -12
- package/cli/update-commit.mjs +101 -9
- package/cli/usage.mjs +8 -4
- package/package.json +12 -4
- package/skills/sdlc/config.yaml +1 -1
- package/skills/sdlc/module-help.csv +3 -1
- package/skills/yad-analysis/SKILL.md +4 -0
- package/skills/yad-architecture/SKILL.md +22 -4
- package/skills/yad-backfill/SKILL.md +4 -0
- package/skills/yad-change/SKILL.md +7 -1
- package/skills/yad-change/references/triage.md +2 -0
- package/skills/yad-checks/SKILL.md +15 -7
- package/skills/yad-checks/references/check-gates.md +70 -35
- package/skills/yad-checks/templates/checks/build-test-lint.sh +7 -0
- package/skills/yad-checks/templates/checks/install-deps.sh +6 -0
- package/skills/yad-checks/templates/checks/member-check.mjs +3 -2
- package/skills/yad-checks/templates/checks/package-manager.sh +42 -0
- package/skills/yad-checks/templates/checks/risk-map-check.sh +2 -2
- package/skills/yad-checks/templates/checks/verified-commits.sh +3 -3
- package/skills/yad-checks/templates/github/yad-checks.yml +358 -69
- package/skills/yad-checks/templates/github/yad-pr-text.yml +118 -0
- package/skills/yad-checks/templates/github/yad-product-checks.yml +130 -54
- package/skills/yad-checks/templates/github/yad-product-pr-text.yml +131 -0
- package/skills/yad-checks/templates/github/yad-update-guard.yml +2 -1
- package/skills/yad-checks/templates/github/yad-verified-commits.yml +49 -3
- package/skills/yad-checks/templates/gitlab/yad-checks.gitlab-ci.yml +80 -20
- package/skills/yad-checks/templates/gitlab/yad-product-checks.gitlab-ci.yml +59 -9
- package/skills/yad-checks/templates/gitlab/yad-update-guard.gitlab-ci.yml +1 -0
- package/skills/yad-checks/templates/gitlab/yad-verified-commits.gitlab-ci.yml +49 -1
- package/skills/yad-checks/templates/hooks/agent-guard.mjs +44 -10
- package/skills/yad-checks/templates/hooks/ledger-guard-cursor.mjs +12 -6
- package/skills/yad-checks/templates/hooks/yad-activity.mjs +238 -0
- package/skills/yad-commit/SKILL.md +3 -1
- package/skills/yad-connect-design/SKILL.md +42 -10
- package/skills/yad-connect-design/references/design-context.md +35 -5
- package/skills/yad-connect-design/references/design-registry.md +3 -3
- package/skills/yad-connect-learning/SKILL.md +12 -2
- package/skills/yad-connect-repos/SKILL.md +18 -0
- package/skills/yad-connect-testing/SKILL.md +22 -5
- package/skills/yad-connect-testing/references/testing-context.md +1 -1
- package/skills/yad-discovery/SKILL.md +11 -0
- package/skills/yad-discovery/references/foundation-schema.md +11 -0
- package/skills/yad-docs/templates/app/package-lock.json +18 -10
- package/skills/yad-docs/templates/app/package.json +6 -1
- package/skills/yad-epic/SKILL.md +7 -0
- package/skills/yad-epic/references/state-schema.md +50 -1
- package/skills/yad-guide/SKILL.md +8 -4
- package/skills/yad-implement/SKILL.md +117 -4
- package/skills/yad-implement/references/implement-conventions.md +2 -1
- package/skills/yad-issues/SKILL.md +54 -0
- package/skills/yad-learn/SKILL.md +4 -0
- package/skills/yad-open-pr/SKILL.md +6 -0
- package/skills/yad-pr-template/SKILL.md +2 -1
- package/skills/yad-pr-template/templates/checks/pr-template.sh +8 -4
- package/skills/yad-pr-template/templates/checks/pr-title.sh +42 -6
- package/skills/yad-product-bridge/references/bridge.md +28 -0
- package/skills/yad-product-bridge/templates/github/yad-gate-sync.yml +25 -11
- package/skills/yad-product-bridge/templates/gitlab/yad-gate-sync.gitlab-ci.yml +38 -14
- package/skills/yad-run/SKILL.md +5 -0
- package/skills/yad-scan/SKILL.md +51 -0
- package/skills/yad-spec/SKILL.md +32 -2
- package/skills/yad-stories/SKILL.md +22 -4
- package/skills/yad-stories/references/story-schema.md +1 -1
- package/skills/yad-test-cases/SKILL.md +18 -2
- package/skills/yad-ui/SKILL.md +43 -8
- package/skills/yad-checks/templates/gitlab/.gitlab-ci.yml +0 -49
package/cli/test-agents-file.mjs
CHANGED
|
@@ -12,6 +12,7 @@ import { execFileSync } from 'node:child_process';
|
|
|
12
12
|
import fs from 'node:fs';
|
|
13
13
|
import os from 'node:os';
|
|
14
14
|
import path from 'node:path';
|
|
15
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
15
16
|
|
|
16
17
|
// The #280 guard (cli/test.mjs): printed lines go to stderr while the runner owns stdout.
|
|
17
18
|
if (process.env.NODE_TEST_CONTEXT) {
|
|
@@ -279,6 +280,75 @@ test('the agent guard refuses every human-only yad command and every force push,
|
|
|
279
280
|
'cat <<< "x"\nyad gate approve EP-x epic',
|
|
280
281
|
'echo $((1<<2))\nyad gate approve EP-x epic',
|
|
281
282
|
'cat <<EOF\r\nx\r\nEOF\r\nyad gate approve EP-x epic',
|
|
283
|
+
// #492: the reader splits as bash and zsh do. A CR is a word character (so `x\r#` holds no comment), a heredoc's
|
|
284
|
+
// end word is every character the shell reads as one word and its end line must match exactly, `(` and `)`
|
|
285
|
+
// end a part, and a command nested past three levels is refused.
|
|
286
|
+
'x\r#; gh pr merge 5', 'x\r#; yad gate approve EP-x epic', 'cat <<EOF#;gh pr merge 5\nbody\nEOF#\n', 'cat <<EOF#;git push --force\nb\nEOF#',
|
|
287
|
+
'cat <<EOF+\nEOF\ncat <<X\nEOF+\ngh pr merge 5\nX\n', 'cat <<EOF\nEOF\r\ncat <<X\nEOF\ngh pr merge 5\nX\n', 'cat <<EOF\r\nEOF\ncat <<X\nEOF\r\ngh pr merge 5\nX\n',
|
|
288
|
+
'cat <(gh pr merge 5)', 'echo x > >(gh pr merge 5)', 'f(){ gh pr merge 5; }; f', 'case x in x) gh pr merge 5;; esac', '(gh pr merge 5)',
|
|
289
|
+
'echo $(echo $(echo $(echo $(gh pr merge 5))))', 'echo $(echo $(echo $(echo $(echo hi))))', "bash -c 'bash -c \"bash -c \\\"bash -c ls\\\"\"'",
|
|
290
|
+
'git push --force\r', 'git push \\\r\n--force',
|
|
291
|
+
// #492 review: a `\`-newline join hiding a `$(` or splitting an end word; an end word holding `$` (the outer
|
|
292
|
+
// shell expands it first); and a heredoc that never ends, whose `$(…)` the shell still runs.
|
|
293
|
+
'echo "$\\\r\n(gh pr merge 5)"', 'cat <<EOF\r\n$\\\r\n(npx yadflow gate advance EP-x)\nEOF\r\n', 'echo "$\\\n(gh pr merge 5)"', 'cat <<EOF\n$\\\n(gh pr merge 5)\nEOF',
|
|
294
|
+
'cat <<EOF\nx\nEO\\\nF\ngh pr merge 5\nEOF\n', 'bash -c "cat <<EOF$x\nEOF\ngh pr merge 5\nEOF$x"', 'bash -lc "cat <<E$F\nE$Fx\ngh pr merge 5\nE$F\n"',
|
|
295
|
+
'cat <<< "x"\ncat <<EOF+\nbody\nEOF#\ncat <<\'EOF\'\n$(git push --force)\nEOF\n\n', "cat <<X\ncat <<'EOF'\n$(gh pr merge 5)\nEOF", "cat <<X\necho '$(gh pr merge 5)'",
|
|
296
|
+
// #492 review 2: an expanding end word is still a redirect, read past; a `#` right after `(` or `)` starts no comment.
|
|
297
|
+
'<<"E$x" gh pr merge 1', 'echo hi; <<"E$x" gh pr merge 1', "<<'E`x`' yad gate approve EP-a", '<<E$x gh pr merge 1', 'x=1 <<E$x gh pr merge 1',
|
|
298
|
+
'[[ a =~ (#|$(gh pr merge 1)) ]]', '[[ a =~ (#|`gh pr merge 1`) ]]', '[[ a =~ (x)#$(gh pr merge 1) ]]', 'shopt -s extglob\necho @(#$(gh pr merge 1))',
|
|
299
|
+
'echo foo(#$(gh pr merge 1))', 'ls foo(#q$(gh pr merge 1))', 'A((#$(gh pr merge 1)', '<<<((###;gh pr merge 1',
|
|
300
|
+
// #492 review 3: every reading the shells may make — an expanding end word as a heredoc, as none, and line by
|
|
301
|
+
// line (its value is unknown); a `#` after a parenthesis as a comment (command position) and as text.
|
|
302
|
+
'cat <<"E$x"\n\'\nE$x\ngh pr merge 1', "cat <<'E$x'\n'\nE$x\ngh pr merge 1", 'cat <<"E$x"\n"\nE$x\ngh pr merge 1', 'bash -c "cat <<E$x\n\'\nE\ngh pr merge 1"',
|
|
303
|
+
'x=F; bash -c "cat <<E$x\n\'\nEF\ngh pr merge 1"', "cat <<E$x\nit's a note\nE$x\ngh pr merge 1",
|
|
304
|
+
"(cd web && npm ci)# don't skip\ngh pr merge 1", "(true)# x '\ngh pr merge 1", "(# '\ngh pr merge 1\n)", "if (true)# '\nthen gh pr merge 1; fi",
|
|
305
|
+
"x=(# '\n$(gh pr merge 1)\n)", "f()# '\n{ gh pr merge 1; }; f", "()# x '\ngh pr merge 1", "x=(a b)# don't\ngh pr merge 1",
|
|
306
|
+
// A body of many `$(` overflowed the stack (the hook then exited 1, which lets the command run).
|
|
307
|
+
`cat <<E >/dev/null\n${'$((1))\n'.repeat(130000)}$(gh pr merge 1)\n`, `cat <<E >/dev/null\n${'$((1))'.repeat(130000)}\nE\ngh pr merge 1\n`,
|
|
308
|
+
// #495: redirects in the middle of a command, $'…' strings, options after bash -c, more prefixes, commands
|
|
309
|
+
// that carry a command, an expanding command word, a shell fed its script, case inside $(…), # after |.
|
|
310
|
+
'git push origin main 2>&1 --force', 'git commit -m x 2>&1 --no-verify', '>out gh pr merge 5', 'gh >x pr merge 5', 'x=<<<1 git commit --no-verify -m x',
|
|
311
|
+
'git push --force>/dev/null', 'yad gate approve> EP-x epic', "git commit -m $'fix: don\\'t\\n' && gh pr merge 5 && echo 'done'", "bash -c $'gh pr merge 5'",
|
|
312
|
+
'bash -c $"gh pr merge 5"', 'bash -c -- "gh pr merge 5"', 'bash -c -x "gh pr merge 5"', 'bash -c -o pipefail "gh pr merge 5"', 'env -S "gh pr merge 5"',
|
|
313
|
+
'env --split-string="gh pr merge 5"', 'exec -a x gh pr merge 5', 'builtin command gh pr merge 5', 'function f { gh pr merge 5; }; f', 'coproc gh pr merge 5',
|
|
314
|
+
'coproc c { gh pr merge 5; }', 'find . -exec gh pr merge 5 \\;', 'find . -execdir git push --force {} +', "git -c alias.p='push --force' p", "git -c alias.m='!gh pr merge 5' m",
|
|
315
|
+
'noglob gh pr merge 5', '{gh pr merge 5;}', 'eval "gh pr merge 5"', 'eval gh pr merge 5', 'echo "gh pr merge 5" | bash', "printf 'gh pr merge 5' | sh",
|
|
316
|
+
'$(echo gh) pr merge 5', '${x:-gh} pr merge 5', '{gh,} pr merge 5', '$CMD pr merge 5', 'echo $(case x in x) gh pr merge 5;; esac)', '[[ a =~ x|#$(gh pr merge 1) ]]',
|
|
317
|
+
'bash <<EOF\ngh pr merge 5\nEOF', "sh <<'EOF'\ngit push --force\nEOF", 'bash <<< "gh pr merge 5"',
|
|
318
|
+
// #495 review: zsh's numeric globs are one word (both readings); and the gaps of the same kinds.
|
|
319
|
+
'git push origin main <-> --force', 'git push <1-9> --force', 'git commit -m x <-> --no-verify', 'git commit -am x <1-3000> -n', 'gh pr review >> 5 <1-9> --approve',
|
|
320
|
+
'bash -c - "gh pr merge 5"', '$SUDO gh pr merge 5', '${DRY} git push --force', '$X yad gate approve EP-x epic', '$SHELL -c "gh pr merge 5"', '$(which bash) -c "gh pr merge 5"',
|
|
321
|
+
'{bash,} -c "gh pr merge 5"', "find . -exec sh -c 'git push --force' {} +", 'env -u X -S "gh pr merge 5"', 'env -C . -S "gh pr merge 5"', "printf '%s\\n' 'gh pr merge 5' | bash",
|
|
322
|
+
'printf "%s" "gh pr merge 5" | bash', 'echo gh pr merge 5 | tee /dev/null | bash', "cat <<'EOF' | bash\ngh pr merge 5\nEOF", 'source /dev/stdin <<< "gh pr merge 5"',
|
|
323
|
+
'. /dev/stdin <<< "gh pr merge 5"', 'git push origin main --{force,}', 'gh pr {merge,} 5', 'git commit -m x --no-{verify,}', '=gh pr merge 5', "git -c alias.a=b -c alias.b='push --force' a",
|
|
324
|
+
// #495 review 2: a shell reads its input after -s, from /dev/stdin, past --rcfile; a brace list expands into words.
|
|
325
|
+
"echo 'gh pr merge 5' | bash -s foo", "bash -s foo <<'EOF'\ngit push --force origin main\nEOF", "echo 'yad gate approve EP-x analysis' | bash /dev/stdin",
|
|
326
|
+
"bash /dev/stdin <<< 'gh pr review 5 --approve'", "zsh /dev/stdin <<< 'git commit -n -m x'", "sh /dev/fd/0 <<'EOF'\nglab mr merge 5\nEOF", "echo 'gh pr merge 5' | bash --rcfile /dev/null",
|
|
327
|
+
"echo 'gh pr merge 5' | bash -xs foo", "echo 'gh pr merge 5' | ksh -s a", "cat <<'EOF' | bash -s x\ngh pr merge 5\nEOF", 'gh {pr,merge} 5', '{gh,pr} merge 5', 'git {push,--force} origin main',
|
|
328
|
+
'git {commit,-n} -m x', 'yad {gate,approve} EP-x analysis', 'gh {pr,review} 5 --approve', 'git push origin main {x,--fo}{rce,y}', 'git push origin {a,{--force,b}}', 'git push -{f..f} origin main',
|
|
329
|
+
'git push origin {main,main,main,main,main,main,main,main,--force}',
|
|
330
|
+
// #495 review 3: empty alternatives (bash drops the empty words); option clusters with o; zsh --emulate; a brace
|
|
331
|
+
// list or an expansion in a shell's words; zsh's one-character sequences.
|
|
332
|
+
'gh pr {,merge} 5', 'gh {,pr} merge 5', "bash -c 'gh pr {,merge} 5'", 'echo {,}; gh pr {,,merge} 5', 'gh pr {,{,{merge,x}}} 5',
|
|
333
|
+
"bash -euo pipefail <<'EOF'\ngh pr merge 5\nEOF", "echo 'gh pr merge 5' | bash -eo pipefail", "zsh --emulate sh <<< 'gh pr merge 5'", "echo 'gh pr merge 5' | bash {-s,}",
|
|
334
|
+
"{bash,} <<< 'gh pr merge 5'", "echo 'gh pr merge 5' | $SHELL", "${SHELL:-bash} <<< 'gh pr merge 5'", "echo 'gh pr merge 5' | bash $NOPE", 'git push origin {+..+}main',
|
|
335
|
+
'git push origin main -{-..-}force',
|
|
336
|
+
// #495 review 4: text printed in a group or a file reaches a real shell; `\||` is a pipe; quoted brace characters are text.
|
|
337
|
+
"{ echo 'gh pr merge 5'; } | bash", "{ echo 'gh pr merge 5'; } 2>&1 | bash", "(echo 'gh pr merge 5'; true) | bash", "if true; then echo 'gh pr merge 5'; fi | bash",
|
|
338
|
+
"for i in 1; do echo 'gh pr merge 5'; done | bash", "case x in x) echo 'gh pr merge 5';; esac | bash", "{ cat <<'E'; } | bash\ngh pr merge 5\nE", "f() { echo 'gh pr merge 5'; }; f | bash",
|
|
339
|
+
"echo 'gh pr merge 5' > f; bash < f", "echo 'gh pr merge 5 #' \\||bash", "git commit -m {'}',-n}", "git commit -m {'{',-n}", 'git commit -m {"}",--no-verify}', "git commit {-n,'{'} -m x",
|
|
340
|
+
// A typed shield character stays inert, in a carried command too.
|
|
341
|
+
'bash -c "gh pr {merge,} 5"', "eval 'gh pr {merge,} 5'", "echo 'gh pr {merge,} 5' | bash", "bash -c 'git push {--force,}'", "bash -c 'git commit {-n,} -m x'",
|
|
342
|
+
// A word too long to expand, or too many words, is refused rather than expanded: a 1 MB JSON word crashed the heap.
|
|
343
|
+
`echo x${'{'.repeat(200000)}a,b${'}'.repeat(200000)}; gh pr merge 5`, `echo ${'{a,b}'.repeat(190000)}; gh pr merge 5`,
|
|
344
|
+
// E167 review 5: a CRLF heredoc ends at `EOF\r`, as the shell's end word is `EOF\r`; a later plain EOF is not its end.
|
|
345
|
+
'cat <<EOF\r\nb\r\nEOF\r\ngh pr merge 5\nEOF\n', "cat <<'EOF'\r\nb\r\nEOF\r\ngh pr merge 5\nEOF\n",
|
|
346
|
+
// E167 review 4: \ + CR + LF does not join lines in bash or zsh.
|
|
347
|
+
'git log \\\r\ngh pr merge 5', 'git status \\\r\ngit push --force', 'git log \\\r\ngit commit --no-verify -m x',
|
|
348
|
+
// ... and a join to a shell that drops CRs (Git Bash with igncr): refused under either reading.
|
|
349
|
+
'git push \\\r\n--force', 'git push \\\r\n-f', 'yad gate \\\r\napprove EP-x epic', 'gh pr \\\r\nmerge 5',
|
|
350
|
+
// ... also inside a bash -c string built from quoted pieces.
|
|
351
|
+
"bash -c 'git push \\'\"\r\n\"'--force'", "bash -c 'git push \\''\r\n--force'",
|
|
282
352
|
'# <<x\nyad gate approve EP-x epic',
|
|
283
353
|
'env -i yad gate approve EP-x epic',
|
|
284
354
|
'sudo -u me yad gate approve EP-x epic',
|
|
@@ -400,7 +470,169 @@ test('the agent guard refuses every human-only yad command and every force push,
|
|
|
400
470
|
'sudo -E yad gate approve EP-x analysis',
|
|
401
471
|
'yarn workspaces foreach -A run yad gate approve EP-x analysis',
|
|
402
472
|
'yarn workspaces foreach --include web exec yad gate approve EP-x analysis',
|
|
403
|
-
|
|
473
|
+
// #498, rows 3–7, each run by a real shell (bash 3.2 and zsh 5.9 unless named). Row 3: `$'…'` inside `$(…)`.
|
|
474
|
+
"echo $(echo $'a\\')'; gh pr merge 5\n)", "echo \"$(echo $'a\\')'; gh pr merge 5\n)\"", "x=$(echo $'a\\')'; git push --force\n)",
|
|
475
|
+
// Row 4: a `case` in `"$(…)"` across a newline (zsh runs it).
|
|
476
|
+
'echo "$(case x in x)\ngh pr merge 5;; esac)"', 'x="$(case x in\nx) gh pr merge 5;; esac)"',
|
|
477
|
+
// Row 5: bash closes a list only after a comma (`{x},-n}` is `x}` and `-n`).
|
|
478
|
+
'git commit -m {x},-n}', 'git push origin {a},+main}', 'git commit -m {a}{b},-n}',
|
|
479
|
+
// Row 6: a backslash kept in double quotes, re-read by the shell `bash -c` and `eval` start.
|
|
480
|
+
"bash -c \"echo \\'; gh pr merge 5; echo \\'\"", "eval \"echo \\'; gh pr merge 5; echo \\'\"", 'bash -c "gh pr {merge,\\}} 5"',
|
|
481
|
+
"sh -c \"echo \\'; git push --force; echo \\'\"",
|
|
482
|
+
// Row 7: a brace list does not see into `$(…)`, backticks or `${…}`.
|
|
483
|
+
"git commit -m {$(echo '}'),-n}", "git push origin {$(echo '}'),+main}", "git commit -m {`echo '}'`,-n}", 'git commit -m {$(echo \\}),-n}',
|
|
484
|
+
// Rows 3 and 7 together, and row 4's `case` inside a list (zsh).
|
|
485
|
+
"git commit -m {$(echo $'\\')'),-n}", 'git commit -m {$(case x in x) echo a;; esac),-n}',
|
|
486
|
+
// Row 4 with the `case` word quoted, escaped, a `$(…)`, longer than 256 characters, or an `esac` that is only a word (zsh).
|
|
487
|
+
'echo "$(case "a b" in\n"a b") gh pr merge 5;; esac)"', "echo \"$(case 'a b' in\n*) gh pr merge 5;; esac)\"", 'echo "$(case a\\ b in\n*) gh pr merge 5;; esac)"',
|
|
488
|
+
'echo "$(case $(echo y) in\n*) gh pr merge 5;; esac)"', `echo "$(case \${y:-${'x'.repeat(300)}} in\n*) gh pr merge 5;; esac)"`,
|
|
489
|
+
'echo "$(case y in x) echo esac;;\n*) gh pr merge 5;; esac)"',
|
|
490
|
+
// Rows 3 and 4 in an unquoted heredoc's text (zsh).
|
|
491
|
+
"cat <<EOF\n$(echo $'a\\')'; gh pr merge 5\n)\nEOF", 'cat <<EOF\n$(case x in\nx) gh pr merge 5;; esac)\nEOF',
|
|
492
|
+
// Row 6 through a here-string and a piped echo or printf.
|
|
493
|
+
"bash <<< \"echo \\'; gh pr merge 5; echo \\'\"", "echo \"echo \\'; gh pr merge 5; echo \\'\" | bash", "printf '%s\\n' \"echo \\'; gh pr merge 5; echo \\'\" | sh",
|
|
494
|
+
// Review round 2: a `$(…)` in a heredoc's text that the shells end sooner hides nothing after it (zsh), and
|
|
495
|
+
// a chain of `case $(` that once took exponential time is refused at once (bash runs line 1 first).
|
|
496
|
+
"cat <<E\n$(echo $'\\''x) it's $(gh pr merge 5)\nE", `gh pr merge 5\necho $(${' case $('.repeat(40)}`,
|
|
497
|
+
// Each `$(…)` on the line is read to that reading's own end.
|
|
498
|
+
"echo $(echo $'\\')'; gh pr merge 5\n) $(echo $'\\')' x\n)",
|
|
499
|
+
// Review round 5: bash 3.2 honours `$'…'` but reads a `case` as text; zsh honours both. A reading per shell,
|
|
500
|
+
// never one mixed reading (bash runs the first, zsh the heredoc one).
|
|
501
|
+
"echo \"$(case x in x) echo ' ;; esac)\" $(echo $'\\')' ; git push -f) ' ;; esac)\"'", "echo $($'\\'' $(case x in x) esac) $(echo ')') ; git push -f ; ",
|
|
502
|
+
"cat <<E\n$(echo $'\\')' ' $(echo ' ) $(git push -f) ' )\nE\n",
|
|
503
|
+
// Round 6: a `#` comment inside `$(…)` (both shells; zsh only in double quotes), and backticks inside it (bash).
|
|
504
|
+
"echo $( # )'\ngit push -f\n)''", "echo \"$( # )\"'\ngit push -f\n)\"''", "echo $( echo hi # )'\ngit push -f\n)''", "echo $(echo $((1)) # )'\ngit push -f\n)''",
|
|
505
|
+
"echo $(echo `echo )'`\ngit push -f\n)''",
|
|
506
|
+
// Round 7: bash starts a comment only after a blank (`;#` is none to it); the brace shield; `${…}` as a `case` word.
|
|
507
|
+
"echo $(echo $'\\'' ;#) ; git push -f ; echo '\n)'", "echo $(echo $'\\''&#) ; git push -f ; echo '\n)'", "echo $(echo $'\\''&&#) ; gh pr merge 5 ; echo '\n)'",
|
|
508
|
+
"git commit -m {$(echo '{'),-n}", 'git commit -m {$(echo {),-n}', 'git commit -m {`echo {`,-n}', 'echo "$(case ${x:-a b} in\n*) git push -f ;; esac)"',
|
|
509
|
+
// Round 4: a reading that trusts the first reading's quotes past a `$(…)` the shells end elsewhere misses these.
|
|
510
|
+
"echo $(echo $'\\')\"') ' $(echo ' ; git push -f ; echo \"' $'\\')\"') '", "echo $(echo $'\\')\"') ' $(echo ' ; gh pr merge 1 ; echo \"' $'\\')\"') '",
|
|
511
|
+
"echo $(echo $'\\')\"') $(echo $'\\')\"') ' $(echo ' ; git push -f ; echo \"' $'\\')\"') '",
|
|
512
|
+
// #516, each run by a real shell. B2 (bash 3.2): a heredoc's text inside `$(…)` is read as code.
|
|
513
|
+
"x=$(cat <<'E'\n) ; git push -f ; echo '\nE\n)'", "echo \"$(cat <<'E'\n)\" ; git push -f ; echo \"\nE\n)\"", "x=$(cat <<E\n) ; gh pr merge 5 ; echo '\nE\n)'",
|
|
514
|
+
// B2's printed text: bash 3.2 ends the `$(…)` at the heredoc's `)`, so `cat` prints the text before it (the heredoc
|
|
515
|
+
// never ends there), joined to the text after it.
|
|
516
|
+
"eval \"$(cat <<'EOF'\ngit p)ush --force\nEOF\n)\"", "bash -c \"$(cat <<'EOF'\ngit commit -)n -m x\n)EOF\n)\"",
|
|
517
|
+
"bash -c \"$(echo 'eval \"$(cat <<'EOF'\nyad gate )approve EP-x epic\n\nEOF\n)\"')\"",
|
|
518
|
+
// B3, B4 (zsh): a `case` word in backticks; a comment, a newline or a `;` before `in`.
|
|
519
|
+
'echo "$(case `echo x` in\nx) gh pr merge 5 ;; esac)"', 'echo "$(case x #c\nin\nx) gh pr merge 5 ;; esac)"', 'echo "$(case x #c\n#d\n\nin\nx) gh pr merge 5 ;; esac)"',
|
|
520
|
+
'echo "$(case x ;in\nx) gh pr merge 5 ;; esac)"',
|
|
521
|
+
// B5 (zsh): `esac` right after `}` or `]]` (and `esac`, `!`, `in`: no line found that main lets through and zsh runs).
|
|
522
|
+
`echo "$(case x in x) { echo '"' ; } esac)" ; git push -f ; echo '"'`, `echo "$(case x in x) [[ '"' ]] esac)" ; git push -f ; echo '"'`,
|
|
523
|
+
// B6 (zsh): `case x { … }`, closed by `}` (anywhere, `echo }` too) or `esac`; a group's `}` inside it closes the group.
|
|
524
|
+
'echo "$(case x {\nx) git push -f ;; })"', `echo "$(case x { x) echo '"' } )" ; git push -f ; echo '"'`, `echo "$(case x { x) echo '"' ;; esac)" ; git push -f ; echo '"'`,
|
|
525
|
+
// Review round 1: an `in` right at the 4,096-character bound on a `case` word still counts, as on main (zsh runs it).
|
|
526
|
+
`echo "$(case x${' '.repeat(4090)}in x) echo '"' ;; esac)" ; git push -f ; echo '"'`,
|
|
527
|
+
// Review round 2: a `#'` right after a `)` on an earlier line (the `)#` reading) with B2, B3 and B6 after it.
|
|
528
|
+
// Round 3: the same after a `|#'` (the `|#` reading), alone or with `)#'`.
|
|
529
|
+
"[[ a =~ x|#'\n' ]] ; x=$(cat <<'E'\n) ; git push -f ; echo '\nE\n)'", "case a in (x|#'\n') ;; esac ; echo \"$(case x {\nx) git push -f ;; })\"",
|
|
530
|
+
"(echo a)#'\ncase a in (x|#'\n') ;; esac ; echo \"$(case x {\nx) git push -f ;; })\"",
|
|
531
|
+
"(echo a)#'\nx=$(cat <<'E'\n) ; git push -f ; echo '\nE\n)'", "(echo a)#'\necho \"$(case `echo x` in\nx) gh pr merge 5 ;; esac)\"", "(echo a)#'\necho \"$(case x {\nx) git push -f ;; })\"",
|
|
532
|
+
// #498, rows 1, 2, 8: text that reaches a shell (bash 3.2 and zsh 5.9 run each). Row 1: a process substitution
|
|
533
|
+
// as a shell's script or input.
|
|
534
|
+
'bash <(echo "gh pr merge 5")', 'bash < <(echo "gh pr merge 5")', 'source <(echo "git push --force")', ". <(echo 'gh pr merge 5')",
|
|
535
|
+
'zsh =(echo "gh pr merge 5")', "bash -s < <(echo 'gh pr merge 5')", "bash -x <(echo 'git commit -n -m x')", "bash <(cat <<'EOF'\ngh pr merge 5\nEOF\n)",
|
|
536
|
+
// Row 2: a `$(…)` whose printed text is known becomes code.
|
|
537
|
+
"bash -c \"$(cat <<'EOF'\ngh pr merge 5\nEOF\n)\"", 'eval "$(echo gh pr merge 5)"', "$(cat <<'EOF'\ngh pr merge 5\nEOF\n)", "bash -c \"$(echo 'true; gh pr merge 5')\"",
|
|
538
|
+
"bash <<< \"$(echo 'true; gh pr merge 5')\"", "bash -c '$(cat <<EOF\ngh pr merge 5\nEOF\n)'", 'bash -c "gh pr $(echo merge) 5"', 'eval "$(printf \'%s\\n\' \'git push --force\')"',
|
|
539
|
+
// Row 8: a file written, then run, by every path a shell finds it by.
|
|
540
|
+
'echo "gh pr merge 5" > f; bash f', "cat > f.sh <<'EOF'\ngh pr merge 5\nEOF\nbash f.sh", "echo 'gh pr merge 5' > f; cat f | bash", "echo 'gh pr merge 5' > f; bash < f",
|
|
541
|
+
"echo 'gh pr merge 5' > f; chmod +x f; ./f", "echo 'gh pr merge 5' > f; bash -- f", "echo 'gh pr merge 5' > ./f; bash f", "echo 'gh pr merge 5' > f; bash ./f",
|
|
542
|
+
"printf '%s\\n' 'gh pr merge 5' > f; source f", "echo 'git push --force' > f; . ./f", "echo 'gh pr merge 5' &> f; bash f",
|
|
543
|
+
// Appended, through `tee`, and a write that may not run (it must not hide the one before).
|
|
544
|
+
"echo 'true' > f; echo 'gh pr merge 5' >> f; bash f", "echo 'gh pr merge 5' | tee f; bash f", "echo 'gh pr merge 5' > f; false && echo ok > f; bash f",
|
|
545
|
+
// Across rows and across the lines a command carries.
|
|
546
|
+
"echo 'gh pr merge 5' > f; bash <(cat f)", "echo 'gh pr merge 5' > f; bash -c \"$(cat f)\"", "echo 'gh pr merge 5' > f; eval \"$(<f)\"",
|
|
547
|
+
"echo 'gh pr merge 5' > f; bash -c 'bash f'", "echo 'gh pr merge 5' > f; eval bash f", "echo \"echo '\" > f; echo 'gh pr merge 5' > f; bash -c \"$(cat f)\"",
|
|
548
|
+
// The differential test: only `echo`'s leading options are options; zsh writes a pipe to a bare `> f`; a
|
|
549
|
+
// backslash in a printer's name; zsh's `print`.
|
|
550
|
+
'echo git commit -n -m x > f; bash f', "echo 'gh pr merge 5' | > f; bash f", "bash <<< \"$(ech\\o 'gh pr merge 5')\"", "print 'gh pr merge 5' > f; source f",
|
|
551
|
+
// Review round 1: a file run by a `$(…)`, a loop or a function after the write; `source --`; a `case` in a
|
|
552
|
+
// process substitution; a second one on the same command; `tee` and filters fed by a here-string, a pipe or `<`.
|
|
553
|
+
"echo 'gh pr merge 5' > f; x=$(bash f)", "echo 'gh pr merge 5' > f; echo `bash f`", "for i in 1 2; do bash f; echo 'gh pr merge 5' > f; done",
|
|
554
|
+
"g(){ bash f; }; echo 'gh pr merge 5' > f; g", "echo 'gh pr merge 5' > f; source -- f", "source -- <(echo 'gh pr merge 5')",
|
|
555
|
+
"bash <(case x in x) echo 'gh pr merge 5';; esac)", "bash <( (case x in x) ;; esac); echo 'gh pr merge 5')", "bash < <(echo x) < <(echo 'gh pr merge 5')",
|
|
556
|
+
"tee f <<< 'gh pr merge 5'; bash f", "tee f <<'E'\ngh pr merge 5\nE\nbash f", "echo 'gh pr merge 5' | cat > f; bash f", "echo 'gh pr merge 5' | sort > f; bash f",
|
|
557
|
+
"echo 'gh pr merge 5' > f; cat < f > g; bash g", "echo 'gh pr merge 5' > f; tee g < f; bash g",
|
|
558
|
+
// Texts joined as the shell joins them: a `>>` after a `$(…)` reading, `cat a b`; a guessed shell; `>&f`; a path
|
|
559
|
+
// spelled another way.
|
|
560
|
+
"echo \"$(echo 'gh pr \\')\" > f; echo 'merge 5' >> f; bash f", "echo 'gh pr \\' > a; echo 'merge 5' > b; cat a b | bash",
|
|
561
|
+
"$SHELL <(echo 'gh pr merge 5')", "echo 'gh pr merge 5' >& f; bash f", "echo 'gh pr merge 5' >&f; bash f", "echo 'gh pr merge 5' > d/../f; bash f",
|
|
562
|
+
// Review round 2: a quoted `case` is no reserved word, and one after `then`, `do` or `{` is; a command read
|
|
563
|
+
// from a file past its limits; zsh's `-`, `--` and `print -f`.
|
|
564
|
+
'bash <(echo gh pr merge 1; "case")', 'bash <(echo gh pr merge 1; \\case)', 'bash <(if :; then case x in x) echo gh pr merge 1;; esac; fi)',
|
|
565
|
+
'bash <({ case x in x) echo gh pr merge 1;; esac; })', `${Array.from({ length: 70 }, (_, i) => `echo a${i} > f`).join('; ')}; echo gh pr merge 1 > f; $(<f)`,
|
|
566
|
+
'print -r -- gh pr merge 1 > f; bash f', 'echo - gh pr merge 1 > f; bash f', "print -f '%s %s %s %s\\n' gh pr merge 1 > f; bash f",
|
|
567
|
+
`bash < <(true) ${'-e '.repeat(70)}<(echo gh pr merge 1)`, 'git push origin >&+main', 'git push origin >& --force',
|
|
568
|
+
// Review round 3: an `esac` that is an argument; a file past its limits in an argument.
|
|
569
|
+
'bash <(case x in y) echo then esac;; x) echo gh pr merge 5;; esac)', "bash <(printf '%b' 'echo hi\\n'; echo gh pr merge 5)",
|
|
570
|
+
"bash <(printf 'echo hi\\012'; printf 'gh pr merge 5')", "bash <(echo -en 'echo hi\\n'; echo gh pr merge 5)",
|
|
571
|
+
`${Array.from({ length: 70 }, (_, i) => `echo t${i} > f`).join('; ')}; echo 'pr merge 5' > f; gh $(<f)`,
|
|
572
|
+
// Review round 4: a held `)` record runs on to the real close; `1>&f` writes f.
|
|
573
|
+
'bash <(: { case; case y in y) echo gh pr merge 5;; esac)', 'bash <(: { case; case y in y) :;; esac; echo gh pr merge 5)',
|
|
574
|
+
'bash <(case y in y) { :; } esac; echo gh pr merge 5)', 'echo gh pr merge 5 1>& f; bash f',
|
|
575
|
+
// Review round 5: `printf --` keeps its format; a quote one printed text opens and the next closes; `>& -n`
|
|
576
|
+
// writes the file `-n`.
|
|
577
|
+
"printf -- 'gh pr merge 1\\n' | bash", "bash <(printf -- 'gh pr merge 1')", `{ echo "x='"; echo "'; gh pr merge 1"; } | bash`,
|
|
578
|
+
"echo gh pr merge 1 >& -'n'; bash ./-n", 'echo gh pr merge 1 >& -n; bash ./-n',
|
|
579
|
+
// Review round 6: main's reading of `echo` (every `-n`/`-e` word dropped) is kept beside the shells'.
|
|
580
|
+
'echo git -c -n core.hooksPath=x commit | bash', 'echo git -c -e core.hooksPath=x commit | zsh',
|
|
581
|
+
// Review round 7: main's reading is judged alone, so the joins still pair the shells' readings.
|
|
582
|
+
`bash -c "$(printf -- 'git commit '; echo - -n)"`,
|
|
583
|
+
// CI review: a `cat` whose file names come from a file read past its limits prints what is not known.
|
|
584
|
+
// A file empty when it is read: the next text stands alone (`printf '' > a` after an open quote).
|
|
585
|
+
`printf '"' > a; printf '' > a; bash <(cat a; printf 'gh pr merge 5')`, `printf '"' > a; printf '' > a; bash -c "$(cat a; printf 'gh pr merge 5')"`,
|
|
586
|
+
`false && printf '"' > a; source <(cat a; printf 'gh pr merge 5')`, `printf 'echo "' "'" > b; source <(cat nofile b; printf 'gh pr merge 5')`,
|
|
587
|
+
// Readings are paired by their place: empty ones from files never written keep theirs.
|
|
588
|
+
...['bash -c "$(cat e1 e2 a; cat q)"', 'eval "$(cat e1 e2 a; cat q)"', 'source <(cat e1 e2 a; cat q)'].map((t) => `printf '"' > a; printf '' > a; printf 'echo hi\\n' > q; printf 'gh pr merge 5\\n' > q; false && printf 'echo z\\n' > q; ${t}`),
|
|
589
|
+
...['cat $(cat f) | bash', 'cat $(<f) | bash', 'cat $(cat f) > g; bash g'].map((t) => `bash -c "printf '%s' 'gh pr merge 5' > h"; cat > f <<'E'\nh${' '.repeat(300000)}\nE\n${t}`),
|
|
590
|
+
// #518, PR 1: the word after `>&` / `<&` as bash 3.2 reads it — an unquoted `-` closes the descriptor and the
|
|
591
|
+
// rest is a word of the command — and as zsh reads it — any word but digits, `-` or `p` is a file written.
|
|
592
|
+
'git commit -m x >&--n', 'git commit -m x >& --n', 'git commit -m x <&--n', 'git commit -m x <& --n', 'git commit -m x 2>&--n', 'git commit -m x 1>&--n',
|
|
593
|
+
'git commit -m x >&\t--n', 'git commit -m x >&-"-n"', 'git commit -m x >& -"-n"', 'git commit -m x >&-\\-n', 'git push origin >& -+main', 'git push origin >&-+main',
|
|
594
|
+
// Crossed with a brace and a `$(…)` word: bash expands the word that follows the `-`.
|
|
595
|
+
'git commit -m x >&--{n,x}', 'git commit -m x >&--$(echo n)', 'git commit -m x >& --$(echo n)', 'git push origin >&-{+main,x}',
|
|
596
|
+
'echo gh pr merge 1 >&--n; bash ./--n', 'echo gh pr merge 1 >&-n; bash ./-n', 'echo gh pr merge 1 >&1--n; bash ./1--n', 'echo gh pr merge 1 >&1-; bash ./1-',
|
|
597
|
+
'echo gh pr merge 1 >&p; bash ./p', 'echo gh pr merge 1 >&--n; source ./--n',
|
|
598
|
+
// zsh's `>!`, `>>!`, `&>!`, `&>>!`, `>>|`, `>&|` and `>&!` write a file, as `>|` does.
|
|
599
|
+
'echo gh pr merge 1 >! f; bash f', 'echo gh pr merge 1 >!f; bash f', 'echo gh pr merge 1 >>! f; bash f', 'echo gh pr merge 1 >>| f; bash f',
|
|
600
|
+
'echo gh pr merge 1 >&| f; bash f', 'echo gh pr merge 1 >&|f; bash f', 'echo gh pr merge 1 >&! f; bash f', 'echo gh pr merge 1 &>! f; bash f',
|
|
601
|
+
'echo gh pr merge 1 &>>! f; bash f', 'echo gh pr merge 1 1>! f; bash f', 'echo gh pr merge 1 >! "f"; bash f', 'bash -c "echo gh pr merge 1 >! f; bash f"',
|
|
602
|
+
// #518 review 1: main's redirect reading still runs under bash's and zsh's `$(…)` end rules — zsh reads `>|!` as
|
|
603
|
+
// `>|` writing the file `!` — and a blank before a descriptor number, a `\`-newline join, and a `#` right after
|
|
604
|
+
// bash's closing `-` are read too.
|
|
605
|
+
'echo "$(case x in x)\ngh pr >|! merge 5;; esac)"', 'echo "$(case x in x)\ngh pr &>|! merge 5;; esac)"', 'echo "$(case x in x)\ngit commit -m x >|! -n;; esac)"',
|
|
606
|
+
'echo gh pr >|! merge 5 >! f; bash f', 'gh pr >& 1 merge 5', 'gh pr 2>& 1 merge 5', 'gh pr >&\t2 merge 5', 'gh pr <& 0 merge 5',
|
|
607
|
+
'git commit -m x >\\\n&--n', 'git commit -m x >&\\\n--n', "echo x >&-#'\ngh pr merge 5\n#'", "echo x 2>&-#'\ngh pr merge 5\n#'", "echo x >& -#'\ngh pr merge 5\n#'",
|
|
608
|
+
`bash -c "echo x >&-#'\ngh pr merge 5\n#'"`,
|
|
609
|
+
// #518 review 2: what a `$(…)` prints is read by each shell's redirect reading too.
|
|
610
|
+
'$(echo gh >& 1 pr merge 5)', '$(echo gh <& 0 pr merge 5)', '$(echo gh 2>& 1 pr merge 5)', "bash -c '$(echo gh >& 1 pr merge 5)'", 'eval "$(echo gh >& 1 pr merge 5)"',
|
|
611
|
+
"bash <<'E'\n$(echo gh >& 1 pr merge 5)\nE", '$(echo gh pr >& 1- merge 5)', 'echo "$(case x in x)\n$(echo gh >& 1 pr merge 5);; esac)"',
|
|
612
|
+
// #518, PR 2: text printed with no newline runs on into what is printed next — through `>>`, a pipe, a group, a
|
|
613
|
+
// process substitution, `cat` of two files and a `$(…)`; `echo -n`, `print -n` and a `printf` format with no
|
|
614
|
+
// newline at its end.
|
|
615
|
+
"printf 'gh pr ' > f; printf 'merge 1' >> f; bash f", "{ printf 'gh pr '; echo merge 5; } | bash", "bash <(printf 'gh pr '; echo merge 1)", "echo -n 'gh pr ' > f; echo merge 1 >> f; bash f",
|
|
616
|
+
"printf '%s' 'gh pr ' > f; echo merge 1 >> f; bash f", "print -n 'gh pr ' > f; echo merge 1 >> f; zsh f", "printf 'gh pr ' > a; echo merge 5 > b; cat a b | bash", "printf 'gh pr ' > a; echo merge 5 > b; cat a b > c; bash c",
|
|
617
|
+
"{ echo -n gh pr; echo ' merge 5'; } | bash", "{ printf 'git com'; printf 'mit -n -m x'; } | bash", "echo -ne 'gh pr ' > f; echo merge 1 >> f; bash f", "printf -- 'gh pr ' > f; echo merge 1 >> f; bash f",
|
|
618
|
+
"printf 'gh pr ' > f; printf 'mer' >> f; echo 'ge 5' >> f; bash f", "source <(printf 'gh pr '; echo merge 1)", "echo x | { printf 'gh pr '; echo merge 5; } | bash",
|
|
619
|
+
// PR 2 review 1: the readings with no newline are kept apart, never among those paired by place — these two main
|
|
620
|
+
// refused only by its pairs, which readings put among them had moved — and the mark passes through `tee`, `cat`,
|
|
621
|
+
// `< f` and a filter; `printf`'s and `echo`'s escapes are made; an escaped backslash before `n` is no newline.
|
|
622
|
+
"printf x > a; echo y >> a; echo 'gh pr \\' > g; echo q0 > b; echo q1 > b; echo q2 > b; echo q3 > b; echo 'merge 5' > b; echo r0 > b; echo r1 > b; echo r2 > b; echo r3 > b; echo 'merge 5' > b; { cat a g; cat b; } | bash",
|
|
623
|
+
"echo x0 > x; echo x1 > x; echo x2 > x; echo x3 > x; echo x4 > x; echo 'gh pr \\' > g; : > a; printf p > b; echo y >> b; echo z > b; echo 'merge 5' > b; echo r0 > b; echo r1 > b; echo r2 > b; echo 'merge 5' > b; { cat x g a; cat b; } | bash",
|
|
624
|
+
"printf 'gh pr ' | tee f; echo merge 1 >> f; bash f", "printf 'gh pr ' | tee -a f; echo merge 1 >> f; bash f", "printf 'gh pr ' | cat > f; echo merge 1 >> f; bash f",
|
|
625
|
+
"{ printf 'gh pr ' | cat; echo merge 5; } | bash", "printf 'gh pr ' > f; { cat < f; echo merge 5; } | bash", "printf 'gh pr ' > f; cat < f > g; echo merge 5 >> g; bash g",
|
|
626
|
+
"bash <(printf 'gh pr ' | cat; echo merge 1)", "printf 'gh pr ' | tr a a > f; echo merge 5 >> f; bash f", "printf 'gh pr ' | sed s/x/x/ > f; echo merge 5 >> f; bash f",
|
|
627
|
+
"printf 'git push --force\\n' | bash", "{ printf 'git push'; printf ' --force\\n'; } | bash", "source <(printf -- 'git push'; printf ' --force\\n')",
|
|
628
|
+
"printf 'git push' > f; printf ' --force\\n' >> f; printf 'x' >> f; bash f", "{ printf 'git -c a=\\\\n'; echo 'b push --force'; } | bash", "{ echo -e 'gh pr \\c'; echo merge 5; } | bash",
|
|
629
|
+
"{ print -f 'gh pr '; echo merge 5; } | zsh",
|
|
630
|
+
// PR 2 review 2: a word's spellings kept apart stay apart too — main refuses this only by pairing `gh` with the
|
|
631
|
+
// last spelling of the next word — and `\c` before a final `\n` still prints no newline (zsh).
|
|
632
|
+
"printf '%s\\n' 'pr merge 5' > c; printf z0 > a; printf '%s\\n' gh > a; eval $(<a) $(<c)",
|
|
633
|
+
"printf '%s\\n' --force >> c; printf '%s\\n' 'pr merge 5' > a; printf '%s' gh > c; echo $(<c) \"$(cat a)\" | bash",
|
|
634
|
+
"{ printf 'gh pr \\c\\n'; echo merge 5; } | zsh",
|
|
635
|
+
]) assert.equal(agentGuardDecision(cmd).allow, false, cmd.slice(-40));
|
|
404
636
|
});
|
|
405
637
|
|
|
406
638
|
test('the agent guard lets the rest through: reads, the agent\'s own pushes, text that only mentions a command', () => {
|
|
@@ -482,6 +714,294 @@ test('the agent guard lets the rest through: reads, the agent\'s own pushes, tex
|
|
|
482
714
|
]) assert.equal(agentGuardDecision(cmd).allow, true, cmd);
|
|
483
715
|
assert.equal(agentGuardDecision(undefined).allow, true);
|
|
484
716
|
assert.deepEqual(shellParts('a "b c" \'d e\' && f\\ g | h'), [['a', 'b c', 'd e'], ['f g'], ['h']]);
|
|
717
|
+
// #492: a heredoc opened and closed with CRLF is text up to its `EOF\r` line; what follows is a command.
|
|
718
|
+
assert.deepEqual(shellParts('cat <<EOF\r\nb\r\nEOF\r\ngh pr merge 5\r\n'), [['cat'], ['gh', 'pr', 'merge', '5\r']]);
|
|
719
|
+
// A redirect's operator is not a word (#495): `<(` is a part break after it.
|
|
720
|
+
assert.deepEqual(shellParts('cat <(gh pr view 5) | f(){ x; }'), [['cat'], ['gh', 'pr', 'view', '5'], ['f'], ['{', 'x'], ['}']]);
|
|
721
|
+
// Redirects and their targets leave the command's words; `$'…'` reads its escapes; a here-string is input.
|
|
722
|
+
assert.deepEqual(shellParts('git push origin main 2>&1 --force'), [['git', 'push', 'origin', 'main', '--force']]);
|
|
723
|
+
assert.deepEqual(shellParts('gh >x pr merge 5 2> err &>all'), [['gh', 'pr', 'merge', '5']]);
|
|
724
|
+
assert.deepEqual(shellParts("git commit -m $'it\\'s\\n'"), [['git', 'commit', '-m', "it's\n"]]);
|
|
725
|
+
assert.deepEqual(shellParts('x=<<<1 git commit'), [['x=', 'git', 'commit']]);
|
|
726
|
+
// Still allowed: a heredoc that ends, a quoted parenthesis, arithmetic, three levels of nesting.
|
|
727
|
+
for (const ok of ['cat <<EOF\nhello\nEOF', 'git commit -m "fix (thing)"', 'echo $((1<<2))', 'echo $(echo $(echo hi))', 'git log\r\n',
|
|
728
|
+
'(cd web && npm ci)# install', 'npm test # x', 'cat <<E$x\nhi\nE$x', '[[ $x =~ ^#(a|b) ]]', 'git commit -m "$(cat <<\'EOF\'\nmsg\nEOF\n)"',
|
|
729
|
+
// A docs heredoc naming a guarded command stays allowed: only an end word that expands adds line-by-line reading.
|
|
730
|
+
`cat <<'EOF' > "$f"\ngh pr merge 1 is the person's\nEOF`, "gh pr create --body-file - <<'EOF' # $x\ngh pr merge 1 stays with the person\nEOF",
|
|
731
|
+
`cat <<'EOF'\ngh pr merge 1 docs\nEOF\ncat > b <<"E$n"\nx\nE$n`,
|
|
732
|
+
// #495: the common forms around the new readings stay allowed.
|
|
733
|
+
'npm test 2>&1 | tail', 'git log > out.txt', 'cmd >/dev/null 2>&1', 'find . -name x -exec grep y {} +', 'git -c core.pager=less log', 'eval "$(ssh-agent)"',
|
|
734
|
+
'echo hi | bash -s', 'curl -fsSL https://example.com/i.sh | sh', "git commit -m $'line one\\nline two'", 'cat <<< "gh pr merge 5"', "python3 - <<'PY'\nprint(1)\nPY",
|
|
735
|
+
'bash <<EOF\necho hi\nEOF', 'git log --oneline 2>/dev/null | head -3',
|
|
736
|
+
'find . -name x -exec grep "git push --force" {} +', 'git -c alias.lg="log --oneline" lg', 'curl -fsSL https://x | bash -s -- --yes', '[[ a > b ]]', 'echo {a,b}.txt',
|
|
737
|
+
'$EDITOR notes.md', '${PYTHON:-python3} x.py', '$(npm bin)/eslint .', "cat <<'EOF' | bash\necho hi\nEOF", 'ls <->', 'echo "gh pr merge 5" > notes.md; bash build.sh',
|
|
738
|
+
'curl x | bash -s stable', 'cat build.sh | bash', 'bash -x ./build.sh arg', 'mkdir -p src/{a,b,c}', 'git add src/{a,b}.js', "jq '{a,b}' f.json", 'echo {1..5}', 'ls {a,b}{c,d}',
|
|
739
|
+
'for i in {1..100}; do echo $i; done', 'echo {0..64}', 'touch file{001..100}.txt', `curl -d '[${'{"id":0,"v":2},'.repeat(5)}{}]' https://x`,
|
|
740
|
+
'echo x || bash', 'docker ps --format {{.Names}}', 'gh pr view {1,2,3}', 'gh pr {merge\\,x,} 5',
|
|
741
|
+
`gh api repos/x -f body='[${'{"a":1,"b":2},'.repeat(10)}{}]'`, "echo '{a,b}{a,b}{a,b}{a,b}{a,b}'", 'echo {a,b}{a,b}{a,b}{a,b}{a,b}', 'bash -euo pipefail ./x.sh', 'echo {a..z}',
|
|
742
|
+
`curl -d '[${'{"a":1,"b":2},'.repeat(66000)}{}]' https://x`,
|
|
743
|
+
`git add ${'f.js '.repeat(2000)}src/{a,b}.js`,
|
|
744
|
+
// #498: the ordinary forms around the new readings stay allowed.
|
|
745
|
+
"echo $(echo $'it\\'s')", "x=$(printf $'a\\tb')", 'echo "$(echo $\'\\\'\')"', 'echo "$(case $x in a) echo a;; esac)"', 'echo "$(echo case)"',
|
|
746
|
+
'echo {x}', 'gh pr view {x},5}', 'git log {x},y}', "echo {$(echo '}'),x}", 'git log {$(echo a),b}', 'gh api repos/{owner}/{repo}/pulls',
|
|
747
|
+
'bash -c "echo \\"hi\\""', 'bash -c "echo \\$HOME"', "bash -c \"grep '\\d' f\"", "bash -c \"printf 'a\\nb'\"", "eval \"echo \\'quoted\\'\"", 'grep "\\d+" f',
|
|
748
|
+
'echo "$(echo test case)"', "echo \"it\\'s\" | cat", 'echo "$(case y in x) echo esac;; esac)"', "cat <<EOF\n$(echo $'it\\'s')\nEOF",
|
|
749
|
+
// The budget counts only a `$(…)` the shells end elsewhere, once per reading: 40 ordinary ones, with a CR too.
|
|
750
|
+
Array(40).fill("x=$(printf $'a\\tb')").join('; '), `${Array(40).fill("x=$(printf $'a\\tb')").join('; ')}\r`,
|
|
751
|
+
// Review round 2: a line read again with `$(:)` is judged at its own depth, and `a|esac)` is a pattern.
|
|
752
|
+
`echo "$(printf $'it\\'s')" "$(printf $'a\\'b')" "$(printf $'c\\'d')"`, 'echo "$(case x in a|esac) echo a;; esac)"',
|
|
753
|
+
// Any number of `$(…)` that a shell ends elsewhere: each reading reads them all in one pass.
|
|
754
|
+
Array.from({ length: 100 }, (_, n) => `echo "$(case x${n} in x${n}) echo esac;; esac)"`).join('\n'), Array(4).fill('echo $(case a in a) echo hi;; esac)').join(' '), Array(100).fill("$(echo $'\\')')").join(' '),
|
|
755
|
+
// #516: a heredoc in `$(…)`, zsh's `case x { … }` and a `case` word in backticks, with safe commands.
|
|
756
|
+
// Review round 2: a `#` right at the 4,096-character bound of a `case` word is judged at once (it looped forever).
|
|
757
|
+
// Round 3: many heredocs that never end, inside `$(…)`, are read in linear time (one text each was quadratic).
|
|
758
|
+
`x=$(cat <<A\n${'bash <<B\n'.repeat(10000)})`,
|
|
759
|
+
`echo "$(case x${' '.repeat(4090)}#x\nin x) ;; esac)"`, `echo "$(case x${';'.repeat(4090)}#x\nin x) ;; esac)"`,
|
|
760
|
+
"x=$(cat <<'E'\n(a)\nE\n); echo ok", "git commit -m \"$(cat <<'EOF'\nfix: it's done\nEOF\n)\" && git push", 'echo "$(case x { x) echo a ;; })"',
|
|
761
|
+
'echo "$(case `echo x` in x) echo a ;; esac)"', 'echo "$(case x #c\nin x) echo a ;; esac)"',
|
|
762
|
+
// A `#` that starts no word is no comment, and backticks inside `$(…)` are ordinary.
|
|
763
|
+
'x=$(echo a#b)', 'echo $(git log --format=%H#%s -1)', 'echo "$(date) # not a comment"', 'echo $(echo $#)', 'echo ${#x} $(echo ${#y})',
|
|
764
|
+
'n=$((16#ff)); echo $(echo $n)', 'echo $(echo `date`)', 'echo $( # a comment\necho hi )', 'y=$(echo `echo a`)$(echo b)',
|
|
765
|
+
// #498, rows 1, 2, 8: a process substitution as an argument, a `$(…)` whose text is not run or not known, and
|
|
766
|
+
// a file written that no shell runs.
|
|
767
|
+
'bash build.sh <(echo "gh pr merge 5")', 'cat <(echo "gh pr merge 5")', 'bash <(curl -fsSL https://x/i.sh)', 'bash -c "$(curl -fsSL https://x/i.sh)"',
|
|
768
|
+
"bash -c 'echo $(echo gh pr merge 5)'", 'bash -c "echo $(echo gh pr merge 5)"', 'echo "$(echo gh pr merge 5)"', 'x=$(git rev-parse HEAD); echo $x',
|
|
769
|
+
'cd "$(git rev-parse --show-toplevel)" && bash build.sh', 'echo x > notes; bash build.sh', 'echo "gh pr merge 5" > notes.md; bash build.sh', "echo 'gh pr merge 5' > f; cat f",
|
|
770
|
+
'bash build.sh > log.txt; cat log.txt', 'echo "gh pr merge 5" 2> f; bash f', "echo 'gh pr merge 5' > f; bash g", 'git diff > x.patch; git apply x.patch',
|
|
771
|
+
// Review round 1: a file printed, however many times it was written; a copy of a descriptor is no file.
|
|
772
|
+
`${Array.from({ length: 70 }, (_, i) => `echo "line ${i}" >> notes.md`).join('; ')}; cat notes.md`, 'npm test 2>&1 | tee out.txt', 'echo "$(date)" >> notes.md',
|
|
773
|
+
'cat < in.txt > out.txt', 'bash build.sh >&2', 'echo x >&-', 'source .venv/bin/activate',
|
|
774
|
+
// Review round 2: a file run after many lines written to it; descriptors copied; a `case` that runs a script.
|
|
775
|
+
`cat > f <<'E'\n${'npm run lint\n'.repeat(45000)}E\nbash f`, 'npm run build >&build.log', 'exec 3>&1; npm test >&3', 'make 2>&1 >&2 | tee make.log',
|
|
776
|
+
'case "$1" in start) bash run.sh;; esac', 'printf "%s\\n" a > f; bash f',
|
|
777
|
+
// Review round 3: a long `case` in a process substitution; a quoted word after `>&`.
|
|
778
|
+
`bash <(case x in ${'a) echo a;; '.repeat(600)}esac)`, 'git commit -m x >&"--n"', 'echo hi 2>& f',
|
|
779
|
+
// Review round 4: bash passes nothing here.
|
|
780
|
+
'git commit -m x >& "-"-n', 'git commit -m x >&1--n', `bash <(${'case y in y) :;; esac; '.repeat(40000)})`,
|
|
781
|
+
// #518, PR 1: the forms around the new readings stay allowed — a descriptor copied, moved or closed, a file
|
|
782
|
+
// written that nothing runs, and words that run nothing in bash or zsh.
|
|
783
|
+
'npm run build 2>&1 | tee build.log', 'git log >& log.txt', 'git diff >& d.txt; cat d.txt', 'echo x >&2', 'cmd 2>&1 >/dev/null', 'git fetch 2>&- ; git status',
|
|
784
|
+
'git commit -m x >&-n', 'git commit -m x >&1-', 'git commit -m x 2>&1--n', 'git commit -m x >&p', 'npm test >! out', 'git status >| s.txt', 'npm test >>! out.log',
|
|
785
|
+
'echo gh pr merge 1 2>&--n; bash ./--n', 'echo gh pr merge 1 2>! f; bash f', 'echo gh pr merge 1 > !f; bash f', 'echo gh pr merge 1 <&--n; bash ./--n',
|
|
786
|
+
'git commit -m x >&| -n', 'git push origin main >&2',
|
|
787
|
+
// #518 review 1: a blank before the number copies a descriptor as before; a `#` after `>&-` with a blank is text.
|
|
788
|
+
'make >& 1 | head', 'echo x >& 2', 'npm test 2>& 1 | tail', 'echo x >&- # done', 'echo "$(case x in x) echo a >&2;; esac)"',
|
|
789
|
+
'x=$(git status >& 1)', 'echo "$(make 2>& 1 | tail)"', 'out=$(npm test >&- 2>&1)',
|
|
790
|
+
// #518, PR 2: a newline printed between them keeps the texts apart; ordinary prompts and files stay allowed.
|
|
791
|
+
"printf 'gh pr \\n' > f; echo merge 1 >> f; bash f", "printf '%s\\n' 'gh pr ' > f; echo merge 1 >> f; bash f", "echo 'gh pr' > f; echo merge 1 >> f; bash f",
|
|
792
|
+
"printf 'building ' ; npm run build", "echo -n 'Password: '; read -s p", "printf 'npm ' > f; echo test >> f; bash f", "{ printf 'a'; echo b; } | cat",
|
|
793
|
+
`${Array.from({ length: 60 }, (_, i) => `printf 'line ${i} ' >> notes.md`).join('; ')}; cat notes.md`,
|
|
794
|
+
// PR 2 review 1: what a `grep -q` is piped is not printed; a script built line by line with `printf` runs.
|
|
795
|
+
"{ printf 'gh pr ' | grep -q x; echo merge 5; } | bash", `printf '#!/bin/sh\\n' > run.sh; ${Array.from({ length: 30 }, (_, i) => `printf 'npm run step${i}\\n' >> run.sh`).join('; ')}; bash run.sh`,
|
|
796
|
+
"printf 'x=1\\n' > env.sh; echo 'echo $x' >> env.sh; bash env.sh"]) {
|
|
797
|
+
assert.equal(agentGuardDecision(ok).allow, true, ok);
|
|
798
|
+
}
|
|
799
|
+
});
|
|
800
|
+
|
|
801
|
+
// A copy of the guard whose deadline a test sets per call (`globalThis.YAD_TEST_BUDGET`, cleared after), with its
|
|
802
|
+
// imports pointing back here (its SHIPPED_CHECKS path points nowhere, which agentGuardDecision never reads).
|
|
803
|
+
async function guardCopy() {
|
|
804
|
+
const dir = path.dirname(fileURLToPath(import.meta.url));
|
|
805
|
+
const src = fs.readFileSync(path.join(dir, 'agent-guard.mjs'), 'utf8')
|
|
806
|
+
.replace('deadline = Date.now() + JUDGE_BUDGET_MS;', 'deadline = Date.now() + (globalThis.YAD_TEST_BUDGET ?? JUDGE_BUDGET_MS);')
|
|
807
|
+
.replace(/from '\.\/([\w-]+\.mjs)'/g, (_, f) => `from ${JSON.stringify(new URL(f, import.meta.url).href)}`);
|
|
808
|
+
assert.ok(src.includes('globalThis.YAD_TEST_BUDGET ?? JUDGE_BUDGET_MS'), 'the budget is set per call in the copy');
|
|
809
|
+
const T = fs.mkdtempSync(path.join(os.tmpdir(), 'yad-guard-copy-'));
|
|
810
|
+
const copy = path.join(T, 'agent-guard.mjs');
|
|
811
|
+
fs.writeFileSync(copy, src);
|
|
812
|
+
const mod = await import(pathToFileURL(copy).href);
|
|
813
|
+
const done = () => { delete globalThis.YAD_TEST_BUDGET; fs.rmSync(T, { recursive: true, force: true }); };
|
|
814
|
+
// A judgement with no deadline: a slow runner takes the real time, and the verdict never depends on it.
|
|
815
|
+
const uncut = (l) => { globalThis.YAD_TEST_BUDGET = Infinity; try { return mod.agentGuardDecision(l); } finally { delete globalThis.YAD_TEST_BUDGET; } };
|
|
816
|
+
return { mod, uncut, done };
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
test('#535 CI: a reading of what a command writes, stopped by the deadline, is refused as out of time, not as too much written', async () => {
|
|
820
|
+
// A slow runner, made on purpose, from the copy whose budget is set per call. Where the deadline lands depends
|
|
821
|
+
// on the machine's speed, so each line is timed with no deadline, then judged again at budgets from a tenth of
|
|
822
|
+
// that time to nearly all of it: every phase the deadline can stop is reached.
|
|
823
|
+
const { mod: slow, done } = await guardCopy();
|
|
824
|
+
try {
|
|
825
|
+
const half = (unit) => unit.repeat(Math.floor(slow.MAX_JUDGED / 2 / unit.length));
|
|
826
|
+
const steps = process.env.NODE_V8_COVERAGE ? [0.1, 0.3, 0.5, 0.7, 0.9] : [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9];
|
|
827
|
+
for (const unit of ['bash -c "$(echo x)";', 'echo x > f; bash f;', 'x=$(echo a > f; bash f);', 'echo $(echo x)', 'bash <(cat <<E\nx\nE\n) ']) {
|
|
828
|
+
const line = half(unit);
|
|
829
|
+
globalThis.YAD_TEST_BUDGET = Infinity;
|
|
830
|
+
const t0 = Date.now();
|
|
831
|
+
assert.equal(slow.agentGuardDecision(line).allow, true, `${unit}: allowed in full time`);
|
|
832
|
+
const full = Date.now() - t0;
|
|
833
|
+
for (const f of steps) {
|
|
834
|
+
globalThis.YAD_TEST_BUDGET = Math.max(1, Math.round(full * f));
|
|
835
|
+
const d = slow.agentGuardDecision(line);
|
|
836
|
+
assert.ok(d.allow || /took too long to judge/.test(d.message), `${unit} at ${globalThis.YAD_TEST_BUDGET} of ${full} ms: ${d.message}`);
|
|
837
|
+
}
|
|
838
|
+
delete globalThis.YAD_TEST_BUDGET;
|
|
839
|
+
}
|
|
840
|
+
} finally { done(); }
|
|
841
|
+
});
|
|
842
|
+
|
|
843
|
+
test('#492: a line of many heredocs that never end is judged in bounded time; one too long to judge is refused', async (t) => {
|
|
844
|
+
// A coverage run makes this work about seven times slower, which says nothing about the guard, so it is not
|
|
845
|
+
// timed. The other runs are timed, but a shared CI machine has passed 3 s on work that takes 0.5 s here, so
|
|
846
|
+
// every limit is 4.5 s: still under the guard's own 5 s cut-off, because a line refused at the cut-off would
|
|
847
|
+
// otherwise pass this test and hide a slowdown. Nested tails keep 6 s; that case runs into the cut-off inside
|
|
848
|
+
// the brace expansion by design. The lines read in full, with their output read too (#535), take about 1.2 s
|
|
849
|
+
// here, and a CI runner has been about four times slower: near the cut-off, which would refuse them. They are
|
|
850
|
+
// judged with no deadline (`uncut`), so the verdict is exact and the time is real, and get `SLOW`: a reading
|
|
851
|
+
// that has turned quadratic takes minutes on such a line, so it still shows.
|
|
852
|
+
const within = (ms) => (process.env.NODE_V8_COVERAGE ? Infinity : ms);
|
|
853
|
+
const { MAX_JUDGED, JUDGE_BUDGET_MS } = await import('./agent-guard.mjs');
|
|
854
|
+
const SLOW = JUDGE_BUDGET_MS + 1500;
|
|
855
|
+
const { uncut, done } = await guardCopy();
|
|
856
|
+
t.after(done);
|
|
857
|
+
const line = `${'cat <<X\n'.repeat(60000)}gh pr merge 5`;
|
|
858
|
+
const t0 = Date.now();
|
|
859
|
+
assert.equal(agentGuardDecision(line).allow, false);
|
|
860
|
+
assert.ok(Date.now() - t0 < within(4500), `took ${Date.now() - t0} ms`);
|
|
861
|
+
// Distinct end words, just under the cap: each end line is found by a lookup, not a scan.
|
|
862
|
+
let distinct = '';
|
|
863
|
+
for (let i = 0; distinct.length < MAX_JUDGED - 20; i++) distinct += `<<A${i}\n`;
|
|
864
|
+
const t1 = Date.now();
|
|
865
|
+
assert.equal(agentGuardDecision(`${distinct}gh pr merge 5`).allow, false);
|
|
866
|
+
assert.ok(Date.now() - t1 < within(4500), `distinct words took ${Date.now() - t1} ms`);
|
|
867
|
+
// Many `$(…)`, each holding a heredoc, near the cap: one line index per text, shared by every `$(` in it.
|
|
868
|
+
for (const unit of ['$(cat <<E\nx\nE\n)', '$(cat <<E\n$(cat <<F\nx\nF\n)\nE\n)']) {
|
|
869
|
+
const many = `${unit.repeat(Math.floor((MAX_JUDGED - 100) / unit.length))}\ngh pr merge 1`;
|
|
870
|
+
const t3 = Date.now();
|
|
871
|
+
assert.equal(agentGuardDecision(many).allow, false);
|
|
872
|
+
assert.ok(Date.now() - t3 < within(4500), `many $( took ${Date.now() - t3} ms`);
|
|
873
|
+
}
|
|
874
|
+
// A run of `<<` with no newline: the expanding-word check reads the matches one by one, never a backtracking regex.
|
|
875
|
+
const t4 = Date.now();
|
|
876
|
+
assert.equal(agentGuardDecision(`gh pr merge 1 # ${'<<'.repeat(125000)}`).allow, false);
|
|
877
|
+
assert.ok(Date.now() - t4 < within(4500), `a run of << took ${Date.now() - t4} ms`);
|
|
878
|
+
// #495 review: a `case` scan per `$(` and a loop over every body for every part were quadratic.
|
|
879
|
+
// Many words under the length bound, each with lists and a nested tail: the budget is checked inside the expansion.
|
|
880
|
+
const nestedTail = `x {a,b}{a,b}{a,b}{a,b}{a,b}{a,b}${'{'.repeat(2000)}${'}'.repeat(2000)};`;
|
|
881
|
+
const t7 = Date.now();
|
|
882
|
+
assert.equal(agentGuardDecision(`${nestedTail.repeat(245)} gh pr merge 5`).allow, false);
|
|
883
|
+
assert.ok(Date.now() - t7 < within(6000), `nested tails took ${Date.now() - t7} ms`);
|
|
884
|
+
// A `{` and many commas with no `}`: the brace check is a scan, never a backtracking regex.
|
|
885
|
+
for (const l of [`echo '{${','.repeat(250000)}'; gh pr merge 5`, `git status {${','.repeat(990000)}`]) {
|
|
886
|
+
const t6 = Date.now();
|
|
887
|
+
agentGuardDecision(l);
|
|
888
|
+
assert.ok(Date.now() - t6 < within(4500), `{ + commas took ${Date.now() - t6} ms`);
|
|
889
|
+
}
|
|
890
|
+
for (const [unit, tail] of [['$()', '$()'], ['$(a) ', '$(a) '], [':<<<x;', 'sh;'], [':<<E\nE\n', 'sh;']]) {
|
|
891
|
+
const l = unit.repeat(Math.floor(495000 / unit.length)) + tail.repeat(Math.floor(495000 / tail.length));
|
|
892
|
+
const t5 = Date.now();
|
|
893
|
+
uncut(l);
|
|
894
|
+
assert.ok(Date.now() - t5 < within(SLOW), `${JSON.stringify(unit)} took ${Date.now() - t5} ms`);
|
|
895
|
+
}
|
|
896
|
+
let inSub = 'echo $(';
|
|
897
|
+
for (let i = 0; i < 10000; i++) inSub += `<<B${i}\n`;
|
|
898
|
+
const t2 = Date.now();
|
|
899
|
+
agentGuardDecision(`${inSub})`);
|
|
900
|
+
assert.ok(Date.now() - t2 < within(4500), `inside $( took ${Date.now() - t2} ms`);
|
|
901
|
+
// #498: each shell's reading of `$(…)` is one pass, and bash's brace rule is bounded per word.
|
|
902
|
+
for (const unit of ["$(echo $'a\\'", '$(case x in x) ', "\"$(case x in x)\n", `{x}${'{x}'.repeat(50)},y `, 'echo "$(case $(', ' case $(', ' case ${', '(case ${', " case '"]) {
|
|
903
|
+
const l = unit.repeat(Math.floor((MAX_JUDGED - 100) / unit.length)) + ' gh pr merge 5';
|
|
904
|
+
const t8 = Date.now();
|
|
905
|
+
agentGuardDecision(l);
|
|
906
|
+
assert.ok(Date.now() - t8 < within(4500), `#498 ${JSON.stringify(unit.slice(0, 12))} took ${Date.now() - t8} ms`);
|
|
907
|
+
}
|
|
908
|
+
// #498, rows 1, 2, 8: each new path is linear; a file run again after each of many appends is refused at its bound.
|
|
909
|
+
// Half the cap (#498 CI): a shared runner has run two to four times slower than a laptop. The trade-off: a path
|
|
910
|
+
// that scans every earlier part costs a quarter as much at half the length, so a mild one may only show near
|
|
911
|
+
// the limit. Judged with no deadline, so every run checks each verdict.
|
|
912
|
+
const half = (unit) => unit.repeat(Math.floor(MAX_JUDGED / 2 / unit.length));
|
|
913
|
+
for (const unit of ['bash <(echo x) ', 'bash <(', 'echo x > f; bash f;', 'echo x >> f; ', 'printf x >> f; ', 'x=$(echo a > f; bash f);', 'bash -c "$(echo x)";', 'echo $(echo x)', 'bash <(cat <<E\nx\nE\n) ', 'x <<< "$(echo a b)" ', '$(<f)']) {
|
|
914
|
+
const l = half(unit);
|
|
915
|
+
const t10 = Date.now();
|
|
916
|
+
const d = uncut(l);
|
|
917
|
+
assert.ok(d.allow, `${unit}: ${d.message}`);
|
|
918
|
+
assert.ok(Date.now() - t10 < within(SLOW), `#498 ${JSON.stringify(unit)} took ${Date.now() - t10} ms`);
|
|
919
|
+
}
|
|
920
|
+
for (const unit of ['echo x >> f; bash f;', 'echo x | tee -a f; cat f | bash;', 'echo x > f; echo "$(cat f)$(cat f)" > f; cat f | bash;']) {
|
|
921
|
+
const l = half(unit);
|
|
922
|
+
const t11 = Date.now();
|
|
923
|
+
// Refused at its bound, or on a runner slow enough to need it at the 5 s cut-off (CI's Node 20 reached the
|
|
924
|
+
// bound in 4.7 s). Either way within the guard's own budget, with slack: a missing deadline check still shows.
|
|
925
|
+
const m = agentGuardDecision(l).message;
|
|
926
|
+
const ms = Date.now() - t11;
|
|
927
|
+
assert.ok(/runs files it writes too many times|took too long/.test(m ?? ''), `${unit}: ${m}`);
|
|
928
|
+
assert.ok(ms < within(JUDGE_BUDGET_MS + 1500), `#498 ${JSON.stringify(unit)} took ${ms} ms`);
|
|
929
|
+
}
|
|
930
|
+
const echoes = uncut(`echo ${'$(echo x)'.repeat(100000)}`);
|
|
931
|
+
assert.ok(echoes.allow, echoes.message);
|
|
932
|
+
// CI review: many file names from a `$(…)`, each read joined to a long text, ran out of memory before the cut-off.
|
|
933
|
+
for (const n of [60000, 150000]) {
|
|
934
|
+
const t14 = Date.now();
|
|
935
|
+
agentGuardDecision(`cat > f <<'E'\nh ${'a '.repeat(n)}\nE\ncat $(cat f) | bash`);
|
|
936
|
+
assert.ok(Date.now() - t14 < within(4500), `#498 cat $(cat f) with ${n} names took ${Date.now() - t14} ms`);
|
|
937
|
+
}
|
|
938
|
+
// Review round 3: many files to one `cat`, many `>&f`, many parts in one process substitution stay linear.
|
|
939
|
+
for (const l of [`echo -n a>f;cat ${'f '.repeat(490000)};gh pr merge 5`, `cat ${'x '.repeat(490000)};gh pr merge 5`, `echo hi ${'>&f '.repeat(245000)}`, `bash <(${'echo a;'.repeat(140000)})`]) {
|
|
940
|
+
const t13 = Date.now();
|
|
941
|
+
uncut(l);
|
|
942
|
+
assert.ok(Date.now() - t13 < within(SLOW), `#498 ${JSON.stringify(l.slice(0, 20))} took ${Date.now() - t13} ms`);
|
|
943
|
+
}
|
|
944
|
+
// From here on, each check expects one exact verdict: an allow, or a refusal for a named reason. The guard's own
|
|
945
|
+
// 5 s cut-off would turn any of them into "took too long" on a slow enough runner: the coverage leg runs the
|
|
946
|
+
// near-limit `case` line in about 1.8 s on a laptop, and a CI runner has been about four times slower. So
|
|
947
|
+
// `exact` always runs the real guard, and on every run but coverage its verdict is the one checked, and the long
|
|
948
|
+
// lines are timed against the real budget (4.5 s, under the 5 s cut-off). Under coverage (not timed) the real guard still runs, so
|
|
949
|
+
// its lines are counted, and the verdict checked is the copy's with no deadline (`uncut`), which never depends
|
|
950
|
+
// on the runner's speed.
|
|
951
|
+
const exact = (l) => { const real = agentGuardDecision(l); return process.env.NODE_V8_COVERAGE ? uncut(l) : real; };
|
|
952
|
+
// Review round 1: nested process substitutions are refused past their bound, not read in quadratic time.
|
|
953
|
+
for (const d of [4000, 16000]) {
|
|
954
|
+
const t12 = Date.now();
|
|
955
|
+
assert.match(exact(`${'bash <('.repeat(d)}echo hi${')'.repeat(d)}`).message, /too many process substitutions/);
|
|
956
|
+
assert.ok(Date.now() - t12 < within(4500), `#498 nested <( took ${Date.now() - t12} ms`);
|
|
957
|
+
}
|
|
958
|
+
assert.equal(exact(Array(60).fill("echo 'echo hi' >> f").join('; ') + '; bash f').allow, true);
|
|
959
|
+
// #498 review round 7: each budget refuses past its limit, and a line well under it stays allowed.
|
|
960
|
+
const t15 = Date.now();
|
|
961
|
+
assert.match(exact(`echo $(${' case ${'.repeat(120000)})`).message, /too many `case` words/);
|
|
962
|
+
assert.ok(Date.now() - t15 < within(4500), `#498 many case words took ${Date.now() - t15} ms`);
|
|
963
|
+
// Near the limit: 240 `case` words of 4,000 characters read about 960,000 of the 1,048,576 allowed. The line
|
|
964
|
+
// must also stay under MAX_JUDGED, or it is refused as too long rather than for its words.
|
|
965
|
+
const nearCase = `echo $(${` case ${'x'.repeat(4000)} in x) :;; esac;`.repeat(240)})`;
|
|
966
|
+
assert.ok(nearCase.length < MAX_JUDGED, `the near-limit line is ${nearCase.length} characters`);
|
|
967
|
+
const t16 = Date.now();
|
|
968
|
+
const near = exact(nearCase);
|
|
969
|
+
assert.equal(near.allow, true, near.message);
|
|
970
|
+
assert.ok(Date.now() - t16 < within(4500), `#498 near-limit case words took ${Date.now() - t16} ms`);
|
|
971
|
+
assert.match(exact(`echo ${'{x}'.repeat(1300)},y`).message, /brace lists expand to too many words/);
|
|
972
|
+
assert.equal(exact(`echo ${'{x}'.repeat(150)},y`).allow, true);
|
|
973
|
+
// #498 review: the kept spelling of a long double-quoted word with many backslashes is built in one pass.
|
|
974
|
+
const n = Math.floor((MAX_JUDGED - 100) / 2);
|
|
975
|
+
for (const [l, allow] of [[`bash -c "${'\\x'.repeat(n)}; gh pr merge 5"`, false], [`echo "${'\\x'.repeat(n)}"`, true]]) {
|
|
976
|
+
const t9 = Date.now();
|
|
977
|
+
assert.equal(exact(l).allow, allow, l.slice(0, 20));
|
|
978
|
+
assert.ok(Date.now() - t9 < within(4500), `#498 kept backslashes took ${Date.now() - t9} ms`);
|
|
979
|
+
}
|
|
980
|
+
const long = agentGuardDecision(`echo ${'x'.repeat(MAX_JUDGED)}`);
|
|
981
|
+
assert.equal(long.allow, false);
|
|
982
|
+
assert.match(long.message, /too long to judge/);
|
|
983
|
+
});
|
|
984
|
+
|
|
985
|
+
test('#518: a long line the bash and zsh readings are added for is judged in bounded time', () => {
|
|
986
|
+
const within = (ms) => (process.env.NODE_V8_COVERAGE ? Infinity : ms);
|
|
987
|
+
for (const line of [`${'npm run lint >&- ; '.repeat(40000)}gh pr view 1`, `${'echo x >! f; '.repeat(40000)}bash f`, `cat > f <<'E'\n${'npm run lint\n'.repeat(45000)}E\nbash f >&-`]) {
|
|
988
|
+
const t0 = Date.now();
|
|
989
|
+
agentGuardDecision(line);
|
|
990
|
+
assert.ok(Date.now() - t0 < within(4500), `took ${Date.now() - t0} ms: ${line.slice(0, 30)}`);
|
|
991
|
+
}
|
|
992
|
+
assert.equal(agentGuardDecision(`${'npm run lint >&- ; '.repeat(40000)}git commit -m x >&--n`).allow, false);
|
|
993
|
+
// PR 2: joins with no newline stay linear — a long run of `>>` after `printf`, and `cat` of many files.
|
|
994
|
+
for (const line of [`${Array.from({ length: 3000 }, (_, i) => `printf 'a${i} ' >> f`).join('; ')}; bash f`, `printf 'x ' > a; ${'cat a a a a a a a a a a | '.repeat(1)}cat ${'a '.repeat(20000)}| bash`]) {
|
|
995
|
+
const t2 = Date.now();
|
|
996
|
+
agentGuardDecision(line);
|
|
997
|
+
assert.ok(Date.now() - t2 < within(4500), `took ${Date.now() - t2} ms: ${line.slice(0, 30)}`);
|
|
998
|
+
}
|
|
999
|
+
// Review 2: a `$(…)` inside a `$(…)` keeps its reading's shell, so nesting does not multiply the readings.
|
|
1000
|
+
const nest = (n) => (n ? `$(echo a >&p >& 1 >&|q ${nest(n - 1)})` : 'x');
|
|
1001
|
+
const nested = Array(20).fill(`echo ${nest(3)}`).join('; ');
|
|
1002
|
+
const t1 = Date.now();
|
|
1003
|
+
assert.equal(agentGuardDecision(nested).allow, true);
|
|
1004
|
+
assert.ok(Date.now() - t1 < within(4500), `nested took ${Date.now() - t1} ms`);
|
|
485
1005
|
});
|
|
486
1006
|
|
|
487
1007
|
test('the refusal says what to do instead: the person runs it', () => {
|
|
@@ -509,27 +1029,125 @@ const runGuard = (T, payload, env = {}) => {
|
|
|
509
1029
|
|
|
510
1030
|
test('hooks/agent-guard.mjs: Claude Code reads the exit code, Cursor a JSON verdict, always', () => {
|
|
511
1031
|
const T = guardScript();
|
|
1032
|
+
// Every assertion carries the hook's exit code, answer and stderr: the reason it allowed is on stderr,
|
|
1033
|
+
// and without it a failure on CI (macOS, #459 and #460) said nothing.
|
|
1034
|
+
const said = (r) => `hook said ${JSON.stringify(r)}`;
|
|
1035
|
+
const parse = (r) => { try { return JSON.parse(r.out); } catch { assert.fail(`not a JSON verdict — ${said(r)}`); } };
|
|
512
1036
|
try {
|
|
513
1037
|
const claude = (command) => runGuard(T, { hook_event_name: 'PreToolUse', tool_name: 'Bash', tool_input: { command } });
|
|
514
1038
|
const denied = claude('yad gate approve EP-a epic --by me');
|
|
515
|
-
assert.equal(denied.code, 2);
|
|
516
|
-
assert.match(denied.err, /the person's to run
|
|
517
|
-
|
|
1039
|
+
assert.equal(denied.code, 2, said(denied));
|
|
1040
|
+
assert.match(denied.err, /the person's to run/, said(denied));
|
|
1041
|
+
const status = claude('yad status');
|
|
1042
|
+
assert.equal(status.code, 0, said(status));
|
|
518
1043
|
const cursor = (command) => runGuard(T, { hook_event_name: 'beforeShellExecution', command });
|
|
519
1044
|
const no = cursor('git push --force');
|
|
520
|
-
assert.equal(no.code, 0);
|
|
521
|
-
assert.equal(
|
|
522
|
-
assert.match(
|
|
523
|
-
|
|
1045
|
+
assert.equal(no.code, 0, said(no));
|
|
1046
|
+
assert.equal(parse(no).permission, 'deny', said(no));
|
|
1047
|
+
assert.match(parse(no).agent_message, /force push/, said(no));
|
|
1048
|
+
const ls = cursor('ls');
|
|
1049
|
+
assert.deepEqual(parse(ls), { permission: 'allow' }, said(ls));
|
|
524
1050
|
// Fail-open: an unreadable payload, a switched-off guard, and no yad at all.
|
|
525
|
-
|
|
526
|
-
assert.equal(
|
|
1051
|
+
const junk = runGuard(T, 'not json');
|
|
1052
|
+
assert.equal(junk.code, 0, said(junk));
|
|
1053
|
+
const off = runGuard(T, { tool_input: { command: 'yad gate advance EP-a epic' } }, { YAD_HOOK_DISABLE: '1' });
|
|
1054
|
+
assert.equal(off.code, 0, said(off));
|
|
527
1055
|
const none = runGuard(T, { hook_event_name: 'beforeShellExecution', command: 'git push -f' }, { YAD_BIN: '', PATH: '' });
|
|
528
|
-
assert.deepEqual(
|
|
529
|
-
assert.match(none.err, /allowing
|
|
1056
|
+
assert.deepEqual(parse(none), { permission: 'allow' }, said(none));
|
|
1057
|
+
assert.match(none.err, /allowing/, said(none));
|
|
530
1058
|
} finally { fs.rmSync(T, { recursive: true, force: true }); }
|
|
531
1059
|
});
|
|
532
1060
|
|
|
1061
|
+
test('#498: yad hook agent-guard judges in a worker; one that runs out of memory, out of time, or into an error refuses', async () => {
|
|
1062
|
+
const { judgeInWorker } = await import('./hook.mjs');
|
|
1063
|
+
const yadBin = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'bin', 'yad.mjs');
|
|
1064
|
+
const ok = await judgeInWorker('npm test', null);
|
|
1065
|
+
assert.equal(ok.verdict.allow, true, JSON.stringify(ok));
|
|
1066
|
+
const no = await judgeInWorker('gh pr merge 5', null);
|
|
1067
|
+
assert.match(no.verdict.message, /approving or merging/);
|
|
1068
|
+
// A worker with almost no memory, or no time, does not answer: the line is refused, never allowed.
|
|
1069
|
+
const big = `echo ${'$(echo x) '.repeat(60000)}`;
|
|
1070
|
+
const oom = await judgeInWorker(big, null, { heapMb: 16 }); // 1-2 MB aborts Node itself, before the worker starts
|
|
1071
|
+
assert.equal(oom.verdict.allow, false, JSON.stringify(oom.verdict));
|
|
1072
|
+
assert.match(oom.verdict.message, /could not be judged/);
|
|
1073
|
+
const slow = await judgeInWorker(big, null, { wallMs: 1 });
|
|
1074
|
+
assert.equal(slow.verdict.allow, false, JSON.stringify(slow.verdict));
|
|
1075
|
+
assert.match(slow.verdict.message, /could not be judged \(no answer/);
|
|
1076
|
+
// The real hook: a refusal is exit 2, and a read-only line is still pre-approved from the worker.
|
|
1077
|
+
const hook = (command) => spawnSync(process.execPath, [yadBin, 'hook', 'agent-guard'], { input: JSON.stringify({ hook_event_name: 'PreToolUse', tool_name: 'Bash', tool_input: { command } }), encoding: 'utf8', env: { ...process.env, CLAUDE_PROJECT_DIR: os.tmpdir(), YAD_HOOK_DISABLE: '' }, cwd: os.tmpdir() });
|
|
1078
|
+
assert.equal(hook('gh pr merge 5').status, 2);
|
|
1079
|
+
assert.match(hook('git status').stdout, /pre-approved/);
|
|
1080
|
+
// Out of time after the verdict: an allowed line stays allowed, only not pre-approved. This line's verdict
|
|
1081
|
+
// takes about 0.2 s on a laptop, its pre-approval about 6.5 s. Under coverage the verdict may be late too.
|
|
1082
|
+
const longRead = 'git status 2>&1; '.repeat(Math.floor(990_000 / 17));
|
|
1083
|
+
const late = await judgeInWorker(longRead, { root: os.tmpdir(), cwd: os.tmpdir() }, { wallMs: 3000 });
|
|
1084
|
+
assert.equal(late.pre, null, JSON.stringify(late.pre));
|
|
1085
|
+
if (!process.env.NODE_V8_COVERAGE) assert.equal(late.verdict.allow, true, late.verdict.message);
|
|
1086
|
+
});
|
|
1087
|
+
|
|
1088
|
+
test('#498: a named pipe where a shipped check should be is not read, so pre-approval never blocks the hook', { skip: process.platform === 'win32', timeout: 20_000 }, async () => {
|
|
1089
|
+
const { agentGuardPreapproval } = await import('./agent-guard.mjs');
|
|
1090
|
+
const T = fs.mkdtempSync(path.join(os.tmpdir(), 'yad-fifo-'));
|
|
1091
|
+
try {
|
|
1092
|
+
fs.mkdirSync(path.join(T, 'checks'));
|
|
1093
|
+
execFileSync('mkfifo', [path.join(T, 'checks', 'spec-link.sh')]);
|
|
1094
|
+
const t = Date.now();
|
|
1095
|
+
assert.equal(agentGuardPreapproval('bash checks/spec-link.sh', { cwd: T, root: T }).preapproved, false);
|
|
1096
|
+
assert.ok(Date.now() - t < 2000);
|
|
1097
|
+
} finally { fs.rmSync(T, { recursive: true, force: true }); }
|
|
1098
|
+
});
|
|
1099
|
+
|
|
1100
|
+
test('the hooks read a payload that arrives late or in pieces, retry a pipe that is not ready (EAGAIN), and never lose a verdict at exit', async () => {
|
|
1101
|
+
const { readStdin } = await import('./hook.mjs');
|
|
1102
|
+
// EAGAIN twice, then data in two pieces, then the end: all of it, once.
|
|
1103
|
+
const pieces = [Buffer.from('{"a":'), Buffer.from('1}')];
|
|
1104
|
+
let calls = 0;
|
|
1105
|
+
const fake = (fd, buf) => {
|
|
1106
|
+
calls += 1;
|
|
1107
|
+
if (calls <= 2) throw Object.assign(new Error('try again'), { code: 'EAGAIN' });
|
|
1108
|
+
const next = pieces.shift();
|
|
1109
|
+
if (!next) return 0;
|
|
1110
|
+
next.copy(buf);
|
|
1111
|
+
return next.length;
|
|
1112
|
+
};
|
|
1113
|
+
assert.equal(readStdin({ readSync: fake }), '{"a":1}');
|
|
1114
|
+
// Windows ends a pipe with an EOF error rather than a read of 0.
|
|
1115
|
+
let once = false;
|
|
1116
|
+
assert.equal(readStdin({ readSync: (fd, buf) => { if (once) throw Object.assign(new Error('end'), { code: 'EOF' }); once = true; buf.write('x'); return 1; } }), 'x');
|
|
1117
|
+
// A pipe that never becomes ready gives up after the limit, and the caller fails open.
|
|
1118
|
+
const t0 = Date.now();
|
|
1119
|
+
assert.throws(() => readStdin({ readSync: () => { throw Object.assign(new Error('try again'), { code: 'EAGAIN' }); }, limitMs: 50 }), /try again/);
|
|
1120
|
+
assert.ok(Date.now() - t0 < 2000, 'the limit holds');
|
|
1121
|
+
assert.throws(() => readStdin({ readSync: () => { throw Object.assign(new Error('bad'), { code: 'EBADF' }); } }), /bad/, 'any other error goes to the caller');
|
|
1122
|
+
// The real script, its payload written late and in pieces: the verdict is still right.
|
|
1123
|
+
const T = guardScript();
|
|
1124
|
+
try {
|
|
1125
|
+
const { spawn } = await import('node:child_process');
|
|
1126
|
+
const child = spawn(process.execPath, [path.join(T, 'hooks/agent-guard.mjs')], {
|
|
1127
|
+
cwd: T, stdio: ['pipe', 'pipe', 'pipe'],
|
|
1128
|
+
env: { ...process.env, YAD_BIN: `"${process.execPath}" "${YAD_MJS}"`, YAD_HOOK_DISABLE: '' },
|
|
1129
|
+
});
|
|
1130
|
+
let out = ''; let err = '';
|
|
1131
|
+
child.stdout.on('data', (d) => { out += d; });
|
|
1132
|
+
child.stderr.on('data', (d) => { err += d; });
|
|
1133
|
+
const payload = JSON.stringify({ hook_event_name: 'beforeShellExecution', command: 'git push --force' });
|
|
1134
|
+
child.stdin.write(payload.slice(0, 10));
|
|
1135
|
+
const { setTimeout: sleep } = await import('node:timers/promises');
|
|
1136
|
+
await sleep(300);
|
|
1137
|
+
child.stdin.end(payload.slice(10));
|
|
1138
|
+
const code = await new Promise((r) => child.on('close', r));
|
|
1139
|
+
assert.equal(code, 0, err);
|
|
1140
|
+
assert.equal(JSON.parse(out.trim()).permission, 'deny', `out ${JSON.stringify(out)} err ${JSON.stringify(err)}`);
|
|
1141
|
+
} finally { fs.rmSync(T, { recursive: true, force: true }); }
|
|
1142
|
+
// Every write of a verdict in the hook scripts is followed by an exit only once it is written.
|
|
1143
|
+
const dir = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'skills/yad-checks/templates/hooks');
|
|
1144
|
+
for (const f of ['agent-guard.mjs', 'ledger-guard-cursor.mjs']) {
|
|
1145
|
+
const src = fs.readFileSync(path.join(dir, f), 'utf8');
|
|
1146
|
+
assert.ok(!/process\.stdout\.write\([^)]*\);?\s*process\.exit\(/.test(src), `${f}: no exit straight after a stdout write`);
|
|
1147
|
+
assert.match(src, /process\.stdout\.write\(text, \(\) => process\.exit\(code\)\)/, `${f}: the verdict is written, then the exit`);
|
|
1148
|
+
}
|
|
1149
|
+
});
|
|
1150
|
+
|
|
533
1151
|
test('check --fix wires the agent guard on every Product, beside the team\'s own hooks; a target that leaves takes its entry', () => {
|
|
534
1152
|
const T = tmp();
|
|
535
1153
|
try {
|
|
@@ -991,3 +1609,358 @@ test('E158b case 14: on a repo wired before E158b the guard files are new, so ya
|
|
|
991
1609
|
assert.deepEqual(guardItems(repoActions(P, repo)).map((a) => a.status), ['new', 'new', 'new']);
|
|
992
1610
|
} finally { fs.rmSync(T, { recursive: true, force: true }); }
|
|
993
1611
|
});
|
|
1612
|
+
|
|
1613
|
+
// ---- pre-approval (E167, #353) ------------------------------------------------------------------------
|
|
1614
|
+
|
|
1615
|
+
const { agentGuardPreapproval, PREAPPROVED, PREAPPROVED_GATES } = await import('./agent-guard.mjs');
|
|
1616
|
+
|
|
1617
|
+
test('E167: a line the guard sees only reads is pre-approved; any write, secret or unknown form is not', () => {
|
|
1618
|
+
const T = tmp();
|
|
1619
|
+
try {
|
|
1620
|
+
fs.writeFileSync(path.join(T, 'a.json'), '{}');
|
|
1621
|
+
fs.mkdirSync(path.join(T, 'src'));
|
|
1622
|
+
const yes = (line) => assert.equal(agentGuardPreapproval(line, { cwd: T }).preapproved, true, `should be pre-approved: ${line}`);
|
|
1623
|
+
const no = (line) => assert.equal(agentGuardPreapproval(line, { cwd: T }).preapproved, false, `must NOT be pre-approved: ${line}`);
|
|
1624
|
+
for (const line of [
|
|
1625
|
+
'gh api repos/o/r/pulls',
|
|
1626
|
+
'gh api repos/o/r/pulls --paginate --jq ".[].number"',
|
|
1627
|
+
'gh api -X GET repos/o/r/commits/abc',
|
|
1628
|
+
'gh api repos/o/r/pulls/5 -H "Accept: application/vnd.github.diff"',
|
|
1629
|
+
'glab api projects/1/merge_requests',
|
|
1630
|
+
"gh api repos/o/r/pulls | jq '.[0].number'",
|
|
1631
|
+
'git log --oneline -5', 'git status --porcelain', 'git diff main...HEAD', 'git show HEAD:package.json', 'git rev-parse HEAD', 'git ls-files', 'git blame a.json',
|
|
1632
|
+
'git status 2>/dev/null', 'git log 2>&1 | head -n 3', 'git diff >/dev/null',
|
|
1633
|
+
'jq . a.json', "jq -r '.a > 1' a.json", "jq --arg x y -n '$x'", "git log -- '*.mjs'", 'git log -n5 -p', 'git blame -L 1,2 a.json',
|
|
1634
|
+
'cat a.json', 'head -5 a.json', 'tail -n 2 a.json', 'wc -l a.json', 'grep -rn foo src/', 'grep -e foo a.json', 'git log | cut -d" " -f1 | uniq -c | tr a-z A-Z',
|
|
1635
|
+
'yad status --json | jq .x', 'yad gate status EP-a', 'yad doctor && git status',
|
|
1636
|
+
// Decoded paths and suffixes do not block ordinary reads.
|
|
1637
|
+
'glab api projects/grp%2Fproj/merge_requests', 'gh api repos/o/r/contents/README.md', 'gh api repos/o/r/releases/tags/v1.2.3', 'gh api repos/o/r/compare/main...feat',
|
|
1638
|
+
// Review 2: forms the allow list of characters keeps.
|
|
1639
|
+
'gh api repos/{owner}/{repo}/pulls', 'git rev-parse @{u}', 'git log -S foo', 'git diff --text', 'git diff HEAD~1', "git diff 'HEAD^' -- a.json", 'git diff main...HEAD -- src',
|
|
1640
|
+
'git log --format=%H -3', "grep -n 'a.*b' a.json", 'git blame --ignore-rev abc a.json', 'git log\t--oneline -2', 'git log \\\n --oneline',
|
|
1641
|
+
]) yes(line);
|
|
1642
|
+
for (const line of [
|
|
1643
|
+
// API writes, GraphQL, other hosts, and the human-only area.
|
|
1644
|
+
'gh api -X POST repos/o/r/issues', 'gh api -XPOST repos/x', 'gh api --method=PATCH repos/x', 'gh api --method DELETE repos/x', 'gh api repos/x -f title=t',
|
|
1645
|
+
'gh api repos/x -F a=@file', 'gh api repos/x --field a=b', 'gh api repos/x --raw-field a=b', 'gh api repos/x --input body.json', 'gh api graphql -f query="{viewer{login}}"',
|
|
1646
|
+
'gh api graphql', 'gh api --hostname evil.example repos/x', 'gh api https://evil.example/x', 'gh api repos/x -H "X-HTTP-Method-Override: DELETE"',
|
|
1647
|
+
'gh api repos/o/r/actions/secrets', 'gh api repos/o/r/actions/variables', 'gh api user/keys', 'gh api repos/o/r/hooks', 'glab api projects/1/variables',
|
|
1648
|
+
'gh api repos/x --jq env', 'gh secret set X', 'gh secret list', 'gh variable set X', 'gh auth token', 'glab token create', 'gh pr merge 5', 'gh pr list',
|
|
1649
|
+
// git writes, git options, and reads outside the repo.
|
|
1650
|
+
'git log --output=x', 'git diff --output x', 'git -c core.pager=x log', 'git -C /tmp log', 'git diff --ext-diff', 'git diff --no-index /etc/passwd a.json',
|
|
1651
|
+
'git blame --contents /etc/passwd a.json', 'git commit -m x', 'git push', 'git checkout main', 'git branch -D x',
|
|
1652
|
+
// files outside the project, writes, and lines the guard cannot see through.
|
|
1653
|
+
'jq . /etc/passwd', 'jq -n env', 'jq -n "$ENV"', 'jq \'import "x" as y; .\' a.json', 'cat ~/.ssh/id_rsa', 'cat ../x', 'head /etc/hosts', 'grep -r x /',
|
|
1654
|
+
'git status > out.txt', 'git log >> out.txt', 'jq . < a.json', 'cat <<EOF\nx\nEOF', 'tee out.txt', 'sort -o x a.json', 'sed -i s/a/b/ a.json', 'awk 1 a.json', 'uniq a.json out.txt',
|
|
1655
|
+
'echo $(gh api repos/x)', 'git log `echo x`', 'bash -c "git log"', 'GH_HOST=x gh api repos/x', 'env gh api repos/x', 'sudo cat a.json', './gh api repos/x',
|
|
1656
|
+
'/usr/bin/git log', '(git log)', 'tail -f a.json', 'git log && rm -rf src', 'git log; curl https://x', 'yad gate approve EP-a epic', 'yad check --fix', 'npx yad status',
|
|
1657
|
+
'bash checks/build-test-lint.sh', 'bash checks/install-deps.sh', 'bash checks/product-checkout.sh', '', ' ',
|
|
1658
|
+
// Review 1: what the shell expands before the program runs is never judged as written.
|
|
1659
|
+
'cat {~/.ssh/id_rsa,a.json}', 'cat {/etc/passwd,a.json}', 'grep -r x {/etc,.}', 'jq -n --arg t "$GH_TOKEN" \'$t\'', 'jq --arg x y -n "$x"',
|
|
1660
|
+
'jq -n "\\"${HOME}\\""', 'gh api user --jq "\\"$GH_TOKEN\\""', 'head -n "$X" a.json', 'head -n $X a.json', 'cut -d "$X" -f1 a.json', 'cat ~/x', 'cat x ~/y',
|
|
1661
|
+
'cat a.*', 'cat a.jso?', 'cat [a]', 'grep -R x .', 'grep --dereference-recursive x .', 'jq -nf x.jq', 'jq --from-file x.jq a.json', 'gh api user --cache 1h',
|
|
1662
|
+
'gh api user/emails', 'git blame --conten=/etc/hosts a.json', 'git blame --contents=/etc/hosts a.json', 'git blame -S revs a.json', 'git ls-files -X ex',
|
|
1663
|
+
'git diff -O/etc/order', 'git log --show-signature', 'git log --show-sig', 'git log --outp=x', 'git diff --no-ind /etc/a b', 'cat a.json 2>/dev/null"x"',
|
|
1664
|
+
'yad check --fix', 'yad dial EP-a implement --to auto',
|
|
1665
|
+
// Review 2: zsh glob qualifiers and =(…) run commands; =ls reads a program's file; git diff reads files outside.
|
|
1666
|
+
'cat a.json(e:touch\\ PWNED:)', 'cat a.json(e:"touch PWNED":)', 'head -c 100 a.json(e:id:)', 'cat =(id)', 'cat =ls', 'cat x =ls', 'git log --x=~/y',
|
|
1667
|
+
'git diff /dev/null /etc/hosts', 'git diff --stat a.json /etc/passwd', 'git diff -- /etc/hosts /etc/passwd', 'git diff ../../x a.json', 'git log --format=%GS -1',
|
|
1668
|
+
'git log --pretty=format:%G?', 'git blame -S revs a.json', 'git diff -Oorder', 'gh api repos/{a,b}', 'cat a!b', 'git log #(x)\nrm -rf src', 'git log a#b',
|
|
1669
|
+
// Review 3: a "comment" the shell reads as part of a word, after a \-newline, a \r, or a character JavaScript calls a space.
|
|
1670
|
+
'git log -1 --format=x\\\n#$GH_TOKEN', 'git log -1 --format=x\r#$GH_TOKEN', 'git log -1 --format=x\u00a0#$GH_TOKEN', 'git log -1 --format=x\v#$GH_TOKEN',
|
|
1671
|
+
'git log -1 --format=x\f#$GH_TOKEN', 'git log -1 --format=x\u2028#$GH_TOKEN', 'git rev-parse --sq-quote x\\\n#${GH_TOKEN}', 'git log --oneline # a note', 'git status\r',
|
|
1672
|
+
// Review 4: \ + CR + LF runs the next line; a zsh extendedglob ^; $ ENV; paths that return secrets.
|
|
1673
|
+
'git log \\\r\ntouch PWNED', 'cat a.json \\\r\ntouch X', 'cat ^a.json', 'git log HEAD^', "jq -n '$ ENV.HOME'", "gh api --jq '$ ENV' user",
|
|
1674
|
+
'glab api projects/1/triggers', 'glab api projects/1/integrations/slack', 'gh api repos/o/r/actions/runs/1/logs', 'glab api projects/1/jobs/2/trace',
|
|
1675
|
+
'glab api projects/1/secure_files/3/download', 'glab api projects/1/terraform/state/prod', 'glab api projects/1/pipeline_schedules/2', 'glab api projects/1/jobs/2/artifacts',
|
|
1676
|
+
'gh api repos/o/r/actions/artifacts/1/zip', 'glab api application/settings', 'glab api user',
|
|
1677
|
+
'glab api user.json', 'glab api user.', 'glab api projects/1/jobs/2/trace.json', 'glab api user%2F', 'glab api application%2Fsettings', 'gh api user', 'gh api %zz',
|
|
1678
|
+
]) no(line);
|
|
1679
|
+
// A refusal always wins: a refused line is never pre-approved.
|
|
1680
|
+
assert.equal(agentGuardDecision('git log && gh pr merge 5').allow, false);
|
|
1681
|
+
// The working folder must be inside the project.
|
|
1682
|
+
assert.equal(agentGuardPreapproval('cat etc/hosts', { cwd: '/', root: T }).preapproved, false);
|
|
1683
|
+
assert.equal(agentGuardPreapproval('cat ../a.json', { cwd: path.join(T, 'src'), root: T }).preapproved, true, 'inside the project, from a subfolder');
|
|
1684
|
+
// A link that leaves the project is outside it.
|
|
1685
|
+
fs.symlinkSync('/etc/hosts', path.join(T, 'hosts'));
|
|
1686
|
+
no('cat hosts');
|
|
1687
|
+
// A name holding a brace-shield character would be read under another name, past the link check: the prompt.
|
|
1688
|
+
fs.symlinkSync('/etc', path.join(T, 'link'));
|
|
1689
|
+
no("cat 'link/hosts'"); no('cat "link/hosts"');
|
|
1690
|
+
no('cat host*'); no('cat host?');
|
|
1691
|
+
} finally { fs.rmSync(T, { recursive: true, force: true }); }
|
|
1692
|
+
});
|
|
1693
|
+
|
|
1694
|
+
test('E167: a read gate is pre-approved only while its file is the copy this yadflow ships', () => {
|
|
1695
|
+
const T = tmp();
|
|
1696
|
+
try {
|
|
1697
|
+
fs.mkdirSync(path.join(T, 'checks'));
|
|
1698
|
+
const shipped = path.join(path.dirname(new URL(import.meta.url).pathname), '..', 'skills/yad-checks/templates/checks');
|
|
1699
|
+
for (const g of PREAPPROVED_GATES) {
|
|
1700
|
+
assert.ok(fs.existsSync(path.join(shipped, g)), `${g} is shipped`);
|
|
1701
|
+
fs.copyFileSync(path.join(shipped, g), path.join(T, 'checks', g));
|
|
1702
|
+
const run = g.endsWith('.mjs') ? `node checks/${g} --base origin/main` : `bash checks/${g} origin/main`;
|
|
1703
|
+
assert.equal(agentGuardPreapproval(run, { cwd: T }).preapproved, true, run);
|
|
1704
|
+
if (!g.endsWith('.mjs')) assert.equal(agentGuardPreapproval(`sh ./checks/${g} origin/main`, { cwd: T }).preapproved, true, g);
|
|
1705
|
+
// The wrong runner, or one byte changed: the prompt.
|
|
1706
|
+
assert.equal(agentGuardPreapproval(g.endsWith('.mjs') ? `bash checks/${g}` : `node checks/${g}`, { cwd: T }).preapproved, false, g);
|
|
1707
|
+
fs.appendFileSync(path.join(T, 'checks', g), ' ');
|
|
1708
|
+
assert.equal(agentGuardPreapproval(run, { cwd: T }).preapproved, false, `${g} edited`);
|
|
1709
|
+
}
|
|
1710
|
+
// The gates that run the repo's own code, install or clone are never on the list.
|
|
1711
|
+
for (const g of ['build-test-lint.sh', 'install-deps.sh', 'product-checkout.sh', 'package-manager.sh']) {
|
|
1712
|
+
assert.ok(!PREAPPROVED_GATES.includes(g), g);
|
|
1713
|
+
fs.copyFileSync(path.join(shipped, g), path.join(T, 'checks', g));
|
|
1714
|
+
assert.equal(agentGuardPreapproval(`bash checks/${g}`, { cwd: T }).preapproved, false, g);
|
|
1715
|
+
}
|
|
1716
|
+
} finally { fs.rmSync(T, { recursive: true, force: true }); }
|
|
1717
|
+
});
|
|
1718
|
+
|
|
1719
|
+
test('E167: the hook answers a read with a PreToolUse allow under Claude Code; Cursor, refusals and the switches are unchanged', () => {
|
|
1720
|
+
const T = guardScript();
|
|
1721
|
+
const said = (r) => `hook said ${JSON.stringify(r)}`;
|
|
1722
|
+
try {
|
|
1723
|
+
const claude = (command, env) => runGuard(T, { hook_event_name: 'PreToolUse', tool_name: 'Bash', tool_input: { command }, cwd: T }, env);
|
|
1724
|
+
const read = claude('git status | head -3');
|
|
1725
|
+
assert.equal(read.code, 0, said(read));
|
|
1726
|
+
const answer = JSON.parse(read.out);
|
|
1727
|
+
assert.equal(answer.hookSpecificOutput.hookEventName, 'PreToolUse', said(read));
|
|
1728
|
+
assert.equal(answer.hookSpecificOutput.permissionDecision, 'allow', said(read));
|
|
1729
|
+
assert.match(answer.hookSpecificOutput.permissionDecisionReason, /read-only command \(git status, head\)/, said(read));
|
|
1730
|
+
// Not a read: no answer, so Claude Code asks as before.
|
|
1731
|
+
const write = claude('gh api -X POST repos/o/r/issues');
|
|
1732
|
+
assert.deepEqual([write.code, write.out], [0, ''], said(write));
|
|
1733
|
+
// A refusal still refuses.
|
|
1734
|
+
const merge = claude('git log && gh pr merge 5');
|
|
1735
|
+
assert.equal(merge.code, 2, said(merge));
|
|
1736
|
+
// The switches: YAD_NO_PREAPPROVE drops only this half; YAD_HOOK_DISABLE the whole guard.
|
|
1737
|
+
const off = claude('git status', { YAD_NO_PREAPPROVE: '1' });
|
|
1738
|
+
assert.deepEqual([off.code, off.out], [0, ''], said(off));
|
|
1739
|
+
assert.equal(claude('gh pr merge 5', { YAD_NO_PREAPPROVE: '1' }).code, 2);
|
|
1740
|
+
const disabled = claude('git status', { YAD_HOOK_DISABLE: '1' });
|
|
1741
|
+
assert.deepEqual([disabled.code, disabled.out], [0, ''], said(disabled));
|
|
1742
|
+
// Cursor's answer is the same literal as before.
|
|
1743
|
+
const cursor = runGuard(T, { hook_event_name: 'beforeShellExecution', command: 'git status' });
|
|
1744
|
+
assert.deepEqual(JSON.parse(cursor.out), { permission: 'allow' }, said(cursor));
|
|
1745
|
+
} finally { fs.rmSync(T, { recursive: true, force: true }); }
|
|
1746
|
+
});
|
|
1747
|
+
|
|
1748
|
+
test('E167: nothing that creates, prints or changes a secret, token or variable is ever pre-approved', () => {
|
|
1749
|
+
const T = tmp();
|
|
1750
|
+
try {
|
|
1751
|
+
const lines = [];
|
|
1752
|
+
for (const prog of ['gh', 'glab']) {
|
|
1753
|
+
for (const sub of ['secret set X', 'secret list', 'secret delete X', 'variable set X', 'variable list', 'auth token', 'auth login', 'token create', 'ssh-key add k', 'gpg-key add k']) lines.push(`${prog} ${sub}`);
|
|
1754
|
+
for (const p of ['repos/o/r/actions/secrets', 'repos/o/r/actions/secrets/public-key', 'repos/o/r/actions/variables', 'orgs/o/actions/secrets', 'repos/o/r/environments/e/secrets',
|
|
1755
|
+
'projects/1/variables', 'user/keys', 'user/gpg_keys', 'repos/o/r/keys', 'repos/o/r/hooks', 'personal_access_tokens', 'user/installations/1/access_tokens', 'projects/1/access_tokens',
|
|
1756
|
+
'repos/o/r/dependabot/secrets', 'repos/o/r/codespaces/secrets', 'groups/1/variables']) {
|
|
1757
|
+
lines.push(`${prog} api ${p}`, `${prog} api ${p} --paginate`, `${prog} api ${p} | jq .`);
|
|
1758
|
+
}
|
|
1759
|
+
}
|
|
1760
|
+
for (const line of lines) assert.equal(agentGuardPreapproval(line, { cwd: T }).preapproved, false, line);
|
|
1761
|
+
assert.match(PREAPPROVED.never, /secrets, tokens, variables/);
|
|
1762
|
+
} finally { fs.rmSync(T, { recursive: true, force: true }); }
|
|
1763
|
+
});
|
|
1764
|
+
|
|
1765
|
+
// #532: output readings stay separate from the historical positional readings.
|
|
1766
|
+
const outputGaps = [
|
|
1767
|
+
'{ echo gh pr merge 5; } > f; bash f',
|
|
1768
|
+
'( echo gh pr merge 5 ) > f; bash f',
|
|
1769
|
+
'{ echo gh pr merge 5; } >! f; zsh f',
|
|
1770
|
+
'{ echo gh pr merge 5; } | tee f; bash f',
|
|
1771
|
+
"echo echo 'gh pr merge 5' | bash > g; bash g",
|
|
1772
|
+
"echo 'echo gh pr merge 5' > f; eval \"$(<f)\" > g; bash g",
|
|
1773
|
+
'$(echo $(echo gh >& 1 pr merge 5))',
|
|
1774
|
+
"printf 'gh pr ' | cat - <(echo merge 5) | bash",
|
|
1775
|
+
"{ printf '%s ' gh pr; echo merge 5; } | bash",
|
|
1776
|
+
"printf 'gh pr ' > a; printf '%s\\n' 'merge 5' >> a; bash a",
|
|
1777
|
+
"{ printf '%b\\n' 'gh pr \\c'; echo merge 5; } | bash",
|
|
1778
|
+
"printf '\\x67h pr merge 5\\n' | bash",
|
|
1779
|
+
"printf 'git push \\055-force\\n' | bash",
|
|
1780
|
+
"{ printf 'gh pr\\n' | tr -d '\\n'; echo ' merge 5'; } | bash",
|
|
1781
|
+
"{ printf 'gh prX' | head -c 5; echo ' merge 5'; } | bash",
|
|
1782
|
+
];
|
|
1783
|
+
test('#532: every reported output gap is refused, while text only printed stays allowed', () => {
|
|
1784
|
+
for (const line of outputGaps) assert.equal(agentGuardDecision(line).allow, false, line);
|
|
1785
|
+
for (const line of ["printf '%s ' gh pr merge 5", '{ echo gh pr merge 5; } > f',
|
|
1786
|
+
"printf '\\x67h pr merge 5\\n'", "{ printf '%s ' echo; echo hello; } | bash",
|
|
1787
|
+
"printf 'echo ' > a; printf '%s\\n' hello >> a; bash a"]) {
|
|
1788
|
+
assert.equal(agentGuardDecision(line).allow, true, line);
|
|
1789
|
+
}
|
|
1790
|
+
});
|
|
1791
|
+
|
|
1792
|
+
test('#532 review 1: a group record never changes what main reads a part writes', () => {
|
|
1793
|
+
// The `}` part's write list was shared with the group record, so a later redirect-only part (`> g`) was added
|
|
1794
|
+
// to main's own reading of `{ :; } > f`, and `g` read as emptied by it: a refusal main makes was lost.
|
|
1795
|
+
assert.deepEqual(shellParts('{ :; } > f; > g').writes.map((w) => w && w.map((x) => x.target)), [null, ['f']]);
|
|
1796
|
+
assert.equal(agentGuardDecision("printf 'gh pr ' > g; { :; } > f; false && > g; echo merge 5 >> g; bash g").allow, false);
|
|
1797
|
+
// A redirect-only part writes a subshell's output only straight after its `)`; later ones are other commands.
|
|
1798
|
+
for (const line of ['( echo gh pr merge 5 ); > f; bash f', '{ echo gh pr merge 5; }; > f; bash f',
|
|
1799
|
+
'( echo gh pr merge 5 ) && > f; bash f']) assert.equal(agentGuardDecision(line).allow, true, line);
|
|
1800
|
+
for (const line of ['( echo gh pr merge 5 ) > f; bash f', '( ( echo gh pr merge 5 ) > f ); bash f'])
|
|
1801
|
+
assert.equal(agentGuardDecision(line).allow, false, line);
|
|
1802
|
+
});
|
|
1803
|
+
|
|
1804
|
+
test('#532 review: lines main allows stay allowed (group writes, process substitutions, long text)', () => {
|
|
1805
|
+
// A part that writes to a file adds nothing to its group's output (review 9).
|
|
1806
|
+
for (const line of ['{ echo gh pr merge 5 > g; echo hi; } > f; bash f', '( echo gh pr merge 5 > g ) > f; bash f'])
|
|
1807
|
+
assert.equal(agentGuardDecision(line).allow, true, line);
|
|
1808
|
+
// Process substitutions are not nesting levels; main bounds them with its own walk (review 10).
|
|
1809
|
+
assert.equal(agentGuardDecision('cat <(cat <(cat <(cat <(echo hi))))').allow, true);
|
|
1810
|
+
assert.equal(agentGuardDecision('cat <(cat <(cat <(cat <(echo gh pr merge 5)))) | bash').allow, false);
|
|
1811
|
+
// The output pass has its own text budget: a long message through a few `tee` stages (review 11).
|
|
1812
|
+
const msg = 'fix: a line of text for the body\n'.repeat(27000);
|
|
1813
|
+
assert.equal(agentGuardDecision(`printf '%s' '${msg}' | ${'tee -a log | '.repeat(8)}git commit -F -`).allow, true);
|
|
1814
|
+
});
|
|
1815
|
+
|
|
1816
|
+
// Lines review 1 found that a real bash 3.2 or zsh 5.9 runs and the output pass let through (main did too).
|
|
1817
|
+
const outputGapsReview = [
|
|
1818
|
+
// zsh's `echo` always makes escapes; double quotes keep the backslash (review 2).
|
|
1819
|
+
"echo '\\x67h pr merge 5' | bash", "{ echo '\\0147h pr merge 5'; } | zsh", 'echo "\\x67h pr merge 5" | zsh',
|
|
1820
|
+
// zsh's `print` options (review 3).
|
|
1821
|
+
"{ print -r -- 'gh pr merge 5'; } > f; zsh f", "{ print -f '%s ' gh pr; print merge 5; } | zsh",
|
|
1822
|
+
"print -l 'gh pr merge 5' | bash",
|
|
1823
|
+
// A write that may not run hides no text before it (review 4).
|
|
1824
|
+
'{ echo gh pr merge 5; } > f; false && echo hi > f; bash f', '( echo gh pr merge 5 ) > f; [ -e g ] && : > f; bash f',
|
|
1825
|
+
// A format reads `\NNN` (review 5), and its other conversions.
|
|
1826
|
+
"printf 'gh pr merge\\0405\\n' | bash", "printf '%c%c pr merge 5\\n' gx hx | bash", "printf '%.2s pr merge 5\\n' ghx | bash",
|
|
1827
|
+
// A process substitution a shell runs (review 6).
|
|
1828
|
+
"bash <(printf '\\x67h pr merge 5\\n')", "source <(printf '\\x67h pr merge 5\\n')", "bash < <(printf '\\x67h pr merge 5\\n')",
|
|
1829
|
+
"zsh =(printf '\\x67h pr merge 5\\n')", "bash <(echo '{ echo gh pr merge 5; } > f; bash f')",
|
|
1830
|
+
// Compound commands' output (review 7).
|
|
1831
|
+
'if true; then echo gh pr merge 5; fi > f; bash f', 'for x in 1; do echo gh pr merge 5; done > f; bash f',
|
|
1832
|
+
'while false; do :; done; { if true; then echo gh pr merge 5; fi; } | bash',
|
|
1833
|
+
// Filters, as the shell spells their words (review 8).
|
|
1834
|
+
"{ printf 'gh pr\\n' | tr -d \"\\n\"; echo ' merge 5'; } | bash", "{ printf 'gh pr\\r\\n' | tr -d '\\r\\n'; echo ' merge 5'; } | bash",
|
|
1835
|
+
"{ printf 'gh pr\\n' | tr '\\n' ' '; echo merge 5; } | bash", "{ printf 'gh pr\\n' | head --bytes=5; echo ' merge 5'; } | bash",
|
|
1836
|
+
];
|
|
1837
|
+
|
|
1838
|
+
test('#532 review: the twins of the output gaps are refused, and text only printed stays allowed', () => {
|
|
1839
|
+
for (const line of outputGapsReview) assert.equal(agentGuardDecision(line).allow, false, line);
|
|
1840
|
+
// bash reads `\0053` in a format as `\005` and `3`: a plain push, not `+main` (a wrong refusal before).
|
|
1841
|
+
for (const line of ["printf 'git push origin \\0053main\\n' | bash", "echo '\\x67h pr merge 5'",
|
|
1842
|
+
"print -r 'gh pr merge 5'", "printf -v x '%s' 'gh pr merge 5'; echo done | bash", 'if true; then echo gh pr merge 5; fi',
|
|
1843
|
+
"bash script.sh <(echo gh pr merge 5)", "{ printf 'gh pr\\n' | tr a-z A-Z; } | bash"]) {
|
|
1844
|
+
assert.equal(agentGuardDecision(line).allow, true, line);
|
|
1845
|
+
}
|
|
1846
|
+
});
|
|
1847
|
+
|
|
1848
|
+
// No time limit under coverage, as in the other timing tests.
|
|
1849
|
+
const withinTime = (ms) => (process.env.NODE_V8_COVERAGE ? Infinity : ms);
|
|
1850
|
+
|
|
1851
|
+
// Lines review 2 found that a real bash 3.2 or zsh 5.9 runs, and the output pass let through.
|
|
1852
|
+
const outputGapsReview2 = [
|
|
1853
|
+
// A file name with a backslash the shell keeps in double quotes is the file its redirect wrote (R1).
|
|
1854
|
+
`{ printf 'gh pr '; echo merge 5; } > "a\\b"; bash "a\\b"`, `{ printf 'gh pr '; echo merge 5; } > "a\\b"; source "a\\b"`,
|
|
1855
|
+
// Each text a file may hold is run on its own: an open quote in one hides no other (R2).
|
|
1856
|
+
`echo "echo '" > f; test -n "$X" || { printf 'gh pr '; echo merge 5; } > f; bash f`,
|
|
1857
|
+
// Compound commands after a reserved word, nested, and a `case` pattern spelled like a closer (R3).
|
|
1858
|
+
"{ if true; then printf 'gh '; fi; printf 'pr '; } > f; echo merge 5 >> f; bash f",
|
|
1859
|
+
"if true; then while false; do :; done; printf 'gh '; printf 'pr '; fi > f; echo merge 5 >> f; bash f",
|
|
1860
|
+
"for i in 1; do for j in 1; do :; done; printf 'gh '; printf 'pr '; done > f; echo merge 5 >> f; bash f",
|
|
1861
|
+
"time for i in 1; do printf 'gh '; printf 'pr '; done > f; echo merge 5 >> f; bash f",
|
|
1862
|
+
"! for i in 1; do printf 'gh '; printf 'pr '; done > f; echo merge 5 >> f; bash f",
|
|
1863
|
+
"case x in x) printf 'gh pr ' ;; done) : ;; esac > f; echo merge 5 >> f; bash f",
|
|
1864
|
+
// A redirect after a process substitution, and `print` options with their value attached.
|
|
1865
|
+
"cat <(printf 'gh pr ') > f; echo merge 5 >> f; bash f", 'print -u1 gh pr merge 5 | zsh', `print -f"%s " gh pr merge 5 | zsh`,
|
|
1866
|
+
];
|
|
1867
|
+
|
|
1868
|
+
test('#532 review 2: holes the round-1 fixes opened are closed, and lines that run nothing stay allowed', () => {
|
|
1869
|
+
for (const line of outputGapsReview2) assert.equal(agentGuardDecision(line).allow, false, line);
|
|
1870
|
+
// A file written past 64 texts is refused only when it is read (B1); only the command word is the shell that
|
|
1871
|
+
// runs a process substitution, and `bash x < <(…)` runs `x` (B2).
|
|
1872
|
+
for (const line of [Array.from({ length: 65 }, (_, i) => `echo ${i} > out.txt`).join('; '),
|
|
1873
|
+
"ls . <(printf 'gh pr '; echo merge 5)", "grep -n sh <(printf 'gh pr '; echo merge 5)",
|
|
1874
|
+
"bash x < <(printf 'gh pr '; echo merge 5)", 'print -u2 gh pr merge 5 | zsh']) {
|
|
1875
|
+
assert.equal(agentGuardDecision(line).allow, true, line);
|
|
1876
|
+
}
|
|
1877
|
+
assert.equal(agentGuardDecision(`${Array.from({ length: 65 }, (_, i) => `echo ${i} > f`).join('; ')}; bash f`).allow, false);
|
|
1878
|
+
// A deep nest of process substitutions is refused at once, with the process substitution message (B3).
|
|
1879
|
+
const t = Date.now();
|
|
1880
|
+
const deep = agentGuardDecision(`${'cat <('.repeat(1500)}echo hi${')'.repeat(1500)}`);
|
|
1881
|
+
assert.equal(deep.allow, false);
|
|
1882
|
+
assert.match(deep.message, /process substitutions/);
|
|
1883
|
+
assert.ok(Date.now() - t < withinTime(2000), `took ${Date.now() - t} ms`);
|
|
1884
|
+
});
|
|
1885
|
+
|
|
1886
|
+
test('#532 review 3: a shell passes its stdin to the text it runs, and an unclosed or missed group keeps its output', () => {
|
|
1887
|
+
for (const line of ["bash -c bash < <(echo 'gh pr merge 5')", "zsh -c zsh < <(echo 'gh pr merge 5')",
|
|
1888
|
+
"bash -c 'cat | bash' < <(echo 'gh pr merge 5')", "echo bash > x; bash x < <(echo 'gh pr merge 5')",
|
|
1889
|
+
"for i in 1; do { printf 'gh pr '; } done > f; echo merge 5 >> f; bash f",
|
|
1890
|
+
"if true; then 'fi'; printf 'gh pr '; fi > f; echo merge 5 >> f; bash f",
|
|
1891
|
+
"case x in x) printf 'gh pr ' ;; while) : ;; esac > f; echo merge 5 >> f; bash f"]) {
|
|
1892
|
+
assert.equal(agentGuardDecision(line).allow, false, line);
|
|
1893
|
+
}
|
|
1894
|
+
for (const line of ["bash x < <(echo 'gh pr merge 5')", "bash -c 'echo hi' < <(echo 'gh pr merge 5')",
|
|
1895
|
+
"bash -c cat < <(echo 'gh pr merge 5')", 'if true; then '.repeat(100) + 'echo hi' + '; fi'.repeat(100)]) {
|
|
1896
|
+
assert.equal(agentGuardDecision(line).allow, true, line.slice(0, 80));
|
|
1897
|
+
}
|
|
1898
|
+
// A closed group is read once by the groups around it: deep nests cost their size (review 4).
|
|
1899
|
+
for (const open of ['if true; then ', 'for i in 1; do ']) {
|
|
1900
|
+
const t = Date.now();
|
|
1901
|
+
const v = agentGuardDecision(open.repeat(10000) + 'echo hi' + (open.startsWith('if') ? '; fi' : '; done').repeat(10000));
|
|
1902
|
+
assert.equal(v.allow, true, open);
|
|
1903
|
+
assert.ok(Date.now() - t < withinTime(1500), `${open} took ${Date.now() - t} ms`);
|
|
1904
|
+
}
|
|
1905
|
+
const t = Date.now();
|
|
1906
|
+
assert.equal(agentGuardDecision('if true; then '.repeat(80) + 'echo a;'.repeat(10000) + '; fi'.repeat(80)).allow, true);
|
|
1907
|
+
assert.ok(Date.now() - t < withinTime(1500), `took ${Date.now() - t} ms`);
|
|
1908
|
+
});
|
|
1909
|
+
|
|
1910
|
+
test('#532 review 4: inside a case, a one-word `if` may start a real block, and a lone pattern prints nothing', () => {
|
|
1911
|
+
for (const line of ["case x in x)\nif\ntrue; then printf 'gh pr '; fi > f;; esac; echo merge 5 >> f; bash f",
|
|
1912
|
+
"case x in x) if\ntrue; then printf 'gh pr '; fi > f;; esac; echo merge 5 >> f; bash f",
|
|
1913
|
+
"case x in x)\nif\ntrue\nthen\nprintf 'gh pr '\nfi > f\n;;\nesac\necho merge 5 >> f\nbash f",
|
|
1914
|
+
"case x in x) printf 'gh pr ' ;; if) : ;; esac > f; echo merge 5 >> f; bash f",
|
|
1915
|
+
"case x in x) printf 'gh pr ' ;; '}') : ;; esac > f; echo merge 5 >> f; bash f",
|
|
1916
|
+
"( ( echo gh pr ) ; echo merge 5 ) | tr '\\n' ' ' | bash"]) {
|
|
1917
|
+
assert.equal(agentGuardDecision(line).allow, false, line);
|
|
1918
|
+
}
|
|
1919
|
+
});
|
|
1920
|
+
|
|
1921
|
+
test('#532 review 5: a block the finder pairs across a folded group, and a case under an open loop, keep their output', () => {
|
|
1922
|
+
for (const line of ["for i in 1; do ( printf 'gh pr '; for j in 1; { echo merge 5; } ); done > f; bash f",
|
|
1923
|
+
"for i in 1; do ( echo gh pr; for j in 1; { echo merge 5; } ); done | tr '\\n' ' ' | bash",
|
|
1924
|
+
"case x in x) for j in 1; { :; }; printf 'gh pr merge 5';; esac > f; bash f",
|
|
1925
|
+
"case x in x) for j in 1; { :; }; printf 'gh pr ';; esac > f; echo merge 5 >> f; bash f",
|
|
1926
|
+
"case x in x) if true; then for j in 1; { :; }; printf 'gh pr merge 5'; fi > f;; esac; bash f"]) {
|
|
1927
|
+
assert.equal(agentGuardDecision(line).allow, false, line);
|
|
1928
|
+
}
|
|
1929
|
+
});
|
|
1930
|
+
|
|
1931
|
+
test('#532: differential output corpus against bash and zsh with harmless executable stubs', () => {
|
|
1932
|
+
const T = tmp();
|
|
1933
|
+
const log = path.join(T, 'executed');
|
|
1934
|
+
const bin = path.join(T, 'bin'); fs.mkdirSync(bin);
|
|
1935
|
+
for (const name of ['gh', 'git']) {
|
|
1936
|
+
fs.writeFileSync(path.join(bin, name), '#!/bin/sh\nprintf reached >> "$GUARD_STREAM_LOG"\n', { mode: 0o755 });
|
|
1937
|
+
}
|
|
1938
|
+
const corpus = [...outputGaps, ...outputGapsReview, ...outputGapsReview2,
|
|
1939
|
+
"bash -c bash < <(echo 'gh pr merge 5')", "for i in 1; do { printf 'gh pr '; } done > f; echo merge 5 >> f; bash f",
|
|
1940
|
+
"case x in x) printf 'gh pr ' ;; while) : ;; esac > f; echo merge 5 >> f; bash f"];
|
|
1941
|
+
for (const printer of ["echo gh pr merge 5", "printf '%s ' gh pr merge 5", "printf '%b' '\\x67h pr merge 5\\n'"]) {
|
|
1942
|
+
for (const group of [`{ ${printer}; }`, `( ${printer} )`]) {
|
|
1943
|
+
for (const sink of ['| bash', '> f; bash f', '| tee f; bash f']) corpus.push(`${group} ${sink}`);
|
|
1944
|
+
}
|
|
1945
|
+
}
|
|
1946
|
+
// Each shell that is installed: macOS has bash 3.2 and zsh; a Linux runner has bash 5 and may have no zsh, so
|
|
1947
|
+
// there this checks bash 5 only. The zsh-only lines are checked by the asserts above on every runner.
|
|
1948
|
+
for (const shell of ['/bin/bash', '/bin/zsh']) {
|
|
1949
|
+
if (!fs.existsSync(shell)) continue;
|
|
1950
|
+
for (const line of corpus) {
|
|
1951
|
+
fs.rmSync(log, { force: true });
|
|
1952
|
+
try {
|
|
1953
|
+
execFileSync(shell, ['-c', line], { cwd: T, timeout: 2000, stdio: 'ignore',
|
|
1954
|
+
env: { PATH: `${bin}:/bin:/usr/bin`, HOME: T, GUARD_STREAM_LOG: log } });
|
|
1955
|
+
} catch { /* A shell-specific syntax error is not an execution witness. */ }
|
|
1956
|
+
if (fs.existsSync(log)) assert.equal(agentGuardDecision(line).allow, false, `${shell}: ${line}`);
|
|
1957
|
+
}
|
|
1958
|
+
}
|
|
1959
|
+
});
|
|
1960
|
+
|
|
1961
|
+
test('#532: repeated cat stdin copies are charged before they exhaust the deadline', () => {
|
|
1962
|
+
const line = `printf x > a; echo go | ${'cat - $(<a) | '.repeat(25)}bash`;
|
|
1963
|
+
const v = agentGuardDecision(line);
|
|
1964
|
+
assert.equal(v.allow, false);
|
|
1965
|
+
assert.doesNotMatch(v.message, /took too long/);
|
|
1966
|
+
});
|