Skip to content

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

refactor(storage): update key generation logic

Steven Enamakelc4304c5d64c2
diff --git a/crates/openhuman-core/src/security/approval/storage_schema.rs b/crates/openhuman-core/src/security/approval/storage_schema.rs
index 0c8e6ff386..bac7825625 100644
--- a/crates/openhuman-core/src/security/approval/storage_schema.rs
+++ b/crates/openhuman-core/src/security/approval/storage_schema.rs
@@ -14,11 +14,11 @@
use anyhow::Result;
use serde_json::Value;
-use tinystoragedrivers::{CollectionSpec, IndexSpec, Key};
+use tinystoragedrivers::{CollectionSpec, IndexSpec};
use crate::storage::schema_support::{
- key_of, map_legacy_collection, time_ms, Body, LegacySourceRecord as SourceRecord,
- LegacyTargetRecord as TargetRecord,
+ key_of, key_of_parts, map_legacy_collection, time_ms, Body,
+ LegacySourceRecord as SourceRecord, LegacyTargetRecord as TargetRecord,
};
/// The v2 request collection: one document per request id.
@@ -74,10 +74,8 @@ pub(super) fn legacy_collections() -> Vec<CollectionSpec> {
/// refused.
pub(super) fn trust_id(flow_id: &str, tool_name: &str) -> String {
#[allow(clippy::expect_used)]
- Key::of([flow_id, tool_name])
+ key_of_parts([flow_id, tool_name])
.expect("a two-part key is valid")
- .as_str()
- .to_owned()
}
fn requests_v2(batch: Vec<SourceRecord>) -> Result<Vec<TargetRecord>> {
diff --git a/crates/openhuman-core/src/storage/schema_support.rs b/crates/openhuman-core/src/storage/schema_support.rs
index 223f169c67..76a1316ddf 100644
--- a/crates/openhuman-core/src/storage/schema_support.rs
+++ b/crates/openhuman-core/src/storage/schema_support.rs
@@ -129,7 +129,37 @@ pub fn time_ms(body: &Value, field: &str) -> Option<i64> {
///
/// When `part` is empty.
pub fn key_of(part: &str) -> Result<String, StorageError> {
- Key::of([part]).map(|key| key.as_str().to_owned())
+ key_of_parts([part])
+}
+
+/// Builds a validated key from logical parts without allowing a part to
+/// introduce a separator or the reserved leading hash marker.
+pub fn key_of_parts<'a>(parts: impl IntoIterator<Item = &'a str>) -> Result<String, StorageError> {
+ let mut encoded = String::new();
+ for (index, part) in parts.into_iter().enumerate() {
+ if part.is_empty() {
+ return Err(StorageError::invalid_input("key parts must not be empty"));
+ }
+ if index != 0 {
+ encoded.push('/');
+ }
+ for ch in part.chars() {
+ match ch {
+ '%' => encoded.push_str("%25"),
+ '/' => encoded.push_str("%2F"),
+ '\0' => encoded.push_str("%00"),
+ '~' => encoded.push_str("%7E"),
+ _ => encoded.push(ch),
+ }
+ }
+ }
+ Key::new(encoded).map(|key| key.as_str().to_owned())
+}
+
+/// Creates an opaque id for a newly generated owner record.
+#[must_use]
+pub fn fresh_key() -> String {
+ uuid::Uuid::new_v4().simple().to_string()
}
/// `camelCase` as `snake_case`: `authorId` -> `author_id`.