Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(session_store): refuse shared default agent and workspace in saas

Steven Enamakel49a4502fa23f
diff --git a/crates/openhuman-core/src/agent/session_store/mod_tests.rs b/crates/openhuman-core/src/agent/session_store/mod_tests.rs
index b95ab62e18..3dbc3258ed 100644
--- a/crates/openhuman-core/src/agent/session_store/mod_tests.rs
+++ b/crates/openhuman-core/src/agent/session_store/mod_tests.rs
@@ -19,7 +19,9 @@ async fn a_scoped_provider_serves_only_its_own_task() {
async fn work_outside_an_agent_context_uses_the_default_agent() {
let provider: Arc<dyn SessionStoreProvider> = Arc::new(InMemorySessionStores::new());
let key = scope(provider.clone(), async {
- current().map(|stores| stores.transcripts.destination_key())
+ try_current()
+ .unwrap()
+ .map(|stores| stores.transcripts.destination_key())
})
.await;
// No agent context in a unit test: the shared default bucket.
@@ -32,7 +34,7 @@ async fn work_outside_an_agent_context_uses_the_default_agent() {
.destination_key()
)
);
- assert!(current().is_none() || is_installed());
+ assert!(try_current().unwrap().is_none() || is_installed());
}
#[tokio::test]
@@ -71,3 +73,37 @@ async fn without_a_store_transcripts_are_workspace_files() {
.await;
assert_eq!(found, expected);
}
+
+#[test]
+fn saas_refuses_the_shared_default_agent_and_single_user_keeps_it() {
+ assert_eq!(
+ current_agent_from(Some("u1".into()), true).as_deref(),
+ Ok("u1")
+ );
+ assert!(current_agent_from(None, true).is_err());
+ assert_eq!(
+ current_agent_from(None, false).as_deref(),
+ Ok(DEFAULT_AGENT)
+ );
+}
+
+#[test]
+fn saas_refuses_the_operators_default_workspace() {
+ let dir = std::path::PathBuf::from("/tmp/ws");
+ assert_eq!(
+ context_workspace_from(Some(dir.clone()), true),
+ Ok(dir.clone())
+ );
+ assert!(context_workspace_from(None, true).is_err());
+ assert!(context_workspace_from(None, false).is_ok());
+}
+
+#[tokio::test]
+async fn a_refused_kv_fails_every_operation() {
+ use tinyagents_harness::store::Store;
+ let store = RefusedStore("nope".into());
+ assert!(store.get("ns", "k").await.is_err());
+ assert!(store.put("ns", "k", serde_json::json!(1)).await.is_err());
+ assert!(store.delete("ns", "k").await.is_err());
+ assert!(store.list("ns").await.is_err());
+}
diff --git a/crates/openhuman-core/src/agent/tinyagents/journal.rs b/crates/openhuman-core/src/agent/tinyagents/journal.rs
index e7acdbd052..71933f5e3b 100644
--- a/crates/openhuman-core/src/agent/tinyagents/journal.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/journal.rs
@@ -104,7 +104,7 @@ async fn resolve_workspace() -> anyhow::Result<PathBuf> {
/// The journal and key-value stores a turn's events and status go to: the
/// current agent's host session store when one is installed
-/// ([`crate::agent::session_store::current`]), else the workspace's
+/// ([`crate::agent::session_store::try_current`]), else the workspace's
/// `tinyagents_store/`.
async fn journal_stores() -> anyhow::Result<(Arc<dyn AppendStore>, Arc<dyn Store>)> {
if let Some(stores) = crate::agent::session_store::try_current().map_err(|e| anyhow::anyhow!(e))? {
diff --git a/crates/openhuman-rpc/src/session_store/mod.rs b/crates/openhuman-rpc/src/session_store/mod.rs
index b59a6ccabf..349f55c578 100644
--- a/crates/openhuman-rpc/src/session_store/mod.rs
+++ b/crates/openhuman-rpc/src/session_store/mod.rs
@@ -45,7 +45,11 @@ use tinyagents_session::transcript::FileTranscriptLocator;
use tinyagents_session::turn_state::TurnStateStore;
/// Resolves the workspace the stores live in, at the moment they are needed.
-type WorkspaceResolver = dyn Fn() -> PathBuf + Send + Sync;
+type WorkspaceResolver = dyn Fn() -> Result<PathBuf, String> + Send + Sync;
+
+/// Where a refused workspace resolves: a path under a file, which nothing can
+/// create or open.
+pub const REFUSED_WORKSPACE: &str = "/dev/null/openhuman-refused-workspace";
/// OpenHuman's on-disk session layout as a session store provider.
#[derive(Clone)]
@@ -56,7 +60,7 @@ pub struct SqliteSessionStores {
impl std::fmt::Debug for SqliteSessionStores {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SqliteSessionStores")
- .field("workspace", &(self.workspace)())
+ .field("workspace", &(self.workspace)().ok())
.finish()
}
}
@@ -68,16 +72,29 @@ impl SqliteSessionStores {
Self::resolving(move || workspace.clone())
}
- /// Stores under whichever workspace `resolve` names when asked — for a
- /// host whose workspace can change while it runs.
- pub fn resolving(resolve: impl Fn() -> PathBuf + Send + Sync + 'static) -> Self {
+ /// Like [`resolving`](Self::resolving), for a resolver that can refuse
+ /// (a SaaS call with no workspace). A refused call is logged and served
+ /// from [`REFUSED_WORKSPACE`], which no store can open, so it fails
+ /// instead of reaching another tenant's files.
+ pub fn try_resolving(
+ resolve: impl Fn() -> Result<PathBuf, String> + Send + Sync + 'static,
+ ) -> Self {
Self {
workspace: Arc::new(resolve),
}
}
+ /// Stores under whichever workspace `resolve` names when asked — for a
+ /// host whose workspace can change while it runs.
+ pub fn resolving(resolve: impl Fn() -> PathBuf + Send + Sync + 'static) -> Self {
+ Self::try_resolving(move || Ok(resolve()))
+ }
+
fn current(&self) -> PathBuf {
- (self.workspace)()
+ (self.workspace)().unwrap_or_else(|error| {
+ log::error!("[rpc:session_store] workspace refused: {error}");
+ PathBuf::from(REFUSED_WORKSPACE)
+ })
}
}
@@ -143,7 +160,7 @@ pub fn install() {
/// [`crate::host::tui`] wires). [`install`] installs the same provider
/// process-wide instead.
pub fn provider() -> Arc<dyn SessionStoreProvider> {
- Arc::new(SqliteSessionStores::resolving(
+ Arc::new(SqliteSessionStores::try_resolving(
crate::core_host::agent::session_store::context_workspace_dir,
))
}