-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhook_handler.bats
More file actions
1578 lines (1454 loc) · 62.8 KB
/
hook_handler.bats
File metadata and controls
1578 lines (1454 loc) · 62.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bats
# tests/hook_handler.bats
# End-to-end coverage for hooks/handlers/pre-tool-use.sh.
# Every test pipes synthetic Claude Code PreToolUse payloads to the handler
# and asserts stdout JSON + exit code + stderr diagnostics. PASSTHRU_USER_HOME
# and PASSTHRU_PROJECT_DIR isolate each test from real ~/.claude; TMPDIR
# isolates the breadcrumb directory.
setup() {
REPO_ROOT="$(cd "$BATS_TEST_DIRNAME/.." && pwd)"
HANDLER="$REPO_ROOT/hooks/handlers/pre-tool-use.sh"
FIXTURES="$REPO_ROOT/tests/fixtures"
TMP="$(mktemp -d -t passthru-hook.XXXXXX)"
USER_ROOT="$TMP/user"
PROJ_ROOT="$TMP/proj"
BCTMP="$TMP/tmp"
mkdir -p "$USER_ROOT/.claude" "$PROJ_ROOT/.claude" "$BCTMP"
export PASSTHRU_USER_HOME="$USER_ROOT"
export PASSTHRU_PROJECT_DIR="$PROJ_ROOT"
export TMPDIR="$BCTMP"
# Scrub multiplexer env vars + any overlay mock sentinels so overlay
# detection stays deterministic across CI and dev machines (local tmux
# sessions otherwise leak the TMUX var into the handler subprocess).
unset TMUX
unset KITTY_WINDOW_ID
unset WEZTERM_PANE
unset PASSTHRU_OVERLAY_MOCK_ANSWER
unset PASSTHRU_OVERLAY_MOCK_RULE_JSON
unset PASSTHRU_OVERLAY_MOCK_EXIT_CODE
}
teardown() {
[ -n "${TMP:-}" ] && rm -rf "$TMP"
}
# Helpers -------------------------------------------------------------------
place() {
# $1 target path, $2 fixture name
cp "$FIXTURES/$2" "$1"
}
run_handler() {
# $1 = stdin JSON
# Returns stdout in $output, status in $status.
run bash -c "printf '%s' \"\$1\" | bash '$HANDLER'" _ "$1"
}
enable_audit() {
touch "$USER_ROOT/.claude/passthru.audit.enabled"
}
audit_log() {
printf '%s/.claude/passthru-audit.log\n' "$USER_ROOT"
}
# ---------------------------------------------------------------------------
# Core decision paths
# ---------------------------------------------------------------------------
@test "handler: no rule files + no multiplexer -> overlay fallback emits ask" {
# Task 8: no rule match + mode does not auto-allow Bash -> overlay path.
# With no multiplexer available in the test env, the overlay fallback
# emits permissionDecision:"ask" so CC surfaces its native dialog.
run_handler '{"tool_name":"Bash","tool_input":{"command":"ls -la"}}'
[ "$status" -eq 0 ]
# Stderr warning about missing multiplexer is lumped into $output by `run`.
[[ "$output" == *"no supported multiplexer"* ]]
# Extract the JSON envelope (stdout) from the mixed stdout+stderr output.
json_line="$(printf '%s\n' "$output" | grep -o '{"hookSpecificOutput".*}' | head -n1)"
[ -n "$json_line" ]
decision="$(jq -r '.hookSpecificOutput.permissionDecision' <<<"$json_line")"
[ "$decision" = "ask" ]
}
@test "handler: allow match emits allow decision JSON" {
place "$USER_ROOT/.claude/passthru.json" "user-only.json"
run_handler '{"tool_name":"Bash","tool_input":{"command":"ls -la"}}'
[ "$status" -eq 0 ]
out="$output"
event="$(jq -r '.hookSpecificOutput.hookEventName' <<<"$out")"
decision="$(jq -r '.hookSpecificOutput.permissionDecision' <<<"$out")"
reason="$(jq -r '.hookSpecificOutput.permissionDecisionReason' <<<"$out")"
[ "$event" = "PreToolUse" ]
[ "$decision" = "allow" ]
[ "$reason" = "passthru allow: safe read-only listing" ]
}
@test "handler: deny match emits deny decision JSON" {
place "$USER_ROOT/.claude/passthru.json" "user-only.json"
run_handler '{"tool_name":"Bash","tool_input":{"command":"rm -rf /"}}'
[ "$status" -eq 0 ]
out="$output"
decision="$(jq -r '.hookSpecificOutput.permissionDecision' <<<"$out")"
reason="$(jq -r '.hookSpecificOutput.permissionDecisionReason' <<<"$out")"
[ "$decision" = "deny" ]
[[ "$reason" == passthru\ deny:* ]]
}
@test "handler: no match with rules present -> overlay fallback emits ask" {
# Task 8: same rationale as the no-rule-files variant. A non-matching
# command with rules on disk still ends up on the overlay path; in the
# test env (no multiplexer) that collapses to permissionDecision:"ask".
place "$USER_ROOT/.claude/passthru.json" "user-only.json"
run_handler '{"tool_name":"Bash","tool_input":{"command":"ps aux"}}'
[ "$status" -eq 0 ]
json_line="$(printf '%s\n' "$output" | grep -o '{"hookSpecificOutput".*}' | head -n1)"
[ -n "$json_line" ]
decision="$(jq -r '.hookSpecificOutput.permissionDecision' <<<"$json_line")"
[ "$decision" = "ask" ]
}
@test "handler: deny wins over allow when both would match" {
# Craft a file where an allow AND a deny both match the same command.
cat > "$USER_ROOT/.claude/passthru.json" <<'EOF'
{
"version": 1,
"allow": [
{ "tool": "Bash", "match": { "command": "^gh " }, "reason": "all gh" }
],
"deny": [
{ "tool": "Bash", "match": { "command": "^gh pr close" }, "reason": "no auto-close" }
]
}
EOF
run_handler '{"tool_name":"Bash","tool_input":{"command":"gh pr close 42"}}'
[ "$status" -eq 0 ]
run jq -r '.hookSpecificOutput.permissionDecision' <<<"$output"
[ "$output" = "deny" ]
}
# ---------------------------------------------------------------------------
# Error / edge cases (fail-open)
# ---------------------------------------------------------------------------
@test "handler: malformed stdin JSON -> passthrough + stderr warning" {
run_handler '{ not valid json'
[ "$status" -eq 0 ]
# $output lumps stdout+stderr under `run`; find the passthrough JSON in it.
[[ "$output" == *'{"continue": true}'* ]]
[[ "$output" == *"malformed"* ]] || [[ "$output" == *"warning"* ]]
}
@test "handler: empty stdin -> passthrough + stderr warning" {
run_handler ''
[ "$status" -eq 0 ]
[[ "$output" == *'{"continue": true}'* ]]
}
@test "handler: disabled sentinel short-circuits to passthrough (even with matching deny rule)" {
place "$USER_ROOT/.claude/passthru.json" "user-only.json"
touch "$USER_ROOT/.claude/passthru.disabled"
run_handler '{"tool_name":"Bash","tool_input":{"command":"rm -rf /"}}'
[ "$status" -eq 0 ]
run jq -r '.continue' <<<"$output"
[ "$output" = "true" ]
}
@test "handler: malformed rule file -> passthrough + stderr" {
echo '{ not json' > "$USER_ROOT/.claude/passthru.json"
run_handler '{"tool_name":"Bash","tool_input":{"command":"ls"}}'
[ "$status" -eq 0 ]
[[ "$output" == *'{"continue": true}'* ]]
}
# ---------------------------------------------------------------------------
# Plugin self-allow
# ---------------------------------------------------------------------------
@test "handler: plugin self-allow for bash .../claude-passthru/scripts/*.sh" {
# Synthetic realistic install path.
cmd='bash /Users/foo/.claude/plugins/cache/owner-name/plugin-slug/1.0.0/plugins/claude-passthru/scripts/verify.sh'
payload="$(jq -cn --arg c "$cmd" '{tool_name:"Bash",tool_input:{command:$c}}')"
run_handler "$payload"
[ "$status" -eq 0 ]
out="$output"
decision="$(jq -r '.hookSpecificOutput.permissionDecision' <<<"$out")"
reason="$(jq -r '.hookSpecificOutput.permissionDecisionReason' <<<"$out")"
[ "$decision" = "allow" ]
[[ "$reason" == *"self-allow"* ]]
}
@test "handler: plugin self-allow matches even when user has no rule file" {
# Explicitly no rules present.
cmd='bash /home/alice/.claude/plugins/cache/some-org/cc-thingz/2.0.0/plugins/claude-passthru/scripts/write-rule.sh'
payload="$(jq -cn --arg c "$cmd" '{tool_name:"Bash",tool_input:{command:$c}}')"
run_handler "$payload"
decision="$(jq -r '.hookSpecificOutput.permissionDecision' <<<"$output")"
[ "$decision" = "allow" ]
}
@test "handler: plugin self-allow does NOT trigger for unrelated bash commands" {
# Unrelated command falls past the self-allow regex and proceeds to the
# overlay path. In the test env (no multiplexer) the overlay fallback
# emits permissionDecision:"ask" rather than the pre-Task-8 passthrough.
cmd='bash /usr/local/bin/something.sh'
payload="$(jq -cn --arg c "$cmd" '{tool_name:"Bash",tool_input:{command:$c}}')"
run_handler "$payload"
json_line="$(printf '%s\n' "$output" | grep -o '{"hookSpecificOutput".*}' | head -n1)"
[ -n "$json_line" ]
decision="$(jq -r '.hookSpecificOutput.permissionDecision' <<<"$json_line")"
[ "$decision" = "ask" ]
}
@test "handler: plugin self-allow via \$CLAUDE_PLUGIN_ROOT (fake path)" {
# Claude Code sets CLAUDE_PLUGIN_ROOT for every hook invocation. Using this
# env var is the authoritative way to locate the plugin install, so the
# self-allow must work even for totally synthetic paths.
fake_root="$TMP/fakeplugin"
mkdir -p "$fake_root/scripts"
cmd="bash $fake_root/scripts/verify.sh"
payload="$(jq -cn --arg c "$cmd" '{tool_name:"Bash",tool_input:{command:$c}}')"
run bash -c "CLAUDE_PLUGIN_ROOT='$fake_root' printf '%s' \"\$1\" | CLAUDE_PLUGIN_ROOT='$fake_root' bash '$HANDLER'" _ "$payload"
[ "$status" -eq 0 ]
decision="$(jq -r '.hookSpecificOutput.permissionDecision' <<<"$output")"
reason="$(jq -r '.hookSpecificOutput.permissionDecisionReason' <<<"$output")"
[ "$decision" = "allow" ]
[[ "$reason" == *"self-allow"* ]]
}
@test "handler: plugin self-allow via \$CLAUDE_PLUGIN_ROOT for realistic cache install path" {
# Real-world install path shape:
# ~/.claude/plugins/cache/<marketplace>/<plugin-name>/<version>/
# The old regex required literal `claude-passthru` in the path, so this
# shape (which is what users actually get) never matched.
real_root="$USER_ROOT/.claude/plugins/cache/passthru/passthru/0.1.0"
mkdir -p "$real_root/scripts"
cmd="bash $real_root/scripts/verify.sh"
payload="$(jq -cn --arg c "$cmd" '{tool_name:"Bash",tool_input:{command:$c}}')"
run bash -c "CLAUDE_PLUGIN_ROOT='$real_root' printf '%s' \"\$1\" | CLAUDE_PLUGIN_ROOT='$real_root' bash '$HANDLER'" _ "$payload"
[ "$status" -eq 0 ]
decision="$(jq -r '.hookSpecificOutput.permissionDecision' <<<"$output")"
[ "$decision" = "allow" ]
}
@test "handler: plugin self-allow via fallback regex for cache/passthru/passthru/<ver>/ path (no env)" {
# Same realistic install path but without CLAUDE_PLUGIN_ROOT set. The
# fallback regex must still recognise `passthru` as a path segment so
# manual pipe-testing and legacy harnesses continue to work.
cmd='bash /Users/alice/.claude/plugins/cache/passthru/passthru/0.1.0/scripts/verify.sh'
payload="$(jq -cn --arg c "$cmd" '{tool_name:"Bash",tool_input:{command:$c}}')"
run_handler "$payload"
[ "$status" -eq 0 ]
decision="$(jq -r '.hookSpecificOutput.permissionDecision' <<<"$output")"
[ "$decision" = "allow" ]
}
@test "handler: self-allow via \$CLAUDE_PLUGIN_ROOT rejects unknown script names" {
# Defense in depth: even if the prefix matches CLAUDE_PLUGIN_ROOT, only
# the plugin's known scripts are self-allowed. An arbitrary foo.sh living
# under the plugin root should not get a free pass.
fake_root="$TMP/fakeplugin"
mkdir -p "$fake_root/scripts"
cmd="bash $fake_root/scripts/evil.sh"
payload="$(jq -cn --arg c "$cmd" '{tool_name:"Bash",tool_input:{command:$c}}')"
run bash -c "CLAUDE_PLUGIN_ROOT='$fake_root' printf '%s' \"\$1\" | CLAUDE_PLUGIN_ROOT='$fake_root' bash '$HANDLER'" _ "$payload"
[ "$status" -eq 0 ]
# Post-Task-8: unrecognised command falls through to overlay path. With no
# multiplexer in test env, overlay fallback emits permissionDecision:"ask".
json_line="$(printf '%s\n' "$output" | grep -o '{"hookSpecificOutput".*}' | head -n1)"
[ -n "$json_line" ]
decision="$(jq -r '.hookSpecificOutput.permissionDecision' <<<"$json_line")"
[ "$decision" = "ask" ]
}
# ---------------------------------------------------------------------------
# Real-world round-trip
# ---------------------------------------------------------------------------
@test "handler: gh api /repos/... with rule -> allow" {
cat > "$USER_ROOT/.claude/passthru.json" <<'EOF'
{
"version": 1,
"allow": [
{ "tool": "Bash", "match": { "command": "^gh api /repos/[^/]+/[^/]+/forks" }, "reason": "forks api" }
],
"deny": []
}
EOF
run_handler '{"tool_name":"Bash","tool_input":{"command":"gh api /repos/owner/repo/forks"}}'
[ "$status" -eq 0 ]
out="$output"
decision="$(jq -r '.hookSpecificOutput.permissionDecision' <<<"$out")"
reason="$(jq -r '.hookSpecificOutput.permissionDecisionReason' <<<"$out")"
[ "$decision" = "allow" ]
[[ "$reason" == *"forks api"* ]]
}
# ---------------------------------------------------------------------------
# Audit log (opt-in)
# ---------------------------------------------------------------------------
@test "audit disabled: no log file, no breadcrumb" {
place "$USER_ROOT/.claude/passthru.json" "user-only.json"
run_handler '{"tool_name":"Bash","tool_input":{"command":"ls -la"},"tool_use_id":"t1"}'
[ ! -f "$(audit_log)" ]
run bash -c "ls '$TMPDIR'/passthru-pre-*.json 2>/dev/null"
[ -z "$output" ]
}
@test "audit enabled: allow match writes one JSONL line, no breadcrumb" {
place "$USER_ROOT/.claude/passthru.json" "user-only.json"
enable_audit
run_handler '{"tool_name":"Bash","tool_input":{"command":"ls -la"},"tool_use_id":"t1"}'
[ "$status" -eq 0 ]
[ -f "$(audit_log)" ]
# Exactly one line.
run bash -c "wc -l < '$(audit_log)'"
[ "${output##* }" = "1" ] || [ "$output" = "1" ]
# Valid JSON with required fields.
line="$(head -n1 "$(audit_log)")"
run jq -c '.' <<<"$line"
[ "$status" -eq 0 ]
run jq -r '.event' <<<"$line"
[ "$output" = "allow" ]
run jq -r '.source' <<<"$line"
[ "$output" = "passthru" ]
run jq -r '.tool' <<<"$line"
[ "$output" = "Bash" ]
run jq -r '.reason' <<<"$line"
[ "$output" = "safe read-only listing" ]
run jq -r '.tool_use_id' <<<"$line"
[ "$output" = "t1" ]
# No breadcrumb for non-passthrough decisions.
run bash -c "ls '$TMPDIR'/passthru-pre-*.json 2>/dev/null"
[ -z "$output" ]
}
@test "audit enabled: deny match writes one JSONL line, no breadcrumb" {
place "$USER_ROOT/.claude/passthru.json" "user-only.json"
enable_audit
run_handler '{"tool_name":"Bash","tool_input":{"command":"rm -rf /"},"tool_use_id":"t2"}'
[ "$status" -eq 0 ]
[ -f "$(audit_log)" ]
line="$(head -n1 "$(audit_log)")"
run jq -r '.event' <<<"$line"
[ "$output" = "deny" ]
run jq -r '.tool_use_id' <<<"$line"
[ "$output" = "t2" ]
run bash -c "ls '$TMPDIR'/passthru-pre-*.json 2>/dev/null"
[ -z "$output" ]
}
@test "audit enabled: no-match -> ask event (overlay unavailable fallback) with tool_use_id" {
# Post-Task-8: no rule match + default mode + Bash (not auto-allowed) +
# no multiplexer in test env -> overlay fallback emits ask. Audit log
# records event=ask and ALSO writes a breadcrumb so post-tool-use.sh can
# classify the native-dialog outcome into an asked_* event. (Pre-fix the
# breadcrumb was missing on every ask-emit path; see docs/rule-format.md:169
# for the behavior contract.)
place "$USER_ROOT/.claude/passthru.json" "user-only.json"
enable_audit
run_handler '{"tool_name":"Bash","tool_input":{"command":"unknown xyz"},"tool_use_id":"tPT"}'
[ "$status" -eq 0 ]
[ -f "$(audit_log)" ]
line="$(head -n1 "$(audit_log)")"
run jq -r '.event' <<<"$line"
[ "$output" = "ask" ]
run jq -r '.tool_use_id' <<<"$line"
[ "$output" = "tPT" ]
# Breadcrumb MUST exist so PostToolUse can classify the native-dialog answer.
[ -f "$TMPDIR/passthru-pre-tPT.json" ]
}
@test "audit enabled: no-match without tool_use_id writes JSONL ask event" {
place "$USER_ROOT/.claude/passthru.json" "user-only.json"
enable_audit
run_handler '{"tool_name":"Bash","tool_input":{"command":"unknown xyz"}}'
[ "$status" -eq 0 ]
[ -f "$(audit_log)" ]
line="$(head -n1 "$(audit_log)")"
run jq -r '.event' <<<"$line"
[ "$output" = "ask" ]
run jq -r '.tool_use_id' <<<"$line"
[ "$output" = "null" ]
run bash -c "ls '$TMPDIR'/passthru-pre-*.json 2>/dev/null"
[ -z "$output" ]
}
@test "audit enabled: stale breadcrumb (>60 min) is unlinked on next invocation" {
place "$USER_ROOT/.claude/passthru.json" "user-only.json"
enable_audit
# Plant a stale crumb by touching with a mtime older than 60 minutes.
stale="$TMPDIR/passthru-pre-OLD.json"
printf '{"ts":"2000-01-01T00:00:00Z","tool":"Bash","tool_input":{}}' > "$stale"
# Use `touch -A -100` on macOS (adjust mtime by -100:00:00) or -d on GNU.
if touch -A -020000 "$stale" 2>/dev/null; then
:
elif touch -d "3 hours ago" "$stale" 2>/dev/null; then
:
else
# Fallback: use perl to set mtime 2 hours back.
perl -e 'my $t=time-7200; utime $t, $t, $ARGV[0]' "$stale"
fi
# Invoke handler; does not matter what it decides.
run_handler '{"tool_name":"Bash","tool_input":{"command":"ls"},"tool_use_id":"tX"}'
[ "$status" -eq 0 ]
[ ! -f "$stale" ]
}
@test "audit enabled: self-allow is logged" {
enable_audit
cmd='bash /Users/x/.claude/plugins/cache/acme/passthru-mirror/1.0.0/plugins/claude-passthru/scripts/verify.sh'
payload="$(jq -cn --arg c "$cmd" '{tool_name:"Bash",tool_input:{command:$c},tool_use_id:"tSA"}')"
run_handler "$payload"
[ -f "$(audit_log)" ]
line="$(head -n1 "$(audit_log)")"
run jq -r '.event' <<<"$line"
[ "$output" = "allow" ]
run jq -r '.reason' <<<"$line"
[[ "$output" == *"self-allow"* ]]
run jq -r '.tool_use_id' <<<"$line"
[ "$output" = "tSA" ]
}
@test "audit_write_line fails open: unwritable log dir does not block decision" {
# Make the audit dir unwritable (chmod 555). The hook should still emit
# its allow JSON and exit 0. The audit log itself will be empty/missing.
place "$USER_ROOT/.claude/passthru.json" "user-only.json"
enable_audit
# Skip under root: r-x dirs are still writable for uid 0, so the
# fail-open path the test exercises will not actually fire.
if [ "$(id -u)" -eq 0 ]; then
skip "running as root: chmod 555 does not deny writes to uid 0"
fi
chmod 555 "$USER_ROOT/.claude" 2>/dev/null || skip "cannot chmod test dir"
run_handler '{"tool_name":"Bash","tool_input":{"command":"ls -la"},"tool_use_id":"tFAIL"}'
# Restore so teardown can rm -rf.
chmod 755 "$USER_ROOT/.claude" 2>/dev/null || true
[ "$status" -eq 0 ]
# Tighten: extract the JSON envelope explicitly, then assert decision.
# `$output` lumps stderr (the permission-denied warning) with stdout
# (the JSON envelope), so we grep the JSON line out before parsing.
json_line="$(printf '%s\n' "$output" | grep -o '{"hookSpecificOutput".*}' | head -n1)"
[ -n "$json_line" ]
run jq -r '.hookSpecificOutput.permissionDecision' <<<"$json_line"
[ "$status" -eq 0 ]
[ "$output" = "allow" ]
}
@test "audit log lines are valid JSONL (each line parses)" {
place "$USER_ROOT/.claude/passthru.json" "user-only.json"
enable_audit
# Run three different decisions in sequence.
run_handler '{"tool_name":"Bash","tool_input":{"command":"ls"},"tool_use_id":"a1"}'
run_handler '{"tool_name":"Bash","tool_input":{"command":"rm -rf /"},"tool_use_id":"a2"}'
run_handler '{"tool_name":"Bash","tool_input":{"command":"unknown"},"tool_use_id":"a3"}'
# Each line must be valid JSON on its own.
while IFS= read -r line; do
[ -z "$line" ] && continue
run jq -c '.' <<<"$line"
[ "$status" -eq 0 ]
done < "$(audit_log)"
# And we got exactly three lines.
n="$(wc -l < "$(audit_log)" | tr -d ' ')"
[ "$n" -eq 3 ]
}
@test "audit log: full schema check on allow line (.ts ISO, .rule_index int, .pattern non-empty)" {
place "$USER_ROOT/.claude/passthru.json" "user-only.json"
enable_audit
run_handler '{"tool_name":"Bash","tool_input":{"command":"ls -la"},"tool_use_id":"schema-allow"}'
[ "$status" -eq 0 ]
line="$(head -n1 "$(audit_log)")"
# ts is ISO 8601 Z form: YYYY-MM-DDTHH:MM:SSZ.
ts="$(jq -r '.ts' <<<"$line")"
[[ "$ts" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$ ]]
# rule_index is an integer (or null for self-allow). For an allow rule from
# the user-only fixture it should be an integer >= 0.
ridx_type="$(jq -r '.rule_index | type' <<<"$line")"
[ "$ridx_type" = "number" ]
# pattern is non-empty when the rule matched.
pat="$(jq -r '.pattern' <<<"$line")"
[ -n "$pat" ]
[ "$pat" != "null" ]
}
@test "audit log: ask line from overlay-unavailable fallback has null rule_index and null pattern" {
# Post-Task-8: the no-match fallback emits ask with no rule fields (there
# was no matching rule). rule_index and pattern stay null; reason carries
# the overlay-failure tag for diagnosability.
place "$USER_ROOT/.claude/passthru.json" "user-only.json"
enable_audit
run_handler '{"tool_name":"Bash","tool_input":{"command":"unknown xyz"},"tool_use_id":"schema-pt"}'
[ "$status" -eq 0 ]
line="$(head -n1 "$(audit_log)")"
ev="$(jq -r '.event' <<<"$line")"
[ "$ev" = "ask" ]
[ "$(jq -r '.rule_index' <<<"$line")" = "null" ]
[ "$(jq -r '.pattern' <<<"$line")" = "null" ]
# reason is a non-null diagnostic ("overlay unavailable").
reason_val="$(jq -r '.reason' <<<"$line")"
[ "$reason_val" != "null" ]
[ -n "$reason_val" ]
}
# ---------------------------------------------------------------------------
# Fail-open: bad regex in rule (find_first_match rc=2 path)
# ---------------------------------------------------------------------------
@test "handler: invalid regex in deny rule -> fail-open passthrough + stderr" {
# Plant a passthru.json with a syntactically broken regex in the deny list.
# find_first_match must return rc=2; the handler must emit `{"continue":
# true}` on stdout and a diagnostic on stderr instead of denying outright.
cat > "$USER_ROOT/.claude/passthru.json" <<'EOF'
{"version":1,"allow":[],"deny":[{"tool":"Bash","match":{"command":"(unclosed"}}]}
EOF
run_handler '{"tool_name":"Bash","tool_input":{"command":"ls"}}'
[ "$status" -eq 0 ]
[[ "$output" == *'{"continue": true}'* ]]
# Stderr diagnostic must be present (run lumps stdout+stderr).
[[ "$output" == *"deny rule regex error"* ]] || [[ "$output" == *"regex compile failure"* ]]
}
@test "handler: invalid regex in allow rule -> fail-open passthrough + stderr" {
# Same shape as above but the bad regex is in allow[]. Deny[] is empty so
# the handler reaches the allow check, hits rc=2, and falls through.
cat > "$USER_ROOT/.claude/passthru.json" <<'EOF'
{"version":1,"allow":[{"tool":"Bash","match":{"command":"[unclosed"}}],"deny":[]}
EOF
run_handler '{"tool_name":"Bash","tool_input":{"command":"ls"}}'
[ "$status" -eq 0 ]
[[ "$output" == *'{"continue": true}'* ]]
[[ "$output" == *"allow rule regex error"* ]] || [[ "$output" == *"regex compile failure"* ]]
}
@test "audit log: multi-key match rule logs ALL keys in .pattern (not just first)" {
# rule_pattern_summary used to call `to_entries | .[0].value`, dropping
# every key after the first. A multi-key match (e.g. WebFetch with both
# url and prompt regex) should surface all keys in the audit pattern.
enable_audit
cat > "$USER_ROOT/.claude/passthru.json" <<'EOF'
{"version":1,"allow":[{"tool":"WebFetch","match":{"url":"^https://example\\.com/","prompt":"^summarize "},"reason":"summary fetch"}],"deny":[]}
EOF
payload="$(jq -cn --arg u "https://example.com/x" --arg p "summarize this" \
'{tool_name:"WebFetch",tool_input:{url:$u,prompt:$p},tool_use_id:"tMK"}')"
run_handler "$payload"
[ "$status" -eq 0 ]
[ -f "$(audit_log)" ]
line="$(head -n1 "$(audit_log)")"
pat="$(jq -r '.pattern' <<<"$line")"
# Both keys must appear in the summary.
[[ "$pat" == *"url="* ]]
[[ "$pat" == *"prompt="* ]]
# Tool segment present too (because the rule has .tool).
[[ "$pat" == *"WebFetch"* ]]
}
# ---------------------------------------------------------------------------
# Task 6: ask decision path (document-order allow+ask walk)
# ---------------------------------------------------------------------------
@test "handler: ask rule match emits ask decision JSON" {
# A v2 file with an ask[] rule must produce permissionDecision "ask" with
# reason prefix "passthru ask:" and the rule's reason appended.
cat > "$USER_ROOT/.claude/passthru.json" <<'EOF'
{
"version": 2,
"allow": [],
"ask": [
{ "tool": "Bash", "match": { "command": "^gh " }, "reason": "confirm gh" }
],
"deny": []
}
EOF
run_handler '{"tool_name":"Bash","tool_input":{"command":"gh api /repos/a/b"}}'
[ "$status" -eq 0 ]
# Post-Task-8: overlay unavailable warning is emitted on stderr and lumped
# into $output by `run`. Extract the JSON envelope explicitly.
out="$(printf '%s\n' "$output" | grep -o '{"hookSpecificOutput".*}' | head -n1)"
[ -n "$out" ]
event="$(jq -r '.hookSpecificOutput.hookEventName' <<<"$out")"
decision="$(jq -r '.hookSpecificOutput.permissionDecision' <<<"$out")"
reason="$(jq -r '.hookSpecificOutput.permissionDecisionReason' <<<"$out")"
[ "$event" = "PreToolUse" ]
[ "$decision" = "ask" ]
[ "$reason" = "passthru ask: confirm gh" ]
}
@test "handler: ask rule without .reason synthesizes a pattern-based reason" {
# Absent reason field -> message still surfaces the matched rule pattern so
# the user sees WHY we are asking.
cat > "$USER_ROOT/.claude/passthru.json" <<'EOF'
{
"version": 2,
"ask": [
{ "tool": "Bash", "match": { "command": "^curl " } }
]
}
EOF
run_handler '{"tool_name":"Bash","tool_input":{"command":"curl https://example.com"}}'
[ "$status" -eq 0 ]
out="$(printf '%s\n' "$output" | grep -o '{"hookSpecificOutput".*}' | head -n1)"
[ -n "$out" ]
decision="$(jq -r '.hookSpecificOutput.permissionDecision' <<<"$out")"
reason="$(jq -r '.hookSpecificOutput.permissionDecisionReason' <<<"$out")"
[ "$decision" = "ask" ]
# Synthesized form: "passthru ask: matched rule [<pattern-summary>]".
[[ "$reason" == passthru\ ask:\ matched\ rule* ]]
# Pattern surface includes the matched tool name and command regex.
[[ "$reason" == *"Bash"* ]]
[[ "$reason" == *"command="* ]]
}
@test "audit: ask decision writes event=ask with rule_index + pattern" {
enable_audit
cat > "$USER_ROOT/.claude/passthru.json" <<'EOF'
{
"version": 2,
"ask": [
{ "tool": "Bash", "match": { "command": "^gh " }, "reason": "confirm gh" }
]
}
EOF
run_handler '{"tool_name":"Bash","tool_input":{"command":"gh pr list"},"tool_use_id":"tASK"}'
[ "$status" -eq 0 ]
[ -f "$(audit_log)" ]
line="$(head -n1 "$(audit_log)")"
run jq -r '.event' <<<"$line"
[ "$output" = "ask" ]
run jq -r '.source' <<<"$line"
[ "$output" = "passthru" ]
run jq -r '.reason' <<<"$line"
[ "$output" = "confirm gh" ]
# Merged ask-array index 0 for the sole ask rule.
run jq -r '.rule_index' <<<"$line"
[ "$output" = "0" ]
run jq -r '.pattern' <<<"$line"
[[ "$output" == *"Bash"* ]]
[[ "$output" == *"command="* ]]
run jq -r '.tool_use_id' <<<"$line"
[ "$output" = "tASK" ]
}
@test "handler: deny still wins over ask when both would match" {
# Deny must be checked first; an ask rule covering the same call is moot.
cat > "$USER_ROOT/.claude/passthru.json" <<'EOF'
{
"version": 2,
"ask": [
{ "tool": "Bash", "match": { "command": "^rm " }, "reason": "confirm rm" }
],
"deny": [
{ "tool": "Bash", "match": { "command": "^rm\\s+-rf" }, "reason": "never rm -rf" }
]
}
EOF
run_handler '{"tool_name":"Bash","tool_input":{"command":"rm -rf /tmp/junk"}}'
[ "$status" -eq 0 ]
decision="$(jq -r '.hookSpecificOutput.permissionDecision' <<<"$output")"
[ "$decision" = "deny" ]
}
@test "handler: document order - narrow allow before broad ask (same file) -> allow wins" {
# JSON key order (via jq keys_unsorted) decides the within-file walking
# order for allow[] vs ask[]. Here allow[] appears first in the file, so
# the narrow allow rule is checked before the broad ask rule and wins.
cat > "$USER_ROOT/.claude/passthru.json" <<'EOF'
{
"version": 2,
"allow": [
{ "tool": "Bash", "match": { "command": "^gh api /repos" }, "reason": "narrow allow" }
],
"ask": [
{ "tool": "Bash", "match": { "command": "^gh " }, "reason": "broad ask" }
]
}
EOF
run_handler '{"tool_name":"Bash","tool_input":{"command":"gh api /repos/foo/bar"}}'
[ "$status" -eq 0 ]
decision="$(jq -r '.hookSpecificOutput.permissionDecision' <<<"$output")"
reason="$(jq -r '.hookSpecificOutput.permissionDecisionReason' <<<"$output")"
[ "$decision" = "allow" ]
[[ "$reason" == *"narrow allow"* ]]
}
@test "handler: document order - narrow ask before broad allow (same file) -> ask wins" {
# Reversed key order: ask[] appears textually before allow[] in the file.
# The narrow ask rule is checked first and wins.
cat > "$USER_ROOT/.claude/passthru.json" <<'EOF'
{
"version": 2,
"ask": [
{ "tool": "Bash", "match": { "command": "^gh api /repos" }, "reason": "narrow ask" }
],
"allow": [
{ "tool": "Bash", "match": { "command": "^gh " }, "reason": "broad allow" }
]
}
EOF
run_handler '{"tool_name":"Bash","tool_input":{"command":"gh api /repos/foo/bar"}}'
[ "$status" -eq 0 ]
out="$(printf '%s\n' "$output" | grep -o '{"hookSpecificOutput".*}' | head -n1)"
[ -n "$out" ]
decision="$(jq -r '.hookSpecificOutput.permissionDecision' <<<"$out")"
reason="$(jq -r '.hookSpecificOutput.permissionDecisionReason' <<<"$out")"
[ "$decision" = "ask" ]
[[ "$reason" == *"narrow ask"* ]]
}
@test "handler: cross-file - user-authored ask before user-imported allow -> ask wins" {
# user-authored comes before user-imported in the fixed scope precedence,
# so an ask rule in passthru.json is checked before an allow rule in
# passthru.imported.json even if both would match.
cat > "$USER_ROOT/.claude/passthru.json" <<'EOF'
{
"version": 2,
"ask": [
{ "tool": "Bash", "match": { "command": "^gh " }, "reason": "authored ask" }
]
}
EOF
cat > "$USER_ROOT/.claude/passthru.imported.json" <<'EOF'
{
"version": 2,
"allow": [
{ "tool": "Bash", "match": { "command": "^gh " }, "reason": "imported allow" }
]
}
EOF
run_handler '{"tool_name":"Bash","tool_input":{"command":"gh pr list"}}'
[ "$status" -eq 0 ]
out="$(printf '%s\n' "$output" | grep -o '{"hookSpecificOutput".*}' | head -n1)"
[ -n "$out" ]
decision="$(jq -r '.hookSpecificOutput.permissionDecision' <<<"$out")"
reason="$(jq -r '.hookSpecificOutput.permissionDecisionReason' <<<"$out")"
[ "$decision" = "ask" ]
[[ "$reason" == *"authored ask"* ]]
}
@test "handler: cross-file - project allow before user ask honored per scope precedence" {
# load_rules scope order is:
# user-authored -> user-imported -> project-authored -> project-imported.
# User-scope ask rules come BEFORE project-scope allow rules even if the
# project file was physically populated later. A user ask must win over a
# project allow for the same call, regardless of anyone's intuition about
# "later overrides earlier".
cat > "$USER_ROOT/.claude/passthru.json" <<'EOF'
{
"version": 2,
"ask": [
{ "tool": "Bash", "match": { "command": "^gh " }, "reason": "user ask" }
]
}
EOF
cat > "$PROJ_ROOT/.claude/passthru.json" <<'EOF'
{
"version": 2,
"allow": [
{ "tool": "Bash", "match": { "command": "^gh " }, "reason": "project allow" }
]
}
EOF
run_handler '{"tool_name":"Bash","tool_input":{"command":"gh pr list"}}'
[ "$status" -eq 0 ]
out="$(printf '%s\n' "$output" | grep -o '{"hookSpecificOutput".*}' | head -n1)"
[ -n "$out" ]
decision="$(jq -r '.hookSpecificOutput.permissionDecision' <<<"$out")"
reason="$(jq -r '.hookSpecificOutput.permissionDecisionReason' <<<"$out")"
[ "$decision" = "ask" ]
[[ "$reason" == *"user ask"* ]]
}
@test "handler: v1 file with stray ask[] key never triggers ask-rule decision" {
# build_ordered_allow_ask must strip ask[] for v1 files to match load_rules.
# Even if a user includes ask[] in a v1 file (accidental or from a future
# schema draft), the hook must ignore it. Post-Task-8 the effective fall
# through is overlay path: we assert the ask decision we DO emit is the
# no-match fallback (overlay unavailable), NOT a decision attributed to
# the stray ask rule (no "should be ignored" reason leaks through).
cat > "$USER_ROOT/.claude/passthru.json" <<'EOF'
{
"version": 1,
"allow": [],
"deny": [],
"ask": [
{ "tool": "Bash", "match": { "command": "^gh " }, "reason": "should be ignored" }
]
}
EOF
run_handler '{"tool_name":"Bash","tool_input":{"command":"gh pr list"}}'
[ "$status" -eq 0 ]
json_line="$(printf '%s\n' "$output" | grep -o '{"hookSpecificOutput".*}' | head -n1)"
[ -n "$json_line" ]
decision="$(jq -r '.hookSpecificOutput.permissionDecision' <<<"$json_line")"
[ "$decision" = "ask" ]
reason="$(jq -r '.hookSpecificOutput.permissionDecisionReason' <<<"$json_line")"
# The stray ask rule's reason must NOT surface - the v1 parser dropped it.
[[ "$reason" != *"should be ignored"* ]]
# The fallback reason references "no rule matched" (default-mode flow).
[[ "$reason" == *"no rule matched"* ]]
}
# ===========================================================================
# Task 8: permission-mode replication + overlay invocation
# ===========================================================================
# Helpers for Task 8 tests --------------------------------------------------
# make_mode_payload <tool_name> <tool_input_json> <mode> [cwd]
# Emits a PreToolUse stdin payload with permission_mode + cwd populated.
make_mode_payload() {
local tool="$1" ti="$2" mode="$3" cwd="${4:-}"
jq -cn --arg t "$tool" --argjson ti "$ti" --arg m "$mode" --arg c "$cwd" \
'{tool_name:$t, tool_input:$ti, permission_mode:$m, cwd:(if $c == "" then null else $c end)}'
}
# setup_overlay_stub <verdict> [exit_code] [rule_json]
# Plants a stub plugin root with a mock scripts/overlay.sh that writes the
# given verdict (and optional rule_json line for always-variants) to
# $PASSTHRU_OVERLAY_RESULT_FILE. Exports CLAUDE_PLUGIN_ROOT so the hook
# discovers the stub overlay instead of the real one. Other plugin files
# (hooks/common.sh, scripts/write-rule.sh, scripts/verify.sh) symlink to
# the real repo so the hook's support scripts still work.
#
# Also arranges the multiplexer presence the overlay_available helper
# requires: exports TMUX and places a no-op tmux binary in $TMP/bin on PATH.
# Without these, overlay_available() returns false and the hook skips the
# stub entirely (native-dialog fallback), which is rarely what tests want.
#
# A non-zero exit_code simulates a launch failure: the stub writes nothing
# and exits with the given code. rule_json is only used when verdict is
# yes_always or no_always.
setup_overlay_stub() {
local verdict="$1" exit_code="${2:-0}" rule_json="${3:-}"
local stub_root="$TMP/stub-plugin"
mkdir -p "$stub_root/hooks/handlers" "$stub_root/scripts"
# Symlink the real files so the hook finds them through CLAUDE_PLUGIN_ROOT.
ln -sfn "$REPO_ROOT/hooks/common.sh" "$stub_root/hooks/common.sh"
ln -sfn "$REPO_ROOT/hooks/handlers/pre-tool-use.sh" "$stub_root/hooks/handlers/pre-tool-use.sh"
ln -sfn "$REPO_ROOT/hooks/handlers/post-tool-use.sh" "$stub_root/hooks/handlers/post-tool-use.sh"
ln -sfn "$REPO_ROOT/scripts/write-rule.sh" "$stub_root/scripts/write-rule.sh"
ln -sfn "$REPO_ROOT/scripts/verify.sh" "$stub_root/scripts/verify.sh"
local stub_overlay="$stub_root/scripts/overlay.sh"
if [ "$exit_code" != "0" ]; then
# Launch-failure path: exit non-zero without writing a result file.
cat > "$stub_overlay" <<STUB
#!/usr/bin/env bash
exit ${exit_code}
STUB
else
# Write verdict + optional rule JSON, then exit 0. The hook reads
# \$PASSTHRU_OVERLAY_RESULT_FILE afterwards.
local rule_literal=""
local scope_literal=""
if [ -n "$rule_json" ]; then
rule_literal="printf '%s\\n' '$rule_json' >> \"\$PASSTHRU_OVERLAY_RESULT_FILE\""
scope_literal="printf '%s\\n' 'project' >> \"\$PASSTHRU_OVERLAY_RESULT_FILE\""
fi
cat > "$stub_overlay" <<STUB
#!/usr/bin/env bash
: "\${PASSTHRU_OVERLAY_RESULT_FILE:?}"
mkdir -p "\$(dirname "\$PASSTHRU_OVERLAY_RESULT_FILE")" 2>/dev/null || true
printf '%s\\n' '${verdict}' > "\$PASSTHRU_OVERLAY_RESULT_FILE"
${rule_literal}
${scope_literal}
# Touch a log file so tests can assert the stub ran.
if [ -n "\${PASSTHRU_OVERLAY_STUB_LOG:-}" ]; then
{
printf 'invoked verdict=%s tool=%s tool_input=%s\\n' \\
'${verdict}' "\${PASSTHRU_OVERLAY_TOOL_NAME:-}" "\${PASSTHRU_OVERLAY_TOOL_INPUT_JSON:-}"
} >> "\$PASSTHRU_OVERLAY_STUB_LOG" 2>/dev/null || true
fi
exit 0
STUB
fi
chmod +x "$stub_overlay"
export CLAUDE_PLUGIN_ROOT="$stub_root"
# Arrange multiplexer presence for overlay_available. TMUX + a no-op
# tmux binary on PATH is the simplest combo. Tests that deliberately
# test "no multiplexer" use setup_overlay_refuses_invocation instead.
local bin_dir="$TMP/bin"
mkdir -p "$bin_dir"
cat > "$bin_dir/tmux" <<'TMUXSTUB'
#!/usr/bin/env bash
exit 0
TMUXSTUB
chmod +x "$bin_dir/tmux"
export PATH="$bin_dir:$PATH"
export TMUX="mock/0"
}
# setup_overlay_refuses_invocation: plants a stub overlay.sh that writes a
# marker file on every invocation (so tests assert the stub NEVER ran).
setup_overlay_refuses_invocation() {
local stub_root="$TMP/stub-plugin"
mkdir -p "$stub_root/hooks/handlers" "$stub_root/scripts"
ln -sfn "$REPO_ROOT/hooks/common.sh" "$stub_root/hooks/common.sh"
ln -sfn "$REPO_ROOT/hooks/handlers/pre-tool-use.sh" "$stub_root/hooks/handlers/pre-tool-use.sh"
ln -sfn "$REPO_ROOT/scripts/write-rule.sh" "$stub_root/scripts/write-rule.sh"
ln -sfn "$REPO_ROOT/scripts/verify.sh" "$stub_root/scripts/verify.sh"
cat > "$stub_root/scripts/overlay.sh" <<STUB
#!/usr/bin/env bash
touch "\${TMP:-/tmp}/overlay-stub-RAN"
exit 0
STUB
chmod +x "$stub_root/scripts/overlay.sh"
export CLAUDE_PLUGIN_ROOT="$stub_root"
}
run_handler_in_stub_root() {
# Same as run_handler but inherits CLAUDE_PLUGIN_ROOT.
run bash -c "printf '%s' \"\$1\" | bash '$CLAUDE_PLUGIN_ROOT/hooks/handlers/pre-tool-use.sh'" _ "$1"
}
# bypassPermissions mode: overlay is still consulted ----------------------------
@test "mode: bypassPermissions + any tool -> overlay path entered" {
# Mode-based auto-allow is removed. All non-internal tools go through
# the overlay regardless of permission_mode.
setup_overlay_stub "yes_once"
payload="$(make_mode_payload 'Bash' '{"command":"rm -rf /tmp/foo"}' 'bypassPermissions')"
run_handler_in_stub_root "$payload"
[ "$status" -eq 0 ]
json_line="$(printf '%s\n' "$output" | grep -o '{"hookSpecificOutput".*}' | head -n1)"
[ -n "$json_line" ]
decision="$(jq -r '.hookSpecificOutput.permissionDecision' <<<"$json_line")"
[ "$decision" = "allow" ]
}
# acceptEdits mode: overlay is still consulted ---------------------------------
@test "mode: acceptEdits + Write inside cwd -> overlay path entered" {
setup_overlay_stub "yes_once"
ti="$(jq -cn --arg fp "$PROJ_ROOT/src/foo.ts" '{file_path:$fp,content:"x"}')"
payload="$(make_mode_payload 'Write' "$ti" 'acceptEdits' "$PROJ_ROOT")"
run_handler_in_stub_root "$payload"
[ "$status" -eq 0 ]
json_line="$(printf '%s\n' "$output" | grep -o '{"hookSpecificOutput".*}' | head -n1)"
[ -n "$json_line" ]
decision="$(jq -r '.hookSpecificOutput.permissionDecision' <<<"$json_line")"
[ "$decision" = "allow" ]
}
@test "mode: acceptEdits + Write OUTSIDE cwd -> native dialog (diff rendering)" {
# Write outside cwd is not mode-auto-allowed. Write tools fall through to
# CC's native dialog (permissionDecision: ask) for diff rendering instead
# of the overlay.
ti='{"file_path":"/tmp/elsewhere/foo.ts","content":"x"}'
payload="$(make_mode_payload 'Write' "$ti" 'acceptEdits' "$PROJ_ROOT")"
run_handler "$payload"
[ "$status" -eq 0 ]
json_line="$(printf '%s\n' "$output" | grep -o '{"hookSpecificOutput".*}' | head -n1)"
[ -n "$json_line" ]
decision="$(jq -r '.hookSpecificOutput.permissionDecision' <<<"$json_line")"
[ "$decision" = "ask" ]
}
@test "mode: acceptEdits + Read inside cwd -> mode auto-allow (superset of default)" {
# acceptEdits is a superset of default: it auto-allows everything default
# does (Read/Grep/Glob inside cwd) PLUS edit tools inside cwd.
ti="$(jq -cn --arg fp "$PROJ_ROOT/src/foo.ts" '{file_path:$fp}')"
payload="$(make_mode_payload 'Read' "$ti" 'acceptEdits' "$PROJ_ROOT")"
run_handler "$payload"
[ "$status" -eq 0 ]
json_line="$(printf '%s\n' "$output" | grep -o '{"hookSpecificOutput".*}' | head -n1)"
[ -n "$json_line" ]
decision="$(jq -r '.hookSpecificOutput.permissionDecision' <<<"$json_line")"
[ "$decision" = "allow" ]
reason="$(jq -r '.hookSpecificOutput.permissionDecisionReason' <<<"$json_line")"
[[ "$reason" == *"mode-allow"* ]]
}
# default mode: all tools go to overlay ----------------------------------------
@test "mode: default + Read inside cwd -> overlay path entered" {
setup_overlay_stub "yes_once"
ti="$(jq -cn --arg fp "$PROJ_ROOT/src/file.ts" '{file_path:$fp}')"
payload="$(make_mode_payload 'Read' "$ti" 'default' "$PROJ_ROOT")"
run_handler_in_stub_root "$payload"
[ "$status" -eq 0 ]
json_line="$(printf '%s\n' "$output" | grep -o '{"hookSpecificOutput".*}' | head -n1)"
[ -n "$json_line" ]
decision="$(jq -r '.hookSpecificOutput.permissionDecision' <<<"$json_line")"
[ "$decision" = "allow" ]
}
@test "mode: default + Read outside cwd -> overlay path entered" {
setup_overlay_stub "yes_once"
ti='{"file_path":"/etc/hosts"}'
payload="$(make_mode_payload 'Read' "$ti" 'default' "$PROJ_ROOT")"
run_handler_in_stub_root "$payload"
[ "$status" -eq 0 ]
json_line="$(printf '%s\n' "$output" | grep -o '{"hookSpecificOutput".*}' | head -n1)"
[ -n "$json_line" ]
decision="$(jq -r '.hookSpecificOutput.permissionDecision' <<<"$json_line")"
[ "$decision" = "allow" ]
}
@test "mode: default + Bash -> overlay path entered (Bash never auto-allowed)" {
# Bash is inherently never auto-allowed in default mode - always consult
# the overlay (or native fallback).