feat(process): add explicitly granted process API

This commit is contained in:
Marqdo Agent committed 2026-11-21 08:46:02 +08:00
1 parent 9922bfae98
commit 3ef18fb496
17 files changed
+433 -15

No files matched your search

+10 -3
View File
@@ -26,6 +26,8 @@ pub struct SandboxSettings {
pub allow_net: Vec<String>,
/// Environment variable grants (ADR-0043).
pub allow_env: Vec<String>,
/// Process program grants (ADR-0044).
pub allow_process: Vec<String>,
/// Foreign execution grant.
pub allow_foreign: bool,
/// Plugin grants.
@@ -77,6 +79,7 @@ impl SandboxSettings {
|| !self.allow_net.is_empty()
|| !self.allow_write.is_empty()
|| !self.allow_env.is_empty()
|| !self.allow_process.is_empty()
|| !self.allow_plugin.is_empty()
|| !self.plugins.is_empty()
|| self.trust_native_plugin
@@ -85,9 +88,9 @@ impl SandboxSettings {
if requested {
return Err(
"strict mode currently supports pure Marqdo and native Math only; \
--allow-read/--allow-write/--allow-env/--allow-foreign/--allow-plugin/\
--plugin/--trust-native-plugin/--kernel/--trace-file are rejected. Use \
--mode project for those capabilities."
--allow-read/--allow-write/--allow-env/--allow-process/--allow-foreign/\
--allow-plugin/--plugin/--trust-native-plugin/--kernel/--trace-file are
rejected. Use --mode project for those capabilities."
.to_string(),
);
}
@@ -103,6 +106,7 @@ impl SandboxSettings {
|| !self.allow_net.is_empty()
|| !self.allow_write.is_empty()
|| !self.allow_env.is_empty()
|| !self.allow_process.is_empty()
|| self.allow_foreign
|| !self.allow_plugin.is_empty()
{
@@ -126,6 +130,9 @@ impl SandboxSettings {
for name in &self.allow_env {
capabilities = capabilities.allow_env(name.clone());
}
for program in &self.allow_process {
capabilities = capabilities.allow_process(program.clone());
}
if self.allow_foreign {
capabilities = capabilities.allow_foreign();
}
+4
View File
@@ -95,6 +95,9 @@ struct SandboxArgs {
/// Grant read access to an environment variable (repeatable, ADR-0043).
#[arg(long = "allow-env", value_name = "NAME")]
allow_env: Vec<String>,
/// Grant execution of an exact program path (repeatable, ADR-0044).
#[arg(long = "allow-process", value_name = "PROGRAM")]
allow_process: Vec<String>,
/// Grant explicit foreign kernel execution.
#[arg(long = "allow-foreign")]
allow_foreign: bool,
@@ -147,6 +150,7 @@ impl SandboxArgs {
allow_net: self.allow_net.clone(),
allow_write: self.allow_write.clone(),
allow_env: self.allow_env.clone(),
allow_process: self.allow_process.clone(),
allow_foreign: self.allow_foreign,
allow_plugin: self.allow_plugin.clone(),
fuel: self.fuel,
+18
View File
@@ -81,6 +81,8 @@ fn product_tests() -> Vec<TestFile> {
"--allow-env",
"MARQDO_MISSING_ENV",
][..]
} else if name == "process.mq.md" {
&["--allow-process", "/bin/echo"][..]
} else {
&[][..]
};
@@ -155,6 +157,7 @@ fn every_public_stdlib_module_has_a_product_test() {
"io.mq.md",
"json.mq.md",
"path.mq.md",
"process.mq.md",
"regex.mq.md",
"stream.mq.md",
"table.mq.md",
@@ -344,6 +347,21 @@ fn env_test_reads_only_granted_names() {
assert_eq!(items[5]["value"], "env.not_found");
}
#[test]
fn process_test_runs_an_explicitly_granted_program() {
let report = run_product_test(
product_tests()
.iter()
.find(|file| file.name == "process.mq.md")
.expect("process test"),
);
let items = report["value"]["items"].as_array().expect("list");
assert_eq!(items[0]["value"], true);
assert_eq!(items[1]["decimal"], "0");
assert_eq!(items[2]["value"], "hello\n");
assert_eq!(items[3]["value"], "");
}
#[test]
fn format_test_pins_value_semantics_and_handle_observation() {
let report = run_product_test(
+26
View File
@@ -319,6 +319,8 @@ pub struct Capabilities {
pub net_hosts: Vec<String>,
/// Allowed environment variable names (empty = no environment).
pub env: Vec<String>,
/// Allowed process program paths (empty = no process execution).
pub process_programs: Vec<String>,
/// May spawn processes (foreign kernels).
pub process: bool,
/// May run foreign kernels (Python/Node).
@@ -339,6 +341,7 @@ impl Capabilities {
fs_write: Vec::new(),
net_hosts: Vec::new(),
env: Vec::new(),
process_programs: Vec::new(),
process: false,
foreign: false,
plugins: Vec::new(),
@@ -383,6 +386,29 @@ impl Capabilities {
}
}
/// Grant execution of one exact program path (ADR-0044).
pub fn allow_process(mut self, program: impl Into<String>) -> Self {
self.process_programs.push(program.into());
self
}
/// Check an exact process-program grant.
pub fn check_process(&self, program: &str, span: Option<Span>) -> Result<(), RuntimeError> {
if self
.process_programs
.iter()
.any(|allowed| allowed == program)
{
Ok(())
} else {
Err(denied(
span,
format!("process `{program}` was not granted"),
format!("grant it with --allow-process {program}"),
))
}
}
/// Grant foreign kernel execution.
pub fn allow_foreign(mut self) -> Self {
self.foreign = true;
+168
View File
@@ -6,6 +6,7 @@
use std::collections::{BTreeMap, BTreeSet};
use std::net::{SocketAddr, TcpStream};
use std::process::{Command, Stdio};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
@@ -943,6 +944,121 @@ impl StdHost {
}
}
fn process_error(code: &str, message: &str) -> Value {
let mut map = BTreeMap::new();
map.insert("ok".to_string(), Value::Bool(false));
map.insert("code".to_string(), Value::text(code));
map.insert("message".to_string(), Value::text(message));
Value::Map(map)
}
fn run_process(
root: &std::path::Path,
program: &str,
args: &[String],
ctx: &ExecutionContext,
) -> Result<Value, RuntimeError> {
const LIMIT: usize = 1024 * 1024;
let child = Command::new(program)
.args(args)
.current_dir(root)
.env_clear()
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn();
let child = match child {
Ok(child) => child,
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
return Ok(process_error(
"process.not_found",
&format!("program `{program}` was not found"),
))
}
Err(error) if error.kind() == std::io::ErrorKind::PermissionDenied => {
return Err(RuntimeError::new(
ErrorCategory::PermissionDenied,
ctx.span,
format!("cannot execute `{program}`: {error}"),
))
}
Err(error) => {
return Err(RuntimeError::new(
ErrorCategory::HostError,
ctx.span,
format!("cannot start `{program}`: {error}"),
))
}
};
let mut child = child;
let deadline = Instant::now() + Duration::from_secs(5);
let mut timed_out = false;
loop {
match child.try_wait() {
Ok(Some(_)) => break,
Ok(None) if Instant::now() < deadline => {
std::thread::sleep(Duration::from_millis(10));
}
Ok(None) => {
timed_out = true;
let _ = child.kill();
break;
}
Err(error) => {
return Err(RuntimeError::new(
ErrorCategory::HostError,
ctx.span,
format!("cannot wait for `{program}`: {error}"),
))
}
}
}
let output = child.wait_with_output().map_err(|error| {
RuntimeError::new(
ErrorCategory::HostError,
ctx.span,
format!("cannot collect `{program}` output: {error}"),
)
})?;
if timed_out {
return Ok(process_error(
"process.timeout",
&format!("program `{program}` exceeded the 5 second limit"),
));
}
if output.stdout.len() > LIMIT || output.stderr.len() > LIMIT {
return Ok(process_error(
"process.output_too_large",
&format!("program `{program}` exceeded the {LIMIT} byte output limit"),
));
}
let stdout = String::from_utf8(output.stdout).map_err(|_| {
RuntimeError::new(
ErrorCategory::Type,
ctx.span,
format!("program `{program}` stdout is not valid UTF-8"),
)
})?;
let stderr = String::from_utf8(output.stderr).map_err(|_| {
RuntimeError::new(
ErrorCategory::Type,
ctx.span,
format!("program `{program}` stderr is not valid UTF-8"),
)
})?;
let mut value = BTreeMap::new();
value.insert(
"status".to_string(),
Value::int(output.status.code().unwrap_or(-1) as i64),
);
value.insert("stdout".to_string(), Value::text(stdout));
value.insert("stderr".to_string(), Value::text(stderr));
let mut map = BTreeMap::new();
map.insert("ok".to_string(), Value::Bool(true));
map.insert("value".to_string(), Value::Map(value));
Ok(Value::Map(map))
}
impl Host for StdHost {
fn invoke(
&mut self,
@@ -1127,6 +1243,58 @@ impl Host for StdHost {
"resource operations accept only Text path arguments",
));
}
if operation == "process.run" {
if args.len() != 2 {
return Err(RuntimeError::new(
ErrorCategory::Arity,
ctx.span,
format!(
"resource operation `process.run` expects 2 arguments, got {}",
args.len()
),
));
}
let program_type = args[0].type_name();
let Value::Text(program) = args[0].clone() else {
return Err(RuntimeError::type_error(
ctx.span,
format!(
"resource operation `process.run` expects a Text program, got {program_type}"
),
));
};
let args_type = args[1].type_name();
let Value::List(values) = args[1].clone() else {
return Err(RuntimeError::type_error(
ctx.span,
format!(
"resource operation `process.run` expects a List of Text arguments, got {args_type}"
),
));
};
let mut program_args = Vec::with_capacity(values.len());
for value in values {
let value_type = value.type_name();
let Value::Text(value) = value else {
return Err(RuntimeError::type_error(
ctx.span,
format!("process arguments must be Text, got {value_type}"),
));
};
program_args.push(value);
}
self.capabilities.check_process(&program, ctx.span)?;
let result = run_process(self.fs.root(), &program, &program_args, ctx)?;
self.record_semantic(
"resource.request",
None,
Some(&ctx.module_id),
ctx.span,
Some("op=process.run"),
);
self.record_resource_effect("resource.request", ctx, started, "process.run");
return Ok(result);
}
if operation == "env.get" {
if args.len() != 1 {
return Err(RuntimeError::new(
@@ -682,3 +682,65 @@ fn env_get_requires_an_exact_grant_and_reports_missing_names() {
.unwrap_err();
assert_eq!(error.category, ErrorCategory::Arity);
}
#[cfg(unix)]
#[test]
fn process_run_requires_an_exact_program_grant() {
let dir = tempfile::tempdir().unwrap();
let ctx = context();
let mut denied = StdHost::new(SandboxMode::Project, Capabilities::project(), dir.path());
let error = denied
.resource_request(
"process.run",
vec![
Value::text("/bin/echo"),
Value::List(vec![Value::text("x")]),
],
&ctx,
)
.unwrap_err();
assert_eq!(error.category, ErrorCategory::PermissionDenied);
let mut pure = StdHost::new(SandboxMode::Pure, Capabilities::pure(), dir.path());
let error = pure
.resource_request(
"process.run",
vec![
Value::text("/bin/echo"),
Value::List(vec![Value::text("x")]),
],
&ctx,
)
.unwrap_err();
assert_eq!(error.category, ErrorCategory::PermissionDenied);
let capabilities = Capabilities::project().allow_process("/bin/echo");
let mut host = StdHost::new(SandboxMode::Project, capabilities, dir.path());
let result = host
.resource_request(
"process.run",
vec![
Value::text("/bin/echo"),
Value::List(vec![Value::text("hello")]),
],
&ctx,
)
.unwrap();
assert_eq!(field(&result, "ok"), &Value::Bool(true));
let value = field(&result, "value");
assert_eq!(field(value, "status"), &Value::int(0));
assert_eq!(field(value, "stdout"), &Value::text("hello\n"));
assert_eq!(field(value, "stderr"), &Value::text(""));
let missing = host
.resource_request(
"process.run",
vec![
Value::text("/bin/definitely-not-a-program"),
Value::List(Vec::new()),
],
&ctx,
)
.unwrap_err();
assert_eq!(missing.category, ErrorCategory::PermissionDenied);
}