Skip to main content

lean_ctx/core/
workspace_trust.rs

1//! Workspace trust for project-local `.lean-ctx.toml` overrides (GH security
2//! audit, finding 4).
3//!
4//! A cloned repository ships its own `.lean-ctx.toml`. Through
5//! `Config::merge_local` that file can raise
6//! *security-sensitive* settings — replace the shell allowlist, widen the path
7//! jail (`allow_paths` / `extra_roots`), repoint the proxy upstream, define
8//! command aliases. Opening an untrusted clone with an agent would let the repo
9//! silently weaken lean-ctx's own boundaries before the user has read a line.
10//!
11//! Mirroring VS Code's *Workspace Trust*, project-local security-sensitive
12//! overrides are honoured only for a workspace the user has explicitly trusted
13//! via `lean-ctx trust`. Trust is pinned to BOTH the workspace path AND a content
14//! hash of its `.lean-ctx.toml`: editing the file after trust invalidates the
15//! pin, so a "trust once, modify later" change can never take effect silently.
16//!
17//! Comfort-only overrides (compression level, theme, memory tuning) are never
18//! gated — only the sensitive set listed in [`crate::core::config`] is withheld
19//! when the workspace is untrusted.
20
21use std::path::{Path, PathBuf};
22
23use serde::{Deserialize, Serialize};
24
25/// Env override: trust every workspace this process sees. Intended for headless,
26/// already-trusted environments (CI / fleet) where no human can answer a prompt.
27/// Accepts `1` / `true`.
28const TRUST_ALL_ENV: &str = "LEAN_CTX_TRUST_WORKSPACE";
29
30/// Env override: comma-separated absolute roots to treat as trusted. Intended
31/// for fleet provisioning where the set of trusted repos is managed centrally.
32const TRUSTED_ROOTS_ENV: &str = "LEAN_CTX_TRUSTED_ROOTS";
33
34const FILE_NAME: &str = "workspace-trust.toml";
35
36/// One trusted workspace: its canonical path plus the content hash of the
37/// `.lean-ctx.toml` reviewed at trust time (empty when no local file existed).
38#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
39pub struct TrustedWorkspace {
40    /// Canonicalized absolute workspace root.
41    pub path: String,
42    /// blake3 hash of `.lean-ctx.toml` at trust time; empty = none present then.
43    pub config_hash: String,
44    /// When it was trusted (RFC 3339) — for the audit conversation, not enforcement.
45    pub added_at: String,
46}
47
48/// The pinned trust set, persisted as `workspace-trust.toml`.
49#[derive(Debug, Clone, Default, Serialize, Deserialize)]
50pub struct TrustStore {
51    #[serde(default, rename = "workspace", skip_serializing_if = "Vec::is_empty")]
52    pub workspaces: Vec<TrustedWorkspace>,
53}
54
55/// Location of the trust file (`<config_dir>/workspace-trust.toml`).
56pub fn store_path() -> Result<PathBuf, String> {
57    Ok(crate::core::paths::config_dir()?.join(FILE_NAME))
58}
59
60/// Load the pinned set. A missing file is the common case and yields an empty
61/// store, never an error.
62pub fn load() -> Result<TrustStore, String> {
63    let path = store_path()?;
64    if !path.exists() {
65        return Ok(TrustStore::default());
66    }
67    let text =
68        std::fs::read_to_string(&path).map_err(|e| format!("read {}: {e}", path.display()))?;
69    toml::from_str(&text).map_err(|e| format!("parse {}: {e}", path.display()))
70}
71
72/// Persist the pinned set (creating the config dir if needed), owner-only.
73pub fn save(store: &TrustStore) -> Result<(), String> {
74    let path = store_path()?;
75    if let Some(parent) = path.parent() {
76        std::fs::create_dir_all(parent).map_err(|e| format!("mkdir config: {e}"))?;
77    }
78    let text = toml::to_string_pretty(store).map_err(|e| format!("serialize trust store: {e}"))?;
79    std::fs::write(&path, &text).map_err(|e| format!("write {}: {e}", path.display()))?;
80    restrict_permissions(&path);
81    Ok(())
82}
83
84#[cfg(unix)]
85fn restrict_permissions(path: &Path) {
86    use std::os::unix::fs::PermissionsExt;
87    let _ = std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o600));
88}
89
90#[cfg(not(unix))]
91fn restrict_permissions(_path: &Path) {}
92
93/// Canonicalize a root for stable comparison. Falls back to the lexical path
94/// when the dir can't be canonicalized (e.g. it no longer exists).
95fn canonical(root: &Path) -> String {
96    std::fs::canonicalize(root)
97        .unwrap_or_else(|_| root.to_path_buf())
98        .to_string_lossy()
99        .to_string()
100}
101
102/// Content hash of a workspace's `.lean-ctx.toml`, or empty when absent. This is
103/// the value pinned at trust time and re-checked on every load.
104#[must_use]
105pub fn config_hash_for(root: &Path) -> String {
106    let local = crate::core::config::Config::local_path(&root.to_string_lossy());
107    std::fs::read_to_string(&local)
108        .ok()
109        .map(|c| crate::core::hasher::hash_str(&c))
110        .unwrap_or_default()
111}
112
113fn now() -> String {
114    chrono::Utc::now().to_rfc3339()
115}
116
117fn env_trusted_roots() -> Vec<String> {
118    std::env::var(TRUSTED_ROOTS_ENV)
119        .ok()
120        .into_iter()
121        .flat_map(|v| {
122            v.split(',')
123                .map(str::trim)
124                .filter(|s| !s.is_empty())
125                .map(|s| canonical(Path::new(s)))
126                .collect::<Vec<_>>()
127        })
128        .collect()
129}
130
131fn trust_all_env() -> bool {
132    matches!(
133        std::env::var(TRUST_ALL_ENV).ok().as_deref(),
134        Some("1" | "true")
135    )
136}
137
138/// Whether `root`'s project-local security-sensitive overrides may be applied,
139/// given the CURRENT content hash of its `.lean-ctx.toml`.
140///
141/// True when the trust-all env is set, the root is in the env root list, or the
142/// store pins this exact `(path, config_hash)` pair. A stored entry whose hash no
143/// longer matches `config_hash` is treated as untrusted — the file changed since
144/// it was reviewed, so re-trust is required.
145#[must_use]
146pub fn is_trusted_for(root: &Path, config_hash: &str) -> bool {
147    if trust_all_env() {
148        return true;
149    }
150    let canon = canonical(root);
151    if canon.is_empty() {
152        return false;
153    }
154    if env_trusted_roots().contains(&canon) {
155        return true;
156    }
157    load().is_ok_and(|s| {
158        s.workspaces
159            .iter()
160            .any(|w| w.path == canon && w.config_hash == config_hash)
161    })
162}
163
164/// Whether `root` is trusted at its current `.lean-ctx.toml` content. Reads the
165/// file to compute the hash; prefer [`is_trusted_for`] when the caller already
166/// holds it (e.g. config load).
167#[must_use]
168pub fn is_trusted(root: &Path) -> bool {
169    is_trusted_for(root, &config_hash_for(root))
170}
171
172/// Trust `root` at its current `.lean-ctx.toml` content. Re-trusting an already
173/// trusted path refreshes its pinned hash (and timestamp). Returns the entry.
174pub fn trust(root: &Path) -> Result<TrustedWorkspace, String> {
175    let canon = canonical(root);
176    if canon.is_empty() {
177        return Err("cannot resolve workspace path".into());
178    }
179    let hash = config_hash_for(root);
180    let mut store = load()?;
181    if let Some(existing) = store.workspaces.iter_mut().find(|w| w.path == canon) {
182        existing.config_hash = hash;
183        existing.added_at = now();
184        let updated = existing.clone();
185        save(&store)?;
186        return Ok(updated);
187    }
188    let entry = TrustedWorkspace {
189        path: canon,
190        config_hash: hash,
191        added_at: now(),
192    };
193    store.workspaces.push(entry.clone());
194    save(&store)?;
195    Ok(entry)
196}
197
198/// Remove `root` from the trust store. Returns `true` when an entry was removed.
199pub fn untrust(root: &Path) -> Result<bool, String> {
200    let canon = canonical(root);
201    let mut store = load()?;
202    let before = store.workspaces.len();
203    store.workspaces.retain(|w| w.path != canon);
204    let removed = store.workspaces.len() != before;
205    if removed {
206        save(&store)?;
207    }
208    Ok(removed)
209}
210
211/// All trusted workspaces from the persisted store (env overrides excluded —
212/// those are provenance-free and shown separately by callers when relevant).
213#[must_use]
214pub fn list() -> Vec<TrustedWorkspace> {
215    load().map(|s| s.workspaces).unwrap_or_default()
216}
217
218/// Actionable, single-paragraph explanation for the MCP tool surfaces (#540):
219/// when the active project's `.lean-ctx.toml` carries SECURITY-sensitive
220/// overrides that are being withheld because the workspace is untrusted, name the
221/// ignored keys and the two ways to make them take effect. `None` when the
222/// workspace is trusted, has no project root, has no local config, or its local
223/// config carries no sensitive overrides.
224///
225/// `Config::merge_local` already logs the identical fact via `tracing::warn`, but
226/// that goes to stderr — invisible over an MCP/stdio transport. So a blocked
227/// command (`shell_allowlist*`) or read (`allow_paths`) otherwise gives the agent
228/// no clue why an edit "did nothing"; this surfaces it inside the error itself.
229#[must_use]
230pub fn untrusted_override_notice() -> Option<String> {
231    let root = crate::core::config::Config::find_project_root()?;
232    untrusted_override_notice_for(Path::new(&root))
233}
234
235/// Root-parameterized core of [`untrusted_override_notice`] (the public wrapper
236/// resolves the active project root; this stays testable with an explicit path).
237fn untrusted_override_notice_for(root: &Path) -> Option<String> {
238    let local = crate::core::config::Config::local_path(&root.to_string_lossy());
239    let toml = std::fs::read_to_string(&local).ok()?;
240    let withheld = crate::core::config::local_sensitive_overrides(&toml);
241    if withheld.is_empty() || is_trusted(root) {
242        return None;
243    }
244    let cfg_path = crate::core::config::Config::path().map_or_else(
245        || "the global config".to_string(),
246        |p| p.display().to_string(),
247    );
248    Some(format!(
249        "This workspace's .lean-ctx.toml sets security-sensitive override(s) [{keys}] that \
250         lean-ctx IGNORES because the workspace is untrusted — the usual reason such an edit \
251         appears to do nothing. To apply them, review the file then run `lean-ctx trust` in \
252         {root}, or move the key(s) into the global config ({cfg_path}), which is never \
253         trust-gated.",
254        keys = withheld.join(", "),
255        root = root.display(),
256    ))
257}
258
259#[cfg(test)]
260mod tests {
261    use super::*;
262    use crate::core::data_dir::isolated_data_dir;
263
264    #[test]
265    fn untrusted_root_is_not_trusted() {
266        let _iso = isolated_data_dir();
267        let dir = tempfile::tempdir().unwrap();
268        assert!(!is_trusted(dir.path()));
269    }
270
271    #[test]
272    fn trust_then_is_trusted_then_untrust() {
273        let _iso = isolated_data_dir();
274        let dir = tempfile::tempdir().unwrap();
275        assert!(!is_trusted(dir.path()));
276        trust(dir.path()).unwrap();
277        assert!(is_trusted(dir.path()));
278        assert!(untrust(dir.path()).unwrap());
279        assert!(!is_trusted(dir.path()));
280    }
281
282    #[test]
283    fn editing_local_config_after_trust_invalidates_pin() {
284        let _iso = isolated_data_dir();
285        let dir = tempfile::tempdir().unwrap();
286        let local = dir.path().join(".lean-ctx.toml");
287        std::fs::write(&local, "theme = \"a\"\n").unwrap();
288        trust(dir.path()).unwrap();
289        assert!(is_trusted(dir.path()));
290        // Content changes → pinned hash no longer matches → untrusted again.
291        std::fs::write(&local, "theme = \"b\"\n").unwrap();
292        assert!(!is_trusted(dir.path()));
293    }
294
295    #[test]
296    fn env_trust_all_overrides_store() {
297        let _iso = isolated_data_dir();
298        let dir = tempfile::tempdir().unwrap();
299        crate::test_env::set_var(TRUST_ALL_ENV, "1");
300        assert!(is_trusted(dir.path()));
301        crate::test_env::remove_var(TRUST_ALL_ENV);
302        assert!(!is_trusted(dir.path()));
303    }
304
305    #[test]
306    fn env_trusted_roots_lists_canonical_path() {
307        let _iso = isolated_data_dir();
308        let dir = tempfile::tempdir().unwrap();
309        let canon = canonical(dir.path());
310        crate::test_env::set_var(TRUSTED_ROOTS_ENV, &canon);
311        assert!(is_trusted(dir.path()));
312        crate::test_env::remove_var(TRUSTED_ROOTS_ENV);
313    }
314
315    #[test]
316    fn retrust_after_edit_repins_new_hash() {
317        let _iso = isolated_data_dir();
318        let dir = tempfile::tempdir().unwrap();
319        let local = dir.path().join(".lean-ctx.toml");
320        std::fs::write(&local, "theme = \"a\"\n").unwrap();
321        trust(dir.path()).unwrap();
322        std::fs::write(&local, "theme = \"b\"\n").unwrap();
323        assert!(!is_trusted(dir.path()));
324        trust(dir.path()).unwrap();
325        assert!(is_trusted(dir.path()));
326    }
327
328    // #540: the notice is the visible counterpart to the stderr-only merge_local
329    // warning — an untrusted workspace with sensitive overrides must explain the
330    // gate (and the `lean-ctx trust` remedy) right where the tool blocks.
331    #[test]
332    fn untrusted_sensitive_override_yields_actionable_notice() {
333        let _iso = isolated_data_dir();
334        let dir = tempfile::tempdir().unwrap();
335        std::fs::write(
336            dir.path().join(".lean-ctx.toml"),
337            "allow_paths = [\"/srv/data\"]\nshell_allowlist_extra = [\"glab\"]\n",
338        )
339        .unwrap();
340        let notice = untrusted_override_notice_for(dir.path()).expect("untrusted → notice");
341        assert!(notice.contains("allow_paths"), "{notice}");
342        assert!(notice.contains("shell_allowlist_extra"), "{notice}");
343        assert!(notice.contains("lean-ctx trust"), "{notice}");
344    }
345
346    #[test]
347    fn trusted_workspace_yields_no_notice() {
348        let _iso = isolated_data_dir();
349        let dir = tempfile::tempdir().unwrap();
350        std::fs::write(
351            dir.path().join(".lean-ctx.toml"),
352            "allow_paths = [\"/srv/data\"]\n",
353        )
354        .unwrap();
355        trust(dir.path()).unwrap();
356        assert!(untrusted_override_notice_for(dir.path()).is_none());
357    }
358
359    #[test]
360    fn no_local_config_yields_no_notice() {
361        let _iso = isolated_data_dir();
362        let dir = tempfile::tempdir().unwrap();
363        assert!(untrusted_override_notice_for(dir.path()).is_none());
364    }
365
366    #[test]
367    fn comfort_only_override_yields_no_notice() {
368        let _iso = isolated_data_dir();
369        let dir = tempfile::tempdir().unwrap();
370        // Comfort knobs are never gated, so they never trigger the trust notice.
371        std::fs::write(dir.path().join(".lean-ctx.toml"), "theme = \"dark\"\n").unwrap();
372        assert!(untrusted_override_notice_for(dir.path()).is_none());
373    }
374}