feat(stdlib): add explicitly granted env API

This commit is contained in:
Marqdo Agent committed 2026-11-21 08:40:54 +08:00
1 parent ae69391bc0
commit 9922bfae98
17 files changed
+322 -15

No files matched your search

+22
View File
@@ -317,6 +317,8 @@ pub struct Capabilities {
pub fs_write: Vec<String>,
/// Allowed network hosts (empty = no network).
pub net_hosts: Vec<String>,
/// Allowed environment variable names (empty = no environment).
pub env: Vec<String>,
/// May spawn processes (foreign kernels).
pub process: bool,
/// May run foreign kernels (Python/Node).
@@ -336,6 +338,7 @@ impl Capabilities {
fs_read: Vec::new(),
fs_write: Vec::new(),
net_hosts: Vec::new(),
env: Vec::new(),
process: false,
foreign: false,
plugins: Vec::new(),
@@ -361,6 +364,25 @@ impl Capabilities {
self
}
/// Grant read access to one environment variable name (ADR-0043).
pub fn allow_env(mut self, name: impl Into<String>) -> Self {
self.env.push(name.into());
self
}
/// Check an environment variable grant. Names are exact, not globs.
pub fn check_env(&self, name: &str, span: Option<Span>) -> Result<(), RuntimeError> {
if self.env.iter().any(|allowed| allowed == name) {
Ok(())
} else {
Err(denied(
span,
format!("environment variable `{name}` was not granted"),
format!("grant it with --allow-env {name}"),
))
}
}
/// Grant foreign kernel execution.
pub fn allow_foreign(mut self) -> Self {
self.foreign = true;