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
//! 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>,
}
#[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,
})),
},
}
}
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());
}
});
}
/// 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(),
})
}
}
}
}