aither-sandbox 0.12.0

Terminal-first command execution with heel sandboxing
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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
//! Refuses commands that kill processes by name or pattern.
//!
//! A command that runs `pkill` or `killall` signals whatever happens to share
//! a name, including processes the agent never started. The terminal tools
//! parse every command as shell before it runs and refuse the ones that invoke
//! such a program, so the agent has to look up a PID and kill that PID.
//!
//! The check walks the syntax tree rather than matching text, so a word that
//! merely contains a killer's name (`echo pkill`, `grep killall`, `pkill.txt`)
//! is not refused, while the program is found through pipelines, lists,
//! compound commands, command and process substitutions, wrappers (`env`,
//! `sudo`, `timeout`, `xargs`, `find -exec`, ...) and the script strings of
//! `sh -c`, `eval` and shells fed through a here-document.
//!
//! Static analysis only sees names that are written out. A program name that is
//! computed at run time (`$tool`, `$(echo pkill)`) or a script piped into a
//! shell's stdin cannot be resolved and is not refused.

use std::cell::Cell;
use std::fmt::{self, Display};

use askama::Template;
use brush_parser::ast::{
    self, AndOrList, Assignment, AssignmentValue, Command, CommandPrefixOrSuffixItem,
    CompoundCommand, CompoundList, ExtendedTestExpr, IoFileRedirectTarget, IoRedirect, Pipeline,
    SimpleCommand,
};
use brush_parser::word::{ParameterExpr, WordPiece, WordPieceWithSource};
use brush_parser::{Parser, ParserOptions, SourceInfo};

/// Deepest nesting of shell strings (`sh -c '... sh -c ...'`) and
/// substitutions the check follows before it refuses the command.
const MAX_DEPTH: usize = 32;

/// Why a command was refused before it ran.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CommandRefusal {
    /// The command invokes a program that kills by name or pattern.
    KillByName {
        /// File name of the refused program, as written in the command.
        program: String,
    },
    /// The command, or a shell string inside it, is not valid shell, so it
    /// cannot be checked.
    Unparsable {
        /// What the parser reported.
        reason: String,
    },
}

#[derive(Template)]
#[template(path = "kill_by_name_refused.txt")]
struct KillByNameTemplate<'a> {
    program: &'a str,
}

#[derive(Template)]
#[template(path = "unparsable_command_refused.txt")]
struct UnparsableTemplate<'a> {
    reason: &'a str,
}

impl Display for CommandRefusal {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let rendered = match self {
            Self::KillByName { program } => KillByNameTemplate { program }.render(),
            Self::Unparsable { reason } => UnparsableTemplate { reason }.render(),
        };
        f.write_str(&rendered.map_err(|_| fmt::Error)?)
    }
}

impl std::error::Error for CommandRefusal {}

/// Refuses `script` when any command in it kills by name or pattern.
///
/// # Errors
/// Returns [`CommandRefusal::KillByName`] when a refused program is invoked,
/// and [`CommandRefusal::Unparsable`] when the script is not valid shell.
pub fn check(script: &str) -> Result<(), CommandRefusal> {
    walk_invocations(script, &|_, _| {})
}

/// Walks `script` through the shell syntax tree, calling `on_argv` with the
/// resolved command line of every simple command it would run — wrappers
/// such as `env`, `sudo`, `timeout`, `xargs` and `find -exec` unwrapped, and
/// the script strings of `sh -c`, `eval` and shell here-documents followed.
/// The second argument reports whether the command sits in an
/// `&`-asynchronous position, so the script itself does not wait on it.
/// Still refuses the same commands [`check`] does.
///
/// # Errors
/// Returns [`CommandRefusal`] for the same reasons [`check`] fails.
pub fn walk_invocations(
    script: &str,
    on_argv: &dyn Fn(&[String], bool),
) -> Result<(), CommandRefusal> {
    script_check(
        script,
        Cx {
            depth: 0,
            lookups: &Cell::new(false),
            on_argv,
            backgrounded: false,
        },
    )
}

fn unparsable(reason: impl Display) -> CommandRefusal {
    CommandRefusal::Unparsable {
        reason: reason.to_string(),
    }
}

/// Traversal state: nesting depth, whether a program that looks processes
/// up by name (`pgrep`, `pidof`) was met anywhere below, and an observer
/// shown every resolved command line.
#[derive(Clone, Copy)]
struct Cx<'a> {
    depth: usize,
    lookups: &'a Cell<bool>,
    /// Called with the argv of every simple command the script would run,
    /// in source order, wrappers unwrapped.
    on_argv: &'a dyn Fn(&[String], bool),
    /// Whether the command sits in an `&`-asynchronous position, so the
    /// script itself does not wait on it. Once a list is detached,
    /// everything below it runs detached.
    backgrounded: bool,
}

impl Cx<'_> {
    const fn deeper(self) -> Self {
        Self {
            depth: self.depth + 1,
            lookups: self.lookups,
            on_argv: self.on_argv,
            backgrounded: self.backgrounded,
        }
    }
}

fn script_check(script: &str, cx: Cx<'_>) -> Result<(), CommandRefusal> {
    if cx.depth > MAX_DEPTH {
        return Err(unparsable(format!(
            "shell strings are nested more than {MAX_DEPTH} levels deep"
        )));
    }
    let program = Parser::new(
        script.as_bytes(),
        &ParserOptions::default(),
        &SourceInfo::default(),
    )
    .parse_program()
    .map_err(unparsable)?;
    for list in &program.complete_commands {
        compound_list(list, cx)?;
    }
    Ok(())
}

fn compound_list(list: &CompoundList, cx: Cx<'_>) -> Result<(), CommandRefusal> {
    for item in &list.0 {
        // `cmd &` detaches the command: the script does not wait on it, and
        // everything it nests runs detached as well.
        let item_cx = Cx {
            backgrounded: cx.backgrounded || matches!(item.1, ast::SeparatorOperator::Async),
            ..cx
        };
        and_or_list(&item.0, item_cx)?;
    }
    Ok(())
}

fn and_or_list(list: &AndOrList, cx: Cx<'_>) -> Result<(), CommandRefusal> {
    pipeline(&list.first, cx)?;
    for next in &list.additional {
        match next {
            ast::AndOr::And(next) | ast::AndOr::Or(next) => pipeline(next, cx)?,
        }
    }
    Ok(())
}

fn pipeline(pipeline: &Pipeline, cx: Cx<'_>) -> Result<(), CommandRefusal> {
    let mut command_lines = Vec::new();
    for command in &pipeline.seq {
        if let Some(argv) = self::command(command, cx)? {
            command_lines.push(argv);
        }
    }
    // `pgrep NAME | xargs kill` kills by name without looking at a PID.
    for (at, producer) in command_lines.iter().enumerate() {
        if chain(producer).iter().any(|name| is_name_lookup(name))
            && command_lines[at + 1..]
                .iter()
                .any(|consumer| xargs_kill(&chain(consumer)))
        {
            return Err(kill_of_lookup());
        }
    }
    Ok(())
}

fn kill_of_lookup() -> CommandRefusal {
    CommandRefusal::KillByName {
        program: "kill of pgrep/pidof output".to_string(),
    }
}

/// Programs that find PIDs by name or pattern.
fn is_name_lookup(name: &str) -> bool {
    matches!(name, "pgrep" | "pidof")
}

/// The program names a command line runs, looking through wrappers.
fn chain(mut argv: &[String]) -> Vec<String> {
    let mut names = Vec::new();
    while let Some((program, arguments)) = argv.split_first() {
        let name = file_name(program);
        let inner = wrapped(&name, arguments);
        names.push(name);
        match inner {
            Some(inner) => argv = inner,
            None => break,
        }
    }
    names
}

fn xargs_kill(chain: &[String]) -> bool {
    chain
        .iter()
        .position(|name| name == "xargs")
        .is_some_and(|at| chain[at + 1..].iter().any(|name| name == "kill"))
}

/// Checks `command`; a simple command returns its resolved command line.
fn command(command: &Command, cx: Cx<'_>) -> Result<Option<Vec<String>>, CommandRefusal> {
    match command {
        Command::Simple(simple) => simple_command(simple, cx).map(Some),
        Command::Compound(compound, redirects) => {
            compound_command(compound, cx)?;
            if let Some(redirects) = redirects {
                for redirect in &redirects.0 {
                    io_redirect(redirect, cx)?;
                }
            }
            Ok(None)
        }
        Command::Function(function) => {
            word(&function.fname, cx)?;
            compound_command(&function.body.0, cx)?;
            if let Some(redirects) = &function.body.1 {
                for redirect in &redirects.0 {
                    io_redirect(redirect, cx)?;
                }
            }
            Ok(None)
        }
        Command::ExtendedTest(test) => extended_test(&test.expr, cx).map(|()| None),
    }
}

fn extended_test(expr: &ExtendedTestExpr, cx: Cx<'_>) -> Result<(), CommandRefusal> {
    match expr {
        ExtendedTestExpr::And(left, right) | ExtendedTestExpr::Or(left, right) => {
            extended_test(left, cx)?;
            extended_test(right, cx)
        }
        ExtendedTestExpr::Not(inner) | ExtendedTestExpr::Parenthesized(inner) => {
            extended_test(inner, cx)
        }
        ExtendedTestExpr::UnaryTest(_, operand) => word(operand, cx).map(drop),
        ExtendedTestExpr::BinaryTest(_, left, right) => {
            word(left, cx)?;
            word(right, cx).map(drop)
        }
    }
}

fn compound_command(compound: &CompoundCommand, cx: Cx<'_>) -> Result<(), CommandRefusal> {
    match compound {
        CompoundCommand::Arithmetic(arithmetic) => text(&arithmetic.expr.value, cx).map(drop),
        CompoundCommand::ArithmeticForClause(clause) => {
            for expr in [&clause.initializer, &clause.condition, &clause.updater]
                .into_iter()
                .flatten()
            {
                text(&expr.value, cx)?;
            }
            compound_list(&clause.body.list, cx)
        }
        CompoundCommand::BraceGroup(group) => compound_list(&group.list, cx),
        CompoundCommand::Subshell(subshell) => compound_list(&subshell.list, cx),
        CompoundCommand::ForClause(clause) => {
            for value in clause.values.iter().flatten() {
                word(value, cx)?;
            }
            compound_list(&clause.body.list, cx)
        }
        CompoundCommand::CaseClause(clause) => {
            word(&clause.value, cx)?;
            for item in &clause.cases {
                for pattern in &item.patterns {
                    word(pattern, cx)?;
                }
                if let Some(body) = &item.cmd {
                    compound_list(body, cx)?;
                }
            }
            Ok(())
        }
        CompoundCommand::IfClause(clause) => {
            compound_list(&clause.condition, cx)?;
            compound_list(&clause.then, cx)?;
            for other in clause.elses.iter().flatten() {
                if let Some(condition) = &other.condition {
                    compound_list(condition, cx)?;
                }
                compound_list(&other.body, cx)?;
            }
            Ok(())
        }
        CompoundCommand::WhileClause(clause) | CompoundCommand::UntilClause(clause) => {
            compound_list(&clause.0, cx)?;
            compound_list(&clause.1.list, cx)
        }
    }
}

/// A simple command's program and arguments with every word unquoted, and
/// the scripts it receives on stdin through here-documents and here-strings.
struct Invocation {
    argv: Vec<String>,
    stdin_scripts: Vec<String>,
    /// Whether a word of the command substitutes the output of `pgrep`/`pidof`.
    lookup_in_words: bool,
}

fn simple_command(simple: &SimpleCommand, cx: Cx<'_>) -> Result<Vec<String>, CommandRefusal> {
    let mut invocation = Invocation {
        argv: Vec::new(),
        stdin_scripts: Vec::new(),
        lookup_in_words: false,
    };
    // Words are traversed with their own flag: a lookup program inside a
    // word's substitution feeds the command's arguments.
    let in_words = Cell::new(false);
    let word_cx = Cx {
        depth: cx.depth,
        lookups: &in_words,
        on_argv: cx.on_argv,
        backgrounded: cx.backgrounded,
    };
    let prefix = simple.prefix.iter().flat_map(|prefix| &prefix.0);
    let suffix = simple.suffix.iter().flat_map(|suffix| &suffix.0);
    for item in prefix {
        // Assignments before the program set its environment; they are not
        // part of the command line.
        if let CommandPrefixOrSuffixItem::AssignmentWord(assignment, _) = item {
            assignment_value(assignment, word_cx)?;
        } else {
            affix_item(item, &mut invocation, word_cx)?;
        }
    }
    if let Some(program) = &simple.word_or_name {
        let program = word(program, word_cx)?;
        invocation.argv.push(program);
    }
    for item in suffix {
        affix_item(item, &mut invocation, word_cx)?;
    }
    invocation.lookup_in_words = in_words.get();
    invoked(
        &invocation.argv,
        &invocation.stdin_scripts,
        invocation.lookup_in_words,
        cx,
    )?;
    Ok(invocation.argv)
}

fn affix_item(
    item: &CommandPrefixOrSuffixItem,
    invocation: &mut Invocation,
    cx: Cx<'_>,
) -> Result<(), CommandRefusal> {
    match item {
        CommandPrefixOrSuffixItem::Word(argument) => {
            let argument = word(argument, cx)?;
            invocation.argv.push(argument);
        }
        CommandPrefixOrSuffixItem::AssignmentWord(assignment, argument) => {
            assignment_value(assignment, cx)?;
            // `declare x=1` style words keep their text as an argument.
            let argument = word(argument, cx)?;
            invocation.argv.push(argument);
        }
        CommandPrefixOrSuffixItem::IoRedirect(redirect) => {
            if let Some(script) = stdin_script(redirect, cx)? {
                invocation.stdin_scripts.push(script);
            }
            io_redirect(redirect, cx)?;
        }
        CommandPrefixOrSuffixItem::ProcessSubstitution(_, subshell) => {
            compound_list(&subshell.list, cx)?;
        }
    }
    Ok(())
}

/// The text a here-document or here-string feeds to stdin.
fn stdin_script(redirect: &IoRedirect, cx: Cx<'_>) -> Result<Option<String>, CommandRefusal> {
    match redirect {
        IoRedirect::HereDocument(_, document) => Ok(Some(document.doc.value.clone())),
        IoRedirect::HereString(_, operand) => word(operand, cx).map(Some),
        IoRedirect::File(..) | IoRedirect::OutputAndError(..) => Ok(None),
    }
}

fn assignment_value(assignment: &Assignment, cx: Cx<'_>) -> Result<(), CommandRefusal> {
    match &assignment.value {
        AssignmentValue::Scalar(value) => word(value, cx).map(drop),
        AssignmentValue::Array(values) => {
            for (key, value) in values {
                if let Some(key) = key {
                    word(key, cx)?;
                }
                word(value, cx)?;
            }
            Ok(())
        }
    }
}

fn io_redirect(redirect: &IoRedirect, cx: Cx<'_>) -> Result<(), CommandRefusal> {
    match redirect {
        IoRedirect::File(_, _, target) => match target {
            IoFileRedirectTarget::Filename(target) | IoFileRedirectTarget::Duplicate(target) => {
                word(target, cx).map(drop)
            }
            IoFileRedirectTarget::Fd(_) => Ok(()),
            IoFileRedirectTarget::ProcessSubstitution(_, subshell) => {
                compound_list(&subshell.list, cx)
            }
        },
        IoRedirect::HereDocument(_, document) => {
            if document.requires_expansion {
                // A here-document body is free text; one that is not a
                // valid word cannot hold a substitution worth following.
                match text(&document.doc.value, cx) {
                    Err(refusal @ CommandRefusal::KillByName { .. }) => return Err(refusal),
                    Err(CommandRefusal::Unparsable { .. }) | Ok(_) => {}
                }
            }
            Ok(())
        }
        IoRedirect::HereString(_, operand) | IoRedirect::OutputAndError(operand, _) => {
            word(operand, cx).map(drop)
        }
    }
}

/// Follows the substitutions inside `word` and returns its text with quotes
/// removed. A part computed at run time contributes nothing to the text.
fn word(word: &ast::Word, cx: Cx<'_>) -> Result<String, CommandRefusal> {
    text(&word.value, cx)
}

fn text(source: &str, cx: Cx<'_>) -> Result<String, CommandRefusal> {
    let pieces =
        brush_parser::word::parse(source, &ParserOptions::default()).map_err(unparsable)?;
    let mut literal = String::new();
    pieces_text(&pieces, &mut literal, cx)?;
    Ok(literal)
}

fn pieces_text(
    pieces: &[WordPieceWithSource],
    literal: &mut String,
    cx: Cx<'_>,
) -> Result<(), CommandRefusal> {
    for WordPieceWithSource { piece, .. } in pieces {
        match piece {
            WordPiece::Text(part)
            | WordPiece::SingleQuotedText(part)
            | WordPiece::AnsiCQuotedText(part) => literal.push_str(part),
            WordPiece::EscapeSequence(part) => {
                literal.push_str(part.strip_prefix('\\').unwrap_or(part));
            }
            WordPiece::DoubleQuotedSequence(inner)
            | WordPiece::GettextDoubleQuotedSequence(inner) => {
                pieces_text(inner, literal, cx)?;
            }
            WordPiece::TildePrefix(_) => {}
            WordPiece::CommandSubstitution(inner)
            | WordPiece::BackquotedCommandSubstitution(inner) => script_check(inner, cx.deeper())?,
            WordPiece::ArithmeticExpression(expr) => {
                text(&expr.value, cx.deeper())?;
            }
            WordPiece::ParameterExpansion(expansion) => parameter_text(expansion, cx)?,
        }
    }
    Ok(())
}

/// Follows the words a parameter expansion carries (`${x:-$(cmd)}`).
fn parameter_text(expansion: &ParameterExpr, cx: Cx<'_>) -> Result<(), CommandRefusal> {
    let embedded: [Option<&str>; 2] = match expansion {
        ParameterExpr::UseDefaultValues { default_value, .. }
        | ParameterExpr::AssignDefaultValues { default_value, .. } => {
            [default_value.as_deref(), None]
        }
        ParameterExpr::IndicateErrorIfNullOrUnset { error_message, .. } => {
            [error_message.as_deref(), None]
        }
        ParameterExpr::UseAlternativeValue {
            alternative_value, ..
        } => [alternative_value.as_deref(), None],
        ParameterExpr::RemoveSmallestSuffixPattern { pattern, .. }
        | ParameterExpr::RemoveLargestSuffixPattern { pattern, .. }
        | ParameterExpr::RemoveSmallestPrefixPattern { pattern, .. }
        | ParameterExpr::RemoveLargestPrefixPattern { pattern, .. }
        | ParameterExpr::UppercaseFirstChar { pattern, .. }
        | ParameterExpr::UppercasePattern { pattern, .. }
        | ParameterExpr::LowercaseFirstChar { pattern, .. }
        | ParameterExpr::LowercasePattern { pattern, .. } => [pattern.as_deref(), None],
        ParameterExpr::Substring { offset, length, .. } => [
            Some(&offset.value),
            length.as_ref().map(|expr| &*expr.value),
        ],
        ParameterExpr::ReplaceSubstring {
            pattern,
            replacement,
            ..
        } => [Some(pattern), replacement.as_deref()],
        ParameterExpr::Parameter { .. }
        | ParameterExpr::ParameterLength { .. }
        | ParameterExpr::Transform { .. }
        | ParameterExpr::VariableNames { .. }
        | ParameterExpr::MemberKeys { .. } => [None, None],
    };
    for source in embedded.into_iter().flatten() {
        text(source, cx.deeper())?;
    }
    Ok(())
}

/// Programs that signal processes selected by name, user or other pattern.
const KILLERS: &[&str] = &["pkill", "killall", "killall5", "skill"];

/// Lower-cased file name of `program` without a Windows executable suffix.
pub fn file_name(program: &str) -> String {
    let name = program.rsplit(['/', '\\']).next().unwrap_or(program);
    let mut name = name.to_ascii_lowercase();
    if let Some(extension_at) = name.len().checked_sub(".exe".len())
        && name.is_char_boundary(extension_at)
        && name[extension_at..] == *".exe"
    {
        name.truncate(extension_at);
    }
    name
}

/// Checks one resolved command line; `stdin_scripts` are the scripts its
/// redirects feed to a shell reading stdin.
fn invoked(
    argv: &[String],
    stdin_scripts: &[String],
    lookup_in_words: bool,
    cx: Cx<'_>,
) -> Result<(), CommandRefusal> {
    let Some((program, arguments)) = argv.split_first() else {
        return Ok(());
    };
    (cx.on_argv)(argv, cx.backgrounded);
    let name = file_name(program);
    if kills_by_name(&name, arguments) {
        return Err(CommandRefusal::KillByName { program: name });
    }
    if is_name_lookup(&name) {
        cx.lookups.set(true);
    }
    if name == "kill" && lookup_in_words {
        return Err(kill_of_lookup());
    }
    if let Some(inner) = wrapped(&name, arguments) {
        invoked(inner, stdin_scripts, lookup_in_words, cx)?;
    }
    if name == "find" {
        for inner in find_exec_commands(arguments) {
            invoked(inner, &[], false, cx)?;
        }
    }
    if name == "eval" && !arguments.is_empty() {
        script_check(&arguments.join(" "), cx.deeper())?;
    }
    if is_shell(&name) {
        match shell_script(arguments) {
            ShellInput::Script(script) => script_check(script, cx.deeper())?,
            ShellInput::Stdin => {
                for script in stdin_scripts {
                    script_check(script, cx.deeper())?;
                }
            }
            ShellInput::File => {}
        }
    }
    Ok(())
}

/// Whether `name` with `arguments` selects the processes it signals by name,
/// by owner or by what they use rather than by PID.
fn kills_by_name(name: &str, arguments: &[String]) -> bool {
    match name {
        "fuser" => arguments.iter().any(|argument| {
            argument == "--kill"
                || argument
                    .strip_prefix('-')
                    .is_some_and(|flags| !flags.starts_with('-') && flags.contains('k'))
        }),
        "taskkill" => arguments.iter().any(|argument| {
            let selector = argument.trim_start_matches(['/', '-']).to_ascii_lowercase();
            argument.starts_with(['/', '-']) && matches!(selector.as_str(), "im" | "fi")
        }),
        _ => KILLERS.contains(&name),
    }
}

fn is_shell(name: &str) -> bool {
    matches!(name, "sh" | "bash" | "dash" | "ash" | "ksh" | "zsh" | "su")
}

/// Where a shell takes its commands from.
enum ShellInput<'a> {
    /// A string given with `-c` (or `su -c`).
    Script(&'a str),
    /// Standard input.
    Stdin,
    /// A script file.
    File,
}

fn shell_script(arguments: &[String]) -> ShellInput<'_> {
    let mut remaining = arguments.iter();
    let mut reads_stdin = false;
    let mut script_file = false;
    while let Some(argument) = remaining.next() {
        if argument == "--command" {
            return remaining
                .next()
                .map_or(ShellInput::Stdin, |script| ShellInput::Script(script));
        }
        if let Some(flags) = argument
            .strip_prefix(['-', '+'])
            .filter(|f| !f.starts_with('-'))
        {
            if argument.starts_with('-') && flags.contains('c') {
                return remaining
                    .next()
                    .map_or(ShellInput::Stdin, |script| ShellInput::Script(script));
            }
            reads_stdin |= flags.contains('s');
            if flags.ends_with(['o', 'O']) {
                remaining.next();
            }
        } else if argument != "--" {
            script_file = true;
            break;
        }
    }
    if script_file && !reads_stdin {
        ShellInput::File
    } else {
        ShellInput::Stdin
    }
}

/// How a command wrapper takes its own options before the command it runs.
struct WrapperSyntax {
    /// Short options that take a value.
    short_values: &'static str,
    /// Long options that take a value when it is not joined with `=`.
    long_values: &'static [&'static str],
    /// Positional arguments before the command.
    positionals: usize,
    /// Whether leading `NAME=value` words belong to the wrapper.
    assignments: bool,
}

const fn wrapper(
    short_values: &'static str,
    long_values: &'static [&'static str],
    positionals: usize,
    assignments: bool,
) -> WrapperSyntax {
    WrapperSyntax {
        short_values,
        long_values,
        positionals,
        assignments,
    }
}

/// What a command wrapper runs, or `None` when `name` is not a wrapper or
/// runs nothing (`command -v pkill` only looks the name up).
fn wrapped<'a>(name: &str, arguments: &'a [String]) -> Option<&'a [String]> {
    let syntax = match name {
        "env" => wrapper("uC", &["unset", "chdir"], 0, true),
        "sudo" => wrapper(
            "ughpCDRTU",
            &["user", "group", "host", "prompt", "chdir", "chroot"],
            0,
            true,
        ),
        "doas" => wrapper("uC", &[], 0, true),
        "nice" => wrapper("n", &["adjustment"], 0, false),
        "ionice" => wrapper("cnp", &["class", "classdata", "pid"], 0, false),
        "stdbuf" => wrapper("ioe", &["input", "output", "error"], 0, false),
        "timeout" => wrapper("sk", &["signal", "kill-after"], 1, false),
        "time" => wrapper("fo", &["format", "output"], 0, false),
        "xargs" => wrapper(
            "IinPLsdEa",
            &[
                "replace",
                "max-args",
                "max-procs",
                "max-lines",
                "max-chars",
                "delimiter",
                "eof",
                "arg-file",
            ],
            0,
            false,
        ),
        "exec" => wrapper("a", &[], 0, false),
        "command" => {
            if arguments
                .iter()
                .any(|argument| argument == "-v" || argument == "-V")
            {
                return None;
            }
            wrapper("", &[], 0, false)
        }
        "builtin" | "nohup" | "setsid" | "busybox" | "toybox" => wrapper("", &[], 0, false),
        _ => return None,
    };
    let mut index = 0;
    while let Some(argument) = arguments.get(index) {
        index += 1;
        if argument == "--" {
            break;
        }
        if let Some(long) = argument.strip_prefix("--") {
            if !long.contains('=') && syntax.long_values.contains(&long) {
                index += 1;
            }
        } else if let Some(flags) = argument.strip_prefix('-').filter(|flags| !flags.is_empty()) {
            // getopt: the first value option takes the rest of the cluster,
            // or the next word when nothing follows it.
            let value_at = flags
                .char_indices()
                .find(|(_, letter)| syntax.short_values.contains(*letter));
            if let Some((at, letter)) = value_at
                && at + letter.len_utf8() == flags.len()
            {
                index += 1;
            }
        } else if syntax.assignments && argument.contains('=') {
        } else {
            index -= 1;
            break;
        }
    }
    index += syntax.positionals;
    arguments.get(index..).filter(|inner| !inner.is_empty())
}

/// The command lines `find` runs through `-exec`, `-execdir`, `-ok` and
/// `-okdir`, each ending at a lone `;` or `+`.
fn find_exec_commands(arguments: &[String]) -> Vec<&[String]> {
    let mut commands = Vec::new();
    let mut index = 0;
    while index < arguments.len() {
        if matches!(
            arguments[index].as_str(),
            "-exec" | "-execdir" | "-ok" | "-okdir"
        ) {
            let start = index + 1;
            let end = arguments[start..]
                .iter()
                .position(|argument| argument == ";" || argument == "+")
                .map_or(arguments.len(), |offset| start + offset);
            commands.push(&arguments[start..end]);
            index = end;
        }
        index += 1;
    }
    commands
}

#[cfg(test)]
mod tests {
    use super::*;

    fn refused_program(script: &str) -> Option<String> {
        match check(script) {
            Err(CommandRefusal::KillByName { program }) => Some(program),
            Err(other) => panic!("`{script}` should parse: {other}"),
            Ok(()) => None,
        }
    }

    #[test]
    fn refuses_killers_by_name_and_path() {
        for script in [
            "pkill foo",
            "killall foo",
            "/usr/bin/pkill -f foo",
            "/usr/bin/killall -9 foo",
            "./killall foo",
            "'pkill' foo",
            "p''kill foo",
            "\\pkill foo",
            "killall5 -9",
            "skill -KILL foo",
            "PKILL foo.exe",
            "fuser -k 8080/tcp",
            "fuser -km /mnt",
            "taskkill /IM foo.exe /F",
        ] {
            assert!(
                refused_program(script).is_some(),
                "`{script}` must be refused"
            );
        }
    }

    #[test]
    fn refuses_killers_nested_in_shell_syntax() {
        for script in [
            "echo hi && pkill foo",
            "echo hi || pkill foo",
            "echo hi; pkill foo",
            "echo hi\npkill foo",
            "sleep 1 & pkill foo",
            "cat x | pkill foo",
            "! pkill foo",
            "(pkill foo)",
            "{ pkill foo; }",
            "if pkill foo; then echo a; fi",
            "if true; then pkill foo; fi",
            "if false; then :; else pkill foo; fi",
            "while pkill foo; do :; done",
            "until true; do pkill foo; done",
            "for i in 1 2; do pkill foo; done",
            "for ((i=0;i<1;i++)); do pkill foo; done",
            "case x in x) pkill foo;; esac",
            "f() { pkill foo; }; f",
            "function f { pkill foo; }",
            "echo $(pkill foo)",
            "echo `pkill foo`",
            "echo \"$(pkill foo)\"",
            "echo ${x:-$(pkill foo)}",
            "echo $(( $(pkill foo) + 1 ))",
            "x=$(pkill foo)",
            "FOO=1 pkill foo",
            "cat <(pkill foo)",
            "echo hi > $(pkill foo)",
            "[[ $(pkill foo) ]]",
            "for x in $(pkill foo); do :; done",
            "case $(pkill foo) in x) :;; esac",
            "echo $(echo $(echo $(pkill foo)))",
        ] {
            assert!(
                refused_program(script).is_some(),
                "`{script}` must be refused"
            );
        }
    }

    #[test]
    fn refuses_killers_behind_wrappers() {
        for script in [
            "env pkill foo",
            "env FOO=1 BAR=2 killall foo",
            "env -u HOME pkill foo",
            "exec pkill foo",
            "command pkill foo",
            "builtin pkill foo",
            "sudo pkill foo",
            "sudo -u root pkill foo",
            "sudo -E killall foo",
            "sudo -Eu root pkill foo",
            "doas pkill foo",
            "nohup pkill foo",
            "nohup pkill foo &",
            "nice pkill foo",
            "nice -n 5 pkill foo",
            "nice -5 pkill foo",
            "ionice -c 3 pkill foo",
            "setsid pkill foo",
            "stdbuf -o0 pkill foo",
            "timeout 5 pkill foo",
            "timeout -s KILL 5 pkill foo",
            "timeout --signal=KILL 5 killall foo",
            "time pkill foo",
            "xargs pkill",
            "echo foo | xargs -n1 pkill",
            "echo foo | xargs -I {} pkill {}",
            "busybox pkill foo",
            "env -- pkill foo",
            "sudo env nice timeout 3 /usr/bin/pkill foo",
            "find . -exec pkill {} \\;",
            "find . -name x -execdir killall {} +",
        ] {
            assert!(
                refused_program(script).is_some(),
                "`{script}` must be refused"
            );
        }
    }

    #[test]
    fn refuses_killers_inside_shell_strings() {
        for script in [
            "sh -c 'pkill foo'",
            "bash -c \"killall foo\"",
            "bash -lc 'pkill foo'",
            "bash -o pipefail -c 'pkill foo'",
            "sh -c 'echo a; /usr/bin/pkill foo'",
            "sh -c \"sh -c 'pkill foo'\"",
            "zsh -c 'pkill foo'",
            "su -c 'pkill foo' root",
            "sudo sh -c 'pkill foo'",
            "env FOO=1 bash -c 'killall foo'",
            "eval pkill foo",
            "eval 'echo a; pkill foo'",
            "sh <<< 'pkill foo'",
            "bash <<EOF\npkill foo\nEOF",
            "cat <<EOF\n$(pkill foo)\nEOF",
            "sh -c 'echo $(pkill foo)'",
        ] {
            assert!(
                refused_program(script).is_some(),
                "`{script}` must be refused"
            );
        }
    }

    #[test]
    fn names_the_refused_program() {
        assert_eq!(
            refused_program("/usr/bin/pkill foo").as_deref(),
            Some("pkill")
        );
        assert_eq!(
            refused_program("sudo killall foo").as_deref(),
            Some("killall")
        );
    }

    #[test]
    fn allows_words_that_only_contain_a_killer_name() {
        for script in [
            "echo pkill",
            "echo 'pkill foo' killall",
            "grep killall file",
            "grep -r pkill .",
            "cat pkill.txt",
            "cat /tmp/killall",
            "ls /usr/bin/pkill",
            "man pkill",
            "which pkill",
            "type killall",
            "command -v pkill",
            "command -V killall",
            "pgrep foo",
            "pidof foo",
            "echo pkill | cat",
            "env echo pkill",
            "sudo echo killall",
            "timeout 5 echo pkill",
            "xargs echo pkill",
            "sh -c 'echo pkill'",
            "bash -c 'grep killall file'",
            "find . -name pkill",
            "find . -exec echo pkill {} \\;",
            "sh script.sh pkill",
            "echo \"$(echo pkill)\"",
            "x=pkill",
            "mypkill foo",
            "pkill_helper",
            "fuser 8080/tcp",
            "fuser -v /mnt",
            "taskkill /PID 42 /F",
            "cat <<EOF\npkill foo\nEOF",
            "sh script.sh <<EOF\npkill foo\nEOF",
            "git commit -m 'stop using pkill'",
            "",
            "   ",
        ] {
            assert_eq!(check(script), Ok(()), "`{script}` must not be refused");
        }
    }

    #[test]
    fn refuses_kills_of_pids_found_by_name() {
        for script in [
            "kill $(pgrep foo)",
            "kill -9 $(pgrep -f foo)",
            "kill `pgrep foo`",
            "kill \"$(pgrep foo)\"",
            "kill $(pidof foo)",
            "kill `pidof foo`",
            "kill $(pgrep foo | head -1)",
            "kill $(/usr/bin/pgrep foo)",
            "/bin/kill $(pgrep foo)",
            "sudo kill $(pgrep foo)",
            "sudo kill -9 $(pidof foo)",
            "env X=1 kill $(pidof foo)",
            "sh -c 'kill $(pgrep foo)'",
            "echo a && kill $(pgrep foo)",
            "pgrep foo | xargs kill",
            "pgrep -f foo | xargs kill -9",
            "pgrep -f foo | xargs -r kill -9",
            "pidof foo | xargs kill",
            "pidof foo | sudo xargs kill",
            "pgrep foo | xargs sudo kill",
            "pgrep foo | tr '\\n' ' ' | xargs kill",
            "/usr/bin/pgrep foo | /usr/bin/xargs /bin/kill",
            "sudo pgrep foo | xargs kill",
        ] {
            assert_eq!(
                refused_program(script).as_deref(),
                Some("kill of pgrep/pidof output"),
                "`{script}` must be refused"
            );
        }
    }

    #[test]
    fn allows_kill_by_pid() {
        for script in [
            "kill 123",
            "kill -9 123",
            "/bin/kill -TERM 123",
            "sleep 30 & kill $!",
            "kill $(cat pidfile)",
            "kill -9 $(cat /tmp/x.pid)",
            "kill $pid",
            "kill \"$pid\"",
            "kill 1 2 3",
            "echo $(pgrep foo)",
            "pgrep foo | wc -l",
            "pgrep foo; kill 123",
            "pidof foo | xargs echo",
            "cat pids | xargs kill",
            "kill $(cat pidfile) && pgrep foo",
            "kill -0 123 2>/dev/null",
            "xargs kill",
            "sudo kill 123",
        ] {
            assert_eq!(check(script), Ok(()), "`{script}` must not be refused");
        }
    }

    #[test]
    fn names_that_are_computed_at_run_time_are_not_resolved() {
        for script in ["$tool foo", "$(echo pkill) foo", "\"${tool}\" foo"] {
            assert_eq!(check(script), Ok(()), "`{script}` cannot be resolved");
        }
    }

    #[test]
    fn refuses_what_cannot_be_parsed() {
        for script in [
            "echo 'unterminated",
            "if true",
            "echo $(",
            "sh -c 'if'",
            "fi",
        ] {
            assert!(
                matches!(check(script), Err(CommandRefusal::Unparsable { .. })),
                "`{script}` must be refused as unparsable"
            );
        }
    }

    #[test]
    fn refuses_runaway_nesting() {
        let script = format!("{}pkill foo", "eval ".repeat(MAX_DEPTH + 2));
        assert!(matches!(
            check(&script),
            Err(CommandRefusal::Unparsable { .. })
        ));
    }

    #[test]
    fn refusal_tells_the_agent_to_kill_by_pid() {
        let kill = CommandRefusal::KillByName {
            program: "pkill".to_string(),
        }
        .to_string();
        assert!(kill.contains("pkill"), "{kill}");
        assert!(kill.contains("PID"), "{kill}");
        let parse = CommandRefusal::Unparsable {
            reason: "syntax error".to_string(),
        }
        .to_string();
        assert!(parse.contains("syntax error"), "{parse}");
    }
}