base/interface/permission/paths.rs
1//! The judgments the engine makes on its own account, outside any tool's
2//! facts, about how far a scope reaches: what a rewind may restore, where a
3//! plugin command or a host-started delegate may write, and whether a
4//! narrow-only grant stays narrow. The permission engine implements them; a
5//! build without it has none, and the engine then writes nowhere and keeps no
6//! narrow-only grant on them.
7
8use async_trait::async_trait;
9use std::path::{Path, PathBuf};
10use std::sync::Arc;
11
12use super::{Grant, GrantError};
13use crate::interface::tool::ToolContext;
14use crate::paths::SensitivePaths;
15use crate::permission::PermissionRule;
16
17#[async_trait]
18pub trait PathGuard: Send + Sync + std::fmt::Debug {
19 /// Whether a rewind may write `target` back, judged from `ctx`:
20 /// `Ok(true)` when it lies outside the session's writable roots, `Err`
21 /// with why when it must not be written at all.
22 async fn restore_check(&self, ctx: &ToolContext, target: &Path) -> Result<bool, String>;
23 /// A plugin command's `writes` globs below `root`: the scope kept, and the
24 /// globs left out because they reach beyond the project.
25 fn command_writes(&self, globs: &[String], root: &Path) -> (Arc<dyn WriteScope>, Vec<String>);
26 /// A host-started delegate's `writes`, every glob kept, a relative one
27 /// anchored at `cwd`.
28 fn host_writes(&self, globs: &[String], cwd: &Path) -> Arc<dyn WriteScope>;
29 /// Whether writing `path` from `cwd` reaches configuration under any of
30 /// its spellings.
31 fn reaches_configuration(&self, path: &str, cwd: &Path, paths: &SensitivePaths) -> bool;
32 /// `Err(TooBroad)` for a narrow-only grant that reaches as far as the
33 /// bare tool, or out of the directory it is narrow within.
34 fn admits(&self, grant: &Grant) -> Result<(), GrantError>;
35}
36
37/// The files one run may write.
38pub trait WriteScope: Send + Sync + std::fmt::Debug {
39 /// Whether writing `path`, as a tool resolves it from `cwd`, stays inside.
40 fn covers(&self, path: &str, cwd: &Path) -> bool;
41 /// An `Edit(...)` allow per glob: what a delegated grant takes.
42 fn grants(&self) -> Vec<PermissionRule>;
43 /// The directories the globs lie under, each inside `root`.
44 fn fixed_prefixes_within(&self, root: &Path) -> Vec<PathBuf>;
45}
46
47/// The scope where no [`PathGuard`] can judge one: it covers nothing.
48#[derive(Debug, Default)]
49pub struct NoWrites;
50
51impl WriteScope for NoWrites {
52 fn covers(&self, _: &str, _: &Path) -> bool {
53 false
54 }
55 fn grants(&self) -> Vec<PermissionRule> {
56 Vec::new()
57 }
58 fn fixed_prefixes_within(&self, _: &Path) -> Vec<PathBuf> {
59 Vec::new()
60 }
61}