lean-ctx 3.11.1

LeanCTX Engine: the open-source runtime for the Context Gateway for AI Systems. CLI, MCP server, and supported proxy integrations.
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
// SPDX-License-Identifier: Apache-2.0
//! Path-jail scope: which directories the jail treats as the user's own code.
//!
//! The jail used to admit only the session's project root (plus allow-lists).
//! Anyone working on several repositories at once hit "path escapes project
//! root" on every cross-project read, and the suggested fixes — one allow entry
//! per directory, or env vars that need an MCP restart — did not scale.
//!
//! - `home` (**default**): every path below the user's home directory is
//!   admitted, except the protected zones where credentials and other
//!   programs' private state live (every top-level dot entry such as `~/.ssh`,
//!   `~/.aws`, `~/.config`, `~/.zshrc` or other agents' `~/.claude`, plus
//!   `~/Library` on macOS, `~/AppData` and the `NTUSER.DAT*` registry hive on
//!   Windows). Paths outside the home directory stay jailed.
//! - `project`: the classic boundary — the project root plus allow-lists.
//!
//! Explicit allow-lists (`allow_paths`, `extra_roots`, `read_only_roots`) and
//! lean-ctx's own state dir still widen either scope, so `lean-ctx allow-path
//! ~/.config/foo` reopens one protected directory without dropping the zone.
//! `path_jail = false` keeps disabling the jail entirely.
//!
//! Resolution precedence (first hit wins): `LEAN_CTX_PATH_JAIL_SCOPE` env →
//! `path_jail_scope` in the global `config.toml` → `home`. Like `path_jail` it
//! is global-only: a project-local `.lean-ctx.toml` is never merged for it.

use std::path::{Path, PathBuf};

/// Top-level home entries (besides every dot entry) holding other programs'
/// private data: keychains, browser profiles, mail, app containers (Linux
/// snaps keep each app's dot dirs — cookies, logins — under `~/snap/<app>`).
const PROTECTED_HOME_DIRS: &[&str] = &["Library", "AppData", "snap"];

/// Upper bound on remembered session roots; a long-lived daemon serving many
/// sessions keeps the most recent ones.
const MAX_SESSION_WRITE_ROOTS: usize = 64;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum PathJailScope {
    /// Everything below `$HOME` except the protected zones.
    #[default]
    Home,
    /// Only the project root plus the configured allow-lists.
    Project,
}

impl PathJailScope {
    /// Lenient parse; unknown text returns `None` so callers fall back to the default.
    pub fn parse(value: &str) -> Option<Self> {
        match value.trim().to_ascii_lowercase().as_str() {
            "home" | "user" | "workspace" => Some(Self::Home),
            "project" | "strict" | "root" => Some(Self::Project),
            _ => None,
        }
    }

    pub fn as_str(self) -> &'static str {
        match self {
            Self::Home => "home",
            Self::Project => "project",
        }
    }

    /// Active scope: env override → config → default (`home`).
    pub fn resolve() -> Self {
        if let Ok(raw) = std::env::var("LEAN_CTX_PATH_JAIL_SCOPE")
            && let Some(scope) = Self::parse(&raw)
        {
            return scope;
        }
        crate::core::config::Config::load()
            .path_jail_scope
            .as_deref()
            .and_then(Self::parse)
            .unwrap_or_default()
    }
}

/// Canonical home directory for the protected-zone checks; `None` when `$HOME`
/// is the filesystem root.
fn zone_home() -> Option<PathBuf> {
    let home = super::pathjail::canonicalize_secure(&dirs::home_dir()?);
    home.parent().is_some().then_some(home)
}

/// Home directory the `home` scope may open. `$HOME` is only an env var: in
/// containers and CI it is often `/`, `/root` or `/tmp`, where "everything
/// below home" would mean the whole system or other users' files. Require at
/// least two normal components and, on Unix, ownership by this user;
/// otherwise the jail falls back to the project boundary.
fn scope_home() -> Option<PathBuf> {
    let home = zone_home()?;
    let depth = home
        .components()
        .filter(|c| matches!(c, std::path::Component::Normal(_)))
        .count();
    if depth < 2 {
        return None;
    }
    #[cfg(unix)]
    {
        use std::os::unix::fs::MetadataExt;
        // SAFETY: geteuid has no arguments or failure state.
        if std::fs::metadata(&home).ok()?.uid() != unsafe { libc::geteuid() } {
            return None;
        }
    }
    Some(home)
}

/// The first path component below `home`, when `path` lies strictly inside it.
fn first_component_below<'a>(path: &'a Path, home: &Path) -> Option<&'a std::ffi::OsStr> {
    path.strip_prefix(home)
        .ok()?
        .components()
        .next()
        .map(std::path::Component::as_os_str)
}

/// The protected zone (e.g. `~/.ssh`) containing `path`, if any. `path` must
/// already be canonical; `home` is the canonical home directory.
fn protected_zone_in(path: &Path, home: &Path) -> Option<PathBuf> {
    let first = first_component_below(path, home)?;
    let name = first.to_string_lossy();
    // Windows keeps the user's registry hive (`NTUSER.DAT*`) directly in home.
    let protected = name.starts_with('.')
        || name.to_ascii_lowercase().starts_with("ntuser.")
        || PROTECTED_HOME_DIRS
            .iter()
            .any(|d| name.eq_ignore_ascii_case(d));
    protected.then(|| home.join(first))
}

/// True when the `home` scope admits the canonical path `base`: it lies
/// strictly below the home directory and outside every protected zone. The
/// home directory itself is not admitted — a tree walk rooted there would
/// descend into `~/Library` and friends.
pub(crate) fn home_scope_admits(base: &Path) -> bool {
    if PathJailScope::resolve() != PathJailScope::Home {
        return false;
    }
    let Some(home) = scope_home() else {
        return false;
    };
    admits_below(base, &home)
}

fn admits_below(base: &Path, home: &Path) -> bool {
    first_component_below(base, home).is_some() && protected_zone_in(base, home).is_none()
}

/// Protected zones are a deny, not only a gap in the widening: a root or allow
/// entry that merely *contains* a zone (a project rooted at `~` for a dotfiles
/// repo, an `extra_roots` entry of `~`, a host reporting `~` as its root) must
/// not open `~/.ssh`. A zone is reachable only through a root or allow entry
/// located inside it — `~/.codex/worktrees/x`, lean-ctx's own `~/.local/...`
/// state, `allow-path ~/.config/myapp`, a registered `~/.cargo/registry`.
pub(crate) fn protected_zone_permits(base: &Path, root: &Path, allow: &[PathBuf]) -> bool {
    let Some(zone) = zone_home().and_then(|home| protected_zone_in(base, &home)) else {
        return true;
    };
    (root.starts_with(&zone) && base.starts_with(root))
        || allow
            .iter()
            .any(|p| p.starts_with(&zone) && base.starts_with(p))
}

fn session_write_roots() -> &'static std::sync::Mutex<Vec<PathBuf>> {
    static ROOTS: std::sync::OnceLock<std::sync::Mutex<Vec<PathBuf>>> = std::sync::OnceLock::new();
    ROOTS.get_or_init(|| std::sync::Mutex::new(Vec::new()))
}

/// Records a session's project (or host-declared) root as writable. Called
/// for every tool call; broad roots (`/`, `~`, temp dirs) are never recorded.
pub fn register_session_root(root: &str) {
    if root.trim().is_empty() {
        return;
    }
    let path = super::pathjail::canonicalize_secure(Path::new(root));
    if crate::core::pathutil::is_broad_or_unsafe_root(&path) {
        return;
    }
    let mut roots = session_write_roots()
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner);
    if roots.iter().any(|r| r == &path) {
        return;
    }
    if roots.len() >= MAX_SESSION_WRITE_ROOTS {
        roots.remove(0);
    }
    roots.push(path);
}

/// The `home` scope opens *reads* across your projects; writes stay where they
/// were before it: the session's project, host-declared roots and explicit
/// allow entries. Otherwise a prompt-injected agent in repo A could plant
/// `~/code/B/.git/hooks/pre-commit` or `~/bin/git`. `target` is the canonical
/// write target. Returns the refusal, or `None` when the write may proceed
/// (including every path the home scope did not admit — the jail decided those).
pub(crate) fn home_scope_write_denial(target: &Path) -> Option<String> {
    if PathJailScope::resolve() != PathJailScope::Home {
        return None;
    }
    let home = scope_home()?;
    if !admits_below(target, &home) {
        return None;
    }
    let mut writable: Vec<PathBuf> = session_write_roots()
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner)
        .clone();
    writable.extend(super::pathjail::allow_paths_from_env_and_config());
    if let Ok(state) = crate::core::paths::state_dir() {
        writable.push(super::pathjail::canonicalize_secure(&state));
    }
    // CLI invocations (`lean-ctx pack`, `lean-ctx call`) have no session; the
    // directory the user ran them in is their project.
    if let Ok(cwd) = std::env::current_dir() {
        let cwd = super::pathjail::canonicalize_secure(&cwd);
        if !crate::core::pathutil::is_broad_or_unsafe_root(&cwd) {
            writable.push(cwd);
        }
    }
    if writable.iter().any(|w| target.starts_with(w)) {
        return None;
    }
    Some(format!(
        "{} is outside the active project — path_jail_scope = home opens other projects \
         for reading only. To allow writes there, the user runs in their terminal: \
         lean-ctx allow-path {} — takes effect immediately, no restart.",
        target.display(),
        suggested_allow_dir(target, Some(&home)).display()
    ))
}

/// Nearest ancestor of `path` (inclusive) carrying a project marker, below
/// `home` when it lies there. Falls back to the containing directory. This is
/// the directory `lean-ctx allow-path` should be offered for: allowing the
/// whole project beats a second rejection on the neighbouring file.
fn suggested_allow_dir(path: &Path, home: Option<&Path>) -> PathBuf {
    let start = if path.is_dir() {
        path.to_path_buf()
    } else {
        path.parent().unwrap_or(path).to_path_buf()
    };
    let mut cur = start.as_path();
    loop {
        if home.is_some_and(|h| cur == h) || cur.parent().is_none() {
            break;
        }
        if crate::core::pathutil::has_project_marker(cur) {
            return cur.to_path_buf();
        }
        match cur.parent() {
            Some(p) => cur = p,
            None => break,
        }
    }
    start
}

/// Agent-facing explanation for a path the jail rejected, naming the one
/// command the *user* runs to admit it (agent shells refuse it, see
/// `shell_allowlist::self_config`). `candidate` is the requested path, `base`
/// its canonical existing ancestor, `root` the active project.
pub(crate) fn escape_hint(candidate: &Path, base: &Path, root: &Path) -> String {
    const TAIL: &str = " — the user runs this in their own terminal; it takes effect \
                        immediately, no restart.";
    let home = zone_home();

    if home.as_deref() == Some(base) {
        return ". Your home directory itself is not a project root — it holds ~/.ssh, \
                ~/Library and other protected locations. Point the tool at a project \
                below it."
            .to_string();
    }
    if let Some(zone) = home.as_deref().and_then(|h| protected_zone_in(base, h)) {
        // Offer the narrowest directory, never the whole zone by default.
        let dir = if base.is_dir() {
            base
        } else {
            base.parent().unwrap_or(base)
        };
        return format!(
            ". {} is a protected location (credentials and other programs' private data \
             stay jailed). To allow it anyway: lean-ctx allow-path {}{TAIL}",
            zone.display(),
            dir.display()
        );
    }

    let suggestion = suggested_allow_dir(base, home.as_deref());
    let under_home = scope_home().is_some_and(|h| admits_below(base, &h));
    // Only reachable with the `project` scope: `home` admits these paths.
    if under_home {
        return format!(
            ". {} is outside the active project ({}). Allow every project in your home \
             directory: lean-ctx config set path_jail_scope home — or only this one: \
             lean-ctx allow-path {}{TAIL}",
            candidate.display(),
            root.display(),
            suggestion.display()
        );
    }
    format!(
        ". {} is outside the active project ({}) and outside your home directory. To \
         allow it: lean-ctx allow-path {}{TAIL}",
        candidate.display(),
        root.display(),
        suggestion.display()
    )
}

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

    #[test]
    fn parse_accepts_canonical_names_and_aliases() {
        assert_eq!(PathJailScope::parse("home"), Some(PathJailScope::Home));
        assert_eq!(
            PathJailScope::parse(" Project "),
            Some(PathJailScope::Project)
        );
        assert_eq!(PathJailScope::parse("strict"), Some(PathJailScope::Project));
        assert_eq!(PathJailScope::parse("anything"), None);
        assert_eq!(PathJailScope::default(), PathJailScope::Home);
        for s in [PathJailScope::Home, PathJailScope::Project] {
            assert_eq!(PathJailScope::parse(s.as_str()), Some(s));
        }
    }

    #[test]
    fn home_zone_admits_code_and_protects_secrets() {
        let home = Path::new("/home/u");
        assert!(admits_below(Path::new("/home/u/code/repo/src/a.rs"), home));
        assert!(admits_below(Path::new("/home/u/Documents/p"), home));

        for protected in [
            "/home/u/.ssh/id_ed25519",
            "/home/u/.aws/credentials",
            "/home/u/.config/gh/hosts.yml",
            "/home/u/.zshrc",
            "/home/u/.claude/projects/x/session.jsonl",
            "/home/u/Library/Keychains/login.keychain-db",
            "/home/u/AppData/Roaming/x",
            "/home/u/NTUSER.DAT",
        ] {
            assert!(!admits_below(Path::new(protected), home), "{protected}");
            assert!(protected_zone_in(Path::new(protected), home).is_some());
        }
        // The home dir itself and anything outside it are never admitted.
        assert!(!admits_below(home, home));
        assert!(!admits_below(Path::new("/etc/passwd"), home));
        assert!(!admits_below(Path::new("/home/user2/x"), home));
    }

    #[test]
    fn protected_zone_is_the_top_level_entry() {
        let home = Path::new("/home/u");
        assert_eq!(
            protected_zone_in(Path::new("/home/u/.config/gh/hosts.yml"), home),
            Some(PathBuf::from("/home/u/.config"))
        );
        // Dot entries deeper in the tree (a repo's .git) are ordinary code.
        assert_eq!(
            protected_zone_in(Path::new("/home/u/code/repo/.git/HEAD"), home),
            None
        );
    }

    /// End-to-end through the real jail with a throwaway `$HOME`: a sibling
    /// project is admitted, protected zones and the outside world are not, and
    /// the `project` scope restores the single-project boundary.
    #[cfg(all(unix, not(feature = "no-jail")))]
    #[test]
    fn jail_admits_sibling_projects_but_not_protected_zones() {
        let _iso = crate::core::data_dir::isolated_data_dir();
        let tmp = tempfile::tempdir().unwrap();
        let home = super::super::pathjail::canonicalize_secure(tmp.path()).join("home");
        let (a, b, ssh) = (home.join("code/a"), home.join("code/b"), home.join(".ssh"));
        for dir in [&a, &b, &ssh] {
            std::fs::create_dir_all(dir).unwrap();
        }
        std::fs::create_dir_all(a.join(".git")).unwrap();
        std::fs::write(b.join("lib.rs"), "x").unwrap();
        std::fs::write(ssh.join("id_ed25519"), "secret").unwrap();
        let outside = tmp.path().join("outside.txt");
        std::fs::write(&outside, "x").unwrap();

        let old_home = std::env::var_os("HOME");
        crate::test_env::set_var("HOME", &home);
        crate::test_env::remove_var("LEAN_CTX_PATH_JAIL_SCOPE");
        let jail = super::super::pathjail::jail_path;

        assert!(
            jail(&b.join("lib.rs"), &a).is_ok(),
            "sibling project under ~"
        );
        let err = jail(&ssh.join("id_ed25519"), &a).unwrap_err().to_string();
        assert!(err.contains("protected location"), "{err}");
        let err = jail(&outside, &a).unwrap_err().to_string();
        assert!(err.contains("outside your home directory"), "{err}");
        let err = jail(&home, &a).unwrap_err().to_string();
        assert!(err.contains("not a project root"), "{err}");

        // A root or allow entry that merely contains a zone does not open it
        // (dotfiles repo at ~); a root inside the zone does.
        assert!(
            jail(&ssh.join("id_ed25519"), &home).is_err(),
            "zone is a deny"
        );
        assert!(
            jail(&ssh.join("id_ed25519"), &ssh).is_ok(),
            "root inside the zone"
        );

        // Reads across projects, writes only in the session's own project.
        let write = super::super::pathjail::enforce_writable;
        let err = write(&b.join("lib.rs")).unwrap_err();
        assert!(
            err.contains("for reading only") && err.contains("allow-path"),
            "{err}"
        );
        assert!(write(&b.join(".git/hooks/pre-commit")).is_err());
        register_session_root(&b.to_string_lossy());
        assert!(write(&b.join("lib.rs")).is_ok(), "session root is writable");

        crate::test_env::set_var("LEAN_CTX_PATH_JAIL_SCOPE", "project");
        let err = jail(&b.join("lib.rs"), &a).unwrap_err().to_string();
        assert!(
            err.contains("path_jail_scope home") && err.contains("allow-path"),
            "{err}"
        );
        crate::test_env::remove_var("LEAN_CTX_PATH_JAIL_SCOPE");

        // An implausible $HOME never turns into "everything below /".
        crate::test_env::set_var("HOME", "/");
        assert!(
            jail(&b.join("lib.rs"), &a).is_err(),
            "HOME=/ falls back to the project"
        );

        match old_home {
            Some(h) => crate::test_env::set_var("HOME", h),
            None => crate::test_env::remove_var("HOME"),
        }
    }

    #[test]
    fn suggestion_prefers_the_enclosing_project() {
        let tmp = tempfile::tempdir().unwrap();
        let repo = tmp.path().join("repo");
        std::fs::create_dir_all(repo.join("src/deep")).unwrap();
        std::fs::create_dir_all(repo.join(".git")).unwrap();
        let file = repo.join("src/deep/a.rs");
        std::fs::write(&file, "x").unwrap();
        assert_eq!(suggested_allow_dir(&file, None), repo);

        let loose = tmp.path().join("loose");
        std::fs::create_dir_all(&loose).unwrap();
        assert_eq!(suggested_allow_dir(&loose, Some(tmp.path())), loose);
    }
}