cc-toolgate 0.6.3

PreToolUse hook for Claude Code that gates Bash commands with compound-command-aware validation
Documentation
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
use super::*;

/// Clear `GIT_CONFIG_GLOBAL` from the process environment so the
/// env-gate fallback in `env_satisfies` doesn't interfere.  Requires nextest.
fn clear_git_env() {
    assert!(
        std::env::var("NEXTEST").is_ok(),
        "this test mutates process env and requires nextest (cargo nextest run)"
    );
    unsafe { std::env::remove_var("GIT_CONFIG_GLOBAL") };
}

// Helper to build a ShellSegment for is_likely_successful tests
fn seg(command: &str) -> ShellSegment {
    let words = parse::tokenize(command);
    ShellSegment {
        command: command.to_string(),
        words,
        redirection: None,
        substitutions: vec![],
    }
}

fn seg_with_subst(command: &str) -> ShellSegment {
    let words = parse::tokenize(command);
    ShellSegment {
        command: command.to_string(),
        words,
        redirection: None,
        substitutions: vec![parse::SubstitutionSpan {
            start: 0,
            end: 1,
            pipeline: ParsedPipeline {
                segments: vec![],
                operators: vec![],
                structural_substitutions: vec![],
                has_parse_errors: false,
            },
        }],
    }
}

// ── is_likely_successful ──

#[test]
fn likely_success_export() {
    assert!(is_likely_successful(&seg("export FOO=bar")));
}

#[test]
fn likely_success_export_multiple() {
    assert!(is_likely_successful(&seg("export A=1 B=2")));
}

#[test]
fn likely_success_bare_assignment() {
    assert!(is_likely_successful(&seg("FOO=bar")));
}

#[test]
fn likely_success_true() {
    assert!(is_likely_successful(&seg("true")));
}

#[test]
fn likely_success_echo() {
    assert!(is_likely_successful(&seg("echo hello")));
}

#[test]
fn likely_success_printf() {
    assert!(is_likely_successful(&seg("printf '%s\\n' hello")));
}

#[test]
fn likely_success_export_with_subshell_is_not_likely() {
    // export FOO=$(cmd) — the substitution could fail
    assert!(!is_likely_successful(&seg_with_subst("export FOO=$(cmd)")));
}

#[test]
fn likely_success_echo_with_subshell_is_not_likely() {
    assert!(!is_likely_successful(&seg_with_subst("echo $(cmd)")));
}

#[test]
fn likely_success_bare_assignment_with_subshell_is_not_likely() {
    assert!(!is_likely_successful(&seg_with_subst("FOO=$(cmd)")));
}

#[test]
fn likely_success_unknown_command() {
    assert!(!is_likely_successful(&seg("some_command --flag")));
}

#[test]
fn likely_success_git() {
    assert!(!is_likely_successful(&seg("git push")));
}

#[test]
fn likely_success_rm() {
    assert!(!is_likely_successful(&seg("rm -rf /")));
}

// ── extract_segment_env ──

/// Helper: tokenize a command string into words for extract_segment_env/extract_unset_vars tests.
fn words(cmd: &str) -> Vec<parse::Word> {
    parse::tokenize(cmd)
}

#[test]
fn extract_env_export_single() {
    let vars = extract_segment_env(&words("export FOO=bar"));
    assert_eq!(vars, vec![("FOO".into(), "bar".into())]);
}

#[test]
fn extract_env_export_multiple() {
    let vars = extract_segment_env(&words("export A=1 B=2"));
    assert_eq!(
        vars,
        vec![("A".into(), "1".into()), ("B".into(), "2".into())]
    );
}

#[test]
fn extract_env_export_with_path() {
    let vars = extract_segment_env(&words("export GIT_CONFIG_GLOBAL=~/.gitconfig.ai"));
    assert_eq!(
        vars,
        vec![("GIT_CONFIG_GLOBAL".into(), "~/.gitconfig.ai".into())]
    );
}

#[test]
fn extract_env_bare_assignment() {
    let vars = extract_segment_env(&words("FOO=bar"));
    assert_eq!(vars, vec![("FOO".into(), "bar".into())]);
}

#[test]
fn extract_env_export_no_value() {
    // `export FOO` (no =) should not extract anything
    let vars = extract_segment_env(&words("export FOO"));
    assert!(vars.is_empty());
}

#[test]
fn extract_env_export_flags() {
    let vars = extract_segment_env(&words("export -p"));
    assert!(vars.is_empty());
}

#[test]
fn extract_env_non_export() {
    let vars = extract_segment_env(&words("git push"));
    assert!(vars.is_empty());
}

// ── Compound command env accumulation (end-to-end via registry) ──

/// Build a registry with git config_env gating enabled.
fn registry_with_git_env_gate() -> CommandRegistry {
    let mut config = crate::config::Config::default_config();
    config.git.allowed_with_config = vec!["push".into(), "commit".into(), "add".into()];
    config
        .git
        .config_env
        .insert("GIT_CONFIG_GLOBAL".into(), "~/.gitconfig.ai".into());
    CommandRegistry::from_config(&config)
}

#[test]
fn export_semicolon_git_push_allows() {
    let reg = registry_with_git_env_gate();
    let result = reg.evaluate("export GIT_CONFIG_GLOBAL=~/.gitconfig.ai ; git push origin main");
    assert_eq!(
        result.decision,
        Decision::Allow,
        "reason: {}",
        result.reason
    );
}

#[test]
fn export_and_git_push_allows() {
    let reg = registry_with_git_env_gate();
    let result = reg.evaluate("export GIT_CONFIG_GLOBAL=~/.gitconfig.ai && git push origin main");
    assert_eq!(
        result.decision,
        Decision::Allow,
        "reason: {}",
        result.reason
    );
}

#[test]
fn multiple_exports_and_git_push_allows() {
    let reg = registry_with_git_env_gate();
    let result = reg.evaluate(
        "export PATH=/usr/bin && export GIT_CONFIG_GLOBAL=~/.gitconfig.ai && git push origin main",
    );
    assert_eq!(
        result.decision,
        Decision::Allow,
        "reason: {}",
        result.reason
    );
}

#[test]
fn export_or_git_push_does_not_allow() {
    clear_git_env();
    // || means git push runs only if export failed → env not set
    let reg = registry_with_git_env_gate();
    let result = reg.evaluate("export GIT_CONFIG_GLOBAL=~/.gitconfig.ai || git push origin main");
    assert_eq!(result.decision, Decision::Ask, "reason: {}", result.reason);
}

#[test]
fn export_pipe_git_push_does_not_allow() {
    clear_git_env();
    // | means subshell boundary → env doesn't propagate
    let reg = registry_with_git_env_gate();
    let result = reg.evaluate("export GIT_CONFIG_GLOBAL=~/.gitconfig.ai | git push origin main");
    assert_eq!(result.decision, Decision::Ask, "reason: {}", result.reason);
}

#[test]
fn unknown_cmd_breaks_and_chain() {
    // unknown_cmd is not is_likely_successful, so && chain breaks
    let reg = registry_with_git_env_gate();
    let result = reg.evaluate(
        "export GIT_CONFIG_GLOBAL=~/.gitconfig.ai && unknown_cmd && git push origin main",
    );
    assert_eq!(result.decision, Decision::Ask, "reason: {}", result.reason);
}

#[test]
fn semicolon_after_unknown_cmd_resumes_accumulation() {
    // ; resets segment_executes to true, so export after ; is accumulated
    let reg = registry_with_git_env_gate();
    let result = reg
        .evaluate("unknown_cmd ; export GIT_CONFIG_GLOBAL=~/.gitconfig.ai ; git push origin main");
    assert_eq!(result.decision, Decision::Ask, "reason: {}", result.reason);
    // Note: still ASK because unknown_cmd itself is ASK (unrecognized),
    // and strictest-wins. Let's verify the git push part specifically.
}

#[test]
fn semicolon_resumes_accumulation_all_known() {
    // echo is allowed AND likely_successful. After ;, export accumulates.
    let reg = registry_with_git_env_gate();
    let result = reg.evaluate(
        "echo starting ; export GIT_CONFIG_GLOBAL=~/.gitconfig.ai ; git push origin main",
    );
    assert_eq!(
        result.decision,
        Decision::Allow,
        "reason: {}",
        result.reason
    );
}

#[test]
fn bare_assignment_semicolon_git_push_allows() {
    let reg = registry_with_git_env_gate();
    let result = reg.evaluate("GIT_CONFIG_GLOBAL=~/.gitconfig.ai ; git push origin main");
    assert_eq!(
        result.decision,
        Decision::Allow,
        "reason: {}",
        result.reason
    );
}

#[test]
fn bare_assignment_and_git_push_allows() {
    let reg = registry_with_git_env_gate();
    let result = reg.evaluate("GIT_CONFIG_GLOBAL=~/.gitconfig.ai && git push origin main");
    assert_eq!(
        result.decision,
        Decision::Allow,
        "reason: {}",
        result.reason
    );
}

#[test]
fn wrong_export_value_still_asks() {
    let reg = registry_with_git_env_gate();
    let result =
        reg.evaluate("export GIT_CONFIG_GLOBAL=~/.gitconfig.wrong && git push origin main");
    assert_eq!(result.decision, Decision::Ask, "reason: {}", result.reason);
}

#[test]
fn export_overridden_by_later_export() {
    let reg = registry_with_git_env_gate();
    // First export sets wrong value, second corrects it
    let result = reg.evaluate(
        "export GIT_CONFIG_GLOBAL=wrong ; export GIT_CONFIG_GLOBAL=~/.gitconfig.ai ; git push origin main",
    );
    assert_eq!(
        result.decision,
        Decision::Allow,
        "reason: {}",
        result.reason
    );
}

#[test]
fn or_after_export_clears_accumulated_env() {
    clear_git_env();
    // export A=1 && echo ok || export B=2 && git push
    // The || clears accumulated env (conservative: can't determine which
    // path was taken). git push doesn't see GIT_CONFIG_GLOBAL.
    let reg = registry_with_git_env_gate();
    let result = reg.evaluate(
        "export GIT_CONFIG_GLOBAL=~/.gitconfig.ai && echo ok || export OTHER=x && git push origin main",
    );
    assert_eq!(result.decision, Decision::Ask, "reason: {}", result.reason);
}

#[test]
fn echo_and_export_and_git_push_allows() {
    // echo is likely_successful, export is likely_successful, chain holds
    let reg = registry_with_git_env_gate();
    let result = reg.evaluate(
        "echo 'Pushing...' && export GIT_CONFIG_GLOBAL=~/.gitconfig.ai && git push origin main",
    );
    assert_eq!(
        result.decision,
        Decision::Allow,
        "reason: {}",
        result.reason
    );
}

#[test]
fn realistic_claude_pattern() {
    // The actual pattern Claude generates
    let reg = registry_with_git_env_gate();
    let result = reg.evaluate(
        "export PATH=/home/user/.cargo/bin:/usr/bin && export GIT_CONFIG_GLOBAL=~/.gitconfig.ai && echo 'Pushing...' && git push -u origin feature-branch",
    );
    assert_eq!(
        result.decision,
        Decision::Allow,
        "reason: {}",
        result.reason
    );
}

#[test]
fn force_push_still_asks_with_export() {
    // Force push flags should escalate even with correct env
    let reg = registry_with_git_env_gate();
    let result =
        reg.evaluate("export GIT_CONFIG_GLOBAL=~/.gitconfig.ai && git push --force origin main");
    assert_eq!(result.decision, Decision::Ask, "reason: {}", result.reason);
}

#[test]
fn subshell_in_export_breaks_and_chain() {
    // export FOO=$(cmd) && git push — subshell makes export's success unpredictable,
    // so the && chain can't guarantee the next segment executes.
    let reg = registry_with_git_env_gate();
    let result = reg
        .evaluate("export GIT_CONFIG_GLOBAL=$(cat ~/.gitconfig.ai.path) && git push origin main");
    assert_eq!(result.decision, Decision::Ask, "reason: {}", result.reason);
}

#[test]
fn subshell_in_echo_breaks_and_chain() {
    // echo $(cmd) && export FOO=bar && git push — echo with subshell is not
    // likely successful, breaking the chain for subsequent accumulation.
    let reg = registry_with_git_env_gate();
    let result = reg.evaluate(
        "echo $(some_status_cmd) && export GIT_CONFIG_GLOBAL=~/.gitconfig.ai && git push origin main",
    );
    assert_eq!(result.decision, Decision::Ask, "reason: {}", result.reason);
}

// ── unset ──

#[test]
fn unset_removes_accumulated_var() {
    clear_git_env();
    let reg = registry_with_git_env_gate();
    let result = reg.evaluate(
        "export GIT_CONFIG_GLOBAL=~/.gitconfig.ai ; unset GIT_CONFIG_GLOBAL ; git push origin main",
    );
    assert_eq!(result.decision, Decision::Ask, "reason: {}", result.reason);
}

#[test]
fn unset_only_removes_named_var() {
    let reg = registry_with_git_env_gate();
    let result = reg.evaluate(
        "export GIT_CONFIG_GLOBAL=~/.gitconfig.ai ; unset OTHER_VAR ; git push origin main",
    );
    assert_eq!(
        result.decision,
        Decision::Allow,
        "reason: {}",
        result.reason
    );
}

#[test]
fn unset_f_does_not_remove_var() {
    // unset -f removes functions, not variables
    let reg = registry_with_git_env_gate();
    let result = reg.evaluate(
        "export GIT_CONFIG_GLOBAL=~/.gitconfig.ai ; unset -f GIT_CONFIG_GLOBAL ; git push origin main",
    );
    assert_eq!(
        result.decision,
        Decision::Allow,
        "reason: {}",
        result.reason
    );
}

// ── extract_unset_vars ──

#[test]
fn extract_unset_single() {
    assert_eq!(extract_unset_vars(&words("unset FOO")), vec!["FOO"]);
}

#[test]
fn extract_unset_multiple() {
    assert_eq!(
        extract_unset_vars(&words("unset FOO BAR")),
        vec!["FOO", "BAR"]
    );
}

#[test]
fn extract_unset_with_v_flag() {
    assert_eq!(extract_unset_vars(&words("unset -v FOO")), vec!["FOO"]);
}

#[test]
fn extract_unset_with_f_flag() {
    let w = words("unset -f my_func");
    let result = extract_unset_vars(&w);
    assert!(result.is_empty());
}

#[test]
fn extract_unset_mixed_flags() {
    // -f disables var unset, -v re-enables it
    assert_eq!(
        extract_unset_vars(&words("unset -f my_func -v MY_VAR")),
        vec!["MY_VAR"]
    );
}

#[test]
fn extract_unset_not_unset_cmd() {
    assert!(extract_unset_vars(&words("export FOO=bar")).is_empty());
}

// ── env -i wrapper ──

#[test]
fn env_i_clears_accumulated_env_for_wrapped_cmd() {
    clear_git_env();
    let reg = registry_with_git_env_gate();
    let result =
        reg.evaluate("export GIT_CONFIG_GLOBAL=~/.gitconfig.ai ; env -i git push origin main");
    assert_eq!(result.decision, Decision::Ask, "reason: {}", result.reason);
}

#[test]
fn env_dash_clears_accumulated_env_for_wrapped_cmd() {
    clear_git_env();
    let reg = registry_with_git_env_gate();
    let result =
        reg.evaluate("export GIT_CONFIG_GLOBAL=~/.gitconfig.ai ; env - git push origin main");
    assert_eq!(result.decision, Decision::Ask, "reason: {}", result.reason);
}

#[test]
fn env_without_i_passes_accumulated_env() {
    let reg = registry_with_git_env_gate();
    let result =
        reg.evaluate("export GIT_CONFIG_GLOBAL=~/.gitconfig.ai ; env git push origin main");
    assert_eq!(
        result.decision,
        Decision::Allow,
        "reason: {}",
        result.reason
    );
}

// ── Project overlay consent annotation ──

/// Build a registry with a project overlay path set.
fn registry_with_project_overlay() -> CommandRegistry {
    let mut config = crate::config::Config::default_config();
    config.project_overlay_path = Some(std::path::PathBuf::from(
        "/fake/repo/.claude/cc-toolgate.toml",
    ));
    CommandRegistry::from_config(&config)
}

#[test]
fn ask_decision_annotated_with_project_overlay_path() {
    let reg = registry_with_project_overlay();
    // "curl" is in the default ask list — should produce ASK with annotation
    let result = reg.evaluate_single("curl https://example.com");
    assert_eq!(result.decision, Decision::Ask);
    assert!(
        result.reason.contains("project config at"),
        "ASK reason should mention project config; got: {}",
        result.reason
    );
    assert!(
        result
            .reason
            .contains("/fake/repo/.claude/cc-toolgate.toml"),
        "ASK reason should include overlay path; got: {}",
        result.reason
    );
}

#[test]
fn allow_decision_not_annotated_with_project_overlay_path() {
    let reg = registry_with_project_overlay();
    // "ls" is in the default allow list — should NOT have annotation
    let result = reg.evaluate_single("ls -la");
    assert_eq!(result.decision, Decision::Allow);
    assert!(
        !result.reason.contains("project config at"),
        "ALLOW reason should not mention project config; got: {}",
        result.reason
    );
}

#[test]
fn deny_decision_not_annotated_with_project_overlay_path() {
    let reg = registry_with_project_overlay();
    // "shred" is in the default deny list — DENY should NOT have annotation
    let result = reg.evaluate_single("shred /etc/passwd");
    assert_eq!(result.decision, Decision::Deny);
    assert!(
        !result.reason.contains("project config at"),
        "DENY reason should not mention project config; got: {}",
        result.reason
    );
}

#[test]
fn no_annotation_without_project_overlay() {
    // Registry without project overlay — no annotation on ASK
    let config = crate::config::Config::default_config();
    let reg = CommandRegistry::from_config(&config);
    let result = reg.evaluate_single("curl https://example.com");
    assert_eq!(result.decision, Decision::Ask);
    assert!(
        !result.reason.contains("project config at"),
        "without project overlay, reason should not mention project config; got: {}",
        result.reason
    );
}

#[test]
fn compound_ask_decision_annotated_with_project_overlay() {
    let reg = registry_with_project_overlay();
    // Compound command where one segment is ASK
    let result = reg.evaluate("ls -la ; curl https://example.com");
    assert_eq!(result.decision, Decision::Ask);
    assert!(
        result.reason.contains("project config at"),
        "compound ASK reason should mention project config; got: {}",
        result.reason
    );
}

// ── background operator ──

#[test]
fn background_operator_evaluates_both_segments() {
    // Background operator (&) separates two commands — both are evaluated,
    // strictest decision wins (rm -rf / → Ask)
    let config = crate::config::Config::default_config();
    let reg = CommandRegistry::from_config(&config);
    let result = reg.evaluate("echo hello & rm -rf /");
    assert_eq!(result.decision, Decision::Ask, "reason: {}", result.reason);
}

#[test]
fn background_operator_allows_benign_commands() {
    // Both commands are benign — background operator alone doesn't escalate
    let config = crate::config::Config::default_config();
    let reg = CommandRegistry::from_config(&config);
    let result = reg.evaluate("echo hello & ls");
    assert_eq!(
        result.decision,
        Decision::Allow,
        "reason: {}",
        result.reason
    );
}