feat(stdlib): add explicitly granted env API
This commit is contained in:
1 parent
ae69391bc0
commit
9922bfae98
17 files changed
+322
-15
No files matched your search
@@ -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;
|
||||
|
||||
@@ -1127,6 +1127,58 @@ impl Host for StdHost {
|
||||
"resource operations accept only Text path arguments",
|
||||
));
|
||||
}
|
||||
if operation == "env.get" {
|
||||
if args.len() != 1 {
|
||||
return Err(RuntimeError::new(
|
||||
ErrorCategory::Arity,
|
||||
ctx.span,
|
||||
format!(
|
||||
"resource operation `env.get` expects 1 argument, got {}",
|
||||
args.len()
|
||||
),
|
||||
));
|
||||
}
|
||||
let value_type = args[0].type_name();
|
||||
let Value::Text(name) = args[0].clone() else {
|
||||
return Err(RuntimeError::type_error(
|
||||
ctx.span,
|
||||
format!("resource operation `env.get` expects a Text name, got {value_type}"),
|
||||
));
|
||||
};
|
||||
self.capabilities.check_env(&name, ctx.span)?;
|
||||
let mut map = BTreeMap::new();
|
||||
match std::env::var(&name) {
|
||||
Ok(value) => {
|
||||
map.insert("ok".to_string(), Value::Bool(true));
|
||||
map.insert("value".to_string(), Value::text(value));
|
||||
}
|
||||
Err(std::env::VarError::NotPresent) => {
|
||||
map.insert("ok".to_string(), Value::Bool(false));
|
||||
map.insert("code".to_string(), Value::text("env.not_found"));
|
||||
map.insert(
|
||||
"message".to_string(),
|
||||
Value::text(format!("environment variable `{name}` is not set")),
|
||||
);
|
||||
}
|
||||
Err(std::env::VarError::NotUnicode(_)) => {
|
||||
map.insert("ok".to_string(), Value::Bool(false));
|
||||
map.insert("code".to_string(), Value::text("env.not_unicode"));
|
||||
map.insert(
|
||||
"message".to_string(),
|
||||
Value::text(format!("environment variable `{name}` is not valid UTF-8")),
|
||||
);
|
||||
}
|
||||
}
|
||||
self.record_semantic(
|
||||
"resource.request",
|
||||
None,
|
||||
Some(&ctx.module_id),
|
||||
ctx.span,
|
||||
Some("op=env.get"),
|
||||
);
|
||||
self.record_resource_effect("resource.request", ctx, started, "env.get");
|
||||
return Ok(Value::Map(map));
|
||||
}
|
||||
if operation == "clock.now" {
|
||||
if !args.is_empty() {
|
||||
return Err(RuntimeError::new(
|
||||
|
||||
Reference in new issue
Block a user