Skip to main content

codewhale_runtime/
workspace_trust.rs

1//! Per-workspace trust list of external paths the agent may read/write
2//! without triggering a `PathEscape` error (#29).
3//!
4//! Storage: `~/.deepseek/workspace-trust.json`. The file is a JSON object
5//! mapping each workspace's canonical path to a sorted list of canonical
6//! paths the user has explicitly trusted from that workspace. Trust granted
7//! in workspace A does not apply when running from workspace B.
8//!
9//! Threat model: this is a deliberate user opt-in to a path the workspace
10//! sandbox would otherwise refuse. The only access the trust list grants is
11//! through CodeWhale's own file tools (`read_file`, `write_file`, etc.) —
12//! it does not loosen the OS sandbox profile (Seatbelt/bubblewrap) used for
13//! shell commands. Sandbox-profile expansion is tracked separately so a
14//! shell tool can opt into the same paths in a future release.
15
16use std::collections::BTreeMap;
17use std::path::{Path, PathBuf};
18
19use anyhow::{Context, Result};
20use serde::{Deserialize, Serialize};
21
22use codewhale_util::utils::write_atomic;
23
24const TRUST_FILE_NAME: &str = "workspace-trust.json";
25
26#[derive(Debug, Default, Clone, Serialize, Deserialize)]
27struct TrustFile {
28    /// Map workspace canonical path → sorted unique trusted paths.
29    #[serde(default)]
30    workspaces: BTreeMap<String, Vec<String>>,
31}
32
33/// In-memory trust list for a single workspace, snapshotted at load time.
34/// Tools consult this snapshot to decide whether an out-of-workspace path
35/// is permitted; the engine refreshes it after `/trust` mutations.
36#[derive(Debug, Default, Clone)]
37pub struct WorkspaceTrust {
38    paths: Vec<PathBuf>,
39}
40
41impl WorkspaceTrust {
42    #[must_use]
43    pub fn empty() -> Self {
44        Self { paths: Vec::new() }
45    }
46
47    /// Load the trusted-paths snapshot for `workspace` from disk. Missing or
48    /// malformed files yield an empty list rather than an error so a corrupt
49    /// trust file never wedges the TUI; the next mutation rewrites it.
50    #[must_use]
51    pub fn load_for(workspace: &Path) -> Self {
52        match trust_file_path() {
53            Some(path) => Self::load_from_file(workspace, &path),
54            None => Self::empty(),
55        }
56    }
57
58    fn load_from_file(workspace: &Path, file_path: &Path) -> Self {
59        let key = workspace_key(workspace);
60        let file = read_trust_file_at(file_path).unwrap_or_default();
61        let paths = file
62            .workspaces
63            .get(&key)
64            .cloned()
65            .unwrap_or_default()
66            .into_iter()
67            .map(PathBuf::from)
68            .collect();
69        Self { paths }
70    }
71
72    /// Return the trusted paths in canonical form.
73    #[must_use]
74    pub fn paths(&self) -> &[PathBuf] {
75        &self.paths
76    }
77
78    /// Whether the candidate is trusted: the candidate (after canonical
79    /// normalization) starts with one of the trusted prefixes. Directory
80    /// trust grants access to anything under the directory.
81    #[must_use]
82    pub fn permits(&self, candidate: &Path) -> bool {
83        let canonical = candidate
84            .canonicalize()
85            .unwrap_or_else(|_| candidate.to_path_buf());
86        self.paths
87            .iter()
88            .any(|trusted| canonical.starts_with(trusted))
89    }
90}
91
92/// Add `path` to `workspace`'s trust list and persist. Returns the canonical
93/// trusted path that was actually stored, so callers can echo it back to the
94/// user.
95pub fn add(workspace: &Path, path: &Path) -> Result<PathBuf> {
96    let trust_path = trust_file_path()
97        .context("home directory not available; cannot persist workspace trust list")?;
98    add_at(workspace, path, &trust_path)
99}
100
101fn add_at(workspace: &Path, path: &Path, trust_path: &Path) -> Result<PathBuf> {
102    let canonical = canonicalize_or_keep(path);
103    let key = workspace_key(workspace);
104    let mut file = read_trust_file_at(trust_path).unwrap_or_default();
105    let entry = file.workspaces.entry(key).or_default();
106    let stored = canonical.to_string_lossy().to_string();
107    if !entry.iter().any(|p| p == &stored) {
108        entry.push(stored.clone());
109        entry.sort();
110        entry.dedup();
111    }
112    write_trust_file_at(&file, trust_path)?;
113    Ok(canonical)
114}
115
116/// Remove `path` from `workspace`'s trust list. Returns true when an entry
117/// was actually removed.
118pub fn remove(workspace: &Path, path: &Path) -> Result<bool> {
119    let Some(trust_path) = trust_file_path() else {
120        return Ok(false);
121    };
122    remove_at(workspace, path, &trust_path)
123}
124
125fn remove_at(workspace: &Path, path: &Path, trust_path: &Path) -> Result<bool> {
126    let canonical = canonicalize_or_keep(path);
127    let key = workspace_key(workspace);
128    let mut file = read_trust_file_at(trust_path).unwrap_or_default();
129    let stored = canonical.to_string_lossy().to_string();
130    let removed = match file.workspaces.get_mut(&key) {
131        Some(entry) => {
132            let len_before = entry.len();
133            entry.retain(|p| p != &stored);
134            let changed = entry.len() != len_before;
135            if entry.is_empty() {
136                file.workspaces.remove(&key);
137            }
138            changed
139        }
140        None => false,
141    };
142    if removed {
143        write_trust_file_at(&file, trust_path)?;
144    }
145    Ok(removed)
146}
147
148fn workspace_key(workspace: &Path) -> String {
149    canonicalize_or_keep(workspace)
150        .to_string_lossy()
151        .into_owned()
152}
153
154fn canonicalize_or_keep(path: &Path) -> PathBuf {
155    path.canonicalize().unwrap_or_else(|_| path.to_path_buf())
156}
157
158fn trust_file_path() -> Option<PathBuf> {
159    codewhale_config::ensure_state_dir(".")
160        .ok()
161        .map(|dir| dir.join(TRUST_FILE_NAME))
162}
163
164fn read_trust_file_at(path: &Path) -> Result<TrustFile> {
165    if !path.exists() {
166        return Ok(TrustFile::default());
167    }
168    let raw = std::fs::read_to_string(path).with_context(|| format!("read {}", path.display()))?;
169    serde_json::from_str(&raw).with_context(|| format!("parse {}", path.display()))
170}
171
172fn write_trust_file_at(file: &TrustFile, path: &Path) -> Result<()> {
173    if let Some(parent) = path.parent() {
174        std::fs::create_dir_all(parent)
175            .with_context(|| format!("create dir {}", parent.display()))?;
176    }
177    let json = serde_json::to_string_pretty(file).context("serialize trust file")?;
178    write_atomic(path, json.as_bytes()).with_context(|| format!("write {}", path.display()))?;
179    Ok(())
180}
181
182#[cfg(test)]
183mod tests {
184    use super::*;
185    use tempfile::TempDir;
186
187    /// Set up an isolated fake `~/.deepseek/workspace-trust.json` location.
188    /// Returns the tmpdir (kept alive for the test) plus the explicit trust
189    /// file path passed to the `*_at` helpers — avoids touching `$HOME` so
190    /// tests run safely in parallel.
191    fn isolated_trust_path() -> (TempDir, PathBuf) {
192        let tmp = TempDir::new().expect("tempdir");
193        let trust_path = tmp.path().join(".deepseek").join("workspace-trust.json");
194        (tmp, trust_path)
195    }
196
197    #[test]
198    fn empty_trust_for_unknown_workspace() {
199        let (tmp, trust_path) = isolated_trust_path();
200        let workspace = tmp.path().join("ws");
201        std::fs::create_dir_all(&workspace).unwrap();
202        let trust = WorkspaceTrust::load_from_file(&workspace, &trust_path);
203        assert!(trust.paths().is_empty());
204        assert!(!trust.permits(Path::new("/anywhere")));
205    }
206
207    #[test]
208    fn add_persists_and_load_returns_path() {
209        let (tmp, trust_path) = isolated_trust_path();
210        let workspace = tmp.path().join("ws");
211        let other = tmp.path().join("data/notes");
212        std::fs::create_dir_all(&workspace).unwrap();
213        std::fs::create_dir_all(&other).unwrap();
214
215        let stored = add_at(&workspace, &other, &trust_path).expect("add");
216        // On macOS, /var/folders is a symlink to /private/var/folders so the
217        // canonical form may live under that prefix. Compare using
218        // canonicalize on both ends.
219        let canonical_other = other.canonicalize().unwrap_or(other.clone());
220        assert_eq!(stored, canonical_other);
221
222        let trust = WorkspaceTrust::load_from_file(&workspace, &trust_path);
223        assert_eq!(trust.paths().len(), 1);
224        // Create the file so canonicalize resolves through any symlinks; the
225        // stored trust path uses the canonical form.
226        let inner = other.join("file.md");
227        std::fs::write(&inner, "x").unwrap();
228        assert!(trust.permits(&inner));
229        assert!(!trust.permits(Path::new("/etc/passwd")));
230    }
231
232    #[test]
233    fn add_is_idempotent() {
234        let (tmp, trust_path) = isolated_trust_path();
235        let workspace = tmp.path().join("ws");
236        let other = tmp.path().join("data/notes");
237        std::fs::create_dir_all(&workspace).unwrap();
238        std::fs::create_dir_all(&other).unwrap();
239
240        let _ = add_at(&workspace, &other, &trust_path).unwrap();
241        let _ = add_at(&workspace, &other, &trust_path).unwrap();
242        let trust = WorkspaceTrust::load_from_file(&workspace, &trust_path);
243        assert_eq!(trust.paths().len(), 1);
244    }
245
246    #[test]
247    fn trust_is_workspace_scoped() {
248        let (tmp, trust_path) = isolated_trust_path();
249        let ws_a = tmp.path().join("ws-a");
250        let ws_b = tmp.path().join("ws-b");
251        let other = tmp.path().join("data/notes");
252        std::fs::create_dir_all(&ws_a).unwrap();
253        std::fs::create_dir_all(&ws_b).unwrap();
254        std::fs::create_dir_all(&other).unwrap();
255
256        add_at(&ws_a, &other, &trust_path).unwrap();
257        assert_eq!(
258            WorkspaceTrust::load_from_file(&ws_a, &trust_path)
259                .paths()
260                .len(),
261            1
262        );
263        assert_eq!(
264            WorkspaceTrust::load_from_file(&ws_b, &trust_path)
265                .paths()
266                .len(),
267            0
268        );
269    }
270
271    #[test]
272    fn remove_deletes_path() {
273        let (tmp, trust_path) = isolated_trust_path();
274        let workspace = tmp.path().join("ws");
275        let other = tmp.path().join("data/notes");
276        std::fs::create_dir_all(&workspace).unwrap();
277        std::fs::create_dir_all(&other).unwrap();
278
279        add_at(&workspace, &other, &trust_path).unwrap();
280        let removed = remove_at(&workspace, &other, &trust_path).unwrap();
281        assert!(removed);
282
283        let trust = WorkspaceTrust::load_from_file(&workspace, &trust_path);
284        assert!(trust.paths().is_empty());
285    }
286}