agentignore 0.1.0

FUSE filesystem that hides files matching .agentignore rules from processes - control what agents can see while building apps
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
//! Allow list — configures process-based bypasses via `.agentallow` files.
//!
//! # File format (`.agentallow`)
//!
//! - One entry per line.
//! - Empty lines and lines starting with `#` are ignored (comments).
//!
//! ## Process name / cmdline matching
//!
//! | Syntax | Behaviour |
//! |--------|-----------|
//! | `node` | Regex; matched against the process `comm` name OR its full
//!   cmdline.  The match also walks up the ancestor chain (parent, grandparent,
//!   etc.). |
//! | `=npm` | Exact match against `comm` OR cmdline; ancestors are also checked. |
//! | `=bash!` | Exact, no ancestor walk — matches only the exact process. |
//! | `node!` | Regex, no ancestor walk. |
//!
//! ## Binary path matching (entry starts with `/`)
//!
//! | Syntax | Behaviour |
//! |--------|-----------|
//! | `/usr/bin/node` | Literal path; matches process or any ancestor. |
//! | `/usr/bin/java!` | Literal path; matches only that exact process. |

use regex::Regex;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Mutex;
use std::time::Instant;
use tracing::{debug, warn};

// ── Pattern matching ────────────────────────────────────────────────────────

#[derive(Debug, Clone)]
enum ProcessPattern {
    /// Regex matched against comm name or cmdline
    Regex(Regex),
    /// Exact string matched against comm name or cmdline
    Exact(String),
}

impl ProcessPattern {
    fn matches(&self, comm: &str, cmdline: &str) -> bool {
        match self {
            ProcessPattern::Regex(re) => re.is_match(comm) || re.is_match(cmdline),
            ProcessPattern::Exact(s) => comm == s || cmdline == s,
        }
    }
}

/// Parse a process name config token (before any trailing `!` is removed).
/// `=foo`  → exact match for "foo"
/// `foo`   → regex /foo/
fn parse_process_pattern(token: &str) -> Option<ProcessPattern> {
    if let Some(exact) = token.strip_prefix('=') {
        Some(ProcessPattern::Exact(exact.to_string()))
    } else {
        match Regex::new(token) {
            Ok(re) => Some(ProcessPattern::Regex(re)),
            Err(e) => {
                warn!("Invalid regex in .agentallow {:?}: {}", token, e);
                None
            }
        }
    }
}

// ── Allow entries ───────────────────────────────────────────────────────────

#[derive(Debug, Clone)]
enum AllowEntry {
    /// Match process/cmdline (regex or exact) or any ancestor
    ProcessName(ProcessPattern),
    /// Match process/cmdline (regex or exact), NO ancestor walk
    ProcessNameExact(ProcessPattern),
    /// Match binary path or any ancestor
    BinaryPath(PathBuf),
    /// Match exact binary path only (no ancestor walk)
    BinaryPathExact(PathBuf),
}

// ── Process info cache ──────────────────────────────────────────────────────

/// Per-process data read from `/proc/<pid>/`.
///
/// Kept for [`PROC_CACHE_TTL`] to balance freshness with reduced I/O.
#[derive(Debug, Clone)]
struct CachedProcess {
    comm: String,
    cmdline: String,
    exe: Option<PathBuf>,
    ppid: Option<u32>,
    loaded_at: Instant,
}

impl CachedProcess {
    fn is_fresh(&self) -> bool {
        self.loaded_at.elapsed() < PROC_CACHE_TTL
    }
}

/// How long a cached `/proc/<pid>/` entry stays valid.
///
/// PID reuse is unlikely within this window under normal loads; the small
/// staleness risk is worth the I/O savings during bursty FUSE traffic.
const PROC_CACHE_TTL: std::time::Duration = std::time::Duration::from_secs(2);

/// Local process-info cache shared across one `AllowList` lifetime.
///
/// Populated lazily and naturally cleared on config reload (which creates a
/// fresh `AllowList`).
#[derive(Debug, Clone, Default)]
struct ProcessCache {
    map: HashMap<u32, CachedProcess>,
}

impl ProcessCache {
    fn get_or_load(&mut self, pid: u32) -> CachedProcess {
        if let Some(cached) = self.map.get(&pid)
            && cached.is_fresh()
        {
            return cached.clone();
        }

        let proc_info = load_process(pid);
        self.map.insert(pid, proc_info.clone());
        proc_info
    }
}

fn load_process(pid: u32) -> CachedProcess {
    let comm = std::fs::read_to_string(format!("/proc/{pid}/comm"))
        .map(|s| s.trim().to_string())
        .unwrap_or_else(|_| "<unknown>".to_string());

    let cmdline = std::fs::read(format!("/proc/{pid}/cmdline"))
        .map(|bytes| {
            bytes
                .split(|&b| b == 0)
                .filter(|s| !s.is_empty())
                .map(|s| String::from_utf8_lossy(s))
                .collect::<Vec<_>>()
                .join(" ")
        })
        .unwrap_or_default();

    let exe = std::fs::read_link(format!("/proc/{pid}/exe")).ok();

    let ppid = get_parent_pid_from_proc(pid);

    CachedProcess {
        comm,
        cmdline,
        exe,
        ppid,
        loaded_at: Instant::now(),
    }
}

/// Get the parent PID from `/proc/<pid>/stat` (uncached).
fn get_parent_pid_from_proc(pid: u32) -> Option<u32> {
    let stat_content = std::fs::read_to_string(format!("/proc/{pid}/stat")).ok()?;
    let after_comm = stat_content.rfind(')')?;
    let rest = &stat_content[after_comm + 1..];
    let fields: Vec<&str> = rest.split_whitespace().collect();
    fields.get(1)?.parse().ok()
}

// ── AllowList (single file) ─────────────────────────────────────────────────

/// An allow list loaded from a single `.agentallow` file.
#[derive(Debug)]
pub struct AllowList {
    entries: Vec<AllowEntry>,
    /// Memoized process info to avoid redundant `/proc/<pid>/` reads.
    process_cache: Mutex<ProcessCache>,
}

impl AllowList {
    /// Load the `.agentallow` file from `root`.
    pub fn load(root: &Path) -> Self {
        let path = root.join(".agentallow");
        Self::load_from_file(&path)
    }

    /// Load from an explicit path.
    pub fn load_from_file(path: &Path) -> Self {
        let mut entries = Vec::new();

        if let Ok(content) = std::fs::read_to_string(path) {
            for line in content.lines() {
                let line = line.trim();
                // Skip comments and empty lines
                if line.is_empty() || line.starts_with('#') {
                    continue;
                }

                // Peel off optional trailing '!' for exact-process-only matching.
                let (token, ancestor_walk) = if let Some(t) = line.strip_suffix('!') {
                    (t.trim(), false)
                } else {
                    (line, true)
                };

                if token.starts_with('/') {
                    // Binary path — always a literal comparison.
                    let pb = PathBuf::from(token);
                    if ancestor_walk {
                        debug!("Allow binary path or child: {} from {:?}", token, path);
                        entries.push(AllowEntry::BinaryPath(pb));
                    } else {
                        debug!("Allow exact binary path: {} from {:?}", token, path);
                        entries.push(AllowEntry::BinaryPathExact(pb));
                    }
                } else {
                    // Process name / cmdline — regex or exact.
                    if let Some(pattern) = parse_process_pattern(token) {
                        if ancestor_walk {
                            debug!(
                                "Allow process pattern or child: {:?} from {:?}",
                                token, path
                            );
                            entries.push(AllowEntry::ProcessName(pattern));
                        } else {
                            debug!("Allow exact process pattern: {:?} from {:?}", token, path);
                            entries.push(AllowEntry::ProcessNameExact(pattern));
                        }
                    }
                }
            }
        }

        if !entries.is_empty() {
            debug!("Loaded {} allow entries from {:?}", entries.len(), path);
        }

        Self {
            entries,
            process_cache: Mutex::new(ProcessCache::default()),
        }
    }

    /// Whether this list has no entries at all.
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// Check if a process (by PID) is allowed according to this list.
    pub fn is_allowed(&self, pid: u32) -> bool {
        if self.entries.is_empty() {
            return false;
        }

        for entry in &self.entries {
            match entry {
                AllowEntry::ProcessName(pattern) => {
                    if self.is_process_or_child_by_name(pid, pattern, None) {
                        debug!("Allow PID {} as process or child matching pattern", pid);
                        return true;
                    }
                }
                AllowEntry::ProcessNameExact(pattern) => {
                    if self.is_process_or_child_by_name(pid, pattern, Some(0)) {
                        debug!("Allow PID {} by exact-process pattern match", pid);
                        return true;
                    }
                }
                AllowEntry::BinaryPath(path) => {
                    if self.is_process_or_child_by_path(pid, path) {
                        debug!("Allow PID {} as process or child of {:?}", pid, path);
                        return true;
                    }
                }
                AllowEntry::BinaryPathExact(path) => {
                    let info = self.get_process_info(pid);
                    if info.exe.as_deref() == Some(path) {
                        debug!("Allow PID {} by exact exe {:?}", pid, path);
                        return true;
                    }
                }
            }
        }

        false
    }

    // ── helpers ──────────────────────────────────────────────────────────────

    /// Look up process info, using the memoization cache.
    fn get_process_info(&self, pid: u32) -> CachedProcess {
        self.process_cache
            .lock()
            .expect("process_cache Mutex poisoned — fatal process state")
            .get_or_load(pid)
    }

    fn check_parent_chain<F>(&self, mut pid: u32, max_depth: Option<u32>, should_stop: F) -> bool
    where
        F: Fn(u32, &str, &str) -> bool,
    {
        let current_pid = std::process::id();
        let mut depth = 0u32;

        loop {
            let info = self.get_process_info(pid);
            debug!("pid={pid} name={:?} cmdline={:?}", info.comm, info.cmdline);

            if should_stop(pid, &info.comm, &info.cmdline) {
                return true;
            }

            // Enforce max depth: stop walking up the chain once we've
            // examined `max_depth` levels (inclusive of the starting pid).
            if let Some(max) = max_depth
                && depth >= max
            {
                break;
            }

            let ppid = info.ppid;

            match ppid {
                Some(0) | None => break,
                Some(parent) if parent == current_pid => break,
                Some(parent) => {
                    pid = parent;
                    depth += 1;
                }
            }
        }

        false
    }

    /// Check if the process or any ancestor (up to `max_depth`) matches the
    /// given pattern (against both comm name and full cmdline).
    ///
    /// `max_depth = None` walks the entire ancestor chain; `Some(0)` checks
    /// only the process identified by `pid` without walking ancestors.
    fn is_process_or_child_by_name(
        &self,
        pid: u32,
        pattern: &ProcessPattern,
        max_depth: Option<u32>,
    ) -> bool {
        debug!(
            "is_process_or_child_by_name pid={} pattern={:?} max_depth={:?}",
            pid, pattern, max_depth
        );

        self.check_parent_chain(pid, max_depth, |_pid, pname, cmdline| {
            pattern.matches(pname, cmdline)
        })
    }

    /// Check if the process or any of its ancestors has the given binary path
    fn is_process_or_child_by_path(&self, mut pid: u32, path: &Path) -> bool {
        loop {
            let info = self.get_process_info(pid);
            if info.exe.as_deref() == Some(path) {
                return true;
            }

            match info.ppid {
                Some(0) | None => return false,
                Some(parent) => {
                    pid = parent;
                }
            }
        }
    }
}

// ── CascadingAllowList ──────────────────────────────────────────────────────

/// A hierarchical allow list that scans `.agentallow` files in subdirectories
/// and combines them with the root-level one.
#[derive(Debug)]
pub struct CascadingAllowList {
    root: PathBuf,
    root_allow_list: AllowList,
    dir_allow_lists: HashMap<PathBuf, Option<AllowList>>,
}

impl CascadingAllowList {
    /// Load the root `.agentallow` and scan subdirectories for additional ones.
    pub fn load(root: &Path) -> Self {
        let root_allow_list = AllowList::load(root);

        let mut dir_allow_lists = HashMap::new();
        Self::scan_dir_allow_lists(root, &mut dir_allow_lists);

        Self {
            root: root.to_path_buf(),
            root_allow_list,
            dir_allow_lists,
        }
    }

    fn scan_dir_allow_lists(current: &Path, allow_lists: &mut HashMap<PathBuf, Option<AllowList>>) {
        if let Ok(entries) = std::fs::read_dir(current) {
            for entry in entries.flatten() {
                let path = entry.path();
                if path.is_dir() {
                    let agentallow = path.join(".agentallow");
                    if agentallow.exists() {
                        let allow_list = AllowList::load_from_file(&agentallow);
                        allow_lists.insert(path.clone(), Some(allow_list));
                    } else {
                        allow_lists.insert(path.clone(), None);
                    }
                    Self::scan_dir_allow_lists(&path, allow_lists);
                }
            }
        }
    }

    /// Walk up the directory tree from `path` collecting every allow list
    /// found along the way (root first, then narrower scopes).
    fn get_combined_allow_lists_for_path(&self, path: &Path) -> Vec<&AllowList> {
        let mut allow_lists = Vec::new();
        allow_lists.push(&self.root_allow_list);

        let mut current = path.to_path_buf();
        while current.starts_with(&self.root) && current != self.root() {
            if let Some(Some(allow_list)) = self.dir_allow_lists.get(&current) {
                allow_lists.push(allow_list);
            }
            if let Some(parent) = current.parent() {
                current = parent.to_path_buf();
            } else {
                break;
            }
        }

        allow_lists
    }

    fn root(&self) -> &Path {
        &self.root
    }

    /// Check if a process is allowed for a specific path by consulting all
    /// applicable allow lists (root-level and any subdirectory lists).
    pub fn is_allowed(&self, path: &Path, pid: u32) -> bool {
        let allow_lists = self.get_combined_allow_lists_for_path(path);
        for allow_list in &allow_lists {
            if allow_list.is_allowed(pid) {
                return true;
            }
        }
        false
    }

    /// Whether any allow list (root or subdirectory) has entries.
    pub fn has_any_entries(&self) -> bool {
        if !self.root_allow_list.is_empty() {
            return true;
        }
        for allow_list_opt in self.dir_allow_lists.values() {
            if let Some(allow_list) = allow_list_opt
                && !allow_list.is_empty()
            {
                return true;
            }
        }
        false
    }
}