feat(plugin): bridge worker capability requests to the host broker (H3-A)

- add request-scoped capability_request/capability_reply/capability_error
  frames to the worker transport (ADR-0018); no C ABI change
- worker routes its capability handler through stdin/stdout, the host serves
  requests inside the existing invoke deadline and binds identity from the
  spawned worker, never from frame text
- both native and worker plugins now execute a granted fs.read through
  ProjectFs with audit; unsupported capabilities return capability.unavailable
- add a real C capability plugin and CLI test for granted read and denial
- document the bridge and that strict OS isolation (H3-B) is still refused
This commit is contained in:
cflmy committed 2026-11-20 13:08:56 +08:00
1 parent 47a2e1883f
commit 0f01a69048
11 files changed
+720 -64

No files matched your search

+93
View File
@@ -154,3 +154,96 @@ fn worker_crash_is_isolated_from_the_host() {
.unwrap();
assert_eq!(survivor.status.code(), Some(0));
}
fn compile_capability(dir: &Path) -> PathBuf {
let output = dir.join("capability.so");
let status = Command::new("cc")
.args(["-std=c11", "-O2", "-shared", "-fPIC"])
.arg("-I")
.arg(repo().join("include"))
.arg(repo().join("tests/abi/capability_plugin.c"))
.arg("-o")
.arg(&output)
.status()
.expect("cc");
assert!(status.success(), "cc failed");
output
}
fn write_capability_manifest(dir: &Path, sha: &str) {
std::fs::write(
dir.join("plugin.yaml"),
format!(
r#"plugin:
name: demo.capability
version: 0.1.0
abi: "^1.0"
kind: worker
exports:
- name: capability.read
signature: "() -> Text"
requires:
- fs.read
artifacts:
- target: {}
file: capability.so
sha256: "{}"
"#,
HostTarget::current().triple(),
sha
),
)
.unwrap();
std::fs::write(
dir.join("doc.mq.md"),
"## main\n\n**结果 = capability.read()**\n\n> 打印(结果)\n",
)
.unwrap();
std::fs::create_dir_all(dir.join("data")).unwrap();
std::fs::write(dir.join("data/allowed.txt"), "hello-worker").unwrap();
}
fn run_capability(dir: &Path, allow_read: bool) -> Output {
let mut command = Command::new(env!("CARGO_BIN_EXE_marqdo"));
command.args([
"run",
dir.join("doc.mq.md").to_str().unwrap(),
"--plugin",
dir.to_str().unwrap(),
"--allow-plugin",
"demo.capability",
]);
if allow_read {
command.args(["--allow-read", "data/**"]);
}
command.output().unwrap()
}
#[test]
fn worker_capability_bridge_reads_granted_file_and_denies_without_grant() {
if !have_cc() {
eprintln!("skipping: cc is unavailable");
return;
}
let dir = TempDir::new("capability");
let artifact = compile_capability(dir.path());
write_capability_manifest(dir.path(), &sha256_file(&artifact).unwrap());
let output = run_capability(dir.path(), true);
assert_eq!(
output.status.code(),
Some(0),
"{}",
String::from_utf8_lossy(&output.stderr)
);
assert_eq!(String::from_utf8_lossy(&output.stdout), "hello-worker\n");
let denied = run_capability(dir.path(), false);
assert_eq!(denied.status.code(), Some(1));
let stderr = String::from_utf8_lossy(&denied.stderr);
assert!(
stderr.contains("permission.denied") || stderr.contains("fs.read"),
"{stderr}"
);
assert!(!String::from_utf8_lossy(&denied.stdout).contains("hello-worker"));
}
+19 -3
View File
@@ -284,9 +284,25 @@ impl StdHost {
.map_err(map_plugin_error)?,
)
}
PluginKind::Worker => PluginTransport::Worker(
WorkerPlugin::spawn(manifest, base_dir).map_err(map_plugin_error)?,
),
PluginKind::Worker => {
let mut broker = CapabilityBroker::with_audit(
self.capabilities.clone(),
Arc::clone(&self.broker_audit),
)
.with_fs(self.fs.clone());
for capability in &self.vendor_capabilities {
broker.allow_vendor(capability.clone());
}
let package = manifest.plugin.name.clone();
let handler: marqdo_plugin_abi::CapabilityHandler =
Box::new(move |capability, request| {
broker.execute(&package, capability, request)
});
PluginTransport::Worker(
WorkerPlugin::spawn(manifest, base_dir, Some(handler))
.map_err(map_plugin_error)?,
)
}
PluginKind::Wasm => {
return Err(RuntimeError::new(
ErrorCategory::KernelUnavailable,
+104 -28
View File
@@ -13,7 +13,9 @@ use std::thread;
use std::time::Duration;
use marqdo_plugin_abi::worker::{read_frame, write_frame, WORKER_COMMAND};
use marqdo_plugin_abi::{PluginError, PluginManifest, WireRequest, WireResponse};
use marqdo_plugin_abi::{
CapabilityHandler, PluginError, PluginManifest, WireRequest, WireResponse,
};
use serde_json::json;
/// A native plugin running in an isolated worker process.
@@ -23,6 +25,7 @@ pub struct WorkerPlugin {
frames: Receiver<Result<serde_json::Value, PluginError>>,
exports: BTreeMap<String, String>,
dead: bool,
capability_handler: Option<CapabilityHandler>,
}
const HANDSHAKE_TIMEOUT: Duration = Duration::from_secs(30);
@@ -39,7 +42,11 @@ impl std::fmt::Debug for WorkerPlugin {
impl WorkerPlugin {
/// Spawn a worker for `manifest` and wait for its ready frame.
pub fn spawn(manifest: &PluginManifest, base_dir: &Path) -> Result<Self, PluginError> {
pub fn spawn(
manifest: &PluginManifest,
base_dir: &Path,
capability_handler: Option<CapabilityHandler>,
) -> Result<Self, PluginError> {
// The worker receives an explicit serialized manifest, so it does not
// depend on the manifest file being named `plugin.yaml`.
let manifest_path = std::env::temp_dir().join(format!(
@@ -185,6 +192,7 @@ impl WorkerPlugin {
frames,
exports,
dead: false,
capability_handler,
})
}
@@ -205,42 +213,110 @@ impl WorkerPlugin {
self.mark_dead();
return Err(error);
}
let frame = match self.frames.recv_timeout(CALL_TIMEOUT) {
Ok(Ok(frame)) => frame,
Ok(Err(error)) => {
self.mark_dead();
return Err(error);
}
Err(_) => {
let deadline = std::time::Instant::now() + CALL_TIMEOUT;
loop {
let remaining = deadline.saturating_duration_since(std::time::Instant::now());
if remaining.is_zero() {
self.mark_dead();
return Err(PluginError::invoke(
"plugin.timeout",
"plugin worker exceeded its invocation timeout",
));
}
};
match frame.get("kind").and_then(serde_json::Value::as_str) {
Some("response") => marqdo_plugin_abi::worker::response_from_frame(&frame),
Some("error") => Err(PluginError::invoke(
frame
.get("code")
.and_then(serde_json::Value::as_str)
.unwrap_or("plugin_call_failed"),
frame
.get("message")
.and_then(serde_json::Value::as_str)
.unwrap_or("worker reported an error"),
)),
_ => {
self.mark_dead();
Err(PluginError::invoke(
"plugin.crashed",
"plugin worker closed or violated the frame protocol",
))
let frame = match self.frames.recv_timeout(remaining) {
Ok(Ok(frame)) => frame,
Ok(Err(error)) => {
self.mark_dead();
return Err(error);
}
Err(_) => {
self.mark_dead();
return Err(PluginError::invoke(
"plugin.timeout",
"plugin worker exceeded its invocation timeout",
));
}
};
match frame.get("kind").and_then(serde_json::Value::as_str) {
Some("capability_request") => {
if let Err(error) = self.handle_capability_request(&frame) {
self.mark_dead();
return Err(error);
}
}
Some("response") => {
return marqdo_plugin_abi::worker::response_from_frame(&frame);
}
Some("error") => {
return Err(PluginError::invoke(
frame
.get("code")
.and_then(serde_json::Value::as_str)
.unwrap_or("plugin_call_failed"),
frame
.get("message")
.and_then(serde_json::Value::as_str)
.unwrap_or("worker reported an error"),
))
}
_ => {
self.mark_dead();
return Err(PluginError::invoke(
"plugin.crashed",
"plugin worker closed or violated the frame protocol",
));
}
}
}
}
fn handle_capability_request(&mut self, frame: &serde_json::Value) -> Result<(), PluginError> {
let id = frame
.get("id")
.and_then(serde_json::Value::as_u64)
.ok_or_else(|| {
PluginError::wire("worker_frame_invalid", "capability request has no id")
})?;
let capability = frame
.get("capability")
.and_then(serde_json::Value::as_str)
.ok_or_else(|| {
PluginError::wire("worker_frame_invalid", "capability request has no name")
})?;
let request_value = frame.get("request").cloned().unwrap_or_else(|| json!({}));
let request_bytes = serde_json::to_vec(&request_value)
.map_err(|error| PluginError::wire("worker_encode_failed", error.to_string()))?;
let result = match &mut self.capability_handler {
Some(handler) => handler(capability, &request_bytes),
None => Err(PluginError::capability(
"capability.unavailable",
"this worker has no host capability bridge",
)),
};
let reply = match result {
Ok(data) => match String::from_utf8(data) {
Ok(data) => json!({"kind":"capability_reply","id":id,"data":data}),
Err(_) => json!({
"kind":"capability_error",
"id":id,
"code":"capability.reply_invalid",
"message":"capability reply is not UTF-8 text",
}),
},
Err(error) => json!({
"kind":"capability_error",
"id":id,
"code":error.code,
"message":error.message,
}),
};
if let Err(error) = write_frame(&mut self.stdin, &reply) {
self.mark_dead();
return Err(error);
}
Ok(())
}
fn mark_dead(&mut self) {
self.dead = true;
let _ = self.child.kill();
@@ -0,0 +1,162 @@
//! End-to-end native plugin capability execution (H3-A).
#![cfg(unix)]
use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::atomic::{AtomicU32, Ordering};
use marqdo_host::{Capabilities, Host, HostOp, StdHost};
use marqdo_plugin_abi::{sha256_file, HostTarget, PluginManifest};
use marqdo_sandbox::SandboxMode;
use marqdo_value::{ErrorCategory, Value};
static COUNTER: AtomicU32 = AtomicU32::new(0);
struct TempDir(PathBuf);
impl TempDir {
fn new() -> Self {
let id = COUNTER.fetch_add(1, Ordering::Relaxed);
let path = std::env::temp_dir().join(format!(
"marqdo-host-capability-{}-{id}",
std::process::id()
));
std::fs::create_dir_all(&path).unwrap();
Self(path)
}
fn path(&self) -> &Path {
&self.0
}
}
impl Drop for TempDir {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
fn repo_root() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR"))
.parent()
.unwrap()
.parent()
.unwrap()
.to_path_buf()
}
fn have_cc() -> bool {
Command::new("cc")
.arg("--version")
.output()
.map(|output| output.status.success())
.unwrap_or(false)
}
fn build_plugin(dir: &Path) -> PathBuf {
let output = dir.join("capability.so");
let status = Command::new("cc")
.args(["-std=c11", "-O2", "-shared", "-fPIC"])
.arg("-I")
.arg(repo_root().join("include"))
.arg(repo_root().join("tests/abi/capability_plugin.c"))
.arg("-o")
.arg(&output)
.status()
.expect("spawn cc");
assert!(status.success(), "cc failed");
output
}
fn write_manifest(dir: &Path, sha: &str) {
let yaml = format!(
r#"plugin:
name: demo.capability
version: 0.1.0
abi: "^1.0"
kind: native
exports:
- name: capability.read
signature: "() -> Text"
requires:
- fs.read
artifacts:
- target: {}
file: capability.so
sha256: "{}"
"#,
HostTarget::current().triple(),
sha
);
std::fs::write(dir.join("plugin.yaml"), yaml).unwrap();
}
#[test]
fn granted_fs_read_returns_real_bytes_and_denial_is_audited() {
if !have_cc() {
eprintln!("skipping: cc is unavailable");
return;
}
let dir = TempDir::new();
std::fs::create_dir_all(dir.path().join("data")).unwrap();
std::fs::write(dir.path().join("data/allowed.txt"), "hello-from-project").unwrap();
let artifact = build_plugin(dir.path());
write_manifest(dir.path(), &sha256_file(&artifact).unwrap());
let manifest = PluginManifest::load(&dir.path().join("plugin.yaml")).unwrap();
let capabilities = Capabilities::project()
.allow_plugin("demo.capability")
.allow_read("data/**");
let mut host = StdHost::new(SandboxMode::Project, capabilities, dir.path());
host.load_plugin(&manifest, dir.path(), true).unwrap();
let ctx = host.context("root:default::test.mq.md", None);
let value = host
.invoke(
HostOp::PluginInvoke,
vec![
Value::Text("capability.read".into()),
Value::Map(Default::default()),
],
&ctx,
)
.unwrap_or_else(|error| panic!("{error:?} audit={:?}", host.capability_audit()));
assert_eq!(value, Value::Text("hello-from-project".into()));
let audit = host.capability_audit();
assert!(
audit
.iter()
.any(|entry| entry.capability == "fs.read" && entry.decision == "granted"),
"{audit:?}"
);
assert!(
!format!("{audit:?}").contains("hello-from-project"),
"audit must not record file contents"
);
// Same load, but no read grant: the broker must not return bytes.
let capabilities = Capabilities::project().allow_plugin("demo.capability");
let mut denied_host = StdHost::new(SandboxMode::Project, capabilities, dir.path());
denied_host
.load_plugin(&manifest, dir.path(), true)
.unwrap();
let ctx = denied_host.context("root:default::test.mq.md", None);
let error = denied_host
.invoke(
HostOp::PluginInvoke,
vec![
Value::Text("capability.read".into()),
Value::Map(Default::default()),
],
&ctx,
)
.unwrap_err();
assert_eq!(error.category, ErrorCategory::PermissionDenied);
let audit = denied_host.capability_audit();
assert!(
audit
.iter()
.any(|entry| entry.capability == "fs.read" && entry.decision == "denied"),
"{audit:?}"
);
}
+118 -19
View File
@@ -8,11 +8,13 @@
use std::io::{Read, Write};
use std::path::Path;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use serde_json::{json, Value};
use crate::error::PluginError;
use crate::loader::LoadedPlugin;
use crate::loader::{LoadOptions, LoadedPlugin};
use crate::manifest::{PluginKind, PluginManifest};
use crate::wire::{WireRequest, WireResponse};
@@ -117,7 +119,94 @@ pub fn serve(manifest_path: &Path, base_dir: &Path) -> i32 {
// natively inside this child process.
let mut native_manifest = manifest.clone();
native_manifest.plugin.kind = PluginKind::Native;
let mut plugin = match LoadedPlugin::open(&native_manifest, base_dir) {
let stdin = Arc::new(Mutex::new(std::io::stdin()));
let stdout = Arc::new(Mutex::new(std::io::stdout()));
let next_request_id = Arc::new(AtomicU64::new(1));
let handler_stdin = Arc::clone(&stdin);
let handler_stdout = Arc::clone(&stdout);
let handler_ids = Arc::clone(&next_request_id);
let capability_handler: crate::loader::CapabilityHandler =
Box::new(move |capability, request| {
let id = handler_ids.fetch_add(1, Ordering::SeqCst);
let request_value: Value = serde_json::from_slice(request).map_err(|error| {
PluginError::capability(
"capability.request_invalid",
format!("capability request is not valid JSON: {error}"),
)
})?;
{
let mut out = handler_stdout.lock().map_err(|_| {
PluginError::invoke("plugin.crashed", "worker stdout lock is poisoned")
})?;
write_frame(
&mut *out,
&json!({
"kind": "capability_request",
"id": id,
"capability": capability,
"request": request_value,
}),
)?;
}
{
let frame = {
let mut input = handler_stdin.lock().map_err(|_| {
PluginError::invoke("plugin.crashed", "worker stdin lock is poisoned")
})?;
read_frame(&mut *input).map_err(|error| {
PluginError::invoke(
error.code.as_str(),
format!("worker capability reply failed: {}", error.message),
)
})?
};
let Some(frame) = frame else {
return Err(PluginError::invoke(
"plugin.crashed",
"host closed the worker during a capability request",
));
};
if frame.get("id").and_then(Value::as_u64) != Some(id) {
return Err(PluginError::wire(
"worker_frame_invalid",
"capability reply id does not match the request",
));
}
match frame.get("kind").and_then(Value::as_str) {
Some("capability_reply") => {
let data = frame.get("data").and_then(Value::as_str).ok_or_else(|| {
PluginError::wire(
"worker_frame_invalid",
"capability reply has no text data",
)
})?;
Ok(data.as_bytes().to_vec())
}
Some("capability_error") => Err(PluginError::capability(
frame
.get("code")
.and_then(Value::as_str)
.unwrap_or("capability.unavailable"),
frame
.get("message")
.and_then(Value::as_str)
.unwrap_or("capability request failed")
.to_string(),
)),
_ => Err(PluginError::wire(
"worker_frame_invalid",
"expected a capability reply frame",
)),
}
}
});
let options = LoadOptions {
host_target: crate::artifact::HostTarget::current(),
capability_handler: Some(capability_handler),
};
let mut plugin = match LoadedPlugin::open_with(&native_manifest, base_dir, options) {
Ok(plugin) => plugin,
Err(error) => {
eprintln!("worker: {}: {}", error.code, error.message);
@@ -128,14 +217,11 @@ pub fn serve(manifest_path: &Path, base_dir: &Path) -> i32 {
.exports()
.map(|export| json!({"name": export.name(), "signature": export.signature()}))
.collect();
let mut stdin = std::io::stdin();
let mut stdout = std::io::stdout();
if let Err(error) = write_frame(&mut stdout, &json!({"kind": "ready", "exports": exports})) {
eprintln!("worker: {}: {}", error.code, error.message);
if send_frame(&stdout, &json!({"kind": "ready", "exports": exports})).is_err() {
return 1;
}
loop {
let frame = match read_frame(&mut stdin) {
let frame = match receive_frame(&stdin) {
Ok(Some(frame)) => frame,
Ok(None) => return 0,
Err(error) => {
@@ -147,8 +233,8 @@ pub fn serve(manifest_path: &Path, base_dir: &Path) -> i32 {
Some("shutdown") => return 0,
Some("invoke") => {
let Some(request_value) = frame.get("request") else {
let _ = write_frame(
&mut stdout,
let _ = send_frame(
&stdout,
&json!({"kind":"error","code":"worker_request_invalid","message":"missing request"}),
);
continue;
@@ -156,8 +242,8 @@ pub fn serve(manifest_path: &Path, base_dir: &Path) -> i32 {
let request_bytes = match serde_json::to_vec(request_value) {
Ok(bytes) => bytes,
Err(error) => {
let _ = write_frame(
&mut stdout,
let _ = send_frame(
&stdout,
&json!({"kind":"error","code":"worker_request_invalid","message":error.to_string()}),
);
continue;
@@ -166,8 +252,8 @@ pub fn serve(manifest_path: &Path, base_dir: &Path) -> i32 {
let request = match WireRequest::from_json_bytes(&request_bytes) {
Ok(request) => request,
Err(error) => {
let _ = write_frame(
&mut stdout,
let _ = send_frame(
&stdout,
&json!({"kind":"error","code":error.code,"message":error.message}),
);
continue;
@@ -180,13 +266,13 @@ pub fn serve(manifest_path: &Path, base_dir: &Path) -> i32 {
.ok()
.and_then(|bytes| serde_json::from_slice::<Value>(&bytes).ok())
.unwrap_or_else(|| json!({"wire_version":1,"kind":"response"}));
if write_frame(&mut stdout, &json!({"kind":"response","response":body})).is_err() {
if send_frame(&stdout, &json!({"kind":"response","response":body})).is_err() {
return 1;
}
}
Err(error) => {
if write_frame(
&mut stdout,
if send_frame(
&stdout,
&json!({"kind":"error","code":error.code,"message":error.message,"error_kind":error.kind.as_str()}),
)
.is_err()
@@ -197,8 +283,8 @@ pub fn serve(manifest_path: &Path, base_dir: &Path) -> i32 {
}
}
_ => {
if write_frame(
&mut stdout,
if send_frame(
&stdout,
&json!({"kind":"error","code":"worker_frame_invalid","message":"unknown frame kind"}),
)
.is_err()
@@ -210,7 +296,20 @@ pub fn serve(manifest_path: &Path, base_dir: &Path) -> i32 {
}
}
/// Build a successful response from an embedded wire response value.
fn send_frame(stdout: &Arc<Mutex<std::io::Stdout>>, value: &Value) -> Result<(), PluginError> {
let mut out = stdout
.lock()
.map_err(|_| PluginError::invoke("plugin.crashed", "worker stdout lock is poisoned"))?;
write_frame(&mut *out, value)
}
fn receive_frame(stdin: &Arc<Mutex<std::io::Stdin>>) -> Result<Option<Value>, PluginError> {
let mut input = stdin
.lock()
.map_err(|_| PluginError::invoke("plugin.crashed", "worker stdin lock is poisoned"))?;
read_frame(&mut *input)
}
pub fn response_from_frame(frame: &Value) -> Result<WireResponse, PluginError> {
let body = frame
.get("response")