The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
feat(storage_grants): add storage grant module
Steven Enamakel4aa158cf31a2
diff --git a/crates/openhuman-core/src/modules/storage_grants.rs b/crates/openhuman-core/src/modules/storage_grants.rsnew file mode 100644index 0000000000..3787fa04e2--- /dev/null+++ b/crates/openhuman-core/src/modules/storage_grants.rs@@ -0,0 +1,417 @@+//! What a loadable module may touch through the storage bus.+//!+//! A module reaches storage only through `ai.tinyhumans.tinystorage.Storage`+//! (`storage_host`). Its authority is a [`StorageGrant`] declared by the host+//! build for that module id: the collections it owns (with their schema), the+//! stream, blob and secret-name prefixes it may use, and whether it may write.+//! A module with no declaration has no storage, and a request that names+//! anything outside its grant is refused before it reaches the backend.+//!+//! [`authorize`] works on the wire request itself, because that is the one+//! place every selector's target is visible. It never trusts a field to+//! *widen* access: an unknown selector, a missing or non-string target and a+//! malformed batch are all refusals.+//!+//! Schema is host-only. The host runs `ensure_collection` for the declared+//! [`CollectionSpec`]s when it binds a module ([`StorageGrant::specs`]); a+//! module's own `EnsureCollection` is answered without touching the backend+//! when it repeats the declared spec, and refused otherwise. `DropCollection`+//! is never allowed.++use std::collections::BTreeSet;++use serde_json::Value;+use tinystoragedrivers::CollectionSpec;++/// The four objects of the storage interface.+#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]+pub(crate) enum Object {+ Documents,+ Streams,+ Blobs,+ Secrets,+}++impl Object {+ /// The wire object name.+ pub(crate) const fn as_str(self) -> &'static str {+ match self {+ Self::Documents => tinystoragedrivers_bus::DOCUMENTS_OBJECT,+ Self::Streams => tinystoragedrivers_bus::STREAMS_OBJECT,+ Self::Blobs => tinystoragedrivers_bus::BLOBS_OBJECT,+ Self::Secrets => tinystoragedrivers_bus::SECRETS_OBJECT,+ }+ }++ /// Every object, in wire order.+ pub(crate) const ALL: [Self; 4] = [Self::Documents, Self::Streams, Self::Blobs, Self::Secrets];++ /// The selectors the contract defines for this object.+ pub(crate) fn selectors(self) -> &'static [&'static str] {+ match self {+ Self::Documents => &tinystoragedrivers_bus::DOCUMENTS_SELECTORS,+ Self::Streams => &tinystoragedrivers_bus::STREAMS_SELECTORS,+ Self::Blobs => &tinystoragedrivers_bus::BLOBS_SELECTORS,+ Self::Secrets => &tinystoragedrivers_bus::SECRETS_SELECTORS,+ }+ }+}++/// Why a call was refused. Carries no request content, so it is safe to log.+#[derive(Clone, Copy, Debug, PartialEq, Eq)]+pub(crate) enum Denied {+ /// The grant does not include this object at all.+ Object,+ /// The target (collection, stream, blob key, secret name) is not granted.+ Target,+ /// The grant is read-only and the selector writes.+ ReadOnly,+ /// Schema and collection lifecycle belong to the host.+ Schema,+ /// The selector is not part of the contract, or carries no usable target.+ Malformed,+ /// The selector needs a bus stream, which this host does not serve yet.+ Unsupported,+}++impl Denied {+ /// A stable label for logs.+ pub(crate) const fn label(self) -> &'static str {+ match self {+ Self::Object => "object-not-granted",+ Self::Target => "target-not-granted",+ Self::ReadOnly => "read-only-grant",+ Self::Schema => "schema-is-host-only",+ Self::Malformed => "malformed-request",+ Self::Unsupported => "unsupported-selector",+ }+ }+}++/// How the reply of an allowed call must be trimmed to the grant.+#[derive(Clone, Copy, Debug, PartialEq, Eq)]+pub(crate) enum ReplyFilter {+ /// Pass the reply through.+ None,+ /// The reply is a list of collection names: keep the granted ones.+ GrantedCollections,+}++/// What to do with an allowed call.+#[derive(Debug, PartialEq)]+pub(crate) enum Plan {+ /// Forward the request to the bound server.+ Forward {+ /// What the call touches, for the audit line (a collection, a stream+ /// name or a granted prefix; never a payload).+ target: String,+ /// How to trim the reply.+ filter: ReplyFilter,+ },+ /// Answer without calling the backend (the host already ran it).+ Answer(Value),+}++/// One module's storage authority.+#[derive(Clone, Debug, Default, PartialEq, Eq)]+pub struct StorageGrant {+ /// Collections the module may use, each with the schema the host ensures.+ pub collections: Vec<CollectionSpec>,+ /// Stream-name prefixes the module may use.+ pub stream_prefixes: Vec<String>,+ /// Blob-key prefixes the module may use.+ pub blob_prefixes: Vec<String>,+ /// Secret-name prefixes the module may use (confidential callers only).+ pub secret_prefixes: Vec<String>,+ /// Whether the module may write; `false` makes every grant read-only.+ pub writable: bool,+}++/// Collection names no grant may hold: the encrypted secret documents and the+/// storage layer's own `_`-prefixed records.+const RESERVED_COLLECTIONS: [&str; 1] = [tinystoragedrivers::secrets::DEFAULT_COLLECTION];++impl StorageGrant {+ /// The schema the host ensures when it binds the module.+ pub fn specs(&self) -> &[CollectionSpec] {+ &self.collections+ }++ /// Refuses a grant that could reach beyond what a module may hold.+ ///+ /// # Errors+ ///+ /// A reserved or empty collection name, a duplicate collection, or an+ /// empty prefix (which would grant the module's whole scope).+ pub fn validate(&self) -> Result<(), String> {+ let mut seen = BTreeSet::new();+ for spec in &self.collections {+ if spec.name.is_empty()+ || spec.name.starts_with('_')+ || RESERVED_COLLECTIONS.contains(&spec.name.as_str())+ {+ return Err("a granted collection name is empty or reserved".into());+ }+ if !seen.insert(spec.name.as_str()) {+ return Err("a collection is granted twice".into());+ }+ }+ let prefixes = self+ .stream_prefixes+ .iter()+ .chain(&self.blob_prefixes)+ .chain(&self.secret_prefixes);+ if prefixes.into_iter().any(String::is_empty) {+ return Err("a granted prefix is empty".into());+ }+ Ok(())+ }++ fn has_object(&self, object: Object) -> bool {+ match object {+ Object::Documents => !self.collections.is_empty(),+ Object::Streams => !self.stream_prefixes.is_empty(),+ Object::Blobs => !self.blob_prefixes.is_empty(),+ Object::Secrets => !self.secret_prefixes.is_empty(),+ }+ }++ fn spec(&self, name: &str) -> Option<&CollectionSpec> {+ self.collections.iter().find(|spec| spec.name == name)+ }++ fn collection(&self, name: &str) -> Result<(), Denied> {+ self.spec(name).map(|_| ()).ok_or(Denied::Target)+ }++ fn prefixed<'a>(prefixes: &'a [String], name: &str) -> Result<&'a str, Denied> {+ prefixes+ .iter()+ .find(|prefix| name.starts_with(prefix.as_str()))+ .map(String::as_str)+ .ok_or(Denied::Target)+ }+}++/// The modules whose build declares storage, and what each may use.+///+/// Empty today: no loadable module consumes the storage bus yet. A module+/// that does adds its row here (a declaration is a reviewable build-time+/// decision, like the registry's digests) and then calls+/// `ai.tinyhumans.tinystorage.Storage`; until then it has no storage at all.+#[must_use]+pub fn declared(module_id: &str) -> Option<StorageGrant> {+ let table: &[(&str, fn() -> StorageGrant)] = &[];+ table+ .iter()+ .find(|(id, _)| *id == module_id)+ .map(|(_, grant)| grant())+}++fn text<'a>(request: &'a Value, field: &str) -> Result<&'a str, Denied> {+ request+ .get(field)+ .and_then(Value::as_str)+ .ok_or(Denied::Malformed)+}++fn forward(target: impl Into<String>) -> Plan {+ Plan::Forward {+ target: target.into(),+ filter: ReplyFilter::None,+ }+}++/// Decides whether `grant` allows `object`'s `selector` with `request`, and+/// how to carry it out.+///+/// # Errors+///+/// A [`Denied`] reason when the grant does not cover the call.+pub(crate) fn authorize(+ grant: &StorageGrant,+ object: Object,+ selector: &str,+ request: &Value,+) -> Result<Plan, Denied> {+ if !object.selectors().contains(&selector) {+ return Err(Denied::Malformed);+ }+ if !grant.has_object(object) {+ return Err(Denied::Object);+ }+ match object {+ Object::Documents => documents(grant, selector, request),+ Object::Streams => streams(grant, selector, request),+ Object::Blobs => blobs(grant, selector, request),+ Object::Secrets => secrets(grant, selector, request),+ }+}++fn writes(grant: &StorageGrant) -> Result<(), Denied> {+ if grant.writable {+ Ok(())+ } else {+ Err(Denied::ReadOnly)+ }+}++fn documents(grant: &StorageGrant, selector: &str, request: &Value) -> Result<Plan, Denied> {+ match selector {+ "Get" | "Query" | "Count" | "Search" | "WatchOpen" => {+ let collection = text(request, "collection")?;+ grant.collection(collection)?;+ Ok(forward(collection))+ }+ "Put" | "Delete" | "DeleteWhere" | "Claim" => {+ writes(grant)?;+ let collection = text(request, "collection")?;+ grant.collection(collection)?;+ Ok(forward(collection))+ }+ "AtomicBatch" | "AtomicBatchChecked" => {+ writes(grant)?;+ batch(grant, request)+ }+ "EnsureCollection" | "ValidateCollectionSpec" => {+ // The host ran `ensure_collection` from the declared spec when it+ // bound this module. Repeating it is harmless; changing it is not+ // the module's call.+ let wanted: CollectionSpec = request+ .get("spec")+ .cloned()+ .and_then(|spec| serde_json::from_value(spec).ok())+ .ok_or(Denied::Malformed)?;+ match grant.spec(&wanted.name) {+ Some(declared) if *declared == wanted => Ok(Plan::Answer(Value::Null)),+ _ => Err(Denied::Schema),+ }+ }+ "DropCollection" => Err(Denied::Schema),+ "Collections" => {+ // Listing is filtered, not refused: a module sees its own names.+ text(request, "prefix").map(|_| ()).or_else(|_| {+ request+ .as_object()+ .filter(|fields| fields.is_empty())+ .map(|_| ())+ .ok_or(Denied::Malformed)+ })?;+ Ok(Plan::Forward {+ target: String::new(),+ filter: ReplyFilter::GrantedCollections,+ })+ }+ "WatchClose" | "Capabilities" => Ok(forward("")),+ _ => Err(Denied::Malformed),+ }+}++fn batch(grant: &StorageGrant, request: &Value) -> Result<Plan, Denied> {+ let mut first: Option<&str> = None;+ for list in ["checks", "ops"] {+ let Some(entries) = request.get(list) else {+ if list == "ops" {+ return Err(Denied::Malformed);+ }+ continue;+ };+ for entry in entries.as_array().ok_or(Denied::Malformed)? {+ let collection = text(entry, "collection")?;+ grant.collection(collection)?;+ first.get_or_insert(collection);+ }+ }+ Ok(forward(first.unwrap_or_default()))+}++fn streams(grant: &StorageGrant, selector: &str, request: &Value) -> Result<Plan, Denied> {+ match selector {+ "ReadWindow" | "Len" => {+ let name = text(request, "stream")?;+ StorageGrant::prefixed(&grant.stream_prefixes, name)?;+ Ok(forward(name))+ }+ "Append" | "AppendBatch" | "AppendBatchIfOffset" | "TruncateBefore" | "DeleteStream" => {+ writes(grant)?;+ let name = text(request, "stream")?;+ StorageGrant::prefixed(&grant.stream_prefixes, name)?;+ Ok(forward(name))+ }+ "Streams" => {+ let prefix = text(request, "prefix")?;+ let granted = StorageGrant::prefixed(&grant.stream_prefixes, prefix)?;+ Ok(forward(granted))+ }+ _ => Err(Denied::Malformed),+ }+}++fn blobs(grant: &StorageGrant, selector: &str, request: &Value) -> Result<Plan, Denied> {+ match selector {+ // Streamed bodies need a bus stream argument, which the dynamic+ // interface here does not carry; the inline selectors cover small+ // blobs and these refuse rather than pretend.+ "PutStream" | "GetStream" => Err(Denied::Unsupported),+ "Get" | "GetRange" | "Head" | "PresignGet" => {+ let key = text(request, "key")?;+ let granted = StorageGrant::prefixed(&grant.blob_prefixes, key)?;+ Ok(forward(granted))+ }+ "Put" | "Delete" | "PresignPut" => {+ writes(grant)?;+ let key = text(request, "key")?;+ let granted = StorageGrant::prefixed(&grant.blob_prefixes, key)?;+ Ok(forward(granted))+ }+ "List" => {+ let prefix = text(request, "prefix")?;+ let granted = StorageGrant::prefixed(&grant.blob_prefixes, prefix)?;+ Ok(forward(granted))+ }+ "Capabilities" => Ok(forward("")),+ _ => Err(Denied::Malformed),+ }+}++fn secrets(grant: &StorageGrant, selector: &str, request: &Value) -> Result<Plan, Denied> {+ match selector {+ "Get" => {+ let granted =+ StorageGrant::prefixed(&grant.secret_prefixes, text(request, "name")?)?;+ Ok(forward(granted))+ }+ "Set" | "Delete" => {+ writes(grant)?;+ let granted =+ StorageGrant::prefixed(&grant.secret_prefixes, text(request, "name")?)?;+ Ok(forward(granted))+ }+ "List" => {+ let granted =+ StorageGrant::prefixed(&grant.secret_prefixes, text(request, "prefix")?)?;+ Ok(forward(granted))+ }+ _ => Err(Denied::Malformed),+ }+}++/// Trims a `Collections` reply to the names the grant holds.+pub(crate) fn filter_reply(grant: &StorageGrant, filter: ReplyFilter, reply: Value) -> Value {+ match (filter, reply) {+ (ReplyFilter::GrantedCollections, Value::Array(names)) => Value::Array(+ names+ .into_iter()+ .filter(|name| {+ name.as_str()+ .is_some_and(|name| grant.spec(name).is_some())+ })+ .collect(),+ ),+ (_, reply) => reply,+ }+}++#[cfg(test)]+#[path = "storage_grants_tests.rs"]+mod tests;