refactor(cli,host): split sandbox, resource-open and process modules
This commit is contained in:
1 parent
3ef18fb496
commit
791819a725
9 files changed
+523
-441
No files matched your search
+5
-137
@@ -3,152 +3,20 @@
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use marqdo_host::{Capabilities, Host, StdHost};
|
||||
use marqdo_host::{Host, StdHost};
|
||||
use marqdo_runtime::{run_with_limits, EvaluatedArg, MemoryLimits, RuntimeError, Value};
|
||||
use marqdo_sandbox::{PlatformCapabilities, SandboxMode};
|
||||
use marqdo_span::Span;
|
||||
use serde_json::json;
|
||||
|
||||
mod sandbox;
|
||||
|
||||
pub use sandbox::SandboxSettings;
|
||||
|
||||
use crate::args;
|
||||
use crate::loader::{self, FileDiagnostic, LoadedProject};
|
||||
use crate::output;
|
||||
|
||||
/// Effective sandbox settings from the command line.
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct SandboxSettings {
|
||||
/// Mode name.
|
||||
pub mode: String,
|
||||
/// Read grants.
|
||||
pub allow_read: Vec<String>,
|
||||
/// Write grants.
|
||||
pub allow_write: Vec<String>,
|
||||
/// Network grants (IP literals, ADR-0034).
|
||||
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.
|
||||
pub allow_plugin: Vec<String>,
|
||||
/// Fuel override.
|
||||
pub fuel: Option<u64>,
|
||||
/// Stack depth override.
|
||||
pub stack: Option<usize>,
|
||||
/// Output limit override.
|
||||
pub max_output: Option<usize>,
|
||||
/// Total committed addressable-memory budget override (bytes).
|
||||
pub max_memory: Option<usize>,
|
||||
/// Per-region committed addressable-memory budget override (bytes).
|
||||
pub max_region_memory: Option<usize>,
|
||||
/// Wall-clock timeout in seconds.
|
||||
pub timeout: Option<u64>,
|
||||
/// Explicitly trusted plugin manifests to load.
|
||||
pub plugins: Vec<PathBuf>,
|
||||
/// Explicit trust decision for in-process native plugin loading.
|
||||
pub trust_native_plugin: bool,
|
||||
/// Optional Trace v2 output path.
|
||||
pub trace_file: Option<PathBuf>,
|
||||
/// Trace event budget override (0 = unlimited).
|
||||
pub trace_max_events: Option<usize>,
|
||||
/// Trace approximate byte budget override (0 = unlimited).
|
||||
pub trace_max_bytes: Option<usize>,
|
||||
/// External foreign-kernel descriptor paths.
|
||||
pub kernels: Vec<PathBuf>,
|
||||
}
|
||||
|
||||
impl SandboxSettings {
|
||||
/// Parse the mode string.
|
||||
pub fn sandbox_mode(&self) -> Result<SandboxMode, String> {
|
||||
SandboxMode::parse(&self.mode).ok_or_else(|| {
|
||||
format!(
|
||||
"unknown sandbox mode `{}` (expected pure, project or strict)",
|
||||
self.mode
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
/// Reject strict-mode requests that this L1 isolation level does not
|
||||
/// support. Strict currently covers pure Marqdo and native Math only;
|
||||
/// granting Foreign, ABI plugins, tools or trace files would require
|
||||
/// extending the supervisor contract first.
|
||||
pub fn strict_preflight(&self) -> Result<(), String> {
|
||||
let requested = self.allow_foreign
|
||||
|| !self.allow_read.is_empty()
|
||||
|| !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
|
||||
|| !self.kernels.is_empty()
|
||||
|| self.trace_file.is_some();
|
||||
if requested {
|
||||
return Err(
|
||||
"strict mode currently supports pure Marqdo and native Math only; \
|
||||
--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(),
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Build the effective capability set.
|
||||
pub fn capabilities(&self) -> Result<Capabilities, String> {
|
||||
let mode = self.sandbox_mode()?;
|
||||
match mode {
|
||||
SandboxMode::Pure => {
|
||||
if !self.allow_read.is_empty()
|
||||
|| !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()
|
||||
{
|
||||
return Err(
|
||||
"pure mode prohibits external I/O grants; use --mode project".to_string(),
|
||||
);
|
||||
}
|
||||
Ok(Capabilities::pure())
|
||||
}
|
||||
SandboxMode::Project => {
|
||||
let mut capabilities = Capabilities::project();
|
||||
for pattern in &self.allow_read {
|
||||
capabilities = capabilities.allow_read(pattern.clone());
|
||||
}
|
||||
for pattern in &self.allow_write {
|
||||
capabilities = capabilities.allow_write(pattern.clone());
|
||||
}
|
||||
for host in &self.allow_net {
|
||||
capabilities = capabilities.allow_net(host.clone());
|
||||
}
|
||||
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();
|
||||
}
|
||||
for plugin in &self.allow_plugin {
|
||||
capabilities = capabilities.allow_plugin(plugin.clone());
|
||||
}
|
||||
Ok(capabilities)
|
||||
}
|
||||
SandboxMode::Strict => {
|
||||
self.strict_preflight()?;
|
||||
Ok(Capabilities::project())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `marqdo init`.
|
||||
pub fn init(dir: Option<&Path>) -> i32 {
|
||||
let dir = dir.unwrap_or_else(|| Path::new("."));
|
||||
|
||||
@@ -0,0 +1,145 @@
|
||||
//! Sandbox mode parsing and capability construction.
|
||||
//!
|
||||
//! This module owns the CLI-to-`Capabilities` mapping and strict preflight;
|
||||
//! command implementations only consume the resulting grant set.
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use marqdo_host::Capabilities;
|
||||
use marqdo_sandbox::SandboxMode;
|
||||
|
||||
/// Effective sandbox settings from the command line.
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct SandboxSettings {
|
||||
/// Mode name.
|
||||
pub mode: String,
|
||||
/// Read grants.
|
||||
pub allow_read: Vec<String>,
|
||||
/// Write grants.
|
||||
pub allow_write: Vec<String>,
|
||||
/// Network grants (IP literals, ADR-0034).
|
||||
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.
|
||||
pub allow_plugin: Vec<String>,
|
||||
/// Fuel override.
|
||||
pub fuel: Option<u64>,
|
||||
/// Stack depth override.
|
||||
pub stack: Option<usize>,
|
||||
/// Output limit override.
|
||||
pub max_output: Option<usize>,
|
||||
/// Total committed addressable-memory budget override (bytes).
|
||||
pub max_memory: Option<usize>,
|
||||
/// Per-region committed addressable-memory budget override (bytes).
|
||||
pub max_region_memory: Option<usize>,
|
||||
/// Wall-clock timeout in seconds.
|
||||
pub timeout: Option<u64>,
|
||||
/// Explicitly trusted plugin manifests to load.
|
||||
pub plugins: Vec<PathBuf>,
|
||||
/// Explicit trust decision for in-process native plugin loading.
|
||||
pub trust_native_plugin: bool,
|
||||
/// Optional Trace v2 output path.
|
||||
pub trace_file: Option<PathBuf>,
|
||||
/// Trace event budget override (0 = unlimited).
|
||||
pub trace_max_events: Option<usize>,
|
||||
/// Trace approximate byte budget override (0 = unlimited).
|
||||
pub trace_max_bytes: Option<usize>,
|
||||
/// External foreign-kernel descriptor paths.
|
||||
pub kernels: Vec<PathBuf>,
|
||||
}
|
||||
|
||||
impl SandboxSettings {
|
||||
/// Parse the mode string.
|
||||
pub fn sandbox_mode(&self) -> Result<SandboxMode, String> {
|
||||
SandboxMode::parse(&self.mode).ok_or_else(|| {
|
||||
format!(
|
||||
"unknown sandbox mode `{}` (expected pure, project or strict)",
|
||||
self.mode
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
/// Reject strict-mode requests that this L1 isolation level does not
|
||||
/// support. Strict currently covers pure Marqdo and native Math only;
|
||||
/// granting Foreign, ABI plugins, tools or trace files would require
|
||||
/// extending the supervisor contract first.
|
||||
pub fn strict_preflight(&self) -> Result<(), String> {
|
||||
let requested = self.allow_foreign
|
||||
|| !self.allow_read.is_empty()
|
||||
|| !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
|
||||
|| !self.kernels.is_empty()
|
||||
|| self.trace_file.is_some();
|
||||
if requested {
|
||||
return Err(
|
||||
"strict mode currently supports pure Marqdo and native Math only; \
|
||||
--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(),
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Build the effective capability set.
|
||||
pub fn capabilities(&self) -> Result<Capabilities, String> {
|
||||
let mode = self.sandbox_mode()?;
|
||||
match mode {
|
||||
SandboxMode::Pure => {
|
||||
if !self.allow_read.is_empty()
|
||||
|| !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()
|
||||
{
|
||||
return Err(
|
||||
"pure mode prohibits external I/O grants; use --mode project".to_string(),
|
||||
);
|
||||
}
|
||||
Ok(Capabilities::pure())
|
||||
}
|
||||
SandboxMode::Project => {
|
||||
let mut capabilities = Capabilities::project();
|
||||
for pattern in &self.allow_read {
|
||||
capabilities = capabilities.allow_read(pattern.clone());
|
||||
}
|
||||
for pattern in &self.allow_write {
|
||||
capabilities = capabilities.allow_write(pattern.clone());
|
||||
}
|
||||
for host in &self.allow_net {
|
||||
capabilities = capabilities.allow_net(host.clone());
|
||||
}
|
||||
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();
|
||||
}
|
||||
for plugin in &self.allow_plugin {
|
||||
capabilities = capabilities.allow_plugin(plugin.clone());
|
||||
}
|
||||
Ok(capabilities)
|
||||
}
|
||||
SandboxMode::Strict => {
|
||||
self.strict_preflight()?;
|
||||
Ok(Capabilities::project())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,6 @@
|
||||
|
||||
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};
|
||||
|
||||
@@ -28,6 +27,8 @@ use crate::resource::{ResourceProvider, ResourceRead, ResourceTable, SharedResou
|
||||
use crate::trace::{EffectTrace, SemanticEvent, TraceBudget, TraceEntry, TraceState};
|
||||
use marqdo_sandbox::{PlatformCapabilities, SandboxMode};
|
||||
|
||||
mod resource_ops;
|
||||
|
||||
/// Host operations reachable from the language surface.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum HostOp {
|
||||
@@ -756,307 +757,6 @@ impl StdHost {
|
||||
summary.to_string(),
|
||||
);
|
||||
}
|
||||
|
||||
fn open_resource_stream(
|
||||
&mut self,
|
||||
kind: &str,
|
||||
descriptor: &str,
|
||||
rights: &[String],
|
||||
ctx: &ExecutionContext,
|
||||
) -> Result<Value, RuntimeError> {
|
||||
let started = Instant::now();
|
||||
let requested: BTreeSet<&str> = rights.iter().map(String::as_str).collect();
|
||||
if self.mode == SandboxMode::Strict {
|
||||
// stdio/stdout is the existing console channel already permitted by
|
||||
// strict L1 (CM-A11); stdin, stderr, file/network/process providers
|
||||
// are not, because strict isolation has not been proven for them.
|
||||
let stdio_console =
|
||||
kind == "stdio" && descriptor == "stdout" && requested == BTreeSet::from(["write"]);
|
||||
if !stdio_console {
|
||||
return Err(RuntimeError::new(
|
||||
ErrorCategory::PermissionDenied,
|
||||
ctx.span,
|
||||
"strict mode permits only the existing stdio/stdout console channel; \
|
||||
other resource streams are refused",
|
||||
));
|
||||
}
|
||||
}
|
||||
if self.mode == SandboxMode::Pure && kind == "file" {
|
||||
return Err(RuntimeError::new(
|
||||
ErrorCategory::PermissionDenied,
|
||||
ctx.span,
|
||||
"pure mode does not provide project file resources",
|
||||
));
|
||||
}
|
||||
match kind {
|
||||
"stdio" => {
|
||||
let allowed = match descriptor {
|
||||
"stdout" | "stderr" => requested == BTreeSet::from(["write"]),
|
||||
"stdin" => requested == BTreeSet::from(["read"]),
|
||||
_ => false,
|
||||
};
|
||||
if !allowed {
|
||||
return Err(RuntimeError::new(
|
||||
ErrorCategory::ResourceUnsupported,
|
||||
ctx.span,
|
||||
"stdio supports stdout/stderr with rights [write] and stdin with rights [read]",
|
||||
));
|
||||
}
|
||||
if !self.capabilities.console {
|
||||
return Err(RuntimeError::new(
|
||||
ErrorCategory::PermissionDenied,
|
||||
ctx.span,
|
||||
"the console capability is required for stdio resources",
|
||||
));
|
||||
}
|
||||
let handle = match descriptor {
|
||||
"stdin" => self.resources.open_stdin(),
|
||||
"stderr" => self.resources.open_stderr(),
|
||||
"stdout" => self.resources.open_stdout(),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
self.record_semantic(
|
||||
"resource.open",
|
||||
None,
|
||||
Some(&ctx.module_id),
|
||||
ctx.span,
|
||||
Some(&format!("kind=stdio descriptor={descriptor}")),
|
||||
);
|
||||
self.record_resource_effect("resource.open", ctx, started, "ok");
|
||||
Ok(Value::Resource(handle))
|
||||
}
|
||||
"file" => {
|
||||
if requested == BTreeSet::from(["read"]) {
|
||||
self.capabilities.check_read(descriptor, ctx.span)?;
|
||||
let (_, relative) = self.fs.resolve(descriptor, ctx.span)?;
|
||||
let bytes = self.fs.read_bytes(descriptor, ctx.span)?;
|
||||
let handle = self.resources.open_file(relative, bytes);
|
||||
self.record_semantic(
|
||||
"resource.open",
|
||||
None,
|
||||
Some(&ctx.module_id),
|
||||
ctx.span,
|
||||
Some("kind=file rights=read"),
|
||||
);
|
||||
self.record_resource_effect("resource.open", ctx, started, "ok");
|
||||
return Ok(Value::Resource(handle));
|
||||
}
|
||||
if requested == BTreeSet::from(["write"]) {
|
||||
self.capabilities.check_write(descriptor, ctx.span)?;
|
||||
let (_, relative) = self.fs.resolve(descriptor, ctx.span)?;
|
||||
let handle = self.resources.open_file_writer(relative);
|
||||
self.record_semantic(
|
||||
"resource.open",
|
||||
None,
|
||||
Some(&ctx.module_id),
|
||||
ctx.span,
|
||||
Some("kind=file rights=write"),
|
||||
);
|
||||
self.record_resource_effect("resource.open", ctx, started, "ok");
|
||||
return Ok(Value::Resource(handle));
|
||||
}
|
||||
Err(RuntimeError::new(
|
||||
ErrorCategory::ResourceUnsupported,
|
||||
ctx.span,
|
||||
"project file streams support exactly [read] or [write] in this build",
|
||||
))
|
||||
}
|
||||
"tcp" => {
|
||||
if self.mode != SandboxMode::Project {
|
||||
return Err(RuntimeError::new(
|
||||
ErrorCategory::PermissionDenied,
|
||||
ctx.span,
|
||||
"tcp resources are available only in project mode",
|
||||
));
|
||||
}
|
||||
if requested != BTreeSet::from(["read", "write"]) {
|
||||
return Err(RuntimeError::new(
|
||||
ErrorCategory::ResourceUnsupported,
|
||||
ctx.span,
|
||||
"tcp resources require exactly [read, write] in this build",
|
||||
));
|
||||
}
|
||||
let address: SocketAddr = descriptor.parse().map_err(|_| {
|
||||
RuntimeError::new(
|
||||
ErrorCategory::ResourceUnsupported,
|
||||
ctx.span,
|
||||
format!(
|
||||
"tcp descriptor `{descriptor}` must be an IP literal address:port (DNS names are not resolved in this build)"
|
||||
),
|
||||
)
|
||||
})?;
|
||||
self.capabilities
|
||||
.check_net(&address.ip().to_string(), ctx.span)?;
|
||||
let stream = TcpStream::connect_timeout(&address, Duration::from_secs(5)).map_err(
|
||||
|error| {
|
||||
RuntimeError::new(
|
||||
ErrorCategory::HostError,
|
||||
ctx.span,
|
||||
format!("cannot connect to {address}: {error}"),
|
||||
)
|
||||
},
|
||||
)?;
|
||||
let _ = stream.set_read_timeout(Some(Duration::from_secs(5)));
|
||||
let _ = stream.set_write_timeout(Some(Duration::from_secs(5)));
|
||||
let handle = self.resources.open_tcp(stream);
|
||||
self.record_semantic(
|
||||
"resource.open",
|
||||
None,
|
||||
Some(&ctx.module_id),
|
||||
ctx.span,
|
||||
Some(&format!("kind=tcp address={address}")),
|
||||
);
|
||||
self.record_resource_effect("resource.open", ctx, started, "ok");
|
||||
Ok(Value::Resource(handle))
|
||||
}
|
||||
other => {
|
||||
if self.mode != SandboxMode::Project {
|
||||
return Err(RuntimeError::new(
|
||||
ErrorCategory::PermissionDenied,
|
||||
ctx.span,
|
||||
"resource providers are available only in project mode",
|
||||
));
|
||||
}
|
||||
let Some((provider_name, provider)) = self.resource_providers.get(other).cloned()
|
||||
else {
|
||||
return Err(RuntimeError::new(
|
||||
ErrorCategory::ResourceUnsupported,
|
||||
ctx.span,
|
||||
format!("resource kind `{other}` is not registered"),
|
||||
));
|
||||
};
|
||||
self.capabilities
|
||||
.check_resource_provider(&provider_name, ctx.span)?;
|
||||
let handle = self
|
||||
.resources
|
||||
.open_provider(provider, other, descriptor, rights, ctx.span)?;
|
||||
self.record_semantic(
|
||||
"resource.open",
|
||||
None,
|
||||
Some(&ctx.module_id),
|
||||
ctx.span,
|
||||
Some(&format!("kind={other} provider={provider_name}")),
|
||||
);
|
||||
self.record_resource_effect("resource.open", ctx, started, "ok");
|
||||
Ok(Value::Resource(handle))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
@@ -1284,7 +984,7 @@ impl Host for StdHost {
|
||||
program_args.push(value);
|
||||
}
|
||||
self.capabilities.check_process(&program, ctx.span)?;
|
||||
let result = run_process(self.fs.root(), &program, &program_args, ctx)?;
|
||||
let result = crate::process::run_process(self.fs.root(), &program, &program_args, ctx)?;
|
||||
self.record_semantic(
|
||||
"resource.request",
|
||||
None,
|
||||
|
||||
@@ -0,0 +1,194 @@
|
||||
//! Resource/stream open dispatch plus provider and TCP endpoints.
|
||||
//!
|
||||
//! Child module of `host`: it may use the parent's private fields, and keeps
|
||||
//! the open-path policy out of the generic `Host::invoke` dispatch.
|
||||
|
||||
use super::*;
|
||||
|
||||
impl StdHost {
|
||||
pub(crate) fn open_resource_stream(
|
||||
&mut self,
|
||||
kind: &str,
|
||||
descriptor: &str,
|
||||
rights: &[String],
|
||||
ctx: &ExecutionContext,
|
||||
) -> Result<Value, RuntimeError> {
|
||||
let started = Instant::now();
|
||||
let requested: BTreeSet<&str> = rights.iter().map(String::as_str).collect();
|
||||
if self.mode == SandboxMode::Strict {
|
||||
// stdio/stdout is the existing console channel already permitted by
|
||||
// strict L1 (CM-A11); stdin, stderr, file/network/process providers
|
||||
// are not, because strict isolation has not been proven for them.
|
||||
let stdio_console =
|
||||
kind == "stdio" && descriptor == "stdout" && requested == BTreeSet::from(["write"]);
|
||||
if !stdio_console {
|
||||
return Err(RuntimeError::new(
|
||||
ErrorCategory::PermissionDenied,
|
||||
ctx.span,
|
||||
"strict mode permits only the existing stdio/stdout console channel; \
|
||||
other resource streams are refused",
|
||||
));
|
||||
}
|
||||
}
|
||||
if self.mode == SandboxMode::Pure && kind == "file" {
|
||||
return Err(RuntimeError::new(
|
||||
ErrorCategory::PermissionDenied,
|
||||
ctx.span,
|
||||
"pure mode does not provide project file resources",
|
||||
));
|
||||
}
|
||||
match kind {
|
||||
"stdio" => {
|
||||
let allowed = match descriptor {
|
||||
"stdout" | "stderr" => requested == BTreeSet::from(["write"]),
|
||||
"stdin" => requested == BTreeSet::from(["read"]),
|
||||
_ => false,
|
||||
};
|
||||
if !allowed {
|
||||
return Err(RuntimeError::new(
|
||||
ErrorCategory::ResourceUnsupported,
|
||||
ctx.span,
|
||||
"stdio supports stdout/stderr with rights [write] and stdin with rights [read]",
|
||||
));
|
||||
}
|
||||
if !self.capabilities.console {
|
||||
return Err(RuntimeError::new(
|
||||
ErrorCategory::PermissionDenied,
|
||||
ctx.span,
|
||||
"the console capability is required for stdio resources",
|
||||
));
|
||||
}
|
||||
let handle = match descriptor {
|
||||
"stdin" => self.resources.open_stdin(),
|
||||
"stderr" => self.resources.open_stderr(),
|
||||
"stdout" => self.resources.open_stdout(),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
self.record_semantic(
|
||||
"resource.open",
|
||||
None,
|
||||
Some(&ctx.module_id),
|
||||
ctx.span,
|
||||
Some(&format!("kind=stdio descriptor={descriptor}")),
|
||||
);
|
||||
self.record_resource_effect("resource.open", ctx, started, "ok");
|
||||
Ok(Value::Resource(handle))
|
||||
}
|
||||
"file" => {
|
||||
if requested == BTreeSet::from(["read"]) {
|
||||
self.capabilities.check_read(descriptor, ctx.span)?;
|
||||
let (_, relative) = self.fs.resolve(descriptor, ctx.span)?;
|
||||
let bytes = self.fs.read_bytes(descriptor, ctx.span)?;
|
||||
let handle = self.resources.open_file(relative, bytes);
|
||||
self.record_semantic(
|
||||
"resource.open",
|
||||
None,
|
||||
Some(&ctx.module_id),
|
||||
ctx.span,
|
||||
Some("kind=file rights=read"),
|
||||
);
|
||||
self.record_resource_effect("resource.open", ctx, started, "ok");
|
||||
return Ok(Value::Resource(handle));
|
||||
}
|
||||
if requested == BTreeSet::from(["write"]) {
|
||||
self.capabilities.check_write(descriptor, ctx.span)?;
|
||||
let (_, relative) = self.fs.resolve(descriptor, ctx.span)?;
|
||||
let handle = self.resources.open_file_writer(relative);
|
||||
self.record_semantic(
|
||||
"resource.open",
|
||||
None,
|
||||
Some(&ctx.module_id),
|
||||
ctx.span,
|
||||
Some("kind=file rights=write"),
|
||||
);
|
||||
self.record_resource_effect("resource.open", ctx, started, "ok");
|
||||
return Ok(Value::Resource(handle));
|
||||
}
|
||||
Err(RuntimeError::new(
|
||||
ErrorCategory::ResourceUnsupported,
|
||||
ctx.span,
|
||||
"project file streams support exactly [read] or [write] in this build",
|
||||
))
|
||||
}
|
||||
"tcp" => {
|
||||
if self.mode != SandboxMode::Project {
|
||||
return Err(RuntimeError::new(
|
||||
ErrorCategory::PermissionDenied,
|
||||
ctx.span,
|
||||
"tcp resources are available only in project mode",
|
||||
));
|
||||
}
|
||||
if requested != BTreeSet::from(["read", "write"]) {
|
||||
return Err(RuntimeError::new(
|
||||
ErrorCategory::ResourceUnsupported,
|
||||
ctx.span,
|
||||
"tcp resources require exactly [read, write] in this build",
|
||||
));
|
||||
}
|
||||
let address: SocketAddr = descriptor.parse().map_err(|_| {
|
||||
RuntimeError::new(
|
||||
ErrorCategory::ResourceUnsupported,
|
||||
ctx.span,
|
||||
format!(
|
||||
"tcp descriptor `{descriptor}` must be an IP literal address:port (DNS names are not resolved in this build)"
|
||||
),
|
||||
)
|
||||
})?;
|
||||
self.capabilities
|
||||
.check_net(&address.ip().to_string(), ctx.span)?;
|
||||
let stream = TcpStream::connect_timeout(&address, Duration::from_secs(5)).map_err(
|
||||
|error| {
|
||||
RuntimeError::new(
|
||||
ErrorCategory::HostError,
|
||||
ctx.span,
|
||||
format!("cannot connect to {address}: {error}"),
|
||||
)
|
||||
},
|
||||
)?;
|
||||
let _ = stream.set_read_timeout(Some(Duration::from_secs(5)));
|
||||
let _ = stream.set_write_timeout(Some(Duration::from_secs(5)));
|
||||
let handle = self.resources.open_tcp(stream);
|
||||
self.record_semantic(
|
||||
"resource.open",
|
||||
None,
|
||||
Some(&ctx.module_id),
|
||||
ctx.span,
|
||||
Some(&format!("kind=tcp address={address}")),
|
||||
);
|
||||
self.record_resource_effect("resource.open", ctx, started, "ok");
|
||||
Ok(Value::Resource(handle))
|
||||
}
|
||||
other => {
|
||||
if self.mode != SandboxMode::Project {
|
||||
return Err(RuntimeError::new(
|
||||
ErrorCategory::PermissionDenied,
|
||||
ctx.span,
|
||||
"resource providers are available only in project mode",
|
||||
));
|
||||
}
|
||||
let Some((provider_name, provider)) = self.resource_providers.get(other).cloned()
|
||||
else {
|
||||
return Err(RuntimeError::new(
|
||||
ErrorCategory::ResourceUnsupported,
|
||||
ctx.span,
|
||||
format!("resource kind `{other}` is not registered"),
|
||||
));
|
||||
};
|
||||
self.capabilities
|
||||
.check_resource_provider(&provider_name, ctx.span)?;
|
||||
let handle = self
|
||||
.resources
|
||||
.open_provider(provider, other, descriptor, rights, ctx.span)?;
|
||||
self.record_semantic(
|
||||
"resource.open",
|
||||
None,
|
||||
Some(&ctx.module_id),
|
||||
ctx.span,
|
||||
Some(&format!("kind={other} provider={provider_name}")),
|
||||
);
|
||||
self.record_resource_effect("resource.open", ctx, started, "ok");
|
||||
Ok(Value::Resource(handle))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ pub mod handles;
|
||||
pub mod host;
|
||||
pub mod kernels;
|
||||
mod plugin_worker;
|
||||
mod process;
|
||||
mod provider_c;
|
||||
mod provider_worker;
|
||||
pub mod resource;
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
//! Explicitly granted process execution (ADR-0044).
|
||||
//!
|
||||
//! The child is **not** an OS sandbox: it inherits the CLI user's privileges.
|
||||
//! The host checks an exact program grant before calling this module.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::path::Path;
|
||||
use std::process::{Command, Stdio};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use marqdo_value::{ErrorCategory, RuntimeError, Value};
|
||||
|
||||
use crate::context::ExecutionContext;
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
pub(crate) fn run_process(
|
||||
root: &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))
|
||||
}
|
||||
@@ -241,6 +241,12 @@ This is still a **local VM lane**, not a self-hosted GitHub Actions security URL
|
||||
now sorts `.mq.md` by the same relative path key as the CLI. Source/test baseline
|
||||
`0d40de7`; later docs-only commits keep code/test bytes identical. Evidence:
|
||||
[`docs-and-test-migration`](evidence/2026-11-21-docs-and-test-migration.md).
|
||||
- Module-boundary refactor (review RFC-CM P1): CLI sandbox/capability policy
|
||||
moved to `crates/cli/src/commands/sandbox.rs`; host resource open dispatch to
|
||||
`crates/marqdo-host/src/host/resource_ops.rs`; explicitly granted process
|
||||
execution to `crates/marqdo-host/src/process.rs`. `commands.rs` still owns
|
||||
run/check/inspect and `host.rs` still owns provider registration; this is a
|
||||
partial, behavior-preserving split.
|
||||
- Minimal structure observation/construction (RFC-CM CM-A04, ADR-0023):
|
||||
value-semantics `list.append/concat`, `map.keys/has/get/set/remove`,
|
||||
Unicode scalar `text.codepoints`/`text.from_codepoint`, and non-negative
|
||||
|
||||
@@ -62,6 +62,9 @@ Layer rules:
|
||||
human/machine output. It is not a semantic policy centre.
|
||||
5. Callable Plugin and Resource Provider share one registry/authorization/audit
|
||||
mechanism, but keep different capability types.
|
||||
6. CLI sandbox policy is `commands/sandbox.rs`; host open-path policy is
|
||||
`host/resource_ops.rs`; process execution is `process.rs`. These are child
|
||||
modules, not new crates or a second backend.
|
||||
|
||||
## 3. Dependency direction and public contracts
|
||||
|
||||
@@ -124,7 +127,7 @@ that a phase is complete.
|
||||
| P1 | `std:fs/file` read/write/copy and `resource.request` fs metadata/directory operations are implemented; append/seek/watch/recursive walk remain open | open |
|
||||
| P1 | No file append/seek/watch/recursive directory walk | open; future Resource Operation extension |
|
||||
| P1 | JSON/CSV product parsers are implemented; no streaming/multi-line CSV or depth-bounded JSON walker | open |
|
||||
| P1 | `crates/cli/src/commands.rs` and `crates/marqdo-host/src/host.rs` still mix multiple responsibilities | open refactor, behavior-preserving |
|
||||
| P1 | CLI sandbox policy moved to `crates/cli/src/commands/sandbox.rs`; host resource open to `host/resource_ops.rs` and process to `process.rs`; `commands.rs`/`host.rs` still mix run/check/inspect and provider registration | partial refactor, behavior-preserving |
|
||||
| P2 | HTTP parsing is Unicode-codepoint oriented; a general byte body is not the only path | open; convert before a general network release |
|
||||
| P2 | No DNS, TLS, listener or UDP provider | open |
|
||||
| P2 | `std:path`, a regex subset, `std:datetime`, `std:clock`, `std:env` and `std:process` are implemented; no secure random or compression | open |
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
# Evidence: CLI/Host responsibility split
|
||||
|
||||
Date: 2026-11-21 (Asia/Shanghai)
|
||||
Source baseline: `3ef18fb496096d97e3c48ff661f0b2ad00cdcdc3`
|
||||
Repository: `marqdo-cli`
|
||||
RFC item: P1 module boundaries
|
||||
|
||||
## Moved
|
||||
|
||||
| New module | Responsibility |
|
||||
|---|---|
|
||||
| `crates/cli/src/commands/sandbox.rs` | `SandboxSettings`, mode parsing, strict preflight and CLI-to-`Capabilities` mapping; `commands.rs` re-exports it |
|
||||
| `crates/marqdo-host/src/host/resource_ops.rs` | `open_resource_stream` stdio/file/tcp/provider dispatch; child module of `host` |
|
||||
| `crates/marqdo-host/src/process.rs` | explicitly granted `process.run` execution, timeout and output caps |
|
||||
|
||||
This is a **behavior-preserving** refactor: no public CLI flag, host trait
|
||||
method, builtin, spec or stdlib API changed. No new crate was added. Provider
|
||||
registration and `commands.rs` run/check/inspect still live in their original
|
||||
files.
|
||||
|
||||
## Commands
|
||||
|
||||
```text
|
||||
cargo fmt --all -- --check PASS
|
||||
cargo clippy --workspace --all-targets -- -D warnings PASS
|
||||
cargo test --workspace 478 passed, 0 failed, 2 ignored
|
||||
cargo test -p marqdo-cli --test docs_consistency 5 passed
|
||||
cargo test -p marqdo-cli --test architecture_boundaries 4 passed
|
||||
python3 tools/pack-stdlib.py PASS
|
||||
```
|
||||
|
||||
## Not covered
|
||||
|
||||
`commands.rs` still contains run/check/inspect/package/tool command bodies
|
||||
and `host.rs` still contains provider registration and capability dispatch.
|
||||
A complete split needs a later behavior-preserving change; it is not claimed
|
||||
here.
|
||||
Reference in new issue
Block a user