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
use std::path::{Path, PathBuf};
/// Environment variable that grants a workspace's project hooks.
pub const TRUST_PROJECT_HOOKS_ENV: &str = "RHO_TRUST_PROJECT_HOOKS";
/// Environment variable that grants a workspace's project agent definitions.
pub const TRUST_PROJECT_AGENTS_ENV: &str = "RHO_TRUST_PROJECT_AGENTS";
/// Environment variable that grants a workspace's project Agent Plugins.
pub const TRUST_PROJECT_PLUGINS_ENV: &str = "RHO_TRUST_PROJECT_PLUGINS";
/// Environment variable that grants a workspace's project OpenAPI sources.
pub const TRUST_PROJECT_OPENAPI_ENV: &str = "RHO_TRUST_PROJECT_OPENAPI";
/// Whether a workspace's project-supplied files may activate.
///
/// Shared by project hooks, agents, Agent Plugins, and OpenAPI sources.
/// Each feature has its own env var; only the exact value `1` grants trust.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ProjectTrust {
Trusted,
Untrusted,
}
impl ProjectTrust {
fn from_env(value: Option<&str>) -> Self {
if value == Some("1") {
Self::Trusted
} else {
Self::Untrusted
}
}
pub const fn is_trusted(self) -> bool {
matches!(self, Self::Trusted)
}
}
/// A workspace's [`ProjectTrust`] for each feature that asks for it, as one
/// environment grants them.
///
/// Read once from the environment of whoever opens a session or starts a
/// workflow run, and kept for as long as that runs: a local `rho`'s own
/// environment, or, in the daemon, the client's (#1779). Nothing reads the
/// variables later, so what a session loaded when it opened and what it
/// loads afterwards agree.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ProjectTrusts {
pub hooks: ProjectTrust,
pub agents: ProjectTrust,
pub plugins: ProjectTrust,
pub openapi: ProjectTrust,
}
impl ProjectTrusts {
/// No trust: what an environment that sets none of the variables grants.
pub const UNTRUSTED: Self = Self {
hooks: ProjectTrust::Untrusted,
agents: ProjectTrust::Untrusted,
plugins: ProjectTrust::Untrusted,
openapi: ProjectTrust::Untrusted,
};
/// The trust the environment `read` reads grants.
pub fn read(read: impl Fn(&str) -> Option<String>) -> Self {
let trust = |name| ProjectTrust::from_env(read(name).as_deref());
Self {
hooks: trust(TRUST_PROJECT_HOOKS_ENV),
agents: trust(TRUST_PROJECT_AGENTS_ENV),
plugins: trust(TRUST_PROJECT_PLUGINS_ENV),
openapi: trust(TRUST_PROJECT_OPENAPI_ENV),
}
}
/// The variables that grant what `self` trusts and `other` does not, in
/// the order the struct lists the features.
pub fn variables_beyond(self, other: Self) -> Vec<&'static str> {
// Destructured so that a new feature cannot be left out: two trusts
// that differ always differ in a variable named here.
let Self {
hooks,
agents,
plugins,
openapi,
} = self;
[
(TRUST_PROJECT_HOOKS_ENV, hooks, other.hooks),
(TRUST_PROJECT_AGENTS_ENV, agents, other.agents),
(TRUST_PROJECT_PLUGINS_ENV, plugins, other.plugins),
(TRUST_PROJECT_OPENAPI_ENV, openapi, other.openapi),
]
.into_iter()
.filter(|(_, own, others)| {
*own == ProjectTrust::Trusted && *others != ProjectTrust::Trusted
})
.map(|(variable, _, _)| variable)
.collect()
}
/// The trust this process's environment grants, for a command that acts
/// for the terminal it runs in. A daemon session takes its client's
/// instead (see [`crate::session_environment::SessionEnvironment`]).
pub fn of_this_process() -> Self {
Self::read(|name| std::env::var(name).ok())
}
}
pub fn project_ancestor_dirs(cwd: &Path) -> Vec<PathBuf> {
let ancestors: Vec<_> = cwd.ancestors().map(Path::to_path_buf).collect();
let Some(root_index) = ancestors.iter().position(|path| path.join(".git").exists()) else {
return vec![cwd.to_path_buf()];
};
ancestors[..=root_index].iter().rev().cloned().collect()
}
#[cfg(test)]
mod tests {
use tempfile::TempDir;
use super::*;
// Covers: project ancestor dirs run from the git root down to cwd, and are
// just cwd outside a repository.
// Owner: workspace root detection
#[test]
fn ancestor_dirs_start_at_the_git_root_or_only_cwd_outside_a_repo() {
let repo = TempDir::new().unwrap();
std::fs::create_dir(repo.path().join(".git")).unwrap();
let child = repo.path().join("src/nested");
std::fs::create_dir_all(&child).unwrap();
let dirs = project_ancestor_dirs(&child);
assert_eq!(
dirs,
vec![repo.path().to_path_buf(), repo.path().join("src"), child]
);
let outside = TempDir::new().unwrap();
let child = outside.path().join("src");
std::fs::create_dir_all(&child).unwrap();
assert_eq!(
project_ancestor_dirs(&child),
vec![child],
"outside a git worktree only cwd is returned"
);
}
// Covers: each `RHO_TRUST_PROJECT_*` variable grants its own feature and
// no other, so trusting a project's agents never activates its hooks.
// Owner: workspace trust policy.
#[test]
fn each_trust_variable_grants_only_its_feature() {
use ProjectTrust::Trusted;
const UNTRUSTED: ProjectTrusts = ProjectTrusts::UNTRUSTED;
let cases = [
(
TRUST_PROJECT_HOOKS_ENV,
ProjectTrusts {
hooks: Trusted,
..UNTRUSTED
},
),
(
TRUST_PROJECT_AGENTS_ENV,
ProjectTrusts {
agents: Trusted,
..UNTRUSTED
},
),
(
TRUST_PROJECT_PLUGINS_ENV,
ProjectTrusts {
plugins: Trusted,
..UNTRUSTED
},
),
(
TRUST_PROJECT_OPENAPI_ENV,
ProjectTrusts {
openapi: Trusted,
..UNTRUSTED
},
),
];
for (variable, expected) in cases {
let granted = ProjectTrusts::read(|name| (name == variable).then(|| "1".to_owned()));
assert_eq!(granted, expected, "{variable}");
}
}
// Covers: only the exact value `1` grants trust, matching the
// RHO_TRUST_PROJECT_* family contract used by hooks, agents, and plugins.
// Owner: workspace trust policy.
#[test]
fn project_trust_requires_exact_opt_in() {
assert_eq!(ProjectTrust::from_env(Some("1")), ProjectTrust::Trusted);
for value in [Some("0"), Some("true"), Some("yes"), Some(""), None] {
assert_eq!(
ProjectTrust::from_env(value),
ProjectTrust::Untrusted,
"{value:?}"
);
}
}
}