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
//! Runtime-mutable filesystem grants for [`TerminalTool`].
//!
//! A [`TerminalGrants`] handle is cheap to clone and shares one store with
//! every clone — a host keeps the handle it got from
//! [`TerminalTool::grants`], adds grants when a workspace request is
//! approved, and every sandbox launched afterwards carries them.
//!
//! A command or shell snapshots the grants once, when it launches: a
//! command already running (and a persistent shell already running) keeps
//! the grants it started with — see [`TerminalGrants::add_readable`].
use std::path::PathBuf;
use std::sync::Arc;
use arc_swap::ArcSwap;
/// The readable and writable path grants one terminal sandbox launches
/// with. Immutable once handed out: snapshots are replaced, never edited.
#[derive(Debug, Clone, Default)]
pub struct GrantSet {
/// Paths sandboxed commands may read but not write.
pub readable: Vec<PathBuf>,
/// Paths sandboxed commands may write to (and therefore also read).
pub writable: Vec<PathBuf>,
/// Absolute paths sandboxed commands may not read, on top of heel's
/// credential table. Everything else on the host stays readable.
pub read_denied: Vec<PathBuf>,
/// The access to the common git directory of a writable linked worktree.
pub git_dir: GitDirAccess,
}
/// The access a terminal has to the common git directory of a linked
/// worktree it may write.
///
/// That directory is the main repository's, so only the session's own
/// terminal writes it; a subagent's terminal reads it, and commits only
/// through a commit grant's repository of its own.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum GitDirAccess {
/// Read, write and execute: the session's own terminal.
#[default]
ReadWrite,
/// Read only: a subagent's terminal.
ReadOnly,
}
impl GitDirAccess {
/// The access to the common git directory of a worktree the terminal
/// can write.
#[must_use]
pub fn writable_worktree_access(self) -> heel::Access {
match self {
Self::ReadWrite => heel::Access::WRITE | heel::Access::EXEC,
Self::ReadOnly => heel::Access::READ,
}
}
}
#[derive(Debug, Clone)]
enum GrantsInner {
/// The terminal's own store.
Own(Arc<ArcSwap<GrantSet>>),
/// A subagent's view: its parent's readable side plus its own grants.
/// Readable grants added to the parent later reach the subagent;
/// writable grants added to the parent never become writable here.
Derived {
parent: Box<TerminalGrants>,
grants: Arc<ArcSwap<GrantSet>>,
},
}
/// Shared, runtime-mutable grants for a [`crate::TerminalTool`].
///
/// Cloning the handle keeps the same underlying store: every clone,
/// including the clones a `TerminalTool` makes for its own bookkeeping and
/// for subagents (see [`Self::derived`]), sees grants added through any
/// handle.
#[derive(Debug, Clone)]
pub struct TerminalGrants {
inner: GrantsInner,
}
impl Default for TerminalGrants {
fn default() -> Self {
Self::new()
}
}
impl TerminalGrants {
/// An empty grant store.
#[must_use]
pub fn new() -> Self {
Self {
inner: GrantsInner::Own(Arc::new(ArcSwap::from_pointee(GrantSet::default()))),
}
}
/// A subagent's view of a parent terminal's grants: the snapshot merges
/// the parent's readable list (kept live) with `own`'s extra readable
/// and writable lists. Writable grants on the parent are never
/// writable through this view.
#[must_use]
pub fn derived(
parent: &Self,
extra_readable: Vec<PathBuf>,
own_writable: Vec<PathBuf>,
) -> Self {
Self {
inner: GrantsInner::Derived {
parent: Box::new(parent.clone()),
grants: Arc::new(ArcSwap::from_pointee(GrantSet {
readable: extra_readable,
writable: own_writable,
read_denied: Vec::new(),
git_dir: GitDirAccess::ReadOnly,
})),
},
}
}
const fn own_store(&self) -> &Arc<ArcSwap<GrantSet>> {
match &self.inner {
GrantsInner::Own(store) | GrantsInner::Derived { grants: store, .. } => store,
}
}
fn update_own(&self, mutate: impl Fn(&mut GrantSet)) {
let store = self.own_store();
store.rcu(|current| {
let mut next = (**current).clone();
mutate(&mut next);
next
});
}
/// Adds `path` as a readable grant. Commands and shells launched after
/// this call read it; commands and persistent shells already running
/// keep the grants they launched with. Dedupes; a path already
/// writable stays writable and is not also listed readable.
pub fn add_readable(&self, path: impl Into<PathBuf>) {
let path = path.into();
self.update_own(|set| {
if !set.writable.contains(&path) && !set.readable.contains(&path) {
set.readable.push(path.clone());
}
});
}
/// Adds `path` as a writable grant, with the same "from the next
/// launch on" semantics as [`Self::add_readable`]. A writable grant
/// subsumes a readable one for the same path.
pub fn add_writable(&self, path: impl Into<PathBuf>) {
let path = path.into();
self.update_own(|set| {
set.readable.retain(|entry| *entry != path);
if !set.writable.contains(&path) {
set.writable.push(path.clone());
}
});
}
/// Denies sandboxed commands reading `path` and everything beneath it,
/// beyond heel's credential table, whatever else is granted. The path
/// must be absolute. Same "from the next launch on" semantics as
/// [`Self::add_readable`]; a subagent's view also denies what its
/// parent denies.
pub fn deny_read(&self, path: impl Into<PathBuf>) {
let path = path.into();
self.update_own(|set| {
if !set.read_denied.contains(&path) {
set.read_denied.push(path.clone());
}
});
}
/// Takes back the readable grant of `path`, with the same "from the next
/// launch on" semantics as [`Self::add_readable`]: commands and shells
/// already running keep the grants they launched with. A writable grant
/// of the same path is a grant of its own and stays. On a subagent's
/// view this removes only its own grant, never one its parent holds.
pub fn remove_readable(&self, path: impl Into<PathBuf>) {
let path = path.into();
self.update_own(|set| set.readable.retain(|entry| *entry != path));
}
/// Takes back the writable grant of `path`, with the same "from the next
/// launch on" semantics as [`Self::add_readable`]. Nothing of it is left
/// readable: read access stays only through a readable grant of the
/// path.
pub fn remove_writable(&self, path: impl Into<PathBuf>) {
let path = path.into();
self.update_own(|set| set.writable.retain(|entry| *entry != path));
}
/// One consistent view of the grants, taken at launch time.
///
/// For a derived handle this merges the parent's current readable
/// list with this handle's own lists, so readable grants added to the
/// parent between the derivation and this snapshot are included.
#[must_use]
pub fn snapshot(&self) -> Arc<GrantSet> {
match &self.inner {
GrantsInner::Own(store) => store.load_full(),
GrantsInner::Derived {
parent,
grants: own,
} => {
let parent_set = parent.snapshot();
let own_set = own.load_full();
Arc::new(GrantSet {
readable: parent_set
.readable
.iter()
.cloned()
.chain(own_set.readable.iter().cloned())
.collect(),
writable: own_set.writable.clone(),
read_denied: parent_set
.read_denied
.iter()
.cloned()
.chain(own_set.read_denied.iter().cloned())
.collect(),
git_dir: GitDirAccess::ReadOnly,
})
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn removing_a_grant_takes_it_back_from_the_next_snapshot() {
let grants = TerminalGrants::new();
grants.add_readable("/data/readable");
grants.add_writable("/data/writable");
let launched = grants.snapshot();
grants.remove_readable("/data/readable");
grants.remove_writable("/data/writable");
let next = grants.snapshot();
assert!(next.readable.is_empty(), "{next:?}");
assert!(next.writable.is_empty(), "{next:?}");
assert_eq!(launched.readable, [PathBuf::from("/data/readable")]);
assert_eq!(launched.writable, [PathBuf::from("/data/writable")]);
}
#[test]
fn removing_one_kind_of_grant_leaves_the_other() {
let grants = TerminalGrants::new();
grants.add_writable("/data/shared");
grants.remove_readable("/data/shared");
assert_eq!(grants.snapshot().writable, [PathBuf::from("/data/shared")]);
grants.add_readable("/data/other");
grants.remove_writable("/data/other");
assert_eq!(grants.snapshot().readable, [PathBuf::from("/data/other")]);
}
#[test]
fn a_subagent_removes_only_its_own_grants() {
let parent = TerminalGrants::new();
parent.add_readable("/data/parent");
let child = TerminalGrants::derived(&parent, vec!["/data/child".into()], Vec::new());
child.remove_readable("/data/parent");
child.remove_readable("/data/child");
assert_eq!(child.snapshot().readable, [PathBuf::from("/data/parent")]);
assert_eq!(parent.snapshot().readable, [PathBuf::from("/data/parent")]);
}
}