Skip to content

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

refactor(security): bind encryption controllers to config gate

Steven Enamakel1ea8cb1da9bc
diff --git a/crates/openhuman-core/src/core/all.rs b/crates/openhuman-core/src/core/all.rs
index 050e103bd7..caea1dc0d1 100644
--- a/crates/openhuman-core/src/core/all.rs
+++ b/crates/openhuman-core/src/core/all.rs
@@ -382,10 +382,10 @@ fn build_registered_controllers() -> Vec<GroupedController> {
crate::hooks::all_hooks_registered_controllers(),
);
// Secret storage and encryption
- push(
+ push_entries(
&mut controllers,
DomainGroup::Security,
- crate::security::encryption::all_encryption_registered_controllers(),
+ crate::security::encryption::all_encryption_controller_entries(),
);
// Keyring consent — user approval before local secret storage fallback
push(
diff --git a/crates/openhuman-core/src/security/encryption/mod.rs b/crates/openhuman-core/src/security/encryption/mod.rs
index dc597082b3..938d73596c 100644
--- a/crates/openhuman-core/src/security/encryption/mod.rs
+++ b/crates/openhuman-core/src/security/encryption/mod.rs
@@ -11,6 +11,7 @@ mod schemas;
pub use core::*;
pub use ops as rpc;
pub use ops::*;
+pub(crate) use schemas::all_controller_entries as all_encryption_controller_entries;
pub use schemas::{
all_controller_schemas as all_encryption_controller_schemas,
all_registered_controllers as all_encryption_registered_controllers,
diff --git a/crates/openhuman-core/src/security/encryption/schemas.rs b/crates/openhuman-core/src/security/encryption/schemas.rs
index 125bde4501..166bdecf28 100644
--- a/crates/openhuman-core/src/security/encryption/schemas.rs
+++ b/crates/openhuman-core/src/security/encryption/schemas.rs
@@ -2,7 +2,11 @@ use crate::core::runtime::DispatchCtx;
use serde::de::DeserializeOwned;
use serde_json::{Map, Value};
-use crate::core::all::{ControllerFuture, RegisteredController};
+use crate::config::Config;
+use crate::core::all::{
+ BoundRegisteredController, ControllerEntry, ControllerFuture, ControllerTask,
+ RegisteredController,
+};
use crate::core::Outcome;
use crate::core::{ControllerSchema, FieldSchema, TypeSchema};
@@ -23,6 +27,13 @@ pub fn all_registered_controllers() -> Vec<RegisteredController> {
]
}
+pub(crate) fn all_controller_entries() -> Vec<ControllerEntry> {
+ vec![
+ ControllerEntry::bound(encrypt_secret_controller()),
+ ControllerEntry::bound(decrypt_secret_controller()),
+ ]
+}
+
#[cfg(test)]
pub(crate) fn encryption_parameter_contracts() -> Vec<crate::core::all::ControllerParamContract> {
tests::parameter_contracts()
@@ -79,18 +90,52 @@ pub fn schemas(function: &str) -> ControllerSchema {
}
}
+fn encrypt_secret_controller() -> BoundRegisteredController {
+ BoundRegisteredController::after_gate(
+ schemas("encrypt_secret"),
+ load_config,
+ parse_encrypt_secret,
+ run_encrypt_secret,
+ )
+}
+
+fn decrypt_secret_controller() -> BoundRegisteredController {
+ BoundRegisteredController::after_gate(
+ schemas("decrypt_secret"),
+ load_config,
+ parse_decrypt_secret,
+ run_decrypt_secret,
+ )
+}
+
fn handle_encrypt_secret(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
+ encrypt_secret_controller().invoke(ctx, params)
+}
+
+fn handle_decrypt_secret(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
+ decrypt_secret_controller().invoke(ctx, params)
+}
+
+fn load_config(ctx: DispatchCtx) -> ControllerTask<Config> {
+ Box::pin(async move { ctx.load_config().await })
+}
+
+fn parse_encrypt_secret(params: Map<String, Value>) -> Result<String, String> {
+ read_required(&params, "plaintext")
+}
+
+fn parse_decrypt_secret(params: Map<String, Value>) -> Result<String, String> {
+ read_required(&params, "ciphertext")
+}
+
+fn run_encrypt_secret(_ctx: DispatchCtx, config: Config, plaintext: String) -> ControllerFuture {
Box::pin(async move {
- let config = ctx.load_config().await?;
- let plaintext = read_required::<String>(&params, "plaintext")?;
to_json(crate::security::encryption::rpc::encrypt_secret(&config, &plaintext).await?)
})
}
-fn handle_decrypt_secret(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
+fn run_decrypt_secret(_ctx: DispatchCtx, config: Config, ciphertext: String) -> ControllerFuture {
Box::pin(async move {
- let config = ctx.load_config().await?;
- let ciphertext = read_required::<String>(&params, "ciphertext")?;
to_json(crate::security::encryption::rpc::decrypt_secret(&config, &ciphertext).await?)
})
}
diff --git a/crates/openhuman-core/src/security/encryption/schemas_tests.rs b/crates/openhuman-core/src/security/encryption/schemas_tests.rs
index 28d21846d5..ca746fa715 100644
--- a/crates/openhuman-core/src/security/encryption/schemas_tests.rs
+++ b/crates/openhuman-core/src/security/encryption/schemas_tests.rs
@@ -63,3 +63,99 @@ fn read_required_parses_values_and_preserves_error_categories() {
.unwrap_err()
.contains("invalid"));
}
+
+#[test]
+fn secret_parsers_preserve_strings_and_required_value_errors() {
+ use serde_json::json;
+
+ let parsers: [(&str, fn(Map<String, Value>) -> Result<String, String>); 2] = [
+ ("plaintext", parse_encrypt_secret),
+ ("ciphertext", parse_decrypt_secret),
+ ];
+
+ for (field, parse) in parsers {
+ for expected in ["", " keep whitespace ", "secret payload"] {
+ let params = Map::from_iter([(field.into(), json!(expected))]);
+ assert_eq!(parse(params).unwrap(), expected, "{field} parser value");
+ }
+
+ assert_eq!(
+ parse(Map::new()).unwrap_err(),
+ format!("missing required param '{field}'")
+ );
+ for value in [json!(null), json!(true), json!(42)] {
+ let params = Map::from_iter([(field.into(), value)]);
+ let error = parse(params).expect_err("secret input must be a string");
+ assert!(
+ error.starts_with(&format!("invalid '{field}':")),
+ "{field} invalid input error: {error}"
+ );
+ }
+ }
+}
+
+async fn runtime_encryption_entries() -> Vec<crate::core::all::ControllerEntry> {
+ use crate::core::runtime::{CoreContext, DomainSet};
+
+ CoreContext::scope(CoreContext::for_test(DomainSet::full(), None), async {
+ crate::core::all::all_registered_controllers()
+ .into_iter()
+ .filter(|entry| matches!(entry.schema.namespace, "encrypt" | "decrypt"))
+ .collect()
+ })
+ .await
+}
+
+#[tokio::test]
+async fn runtime_secret_entries_load_config_before_parsing_and_parse_after_success() {
+ use crate::config::test_env::EnvVarGuard;
+ use crate::core::runtime::{CoreContext, DispatchCtx, DomainSet};
+
+ let tmp = tempfile::tempdir().unwrap();
+ std::fs::create_dir(tmp.path().join("config.toml")).unwrap();
+ let _env = EnvVarGuard::workspace_async(tmp.path()).await;
+
+ let entries = runtime_encryption_entries().await;
+ assert_eq!(entries.len(), 2, "both runtime encryption entries exist");
+ let ctx = DispatchCtx::new(CoreContext::for_test(DomainSet::full(), None));
+ for (method, field) in [
+ ("encrypt.secret", "plaintext"),
+ ("decrypt.secret", "ciphertext"),
+ ] {
+ let entry = entries
+ .iter()
+ .find(|entry| entry.schema.method_name() == method)
+ .expect("runtime entry exists");
+ let error = entry
+ .invoke(ctx.clone(), Map::new())
+ .await
+ .expect_err("malformed params remain blocked by a config gate error");
+ assert!(
+ error.contains("is a directory") || error.contains("not a file"),
+ "{method} must report the config gate error first: {error}"
+ );
+ assert!(
+ !error.contains(&format!("missing required param '{field}'")),
+ "{method} must not parse before its config gate: {error}"
+ );
+ }
+
+ let valid_ctx = DispatchCtx::new(CoreContext::for_test_with_config(
+ DomainSet::full(),
+ crate::config::Config::default(),
+ ));
+ for (method, field) in [
+ ("encrypt.secret", "plaintext"),
+ ("decrypt.secret", "ciphertext"),
+ ] {
+ let entry = entries
+ .iter()
+ .find(|entry| entry.schema.method_name() == method)
+ .expect("runtime entry exists");
+ let error = entry
+ .invoke(valid_ctx.clone(), Map::new())
+ .await
+ .expect_err("missing input is rejected before any encryption operation");
+ assert_eq!(error, format!("missing required param '{field}'"));
+ }
+}