feat(net): add DNS, UDP, TCP listener and TLS endpoints

This commit is contained in:
Marqdo Agent committed 2026-11-21 10:43:08 +08:00
1 parent ee0c6cc80a
commit ff3f19a2ea
20 files changed
+1327 -61

No files matched your search

Generated
+340 -8
View File
@@ -38,7 +38,7 @@ version = "1.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc"
dependencies = [
"windows-sys",
"windows-sys 0.61.2",
]
[[package]]
@@ -49,7 +49,7 @@ checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d"
dependencies = [
"anstyle",
"once_cell_polyfill",
"windows-sys",
"windows-sys 0.61.2",
]
[[package]]
@@ -58,6 +58,12 @@ version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53"
[[package]]
name = "base64"
version = "0.22.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
[[package]]
name = "bitflags"
version = "2.13.2"
@@ -82,6 +88,16 @@ dependencies = [
"unicode-normalization",
]
[[package]]
name = "cc"
version = "1.11.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02f0e023adb368345ce0ca6cdfc0f24a06554468d0e8503e14a0841de79bfa69"
dependencies = [
"find-msvc-tools",
"shlex",
]
[[package]]
name = "cfg-if"
version = "1.0.5"
@@ -151,6 +167,22 @@ dependencies = [
"typed-arena",
]
[[package]]
name = "core-foundation"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b2a6cd9ae233e7f62ba4e9353e81a88df7fc8a5987b8d445b4d90c879bd156f6"
dependencies = [
"core-foundation-sys",
"libc",
]
[[package]]
name = "core-foundation-sys"
version = "0.8.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
[[package]]
name = "cpufeatures"
version = "0.2.17"
@@ -170,6 +202,12 @@ dependencies = [
"typenum",
]
[[package]]
name = "deranged"
version = "0.5.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e9de72ce2ad1f90dc62fa25f0f430ef85eb4b0d8fa0be4f30373bc40a21d28e"
[[package]]
name = "digest"
version = "0.10.7"
@@ -199,7 +237,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e35d6e78f4cebb90e51d543db8fe41528851d6deeef6ef21ebd4bfdc1e784776"
dependencies = [
"libc",
"windows-sys",
"windows-sys 0.61.2",
]
[[package]]
@@ -225,6 +263,12 @@ dependencies = [
"syn",
]
[[package]]
name = "find-msvc-tools"
version = "0.1.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1c66915cc5391097de7a2b0b972e8505aa33c0df401fd3a6811dbdb32881ba48"
[[package]]
name = "finl_unicode"
version = "1.5.1"
@@ -241,6 +285,17 @@ dependencies = [
"version_check",
]
[[package]]
name = "getrandom"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "getrandom"
version = "0.4.3"
@@ -393,7 +448,7 @@ dependencies = [
name = "marqdo-host"
version = "0.1.0"
dependencies = [
"getrandom",
"getrandom 0.4.3",
"glob",
"libloading",
"marqdo-foreign",
@@ -402,6 +457,10 @@ dependencies = [
"marqdo-sandbox",
"marqdo-span",
"marqdo-value",
"rcgen",
"rustls",
"rustls-native-certs",
"rustls-pemfile",
"serde",
"serde_json",
"tempfile",
@@ -561,6 +620,12 @@ dependencies = [
"serde",
]
[[package]]
name = "num-conv"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "521739c6d2bac4aa25192232afe6841231376b2b26d4d9fae5ecf8ca5772e441"
[[package]]
name = "num-integer"
version = "0.1.47"
@@ -603,6 +668,22 @@ version = "1.70.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
[[package]]
name = "openssl-probe"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e036b411754132fd6b74e94b74e9a13c5cf606fa93d1885c35860d3060820b1"
[[package]]
name = "pem"
version = "3.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d30c53c26bc5b31a98cd02d20f25a7c8567146caf63ed593a9d87b2775291be"
dependencies = [
"base64",
"serde_core",
]
[[package]]
name = "phf"
version = "0.14.0"
@@ -642,6 +723,12 @@ dependencies = [
"siphasher",
]
[[package]]
name = "powerfmt"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4a6394b9e965e73d0a289ee54f589087e2c676aedf60885baf52c76b771e4958"
[[package]]
name = "proc-macro2"
version = "1.0.107"
@@ -666,6 +753,33 @@ version = "6.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf"
[[package]]
name = "rcgen"
version = "0.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75e669e5202259b5314d1ea5397316ad400819437857b90861765f24c4cf80a2"
dependencies = [
"pem",
"ring",
"rustls-pki-types",
"time",
"yasna",
]
[[package]]
name = "ring"
version = "0.17.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4689e6c2294d81e88dc6261c768b63bc4fcdb852be6d1352498b114f61383b7"
dependencies = [
"cc",
"cfg-if",
"getrandom 0.2.17",
"libc",
"untrusted",
"windows-sys 0.52.0",
]
[[package]]
name = "rustc-hash"
version = "2.1.3"
@@ -682,7 +796,62 @@ dependencies = [
"errno",
"libc",
"linux-raw-sys",
"windows-sys",
"windows-sys 0.61.2",
]
[[package]]
name = "rustls"
version = "0.23.46"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "48e13bd8c0e9365c43cfa5c9e8f9ad49d3c8444926c9aac819e0e4dc503c8fdf"
dependencies = [
"once_cell",
"ring",
"rustls-pki-types",
"rustls-webpki",
"subtle",
"zeroize",
]
[[package]]
name = "rustls-native-certs"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4a073f5dc7469f984c52ad2752b63b0807745133b6de880b7b64c1ac4c48aec4"
dependencies = [
"openssl-probe",
"rustls-pki-types",
"schannel",
"security-framework",
]
[[package]]
name = "rustls-pemfile"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dce314e5fee3f39953d46bb63bb8a46d40c2f8fb7cc5a3b6cab2bde9721d6e50"
dependencies = [
"rustls-pki-types",
]
[[package]]
name = "rustls-pki-types"
version = "1.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "102f35f7de7b06d8c61f1cff6592f5253e0dea431c74867aad5dfd70f44663fb"
dependencies = [
"zeroize",
]
[[package]]
name = "rustls-webpki"
version = "0.103.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3a0760a887c14823fca5a1c970b01d47c328c696c0c64829d1df50e8f853fd5"
dependencies = [
"ring",
"rustls-pki-types",
"untrusted",
]
[[package]]
@@ -700,6 +869,38 @@ dependencies = [
"winapi-util",
]
[[package]]
name = "schannel"
version = "0.1.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9cb23c424d8ee841bb4d813677ca237e2a610067d594fec130f87d6093495ab5"
dependencies = [
"windows-sys 0.61.2",
]
[[package]]
name = "security-framework"
version = "3.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7f4bc775c73d9a02cde8bf7b2ec4c9d12743edf609006c7facc23998404cd1d"
dependencies = [
"bitflags",
"core-foundation",
"core-foundation-sys",
"libc",
"security-framework-sys",
]
[[package]]
name = "security-framework-sys"
version = "2.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ce2691df843ecc5d231c0b14ece2acc3efb62c0a398c7e1d875f3983ce020e3"
dependencies = [
"core-foundation-sys",
"libc",
]
[[package]]
name = "semver"
version = "1.0.28"
@@ -773,6 +974,12 @@ dependencies = [
"digest",
]
[[package]]
name = "shlex"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba"
[[package]]
name = "siphasher"
version = "1.0.4"
@@ -791,6 +998,12 @@ version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]]
name = "subtle"
version = "2.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
[[package]]
name = "syn"
version = "3.0.10"
@@ -809,10 +1022,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd"
dependencies = [
"fastrand",
"getrandom",
"getrandom 0.4.3",
"once_cell",
"rustix",
"windows-sys",
"windows-sys 0.61.2",
]
[[package]]
@@ -835,6 +1048,25 @@ dependencies = [
"syn",
]
[[package]]
name = "time"
version = "0.3.55"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdb87b95ec50ddfa440816d227a17b2ccbdda963a316a727fda0fc4334f7d134"
dependencies = [
"deranged",
"num-conv",
"powerfmt",
"serde_core",
"time-core",
]
[[package]]
name = "time-core"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e1c906769ad99c88eaa54e728060edef082f8e358ff32030cb7c7d315e81109"
[[package]]
name = "tinyvec"
version = "1.13.3"
@@ -874,6 +1106,12 @@ version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861"
[[package]]
name = "untrusted"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1"
[[package]]
name = "utf8parse"
version = "0.2.2"
@@ -896,13 +1134,19 @@ dependencies = [
"winapi-util",
]
[[package]]
name = "wasi"
version = "0.11.1+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b"
[[package]]
name = "winapi-util"
version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
dependencies = [
"windows-sys",
"windows-sys 0.61.2",
]
[[package]]
@@ -911,6 +1155,15 @@ version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
[[package]]
name = "windows-sys"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-sys"
version = "0.61.2"
@@ -920,6 +1173,85 @@ dependencies = [
"windows-link",
]
[[package]]
name = "windows-targets"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_gnullvm",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
[[package]]
name = "windows_i686_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
[[package]]
name = "windows_i686_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
[[package]]
name = "windows_i686_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[package]]
name = "yasna"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e17bb3549cc1321ae1296b9cdc2698e2b6cb1992adfa19a8c72e5b7a738f44cd"
dependencies = [
"time",
]
[[package]]
name = "zeroize"
version = "1.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85ccff8a4805aba1eaa4cd4afcd49f7557dd2cf5fd79ffc37f8959466effc5aa"
[[package]]
name = "zmij"
version = "1.0.23"
+4
View File
@@ -54,6 +54,10 @@ hex = "0.4"
libloading = "0.8"
tempfile = "3"
getrandom = "0.4"
rustls = { version = "0.23", default-features = false, features = ["ring", "std", "tls12"] }
rustls-native-certs = "0.8"
rustls-pemfile = "2"
rcgen = "0.13"
thiserror = "2"
clap = { version = "4", features = ["derive"] }
walkdir = "2"
+2 -2
View File
@@ -86,8 +86,8 @@ struct SandboxArgs {
/// Grant read access to a project-relative glob (repeatable).
#[arg(long = "allow-read", value_name = "GLOB")]
allow_read: Vec<String>,
/// Grant network access to an IP literal host (repeatable, ADR-0034).
#[arg(long = "allow-net", value_name = "IP")]
/// Grant network access to a host name or IP literal (repeatable, ADR-0034/0047).
#[arg(long = "allow-net", value_name = "HOST")]
allow_net: Vec<String>,
/// Grant write access to a project-relative glob (repeatable).
#[arg(long = "allow-write", value_name = "GLOB")]
+4
View File
@@ -10,6 +10,8 @@ authors.workspace = true
[dependencies]
libloading = { workspace = true }
rustls = { workspace = true }
rustls-native-certs = { workspace = true }
marqdo-plugin-abi = { workspace = true }
getrandom = { workspace = true }
glob = { workspace = true }
@@ -25,3 +27,5 @@ thiserror = { workspace = true }
[dev-dependencies]
tempfile = { workspace = true }
rcgen = { workspace = true }
rustls-pemfile = { workspace = true }
+52 -1
View File
@@ -5,7 +5,7 @@
//! execution, not at parse/compile time.
use std::collections::{BTreeMap, BTreeSet};
use std::net::{SocketAddr, TcpStream};
use std::net::{TcpListener, TcpStream, ToSocketAddrs, UdpSocket};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
@@ -14,6 +14,7 @@ use marqdo_plugin_abi::{
};
use marqdo_span::Span;
use marqdo_value::{ErrorCategory, ResourceHandle, RuntimeError, Value};
use rustls::pki_types::CertificateDer;
use crate::budget::Budget;
use crate::capability::{Capabilities, CapabilityAudit, CapabilityBroker};
@@ -144,6 +145,22 @@ pub trait Host {
))
}
/// Accept one inbound TCP connection from a `tcp-listener` handle.
///
/// Default: unavailable. The standard host returns a new `Resource` in a
/// recoverable `{ok,value}` map or `{ok:false,code:"tcp.timeout"}`.
fn resource_accept(
&mut self,
_handle: ResourceHandle,
ctx: &ExecutionContext,
) -> Result<Value, RuntimeError> {
Err(RuntimeError::new(
ErrorCategory::ResourceUnsupported,
ctx.span,
"this host does not provide tcp listeners",
))
}
/// Execute one non-streaming resource operation (ADR-0041).
///
/// Default: unavailable. The standard host implements the `fs.*` group;
@@ -221,6 +238,8 @@ pub struct StdHost {
plugin_exports: BTreeMap<String, usize>,
/// Trusted vendor capability policy entries for the broker.
vendor_capabilities: BTreeSet<String>,
/// Extra TLS roots explicitly trusted by the embedder (DER).
tls_extra_roots: Vec<CertificateDer<'static>>,
/// Shared audit log for plugin capability requests.
broker_audit: Arc<Mutex<Vec<CapabilityAudit>>>,
/// Host-issued opaque plugin handles.
@@ -289,6 +308,7 @@ impl StdHost {
plugins: Vec::new(),
plugin_exports: BTreeMap::new(),
vendor_capabilities: BTreeSet::new(),
tls_extra_roots: Vec::new(),
broker_audit: Arc::new(Mutex::new(Vec::new())),
handles: PluginHandleTable::default(),
stderr: Vec::new(),
@@ -300,6 +320,16 @@ impl StdHost {
}
}
/// Add one explicit TLS root certificate (DER) for `resource.open("tls")`.
///
/// System roots are always tried first; this adds a private CA for
/// embedders and tests. The certificate is a trust decision, not a grant:
/// the exact host grant is still required.
pub fn with_tls_ca(mut self, certificate: CertificateDer<'static>) -> Self {
self.tls_extra_roots.push(certificate);
self
}
/// Load a native plugin in-process after an explicit trust decision.
///
/// In-process native code shares the host address space and is **not**
@@ -712,6 +742,27 @@ impl Host for StdHost {
Ok(())
}
fn resource_accept(
&mut self,
handle: ResourceHandle,
ctx: &ExecutionContext,
) -> Result<Value, RuntimeError> {
let started = Instant::now();
let accepted = self.resources.accept(handle, ctx.span)?;
self.record_semantic(
"resource.accept",
None,
Some(&ctx.module_id),
ctx.span,
Some("kind=tcp-listener"),
);
self.record_resource_effect("resource.accept", ctx, started, "ok");
let mut map = BTreeMap::new();
map.insert("ok".to_string(), Value::Bool(true));
map.insert("value".to_string(), Value::Resource(accepted));
Ok(Value::Map(map))
}
fn resource_request(
&mut self,
operation: &str,
+285 -17
View File
@@ -5,6 +5,31 @@
use super::*;
fn parse_host_port(descriptor: &str, span: Option<Span>) -> Result<(String, u16), RuntimeError> {
let invalid = || {
RuntimeError::new(
ErrorCategory::ResourceUnsupported,
span,
format!("tcp descriptor `{descriptor}` must be `host:port` or `[ipv6]:port`"),
)
};
if let Some(rest) = descriptor.strip_prefix('[') {
let (host, tail) = rest.split_once(']').ok_or_else(invalid)?;
let port = tail.strip_prefix(':').ok_or_else(invalid)?;
let port = port.parse::<u16>().map_err(|_| invalid())?;
if host.is_empty() {
return Err(invalid());
}
return Ok((host.to_string(), port));
}
let (host, port) = descriptor.rsplit_once(':').ok_or_else(invalid)?;
if host.is_empty() {
return Err(invalid());
}
let port = port.parse::<u16>().map_err(|_| invalid())?;
Ok((host.to_string(), port))
}
impl StdHost {
pub(crate) fn open_resource_stream(
&mut self,
@@ -125,26 +150,37 @@ impl StdHost {
"tcp resources require exactly [read, write] in this build",
));
}
let address: SocketAddr = descriptor.parse().map_err(|_| {
let (host, port) = parse_host_port(descriptor, ctx.span)?;
self.capabilities.check_net(&host, ctx.span)?;
let addresses = (host.as_str(), port).to_socket_addrs().map_err(|error| {
RuntimeError::new(
ErrorCategory::ResourceUnsupported,
ErrorCategory::HostError,
ctx.span,
format!(
"tcp descriptor `{descriptor}` must be an IP literal address:port (DNS names are not resolved in this build)"
),
format!("cannot resolve `{host}:{port}`: {error}"),
)
})?;
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 mut stream = None;
let mut last_error = None;
for address in addresses {
match TcpStream::connect_timeout(&address, Duration::from_secs(5)) {
Ok(connected) => {
stream = Some(connected);
break;
}
Err(error) => last_error = Some((address, error)),
}
}
let Some(stream) = stream else {
let detail = match last_error {
Some((address, error)) => format!("{address}: {error}"),
None => format!("`{host}:{port}` resolved to no addresses"),
};
return Err(RuntimeError::new(
ErrorCategory::HostError,
ctx.span,
format!("cannot connect to `{host}:{port}`: {detail}"),
));
};
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);
@@ -153,7 +189,239 @@ impl StdHost {
None,
Some(&ctx.module_id),
ctx.span,
Some(&format!("kind=tcp address={address}")),
Some(&format!("kind=tcp host={host} port={port}")),
);
self.record_resource_effect("resource.open", ctx, started, "ok");
Ok(Value::Resource(handle))
}
"tls" => {
if self.mode != SandboxMode::Project {
return Err(RuntimeError::new(
ErrorCategory::PermissionDenied,
ctx.span,
"tls resources are available only in project mode",
));
}
if requested != BTreeSet::from(["read", "write"]) {
return Err(RuntimeError::new(
ErrorCategory::ResourceUnsupported,
ctx.span,
"tls resources require exactly [read, write] in this build",
));
}
let (host, port) = parse_host_port(descriptor, ctx.span)?;
self.capabilities.check_net(&host, ctx.span)?;
let addresses = (host.as_str(), port).to_socket_addrs().map_err(|error| {
RuntimeError::new(
ErrorCategory::HostError,
ctx.span,
format!("cannot resolve `{host}:{port}`: {error}"),
)
})?;
let mut tcp = None;
let mut last_error = None;
for address in addresses {
match TcpStream::connect_timeout(&address, Duration::from_secs(5)) {
Ok(connected) => {
tcp = Some(connected);
break;
}
Err(error) => last_error = Some((address, error)),
}
}
let Some(tcp) = tcp else {
let detail = match last_error {
Some((address, error)) => format!("{address}: {error}"),
None => format!("`{host}:{port}` resolved to no addresses"),
};
return Err(RuntimeError::new(
ErrorCategory::HostError,
ctx.span,
format!("cannot connect tls to `{host}:{port}`: {detail}"),
));
};
let _ = tcp.set_read_timeout(Some(Duration::from_secs(5)));
let _ = tcp.set_write_timeout(Some(Duration::from_secs(5)));
let mut roots = rustls::RootCertStore::empty();
for certificate in rustls_native_certs::load_native_certs().certs {
let _ = roots.add(certificate);
}
for certificate in &self.tls_extra_roots {
roots.add(certificate.clone()).map_err(|error| {
RuntimeError::new(
ErrorCategory::HostError,
ctx.span,
format!("cannot add TLS root: {error}"),
)
})?;
}
if roots.is_empty() {
return Err(RuntimeError::new(
ErrorCategory::HostError,
ctx.span,
"no trusted TLS roots are available",
));
}
let config = rustls::ClientConfig::builder()
.with_root_certificates(roots)
.with_no_client_auth();
let server_name =
rustls::pki_types::ServerName::try_from(host.clone()).map_err(|_| {
RuntimeError::new(
ErrorCategory::ResourceUnsupported,
ctx.span,
format!("`{host}` is not a valid TLS server name"),
)
})?;
let connection =
rustls::ClientConnection::new(std::sync::Arc::new(config), server_name)
.map_err(|error| {
RuntimeError::new(
ErrorCategory::HostError,
ctx.span,
format!("cannot start TLS: {error}"),
)
})?;
let stream = rustls::StreamOwned::new(connection, tcp);
let handle = self.resources.open_tls(stream);
self.record_semantic(
"resource.open",
None,
Some(&ctx.module_id),
ctx.span,
Some(&format!("kind=tls host={host} port={port}")),
);
self.record_resource_effect("resource.open", ctx, started, "ok");
Ok(Value::Resource(handle))
}
"tcp-listener" => {
if self.mode != SandboxMode::Project {
return Err(RuntimeError::new(
ErrorCategory::PermissionDenied,
ctx.span,
"tcp-listener resources are available only in project mode",
));
}
if requested != BTreeSet::from(["accept"]) {
return Err(RuntimeError::new(
ErrorCategory::ResourceUnsupported,
ctx.span,
"tcp-listener requires exactly [accept] in this build",
));
}
let (host, port) = parse_host_port(descriptor, ctx.span)?;
self.capabilities.check_net(&host, ctx.span)?;
let addresses = (host.as_str(), port).to_socket_addrs().map_err(|error| {
RuntimeError::new(
ErrorCategory::HostError,
ctx.span,
format!("cannot resolve `{host}:{port}`: {error}"),
)
})?;
let mut listener = None;
let mut last_error = None;
for address in addresses {
match TcpListener::bind(address) {
Ok(bound) => {
listener = Some(bound);
break;
}
Err(error) => last_error = Some((address, error)),
}
}
let Some(listener) = listener else {
let detail = match last_error {
Some((address, error)) => format!("{address}: {error}"),
None => format!("`{host}:{port}` resolved to no addresses"),
};
return Err(RuntimeError::new(
ErrorCategory::HostError,
ctx.span,
format!("cannot listen on `{host}:{port}`: {detail}"),
));
};
listener.set_nonblocking(true).map_err(|error| {
RuntimeError::new(
ErrorCategory::HostError,
ctx.span,
format!("cannot configure tcp-listener: {error}"),
)
})?;
let handle = self.resources.open_tcp_listener(listener);
self.record_semantic(
"resource.open",
None,
Some(&ctx.module_id),
ctx.span,
Some(&format!("kind=tcp-listener host={host} port={port}")),
);
self.record_resource_effect("resource.open", ctx, started, "ok");
Ok(Value::Resource(handle))
}
"udp" => {
if self.mode != SandboxMode::Project {
return Err(RuntimeError::new(
ErrorCategory::PermissionDenied,
ctx.span,
"udp resources are available only in project mode",
));
}
if requested != BTreeSet::from(["read", "write"]) {
return Err(RuntimeError::new(
ErrorCategory::ResourceUnsupported,
ctx.span,
"udp resources require exactly [read, write] in this build",
));
}
let (host, port) = parse_host_port(descriptor, ctx.span)?;
self.capabilities.check_net(&host, ctx.span)?;
let addresses = (host.as_str(), port).to_socket_addrs().map_err(|error| {
RuntimeError::new(
ErrorCategory::HostError,
ctx.span,
format!("cannot resolve `{host}:{port}`: {error}"),
)
})?;
let mut socket = None;
let mut last_error = None;
for address in addresses {
let bind = if address.is_ipv4() {
"0.0.0.0:0"
} else {
"[::]:0"
};
let candidate = UdpSocket::bind(bind).and_then(|socket| {
socket.connect(address)?;
Ok(socket)
});
match candidate {
Ok(connected) => {
socket = Some(connected);
break;
}
Err(error) => last_error = Some((address, error)),
}
}
let Some(socket) = socket else {
let detail = match last_error {
Some((address, error)) => format!("{address}: {error}"),
None => format!("`{host}:{port}` resolved to no addresses"),
};
return Err(RuntimeError::new(
ErrorCategory::HostError,
ctx.span,
format!("cannot connect udp to `{host}:{port}`: {detail}"),
));
};
let _ = socket.set_read_timeout(Some(Duration::from_secs(5)));
let _ = socket.set_write_timeout(Some(Duration::from_secs(5)));
let handle = self.resources.open_udp(socket);
self.record_semantic(
"resource.open",
None,
Some(&ctx.module_id),
ctx.span,
Some(&format!("kind=udp host={host} port={port}")),
);
self.record_resource_effect("resource.open", ctx, started, "ok");
Ok(Value::Resource(handle))
+161 -2
View File
@@ -7,7 +7,9 @@
use std::collections::BTreeMap;
use std::io::{Read, Write};
use std::net::TcpStream;
use std::net::{TcpListener, TcpStream, UdpSocket};
use rustls::{ClientConnection, StreamOwned};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
@@ -81,6 +83,7 @@ struct StdinCursor {
position: usize,
}
#[allow(clippy::large_enum_variant)]
enum Stream {
Stdin {
cursor: Arc<Mutex<StdinCursor>>,
@@ -113,6 +116,18 @@ enum Stream {
stream: TcpStream,
closed: bool,
},
Udp {
socket: UdpSocket,
closed: bool,
},
TcpListener {
listener: TcpListener,
closed: bool,
},
Tls {
stream: StreamOwned<ClientConnection, TcpStream>,
closed: bool,
},
}
struct Slot {
@@ -130,6 +145,9 @@ impl Slot {
Stream::FileWriter { closed, .. } => *closed,
Stream::Provider { closed, .. } => *closed,
Stream::Tcp { closed, .. } => *closed,
Stream::Udp { closed, .. } => *closed,
Stream::TcpListener { closed, .. } => *closed,
Stream::Tls { closed, .. } => *closed,
}
}
}
@@ -205,7 +223,11 @@ impl ResourceTable {
Stream::Stdout { .. } => Some("stdout"),
Stream::Stderr { .. } => Some("stderr"),
Stream::File { .. } | Stream::FileWriter { .. } => None,
Stream::Provider { .. } | Stream::Tcp { .. } => None,
Stream::Provider { .. }
| Stream::Tcp { .. }
| Stream::Udp { .. }
| Stream::TcpListener { .. }
| Stream::Tls { .. } => None,
})
}
@@ -236,6 +258,83 @@ impl ResourceTable {
})
}
/// Register a connected UDP socket.
pub fn open_udp(&mut self, socket: UdpSocket) -> ResourceHandle {
self.insert(Stream::Udp {
socket,
closed: false,
})
}
/// Register a bound TCP listener.
pub fn open_tcp_listener(&mut self, listener: TcpListener) -> ResourceHandle {
self.insert(Stream::TcpListener {
listener,
closed: false,
})
}
/// Register a TLS client stream over a TCP socket.
pub fn open_tls(&mut self, stream: StreamOwned<ClientConnection, TcpStream>) -> ResourceHandle {
self.insert(Stream::Tls {
stream,
closed: false,
})
}
/// Accept one inbound TCP connection from a listener, with a 5 s deadline.
pub fn accept(
&mut self,
handle: ResourceHandle,
span: Option<Span>,
) -> Result<ResourceHandle, RuntimeError> {
let slot = self.slot_mut(handle, span)?;
if slot.closed() {
return Err(RuntimeError::new(
ErrorCategory::ResourceClosed,
span,
"Resource handle is closed",
));
}
let Stream::TcpListener { listener, .. } = &mut slot.stream else {
return Err(RuntimeError::new(
ErrorCategory::ResourceUnsupported,
span,
"resource.accept requires a tcp-listener handle",
));
};
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
loop {
match listener.accept() {
Ok((stream, _)) => {
let _ = stream.set_read_timeout(Some(std::time::Duration::from_secs(5)));
let _ = stream.set_write_timeout(Some(std::time::Duration::from_secs(5)));
return Ok(self.insert(Stream::Tcp {
stream,
closed: false,
}));
}
Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => {
if std::time::Instant::now() >= deadline {
return Err(RuntimeError::new(
ErrorCategory::HostError,
span,
"tcp-listener accept timed out after 5 seconds",
));
}
std::thread::sleep(std::time::Duration::from_millis(10));
}
Err(error) => {
return Err(RuntimeError::new(
ErrorCategory::HostError,
span,
format!("tcp-listener accept failed: {error}"),
))
}
}
}
}
/// Open a provider token and wrap it in a host-issued handle.
pub fn open_provider(
&mut self,
@@ -395,6 +494,41 @@ impl ResourceTable {
eof: count == 0,
})
}
Stream::TcpListener { .. } => Err(RuntimeError::new(
ErrorCategory::ResourceUnsupported,
span,
"a tcp-listener does not support resource.read; use resource.accept",
)),
Stream::Tls { stream, .. } => {
let mut buffer = vec![0u8; max_bytes];
let count = stream.read(&mut buffer).map_err(|error| {
RuntimeError::new(
ErrorCategory::HostError,
span,
format!("tls read failed: {error}"),
)
})?;
buffer.truncate(count);
Ok(ResourceRead {
bytes: buffer,
eof: count == 0,
})
}
Stream::Udp { socket, .. } => {
let mut buffer = vec![0u8; max_bytes];
let count = socket.recv(&mut buffer).map_err(|error| {
RuntimeError::new(
ErrorCategory::HostError,
span,
format!("udp read failed: {error}"),
)
})?;
buffer.truncate(count);
Ok(ResourceRead {
bytes: buffer,
eof: false,
})
}
}
}
@@ -478,6 +612,25 @@ impl ResourceTable {
format!("tcp write failed: {error}"),
)
}),
Stream::TcpListener { .. } => Err(RuntimeError::new(
ErrorCategory::ResourceUnsupported,
span,
"a tcp-listener does not support resource.write; use resource.accept",
)),
Stream::Tls { stream, .. } => stream.write(bytes).map_err(|error| {
RuntimeError::new(
ErrorCategory::HostError,
span,
format!("tls write failed: {error}"),
)
}),
Stream::Udp { socket, .. } => socket.send(bytes).map_err(|error| {
RuntimeError::new(
ErrorCategory::HostError,
span,
format!("udp write failed: {error}"),
)
}),
}
}
@@ -515,6 +668,9 @@ impl ResourceTable {
let _ = stream.shutdown(std::net::Shutdown::Both);
*closed = true;
}
Stream::Udp { closed, .. } => *closed = true,
Stream::TcpListener { closed, .. } => *closed = true,
Stream::Tls { closed, .. } => *closed = true,
}
Ok(())
}
@@ -555,6 +711,9 @@ impl ResourceTable {
Stream::File { .. } | Stream::FileWriter { .. } => "file".to_string(),
Stream::Provider { kind, .. } => kind.clone(),
Stream::Tcp { .. } => "tcp".to_string(),
Stream::Udp { .. } => "udp".to_string(),
Stream::TcpListener { .. } => "tcp-listener".to_string(),
Stream::Tls { .. } => "tls".to_string(),
})
}
}
+267 -19
View File
@@ -471,28 +471,21 @@ fn tcp_resource_round_trips_with_a_network_grant() {
}
#[test]
fn tcp_requires_network_grant_and_rejects_dns_names() {
fn tcp_requires_a_host_grant_in_project_mode() {
let dir = tempfile::tempdir().unwrap();
let mut host = StdHost::new(SandboxMode::Project, Capabilities::project(), dir.path());
let ctx = context();
let denied = host
.resource_open(
"tcp",
"127.0.0.1:9",
&["read".to_string(), "write".to_string()],
&ctx,
)
.unwrap_err();
assert_eq!(denied.category, ErrorCategory::PermissionDenied);
let dns = host
.resource_open(
"tcp",
"example.test:80",
&["read".to_string(), "write".to_string()],
&ctx,
)
.unwrap_err();
assert_eq!(dns.category, ErrorCategory::ResourceUnsupported);
for descriptor in ["127.0.0.1:9", "example.test:80"] {
let denied = host
.resource_open(
"tcp",
descriptor,
&["read".to_string(), "write".to_string()],
&ctx,
)
.unwrap_err();
assert_eq!(denied.category, ErrorCategory::PermissionDenied);
}
for mode in [SandboxMode::Pure, SandboxMode::Strict] {
let mut host = StdHost::new(
@@ -512,6 +505,42 @@ fn tcp_requires_network_grant_and_rejects_dns_names() {
}
}
#[test]
fn tcp_dns_name_connects_with_a_host_grant() {
use std::io::{Read, Write};
use std::net::TcpListener;
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
let port = listener.local_addr().unwrap().port();
let server = std::thread::spawn(move || {
let (mut stream, _) = listener.accept().unwrap();
let mut buffer = [0u8; 4];
stream.read_exact(&mut buffer).unwrap();
stream.write_all(&buffer).unwrap();
});
let dir = tempfile::tempdir().unwrap();
let mut host = StdHost::new(
SandboxMode::Project,
Capabilities::project().allow_net("localhost"),
dir.path(),
);
let ctx = context();
let handle = handle(
host.resource_open(
"tcp",
&format!("localhost:{port}"),
&["read".to_string(), "write".to_string()],
&ctx,
)
.unwrap(),
);
assert_eq!(host.resource_write(handle, b"ping", &ctx).unwrap(), 4);
assert_eq!(host.resource_read(handle, 4, &ctx).unwrap().bytes, b"ping");
host.resource_close(handle, &ctx).unwrap();
server.join().unwrap();
}
fn field<'a>(value: &'a Value, name: &str) -> &'a Value {
match value {
Value::Map(map) => map.get(name).unwrap_or_else(|| panic!("missing `{name}`")),
@@ -843,3 +872,222 @@ fn random_bytes_require_the_entropy_grant() {
.unwrap_err();
assert_eq!(error.category, ErrorCategory::PermissionDenied);
}
#[test]
fn udp_resource_round_trips_a_datagram_with_a_network_grant() {
use std::net::UdpSocket;
let server = UdpSocket::bind("127.0.0.1:0").unwrap();
let port = server.local_addr().unwrap().port();
let server = std::thread::spawn(move || {
let mut buffer = [0u8; 16];
let (count, peer) = server.recv_from(&mut buffer).unwrap();
server.send_to(&buffer[..count], peer).unwrap();
});
let dir = tempfile::tempdir().unwrap();
let mut host = StdHost::new(
SandboxMode::Project,
Capabilities::project().allow_net("127.0.0.1"),
dir.path(),
);
let ctx = context();
let handle = handle(
host.resource_open(
"udp",
&format!("127.0.0.1:{port}"),
&["read".to_string(), "write".to_string()],
&ctx,
)
.unwrap(),
);
assert_eq!(host.resource_write(handle, b"ping", &ctx).unwrap(), 4);
let read = host.resource_read(handle, 16, &ctx).unwrap();
assert_eq!(read.bytes, b"ping");
assert!(!read.eof);
host.resource_close(handle, &ctx).unwrap();
server.join().unwrap();
}
#[test]
fn udp_is_project_only_and_grant_checked() {
let dir = tempfile::tempdir().unwrap();
let ctx = context();
for mode in [SandboxMode::Pure, SandboxMode::Strict] {
let mut host = StdHost::new(
mode,
Capabilities::project().allow_net("127.0.0.1"),
dir.path(),
);
let error = host
.resource_open(
"udp",
"127.0.0.1:9",
&["read".to_string(), "write".to_string()],
&ctx,
)
.unwrap_err();
assert_eq!(error.category, ErrorCategory::PermissionDenied);
}
let mut project = StdHost::new(SandboxMode::Project, Capabilities::project(), dir.path());
let error = project
.resource_open(
"udp",
"127.0.0.1:9",
&["read".to_string(), "write".to_string()],
&ctx,
)
.unwrap_err();
assert_eq!(error.category, ErrorCategory::PermissionDenied);
}
#[test]
fn tcp_listener_accepts_an_inbound_connection() {
use std::io::{Read, Write};
use std::net::{TcpListener, TcpStream};
let probe = TcpListener::bind("127.0.0.1:0").unwrap();
let port = probe.local_addr().unwrap().port();
drop(probe);
let dir = tempfile::tempdir().unwrap();
let mut host = StdHost::new(
SandboxMode::Project,
Capabilities::project().allow_net("127.0.0.1"),
dir.path(),
);
let ctx = context();
let listener = handle(
host.resource_open(
"tcp-listener",
&format!("127.0.0.1:{port}"),
&["accept".to_string()],
&ctx,
)
.unwrap(),
);
let client = std::thread::spawn(move || {
let mut stream = TcpStream::connect(format!("127.0.0.1:{port}")).unwrap();
stream.write_all(b"ping").unwrap();
let mut buffer = [0u8; 4];
stream.read_exact(&mut buffer).unwrap();
assert_eq!(&buffer, b"ping");
});
let accepted = host.resource_accept(listener, &ctx).unwrap();
let value = field(&accepted, "value");
let Value::Resource(connection) = value else {
panic!("resource.accept did not return a Resource");
};
assert_eq!(
host.resource_read(*connection, 4, &ctx).unwrap().bytes,
b"ping"
);
assert_eq!(host.resource_write(*connection, b"ping", &ctx).unwrap(), 4);
host.resource_close(*connection, &ctx).unwrap();
host.resource_close(listener, &ctx).unwrap();
client.join().unwrap();
}
#[test]
fn tcp_listener_requires_accept_rights_and_project_mode() {
let dir = tempfile::tempdir().unwrap();
let ctx = context();
let mut project = StdHost::new(
SandboxMode::Project,
Capabilities::project().allow_net("127.0.0.1"),
dir.path(),
);
let error = project
.resource_open("tcp-listener", "127.0.0.1:9", &["read".to_string()], &ctx)
.unwrap_err();
assert_eq!(error.category, ErrorCategory::ResourceUnsupported);
let mut strict = StdHost::new(
SandboxMode::Strict,
Capabilities::project().allow_net("127.0.0.1"),
dir.path(),
);
let error = strict
.resource_open("tcp-listener", "127.0.0.1:9", &["accept".to_string()], &ctx)
.unwrap_err();
assert_eq!(error.category, ErrorCategory::PermissionDenied);
}
#[test]
fn tls_resource_round_trips_with_an_explicit_root() {
use std::io::{Read, Write};
use std::net::TcpListener;
use std::sync::Arc;
let certified = rcgen::generate_simple_self_signed(vec!["localhost".to_string()]).unwrap();
let certificate = certified.cert.der().clone();
let key = rustls::pki_types::PrivateKeyDer::Pkcs8(rustls::pki_types::PrivatePkcs8KeyDer::from(
certified.key_pair.serialize_der(),
));
let config = rustls::ServerConfig::builder()
.with_no_client_auth()
.with_single_cert(vec![certificate.clone()], key)
.unwrap();
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
let port = listener.local_addr().unwrap().port();
let server = std::thread::spawn(move || {
let (stream, _) = listener.accept().unwrap();
let connection = rustls::ServerConnection::new(Arc::new(config)).unwrap();
let mut stream = rustls::StreamOwned::new(connection, stream);
let mut buffer = [0u8; 4];
stream.read_exact(&mut buffer).unwrap();
stream.write_all(&buffer).unwrap();
});
let dir = tempfile::tempdir().unwrap();
let mut host = StdHost::new(
SandboxMode::Project,
Capabilities::project().allow_net("localhost"),
dir.path(),
)
.with_tls_ca(certificate);
let ctx = context();
let handle = handle(
host.resource_open(
"tls",
&format!("localhost:{port}"),
&["read".to_string(), "write".to_string()],
&ctx,
)
.unwrap(),
);
assert_eq!(host.resource_write(handle, b"ping", &ctx).unwrap(), 4);
assert_eq!(host.resource_read(handle, 4, &ctx).unwrap().bytes, b"ping");
host.resource_close(handle, &ctx).unwrap();
server.join().unwrap();
}
#[test]
fn tls_requires_project_mode_and_a_host_grant() {
let dir = tempfile::tempdir().unwrap();
let ctx = context();
let mut project = StdHost::new(SandboxMode::Project, Capabilities::project(), dir.path());
let error = project
.resource_open(
"tls",
"localhost:9",
&["read".to_string(), "write".to_string()],
&ctx,
)
.unwrap_err();
assert_eq!(error.category, ErrorCategory::PermissionDenied);
let mut strict = StdHost::new(
SandboxMode::Strict,
Capabilities::project().allow_net("localhost"),
dir.path(),
);
let error = strict
.resource_open(
"tls",
"localhost:9",
&["read".to_string(), "write".to_string()],
&ctx,
)
.unwrap_err();
assert_eq!(error.category, ErrorCategory::PermissionDenied);
}
+8
View File
@@ -305,6 +305,13 @@ const RESOURCE_REQUEST: Builtin = Builtin {
named_only: false,
effectful: true,
};
const RESOURCE_ACCEPT: Builtin = Builtin {
name: "resource.accept",
aliases: &[],
params: &[req("handle")],
named_only: false,
effectful: true,
};
const TABLE_CELL: Builtin = Builtin {
name: "table.cell",
aliases: &[],
@@ -353,6 +360,7 @@ pub const BUILTINS: &[Builtin] = &[
RESOURCE_WRITE,
RESOURCE_CLOSE,
RESOURCE_REQUEST,
RESOURCE_ACCEPT,
];
/// Find a builtin by canonical name or alias.
@@ -478,6 +478,10 @@ pub(crate) fn dispatch(
Err(error) => resource_recoverable(error),
}
}
"resource.accept" => {
let handle = resource_handle(take(&bound, "handle", builtin, span)?, span)?;
interpreter.resource_accept(handle, &module, span)
}
"resource.request" => {
let operation = expect_text_value(take(&bound, "operation", builtin, span)?, span)?;
let value_type = bound["arguments"].type_name();
+16
View File
@@ -903,6 +903,22 @@ impl<'a> Interpreter<'a> {
})
}
/// Accept one inbound TCP connection from a listener handle.
pub(crate) fn resource_accept(
&mut self,
handle: ResourceHandle,
module: &ModuleId,
span: Option<Span>,
) -> Result<Value, RuntimeError> {
let context = ExecutionContext::new(module.to_string(), span);
self.host
.resource_accept(handle, &context)
.map_err(|mut error| {
error.mark_callsite();
error
})
}
/// Execute one non-streaming resource operation.
pub(crate) fn resource_request(
&mut self,
+7 -6
View File
@@ -58,7 +58,7 @@ byte-identical).
cargo fmt --all -- --check PASS
cargo clippy --workspace --all-targets -- -D warnings PASS
cargo test --workspace --locked PASS
481 passed, 0 failed, 2 ignored; includes 7/7 Linux security tests
488 passed, 0 failed, 2 ignored; includes 7/7 Linux security tests
```
The documentation and test migration is local work; re-run this block after it
@@ -267,11 +267,12 @@ This is still a **local VM lane**, not a self-hosted GitHub Actions security URL
4. `fs.write`, `net.request`, `process`, `clock`, `random` are not executed
broker services; unsupported requests must return `capability.unavailable`.
5. Resource streams cover project-file read/write (including `std:fs/file`
read/write/copy), non-streaming `resource.request` `fs.*` metadata/directory
operations, stdout, compatible print, trusted in-process providers and
IP-literal TCP; append/seek/watch, process, device, DNS/TLS/HTTP, UDP and
provider packaging remain open. Strict permits only stdio and refuses the
rest (CM-A11 lane PASS).
read/write/copy/append/read_at/walk), non-streaming `resource.request`
`fs.*` metadata/directory operations, stdout, compatible print, trusted
in-process providers and TCP/UDP/listener/TLS endpoints with DNS
resolution after an exact host grant. File watch, device and provider
packaging remain open. Strict permits only stdio and refuses the rest (CM-A11 lane
PASS).
6. uv/npm/venv provisioning is not implemented. Only a pre-provisioned,
digest-pinned directory with a regular-file executable is supported.
7. No Windows/macOS RC archive exists from this host. The release-candidate
+46
View File
@@ -0,0 +1,46 @@
# ADR-0047: DNS resolution, UDP and TCP listener endpoints
Status: **Accepted**
Date: 2026-11-21
Relates to: ADR-0034, ADR-0041
Specs: `spec/security.md`, `spec/evaluation.md`
Evidence: `docs/evidence/2026-11-21-net-endpoints.md`
## Context
ADR-0034 opened only an outbound IP-literal TCP stream. That left DNS names,
datagrams and inbound listeners unavailable, and the `--allow-net` grant was
documented as IP-only. The audit RFC's P2 network gap requires real endpoints,
not a `TODO`.
## Decision
* `resource.open("tcp", "host:port", ["read","write"])` now accepts a DNS name
or IP literal. The **host** (not the resolved IP) is checked against the
exact `--allow-net HOST` grant **before** `ToSocketAddrs`; every resolved
address is tried with a 5 s connect timeout.
* Add `resource.open("udp", "host:port", ["read","write"])`: a connected
`UdpSocket` from an ephemeral local port. `resource.read` returns one
datagram (truncated to the destination `Memory` bound, `eof=false`);
`resource.write` sends one datagram.
* Add `resource.open("tcp-listener", "host:port", ["accept"])` plus the
`resource.accept(listener)` builtin. `accept` polls a non-blocking listener
for up to 5 s, returns a new TCP `Resource` in `{ok:true, value:Resource}`,
or `{ok:false, code:"tcp.timeout"}`/`tcp.accept` on failure.
* All three are project-mode only. `pure`/`strict` return `permission.denied`;
the listener still requires the exact host grant.
* Host names are accepted but **not** pinned to an IP: a DNS answer may change
between grant and connect. No TLS, certificate validation, server sockets
outside project mode or UDP broadcast/multicast is added.
* The grant flag remains `--allow-net HOST` (repeatable); its help text now says
host name or IP literal.
## Consequences
* DNS, UDP and inbound TCP are reachable from `.mq.md` without a new builtin
per transport. `resource.accept` is the only new builtin and returns an
opaque handle like `resource.open`.
* TLS still needs a separate decision (certificate validation and trust
anchors); this ADR does not claim TLS.
* `resource.read` on UDP is datagram-based; a zero-length datagram is a
successful empty read, and there is no stream EOF.
+39
View File
@@ -0,0 +1,39 @@
# ADR-0048: TLS client resource endpoint
Status: **Accepted**
Date: 2026-11-21
Relates to: ADR-0034, ADR-0047
Specs: `spec/security.md`, `spec/evaluation.md`
Evidence: `docs/evidence/2026-11-21-tls-endpoint.md`
## Context
ADR-0047 added DNS, UDP and TCP listeners but deliberately left TLS out: a TLS
client needs a trust-anchor policy and certificate validation, not just a
socket. The audit RFC lists TLS as a P2 provider gap.
## Decision
* `resource.open("tls", "host:port", ["read","write"])` connects with TCP,
validates the server certificate with `rustls`, and returns an opaque
`tls` Resource. `resource.read`/`write`/`close` work like TCP.
* The exact host grant (`--allow-net HOST`) is checked before DNS resolution.
`pure`/`strict` refuse; only project mode is supported.
* Trust roots are the platform native roots plus any DER certificates added by
an embedder through `StdHost::with_tls_ca`. There is no CLI flag for a private
CA in this build; the API is for embedders/tests.
* Server-name verification uses the descriptor host, not the resolved IP.
Certificate or handshake failures are fatal `host.error`; a failed handshake
closes the connection and does not return a Resource.
* TLS is client-only. No server TLS acceptor, mutual TLS, ALPN, session
resumption, OCSP or custom root CLI is added.
* `rustls` is used instead of `native-tls` so the build does not require
system OpenSSL headers; it remains a normal Rust dependency.
## Consequences
* `resource.open("tls")` is a real validated TLS client endpoint and is covered
by a local self-signed test that adds an explicit DER root.
* A future CLI `--tls-ca` or provider-managed trust store needs a separate
grant/ADR; this decision does not silently trust unknown certificates.
* TLS server sockets and mutual TLS remain open.
+5 -2
View File
@@ -18,7 +18,7 @@ platform".
| Dimension | Current state | Source |
|---|---|---|
| Implemented | Rust workspace `0.1.0`: Markdown/frontend/IR, tree-walk runtime, `Memory`, capability host, Tool/Analysis/Trace, Plugin ABI, Foreign kernels, bundled stdlib, CLI composition root | `Cargo.toml`, `crates/`, `stdlib/` |
| Tested locally | `cargo test --workspace --locked`: **481 passed, 0 failed, 2 ignored** on Linux x86_64 at the baseline | `docs/STATUS.md` §3 |
| Tested locally | `cargo test --workspace --locked`: **488 passed, 0 failed, 2 ignored** on Linux x86_64 at the baseline | `docs/STATUS.md` §3 |
| Bundled stdlib released | In-repo bundle `marqdo-stdlib@0.1.0`, SHA-256 digest verified before loading; it is **not** an external registry package | `stdlib/stdlib.json`, `stdlib/README.md` |
| Multi-platform verified | Linux local gate is the only executed lane here; Windows/macOS native lanes are **UNVERIFIED**, and the GitHub security lane remains a **VM-only** gate | `docs/STATUS.md` §1, §4 |
@@ -101,6 +101,7 @@ review RFC, not a new crate per provider.
| `std:env` | `env.读取/存在` -> `resource.request("env.get", [name])`; exact `--allow-env` grant | implemented (project mode; no `env.list`) | `resource_streams`, `stdlib_product` |
| `std:process` | `process.运行(程序, 参数列表)` -> `resource.request("process.run", ...)`; exact `--allow-process` grant, 5 s timeout, 1 MiB caps | implemented (project mode; not an OS sandbox) | `resource_streams`, `stdlib_product` |
| `std:random` | `random.字节/整数` -> `resource.request("random.bytes", [count])`; exact `--allow-random` grant | implemented (project mode; byte-list RNG) | `resource_streams`, `stdlib_product` |
| network endpoints | `tcp`/`udp`/`tcp-listener`/`tls` open + `resource.accept`; exact host grant, DNS after grant, TLS certificate validation | implemented (project mode) | `resource_streams` |
| file metadata / directory / rename / remove / mkdir | not part of the current Resource contract | **not implemented**; needs a separate Resource Operation ADR | — |
| TCP | `resource.open("tcp", ip:port, ["read","write"])`; exact IP grant, 5 s timeout | implemented; no DNS/TLS/server socket/UDP | `tcp_endpoint`, `http_over_tcp` |
| HTTP/1.1 | `stdlib/net/http.mq.md`: status/header/chunked-byte parse, request build | implemented over an already-authorized TCP stream; not a DNS/TLS client | `stdlib_http`, `http_over_tcp`, `stdlib_product` |
@@ -131,7 +132,7 @@ that a phase is complete.
| P1 | JSON/CSV product parsers are implemented; no streaming/multi-line CSV or depth-bounded JSON walker | open |
| P1 | CLI split into `commands/{sandbox,run,check,inspect,init,package,tool}.rs`; host split into `host/{resource_ops,providers,effects,dispatch}.rs` plus `process.rs`; `commands.rs` keeps shared project/runtime helpers and `host.rs` keeps the `Host` trait methods and capability checks | split; shared helpers and trait methods remain by design |
| 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 | DNS, UDP, TCP listener and TLS client endpoints are implemented; no TLS server/mTLS or custom-CA CLI | open |
| P2 | `std:path`, a regex subset, `std:datetime`, `std:clock`, `std:env`, `std:process` and `std:random` are implemented; no compression | open |
| P2 | Memory limits: 16 MiB per region, 64 MiB per run | current budget, not a product limit |
| P3 | No concurrency tasks, atomics or cancellable long-running scheduling | open; separate ADR |
@@ -168,5 +169,7 @@ that a phase is complete.
- [ADR-0044: `process.run` resource operation](../adr/0044-process-resource-operation.md) — accepted exact-path process grant; not a sandbox.
- [ADR-0045: fs append/read_at/walk](../adr/0045-fs-append-read-at-walk.md) — accepted bounded content and directory walk.
- [ADR-0046: `random.bytes`](../adr/0046-random-resource-operation.md) — accepted explicitly granted OS entropy.
- [ADR-0047: DNS/UDP/TCP listener](../adr/0047-net-dns-udp-listener.md) — accepted project-mode network endpoints.
- [ADR-0048: TLS client endpoint](../adr/0048-tls-resource-endpoint.md) — accepted validated rustls client; system + embedder DER roots.
- [Compute-machine RFC-0001](../MARQDO_COMPUTE_MACHINE_RFC_0001.md) and [core/stdlib RFC-0002](../MARQDO_COMPUTE_CORE_STDLIB_MIGRATION_RFC_0002.md) — design input.
- [Architecture audit and refactor RFC](Marqdo_CLI_Latest_Architecture_Audit_and_Refactor_RFC.md) — review input for this migration.
+2
View File
@@ -51,6 +51,8 @@ The implemented first slices of these RFCs are recorded in `CURRENT.md` and
| `process.run` resource operation | [0044](../adr/0044-process-resource-operation.md) | Accepted (exact program grant, project only, not a sandbox) |
| `fs.append`/`fs.read_at`/`fs.walk` | [0045](../adr/0045-fs-append-read-at-walk.md) | Accepted (bounded content, directory walk) |
| `random.bytes` resource operation | [0046](../adr/0046-random-resource-operation.md) | Accepted (explicit `--allow-random`, project only) |
| DNS/UDP/TCP-listener endpoints | [0047](../adr/0047-net-dns-udp-listener.md) | Accepted (project only, exact host grant, no TLS) |
| TLS client endpoint | [0048](../adr/0048-tls-resource-endpoint.md) | Accepted (rustls, validated, project only) |
| Table physical storage (T1/T2/T3), strict stdin/stderr/file/network resources | — | **Not decided**; do not implement by implication |
## Evidence and status
+39
View File
@@ -0,0 +1,39 @@
# Evidence: DNS, UDP and TCP listener endpoints
Date: 2026-11-21 (Asia/Shanghai)
Source baseline: `ee0c6cc80a2782c4e88de63195eaf41a27077dcf`
Repository: `marqdo-cli`
Decision record: [ADR-0047](../adr/0047-net-dns-udp-listener.md)
## Implemented
- `resource.open("tcp", "host:port", ["read","write"])`: exact host grant is
checked **before** `ToSocketAddrs`; every resolved address is tried with a
5 s connect timeout.
- `resource.open("udp", "host:port", ["read","write"])`: connected datagram
socket from an ephemeral local port; `read` returns one datagram with
`eof=false`; `write` sends one datagram.
- `resource.open("tcp-listener", "host:port", ["accept"])` plus
`resource.accept(listener)`: non-blocking 5 s poll, returns a new TCP
`Resource` in `{ok:true,value:Resource}` or `tcp.timeout`.
- All project-mode only; pure/strict refuse; no TLS.
## Tests
`crates/marqdo-host/tests/resource_streams.rs` (30 tests) covers TCP DNS
round-trip via `localhost`, UDP datagram round-trip, listener accept/read/write,
host-grant denial and project-only refusal.
```text
cargo fmt --all -- --check PASS
cargo clippy --workspace --all-targets -- -D warnings PASS
cargo test --workspace 486 passed, 0 failed, 2 ignored
cargo test -p marqdo-host --test resource_streams 30 passed
python3 tools/pack-stdlib.py PASS
```
## Not covered
No TLS/certificate validation, no server sockets outside project mode, no UDP
broadcast/multicast, no listener port discovery API, no connection pool and no
DNS pinning. TLS remains a separate ADR.
+39
View File
@@ -0,0 +1,39 @@
# Evidence: TLS client endpoint
Date: 2026-11-21 (Asia/Shanghai)
Source baseline: `ee0c6cc80a2782c4e88de63195eaf41a27077dcf`
Repository: `marqdo-cli`
Decision record: [ADR-0048](../adr/0048-tls-resource-endpoint.md)
## Implemented
`resource.open("tls", "host:port", ["read","write"])` is a project-mode
validated TLS client:
- exact host grant before DNS;
- TCP connect with 5 s timeout, then `rustls` handshake using platform native
roots plus `StdHost::with_tls_ca` DER roots;
- server name is the descriptor host; certificate/handshake failure is fatal
`host.error`;
- the resulting `tls` Resource supports `resource.read/write/close`;
- no TLS server, mTLS, ALPN or custom-CA CLI.
## Tests
`crates/marqdo-host/tests/resource_streams.rs` starts a local `rustls` server
with an `rcgen` self-signed certificate, adds that DER as an explicit root and
round-trips a 4-byte payload. A second test pins project-only/host-grant
refusal (32 host tests).
```text
cargo fmt --all -- --check PASS
cargo clippy --workspace --all-targets -- -D warnings PASS
cargo test --workspace 488 passed, 0 failed, 2 ignored
cargo test -p marqdo-host --test resource_streams 32 passed
python3 tools/pack-stdlib.py PASS
```
## Not covered
No TLS server/mTLS, no ALPN, no OCSP/CRL, no client certificates, no CLI
private-CA flag and no session resumption.
+3
View File
@@ -166,6 +166,8 @@ resource.write(handle, memory: Memory, offset: Int, length: Int)
-> {ok:true, value:{count:Int}} | {ok:false, code, message}
resource.close(handle)
-> {ok:true, value:None} | {ok:false, code, message}
resource.accept(listener)
-> {ok:true, value:Resource} | {ok:false, code, message}
resource.request(operation: Text, arguments: List) -> Result
```
@@ -342,6 +344,7 @@ host boundary. Names are stable; adding one requires a spec update.
| `resource.read` | `(handle, memory: Memory, offset: Int, max_bytes: Int) -> Result` | bounded chunk read into Memory; `{count,eof}` |
| `resource.write` | `(handle, memory: Memory, offset: Int, length: Int) -> Result` | bounded byte write; `{count}` |
| `resource.close` | `(handle) -> Result` | releases the host slot |
| `resource.accept` | `(listener) -> Result` | accepts one TCP connection from a `tcp-listener` handle (ADR-0047) |
| `resource.request` | `(operation: Text, arguments: List) -> Result` | non-streaming `fs.*` operations (ADR-0041) `clock.now` (ADR-0042), `env.get` (ADR-0043), `process.run` (ADR-0044) and `random.bytes` (ADR-0046); project-mode and grant-checked |
| `math.parse/eval/substitute` | see `spec/math.md` | P5; `math.simplify`/`math.differentiate` are declared but return `math.unsupported` in this build |
| `foreign.run/call` | see `spec/foreign.md` | P5, capability-gated |
+4 -4
View File
@@ -27,7 +27,7 @@ Normative implementation:
| Mode | Intended guarantee | Actual status in this build |
|---|---|---|
| `pure` | No external I/O; memory computation and budgets only. | **Enforced.** `Capabilities::pure()` = `console`, `math` only; every FS/foreign/plugin grant flag makes the CLI exit 2 with "pure mode prohibits external I/O grants". |
| `project` | Operator-approved project resources under `CapabilityHost` + bounded resources. | **Enforced for the implemented operations.** Defaults equal `pure`; `--allow-read`, `--allow-write`, `--allow-net`, `--allow-env`, `--allow-process`, `--allow-random`, `--allow-foreign`, `--allow-plugin` add grants. `resource.open` supports stdin read, separate stdout/stderr write, granted project-file read/write (ADR-0025/0026/0040), granted in-process provider kinds (ADR-0033) and granted IP-literal TCP streams (ADR-0034); process/device remain unsupported. Sandboxing is cooperative (in-process checks), not OS isolation; in-process providers are trusted, not sandboxed. |
| `project` | Operator-approved project resources under `CapabilityHost` + bounded resources. | **Enforced for the implemented operations.** Defaults equal `pure`; `--allow-read`, `--allow-write`, `--allow-net`, `--allow-env`, `--allow-process`, `--allow-random`, `--allow-foreign`, `--allow-plugin` add grants. `resource.open` supports stdin read, separate stdout/stderr write, granted project-file read/write (ADR-0025/0026/0040), granted in-process provider kinds (ADR-0033) and granted TCP/UDP/listener/TLS streams, including DNS names after an exact host grant (ADR-0034/0047/0048); process/device remain unsupported. Sandboxing is cooperative (in-process checks), not OS isolation; in-process providers are trusted, not sandboxed. |
| `strict` | Untrusted documents under real OS isolation. | **Linux: enforced when the runtime probe passes (ADR-0021).** `resource.open` is **refused** in strict except for the existing `stdio/stdout` console channel (console grant); stdin/stderr, file/TCP/provider/process resources remain refused until a provider passes the isolation lane (CM-A11, ADR-0030/0034/0040). `PlatformCapabilities::detect()` runs a real Bubblewrap smoke test; success reports `Available { mechanism }`, failure reports `Unavailable { reason }`. Strict L1 supports the interpreter, console, native Math and the read-only bundled stdlib (published at `/run/marqdo-stdlib` for the Compat-C print chain); `--allow-read/--allow-write/--allow-net/--allow-env/--allow-process/--allow-random/--allow-foreign/--allow-plugin/--plugin/--trust-native-plugin/--kernel/--trace-file` are rejected with exit 2. macOS/Windows and Linux hosts without the mechanisms remain `Unavailable`; there is **no fallback** to an unrestricted process. |
`marqdo inspect <file> --platform` prints the detection report, for example:
@@ -59,7 +59,7 @@ set for one run:
| `math: bool` | `true` | `math.*` host ops |
| `fs_read: Vec<String>` | empty | `fs.read` and project `resource.open("file", path, ["read"])` |
| `fs_write: Vec<String>` | empty | `fs.write` and project `resource.open("file", path, ["write"])` |
| `net_hosts: Vec<String>` | empty | exact IP-literal grants for `resource.open("tcp", "<ip>:<port>", ["read","write"])`; DNS is not resolved; `--allow-net <IP>` adds a grant (ADR-0034) |
| `net_hosts: Vec<String>` | empty | exact host/IP grants for `resource.open("tcp"|"udp"|"tcp-listener"|"tls", "host:port", ...)`; DNS is resolved only after the host grant; TLS still validates certificates; `--allow-net <HOST>` adds a grant (ADR-0034/0047/0048) |
| `env: Vec<String>` | empty | exact-name grants for `resource.request("env.get", [name])`; no glob and no `env.list`; `--allow-env <NAME>` adds a grant (ADR-0043) |
| `process_programs: Vec<String>` | empty | exact program paths for `resource.request("process.run", [program, args])`; no shell, `env_clear`, project root cwd, 5 s timeout, 1 MiB stdout/stderr cap; `--allow-process <PROGRAM>` adds a grant (ADR-0044); not an OS sandbox |
| `random: bool` | `false` | OS entropy for `resource.request("random.bytes", [count])`; `--allow-random` enables it; project mode only (ADR-0046) |
@@ -79,8 +79,8 @@ Requests versus grants:
* CLI grant flags (`crates/cli/src/main.rs::SandboxArgs`):
* `--allow-read <GLOB>` and `--allow-write <GLOB>` (repeatable, project mode
only);
* `--allow-net <IP>` (repeatable, exact IP literal, project mode only,
ADR-0034);
* `--allow-net <HOST>` (repeatable, exact host name or IP literal, project
mode only, ADR-0034/0047);
* `--allow-env <NAME>` (repeatable, exact environment variable name, project
mode only, ADR-0043);
* `--allow-process <PROGRAM>` (repeatable, exact program path, project mode