safe-chains 0.240.0

Auto-allow safe bash commands in agentic coding tools
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
//! Walking the tree below a recursive operand for the write-frozen names it reaches.
//!
//! Without `follow`, links below the operand are not entered (`chmod -R`, `cp -r`, `find`). With it
//! (`find -L`, `fd -L`, `cp -RL`, `rsync -L`), a link to a folder inside the project is entered once,
//! and a link out of the tree is reported under its own name so the caller judges where it leads.
//! A traversal's own filters (`find -path ./.git -prune`) narrow what is reached; see [`Skip`].

use std::collections::{HashSet, VecDeque};

use super::frozen_reach::{Reach, Sight, fold_sight, sight_of, stops_before_matches};

pub(crate) const MAX_ENTRIES: usize = 20_000;
const MAX_LINKS: usize = 64;

#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum WalkLinks {
    Ignore,
    Items,
    Follow,
}

/// How a recursive command walks: whether it lists hidden entries, how it treats links, and which
/// entries its own filters keep from its action.
#[derive(Clone, Copy)]
pub(crate) struct Walk<'a> {
    pub hidden: bool,
    pub links: WalkLinks,
    pub skip: &'a Skip,
}

impl Walk<'_> {
    pub(crate) fn plain(hidden: bool) -> Walk<'static> {
        Walk { hidden, links: WalkLinks::Ignore, skip: &NO_SKIP }
    }

    pub(crate) fn follow() -> Walk<'static> {
        Walk { hidden: true, links: WalkLinks::Follow, skip: &NO_SKIP }
    }

    pub(crate) fn items(hidden: bool, follow: bool) -> Walk<'static> {
        Walk { hidden, links: if follow { WalkLinks::Follow } else { WalkLinks::Items }, skip: &NO_SKIP }
    }
}

static NO_SKIP: Skip = Skip { pruned: Vec::new(), excluded: Vec::new(), files_only: false };

/// The entries a traversal's filters keep from its action: `pruned` ones are neither acted on nor
/// entered (`-path ./.git -prune -o …`), `excluded` ones are not acted on but are entered
/// (`-not -path './.git/*'`), and `files_only` keeps folders from the action (`-type f`). A test
/// whose pattern cannot be read matches nothing, so it keeps nothing away.
#[derive(Default, Debug, PartialEq, Eq)]
pub(crate) struct Skip {
    pub pruned: Vec<(Test, bool)>,
    pub excluded: Vec<Test>,
    pub files_only: bool,
}

/// One `find` test: `-path`/`-wholename` against the path as find prints it, `-name` against the
/// last component, each optionally without case. The `bool` beside a pruned test is `-type d`.
#[derive(Debug, PartialEq, Eq)]
pub(crate) enum Test {
    Path(String, bool),
    Name(String, bool),
}

impl Test {
    fn holds(&self, path: &str, name: &str) -> bool {
        let (pattern, against, fold) = match self {
            Test::Path(p, fold) => (p, path, *fold),
            Test::Name(p, fold) => (p, name, *fold),
        };
        let (pattern, against) =
            if fold { (pattern.to_lowercase(), against.to_lowercase()) } else { (pattern.clone(), against.to_string()) };
        crate::cst::opaque::glob::matches(pattern.as_bytes(), against.as_bytes()) == Some(true)
    }

    /// Whether every entry below the folder at `path` holds: a path pattern `X/*` whose `X` matches
    /// the folder, since `*` also matches `/`.
    fn holds_below(&self, path: &str) -> bool {
        matches!(self, Test::Path(p, fold) if p.strip_suffix("/*").is_some_and(|x| Test::Path(x.to_string(), *fold).holds(path, "")))
    }
}

impl Skip {
    fn pruned(&self, path: &str, name: &str, dir: bool) -> bool {
        self.pruned.iter().any(|(t, dir_only)| (dir || !dir_only) && t.holds(path, name))
    }

    fn excluded(&self, path: &str, name: &str, dir: bool) -> bool {
        (dir && self.files_only) || self.excluded.iter().any(|t| t.holds(path, name))
    }

    fn excludes_below(&self, path: &str) -> bool {
        self.excluded.iter().any(|t| t.holds_below(path))
    }
}

pub(crate) fn unlisted(base: &str) -> Reach {
    Reach::Unlisted(format!("{}/.git/f", base.trim_end_matches('/')))
}

/// The frozen names, and with `follow` the links out of the tree, below the operand `base`. Empty
/// when nothing is reached, when `base` cannot be placed on disk, or when `base` already lies
/// outside the readable and writable sandbox. Without `follow` the walk stops at the first frozen
/// name. A spelling that contains `*`, `?` or `[` is the literal path, when it exists, and every match.
pub(crate) fn reach_below(base: &str, walk: Walk) -> Vec<Reach> {
    let mut seen = 0;
    reach_below_with(base, walk, &mut seen)
}

fn reach_below_with(base: &str, walk: Walk, seen: &mut usize) -> Vec<Reach> {
    if !base.contains(['*', '?', '[']) {
        return walk_literal(base, walk, seen);
    }
    // Literal first, so a clean match cannot hide a frozen name there. `outside_sandbox` globs `*` and `[`.
    let spelled = spelled_dir(base).filter(|dir| in_project(dir) || !outside_sandbox(base));
    let mut reached = spelled.as_ref().map(|dir| walk_placed(base, dir, walk, seen)).unwrap_or_default();
    if stops_before_matches(walk.links == WalkLinks::Ignore, sight_of(&reached)) {
        return reached;
    }
    reached.extend(through_glob(base, walk, seen, spelled.as_deref()));
    reached
}

/// The operand joined onto the working directory when that path exists. Lexical, so `*` and `[` stay literal.
fn spelled_dir(base: &str) -> Option<String> {
    if base.contains('$') || base.contains(crate::cst::eval::TAGGED_PREFIX) {
        return None;
    }
    let cwd = crate::pathctx::cwd().filter(|c| c.starts_with('/'))?;
    let trimmed = base.trim_end_matches('/');
    let path = if base.starts_with('/') {
        if trimmed.is_empty() { "/".to_string() } else { trimmed.to_string() }
    } else if trimmed.is_empty() || trimmed == "." {
        cwd
    } else {
        format!("{}/{trimmed}", cwd.trim_end_matches('/'))
    };
    std::fs::symlink_metadata(&path).is_ok().then_some(path)
}

fn in_project(path: &str) -> bool {
    let Some(root) = crate::pathctx::root().filter(|r| r.starts_with('/')) else { return false };
    let root = root.trim_end_matches('/');
    let path = path.trim_end_matches('/');
    std::fs::canonicalize(path)
        .unwrap_or_else(|_| path.into())
        .starts_with(std::fs::canonicalize(root).unwrap_or_else(|_| root.into()))
}

/// The concrete directory `base`, placed through the ordinary path resolver.
fn walk_literal(base: &str, walk: Walk, seen: &mut usize) -> Vec<Reach> {
    let Some(dir) = on_disk(base) else {
        let unplaced = base.contains('$') || base.contains(crate::cst::eval::TAGGED_PREFIX);
        return if unplaced && crate::pathctx::root().is_some_and(|r| r.starts_with('/')) { vec![unlisted(base)] } else { Vec::new() };
    };
    if outside_sandbox(base) {
        return Vec::new();
    }
    walk_placed(base, &dir, walk, seen)
}

/// `dir` is the directory already listed for the operand spelling `base`.
fn walk_placed(base: &str, dir: &str, walk: Walk, seen: &mut usize) -> Vec<Reach> {
    if walk.links != WalkLinks::Follow && !base.ends_with('/') && std::fs::symlink_metadata(dir).is_ok_and(|m| m.file_type().is_symlink()) {
        return Vec::new();
    }
    // A filter is matched against the path as find prints it, which a trailing `/` changes.
    let skip = if base.len() > 1 && base.ends_with('/') { &NO_SKIP } else { walk.skip };
    Walker { base: base.trim_end_matches('/'), dir, walk: Walk { skip, ..walk }, reached: Vec::new(), links: 0 }.run(seen)
}

struct Walker<'a> {
    base: &'a str,
    dir: &'a str,
    walk: Walk<'a>,
    reached: Vec<Reach>,
    links: usize,
}

impl Walker<'_> {
    fn run(mut self, seen: &mut usize) -> Vec<Reach> {
        let root = (self.walk.links != WalkLinks::Ignore)
            .then(crate::pathctx::root)
            .flatten()
            .and_then(|r| std::fs::canonicalize(r).ok());
        let walked = std::fs::canonicalize(self.dir).unwrap_or_else(|_| self.dir.into());
        let mut entered = HashSet::new();
        let mut missed = false;
        let mut queue = VecDeque::from([String::new()]);
        while let Some(rel) = queue.pop_front() {
            let here = if rel.is_empty() { self.dir.to_string() } else { format!("{}/{rel}", self.dir) };
            let entries = match open_dir(&here) {
                Opened::Entries(entries) => entries,
                Opened::Unlisted => {
                    missed = true;
                    continue;
                }
                Opened::Absent => continue,
            };
            for entry in entries {
                *seen += 1;
                let Some(entry) = entry.ok().filter(|_| *seen <= MAX_ENTRIES) else { return vec![unlisted(self.base)] };
                let Ok(name) = entry.file_name().into_string() else { return vec![unlisted(self.base)] };
                if !self.walk.hidden && name.starts_with('.') {
                    continue;
                }
                let below = if rel.is_empty() { name.clone() } else { format!("{rel}/{name}") };
                let shown = format!("{}/{below}", self.base);
                let kind = entry.file_type().ok();
                let is_link = kind.is_some_and(|t| t.is_symlink());
                let is_dir =
                    if is_link { std::fs::metadata(entry.path()).is_ok_and(|m| m.is_dir()) } else { kind.is_some_and(|t| t.is_dir()) };
                if self.walk.skip.pruned(&shown, &name, is_dir && (!is_link || self.walk.links == WalkLinks::Follow)) {
                    continue;
                }
                if is_frozen(&name) {
                    self.frozen(&shown, &name, is_dir);
                    if self.walk.links == WalkLinks::Ignore && !self.reached.is_empty() {
                        return self.reached;
                    }
                    continue;
                }
                if !is_link {
                    if is_dir {
                        queue.push_back(below);
                    }
                    continue;
                }
                if self.walk.links != WalkLinks::Follow {
                    if self.walk.links == WalkLinks::Items
                        && !self.walk.skip.files_only
                        && !self.walk.skip.excluded(&shown, &name, false)
                        && !std::fs::canonicalize(entry.path()).is_ok_and(|t| clean_in_root(&t, root.as_deref()))
                        && !self.push_link(shown)
                    {
                        return vec![Reach::Unlisted(super::locus::UNKNOWABLE_ITEM.to_string())];
                    }
                    continue;
                }
                match std::fs::canonicalize(entry.path()) {
                    Ok(target) if is_dir && clean_in_root(&target, root.as_deref()) => {
                        if entered.insert(target) {
                            queue.push_back(below);
                        }
                    }
                    Ok(target) if !is_dir && target.starts_with(&walked) && clean_in_root(&target, root.as_deref()) => {}
                    _ => {
                        if !self.push_link(if is_dir { format!("{shown}/.git/f") } else { shown }) {
                            return vec![Reach::Unlisted(super::locus::UNKNOWABLE_ITEM.to_string())];
                        }
                    }
                }
            }
        }
        let seen = fold_sight(sight_of(&self.reached), if missed { Sight::Unlisted } else { Sight::Empty });
        if seen == Sight::Unlisted { vec![unlisted(self.base)] } else { self.reached }
    }

    fn push_link(&mut self, item: String) -> bool {
        self.links += 1;
        if self.links > MAX_LINKS {
            return false;
        }
        self.reached.push(Reach::Found(item));
        true
    }

    /// A frozen entry is reached through what lies inside it unless the filters keep all of that
    /// away, and then as itself unless they keep it away too.
    fn frozen(&mut self, shown: &str, name: &str, is_dir: bool) {
        if !(is_dir && self.walk.skip.excludes_below(shown)) {
            self.reached.push(Reach::Found(format!("{shown}/f")));
        } else if !self.walk.skip.excluded(shown, name, is_dir) {
            self.reached.push(Reach::Found(shown.to_string()));
        }
    }
}

/// Each match of a glob operand, as a literal name (`*` in a match must not be re-globbed).
fn through_glob(base: &str, walk: Walk, seen: &mut usize, walked: Option<&str>) -> Vec<Reach> {
    let walk = Walk { skip: &NO_SKIP, ..walk };
    let trimmed = base.trim_end_matches('/');
    let (parent, pattern) = trimmed.rsplit_once('/').map_or((".", trimmed), |(p, l)| (if p.is_empty() { "/" } else { p }, l));
    if parent.contains(['*', '?', '[']) {
        return on_disk(".").map(|_| unlisted(base)).into_iter().collect();
    }
    if outside_sandbox(parent) {
        return Vec::new();
    }
    let Some(parent_path) = on_disk(parent) else { return Vec::new() };
    let entries = match open_dir(&parent_path) {
        Opened::Entries(entries) => entries,
        Opened::Unlisted => return vec![unlisted(base)],
        Opened::Absent => return Vec::new(),
    };
    let mut reached = Vec::new();
    for entry in entries {
        *seen += 1;
        let Some(entry) = entry.ok().filter(|_| *seen <= MAX_ENTRIES) else { return vec![unlisted(base)] };
        let listed = entry.path();
        let Some(name) = entry.file_name().into_string().ok() else { return vec![unlisted(base)] };
        if name.starts_with('.') && !pattern.starts_with('.') {
            continue;
        }
        match crate::cst::opaque::glob::matches(pattern.as_bytes(), name.as_bytes()) {
            Some(true) => {}
            Some(false) => continue,
            None => return vec![unlisted(base)],
        }
        if walked.is_some_and(|dir| same_place(dir, &listed)) {
            continue;
        }
        let frozen = is_frozen(&name);
        let each = if parent == "." { name } else { format!("{}/{name}", parent.trim_end_matches('/')) };
        if frozen {
            reached.push(Reach::Found(format!("{each}/f")));
        } else if each.contains(['*', '?', '[']) {
            let dir = link_dir(&listed).or_else(|| listed.to_str().map(str::to_string));
            let Some(dir) = dir else { return vec![unlisted(base)] };
            reached.extend(walk_placed(&each, &dir, walk, seen));
        } else {
            reached.extend(walk_literal(&each, walk, seen));
        }
        if stops_before_matches(walk.links == WalkLinks::Ignore, sight_of(&reached)) {
            reached.retain(|r| matches!(r, Reach::Found(_)));
            reached.truncate(1);
            return reached;
        }
    }
    reached
}

enum Opened {
    Entries(std::fs::ReadDir),
    Unlisted,
    Absent,
}

/// [`Opened::Unlisted`] when a directory cannot be listed. [`Opened::Absent`] for a missing path or a file.
fn open_dir(path: &str) -> Opened {
    match std::fs::read_dir(path) {
        Ok(entries) => Opened::Entries(entries),
        Err(err) if dir_unreadable(&err, path) => Opened::Unlisted,
        Err(_) => Opened::Absent,
    }
}

fn dir_unreadable(err: &std::io::Error, path: &str) -> bool {
    !matches!(err.kind(), std::io::ErrorKind::NotFound | std::io::ErrorKind::NotADirectory)
        && match std::fs::metadata(path) {
            Ok(meta) => meta.is_dir(),
            Err(stat_err) => !matches!(stat_err.kind(), std::io::ErrorKind::NotFound | std::io::ErrorKind::NotADirectory),
        }
}

fn same_place(walked: &str, listed: &std::path::Path) -> bool {
    listed.to_str() == Some(walked)
}
fn link_dir(listed: &std::path::Path) -> Option<String> {
    let target = std::fs::read_link(listed).ok()?;
    let joined = if target.is_absolute() { target } else { listed.parent()?.join(target) };
    joined.is_dir().then(|| joined.to_str().map(str::to_string)).flatten()
}

fn outside_sandbox(base: &str) -> bool {
    let repr = format!("{}/f", base.trim_end_matches('/'));
    let placed = crate::pathctx::on_the_link(|| crate::pathctx::resolve(&repr));
    let canonical = super::locus::canonicalize(&placed);
    (placed.starts_with('/') || placed.starts_with('~'))
        && (super::locus::is_unpinnable(&canonical) || {
            let role = super::regions::classify_region(&canonical);
            role.read_locus >= crate::engine::facet::LocalLocus::User && role.write_locus >= crate::engine::facet::LocalLocus::User
        })
}

fn clean_in_root(target: &std::path::Path, root: Option<&std::path::Path>) -> bool {
    root.and_then(|r| target.strip_prefix(r).ok())
        .is_some_and(|rel| rel.components().all(|c| c.as_os_str().to_str().is_some_and(|s| !is_frozen(s))))
}

pub(crate) fn is_frozen(name: &str) -> bool {
    let fold = cfg!(target_os = "macos");
    super::regions::metadata::frozen_names()
        .iter()
        .any(|n| if fold { n.eq_ignore_ascii_case(name) } else { *n == name })
}

pub(crate) fn on_disk(base: &str) -> Option<String> {
    let root = crate::pathctx::root().filter(|r| r.starts_with('/'))?;
    if base.contains('$') || base.contains(crate::cst::eval::TAGGED_PREFIX) {
        return None;
    }
    let placed = crate::pathctx::resolve(base);
    if placed.starts_with('/') {
        return Some(placed.into_owned());
    }
    Some(if placed.is_empty() || placed == "." { root } else { format!("{root}/{placed}") })
}

#[cfg(test)]
#[path = "frozen_walk_tests.rs"]
mod tests;