feat(random): add explicitly granted OS entropy API
This commit is contained in:
1 parent
6338fa10e9
commit
ee0c6cc80a
20 files changed
+309
-15
No files matched your search
@@ -11,6 +11,7 @@ authors.workspace = true
|
||||
[dependencies]
|
||||
libloading = { workspace = true }
|
||||
marqdo-plugin-abi = { workspace = true }
|
||||
getrandom = { workspace = true }
|
||||
glob = { workspace = true }
|
||||
marqdo-span = { workspace = true }
|
||||
marqdo-value = { workspace = true }
|
||||
|
||||
@@ -321,6 +321,8 @@ pub struct Capabilities {
|
||||
pub env: Vec<String>,
|
||||
/// Allowed process program paths (empty = no process execution).
|
||||
pub process_programs: Vec<String>,
|
||||
/// Whether OS entropy may be read (ADR-0046).
|
||||
pub random: bool,
|
||||
/// May spawn processes (foreign kernels).
|
||||
pub process: bool,
|
||||
/// May run foreign kernels (Python/Node).
|
||||
@@ -342,6 +344,7 @@ impl Capabilities {
|
||||
net_hosts: Vec::new(),
|
||||
env: Vec::new(),
|
||||
process_programs: Vec::new(),
|
||||
random: false,
|
||||
process: false,
|
||||
foreign: false,
|
||||
plugins: Vec::new(),
|
||||
@@ -409,6 +412,25 @@ impl Capabilities {
|
||||
}
|
||||
}
|
||||
|
||||
/// Grant OS entropy reads (ADR-0046).
|
||||
pub fn allow_random(mut self) -> Self {
|
||||
self.random = true;
|
||||
self
|
||||
}
|
||||
|
||||
/// Check the entropy grant.
|
||||
pub fn check_random(&self, span: Option<Span>) -> Result<(), RuntimeError> {
|
||||
if self.random {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(denied(
|
||||
span,
|
||||
"OS entropy was not granted",
|
||||
"grant it with --allow-random",
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
/// Grant foreign kernel execution.
|
||||
pub fn allow_foreign(mut self) -> Self {
|
||||
self.foreign = true;
|
||||
|
||||
@@ -785,6 +785,58 @@ impl Host for StdHost {
|
||||
self.record_resource_effect("resource.request", ctx, started, "process.run");
|
||||
return Ok(result);
|
||||
}
|
||||
if operation == "random.bytes" {
|
||||
if args.len() != 1 {
|
||||
return Err(RuntimeError::new(
|
||||
ErrorCategory::Arity,
|
||||
ctx.span,
|
||||
format!(
|
||||
"resource operation `random.bytes` expects 1 argument, got {}",
|
||||
args.len()
|
||||
),
|
||||
));
|
||||
}
|
||||
let count = match &args[0] {
|
||||
Value::Int(value) => value
|
||||
.to_string()
|
||||
.parse::<usize>()
|
||||
.ok()
|
||||
.filter(|count| (1..=65536).contains(count)),
|
||||
_ => None,
|
||||
};
|
||||
let Some(count) = count else {
|
||||
return Err(RuntimeError::new(
|
||||
ErrorCategory::Type,
|
||||
ctx.span,
|
||||
"random.bytes requires an Int in 1..=65536",
|
||||
));
|
||||
};
|
||||
self.capabilities.check_random(ctx.span)?;
|
||||
let mut bytes = vec![0u8; count];
|
||||
getrandom::fill(&mut bytes).map_err(|error| {
|
||||
RuntimeError::new(
|
||||
ErrorCategory::HostError,
|
||||
ctx.span,
|
||||
format!("OS entropy is unavailable: {error}"),
|
||||
)
|
||||
})?;
|
||||
let values = bytes
|
||||
.into_iter()
|
||||
.map(|byte| Value::int(byte as i64))
|
||||
.collect();
|
||||
self.record_semantic(
|
||||
"resource.request",
|
||||
None,
|
||||
Some(&ctx.module_id),
|
||||
ctx.span,
|
||||
Some("op=random.bytes"),
|
||||
);
|
||||
self.record_resource_effect("resource.request", ctx, started, "random.bytes");
|
||||
let mut map = BTreeMap::new();
|
||||
map.insert("ok".to_string(), Value::Bool(true));
|
||||
map.insert("value".to_string(), Value::List(values));
|
||||
return Ok(Value::Map(map));
|
||||
}
|
||||
if operation == "env.get" {
|
||||
if args.len() != 1 {
|
||||
return Err(RuntimeError::new(
|
||||
|
||||
@@ -795,3 +795,51 @@ fn fs_append_read_at_and_walk_are_bounded() {
|
||||
.unwrap_err();
|
||||
assert_eq!(error.category, ErrorCategory::Type);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn random_bytes_require_the_entropy_grant() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let ctx = context();
|
||||
let mut denied = StdHost::new(SandboxMode::Project, Capabilities::project(), dir.path());
|
||||
let error = denied
|
||||
.resource_request("random.bytes", vec![Value::int(8)], &ctx)
|
||||
.unwrap_err();
|
||||
assert_eq!(error.category, ErrorCategory::PermissionDenied);
|
||||
|
||||
let mut host = StdHost::new(
|
||||
SandboxMode::Project,
|
||||
Capabilities::project().allow_random(),
|
||||
dir.path(),
|
||||
);
|
||||
let value = host
|
||||
.resource_request("random.bytes", vec![Value::int(16)], &ctx)
|
||||
.unwrap();
|
||||
assert_eq!(field(&value, "ok"), &Value::Bool(true));
|
||||
let bytes = field(&value, "value").clone();
|
||||
let Value::List(bytes) = bytes else {
|
||||
panic!("random.bytes did not return a List");
|
||||
};
|
||||
assert_eq!(bytes.len(), 16);
|
||||
for byte in bytes {
|
||||
let Value::Int(byte) = byte else {
|
||||
panic!("random byte is not an Int");
|
||||
};
|
||||
let byte = byte.to_string().parse::<i64>().unwrap();
|
||||
assert!((0..=255).contains(&byte));
|
||||
}
|
||||
|
||||
let error = host
|
||||
.resource_request("random.bytes", vec![Value::int(0)], &ctx)
|
||||
.unwrap_err();
|
||||
assert_eq!(error.category, ErrorCategory::Type);
|
||||
|
||||
let mut pure = StdHost::new(
|
||||
SandboxMode::Pure,
|
||||
Capabilities::pure().allow_random(),
|
||||
dir.path(),
|
||||
);
|
||||
let error = pure
|
||||
.resource_request("random.bytes", vec![Value::int(8)], &ctx)
|
||||
.unwrap_err();
|
||||
assert_eq!(error.category, ErrorCategory::PermissionDenied);
|
||||
}
|
||||
Reference in new issue
Block a user