Skip to content

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

fix(memory): restore brain test coverage and file handling

Steven Enamakel34036948429d
diff --git a/crates/openhuman-core/src/memory/brain_tests.rs b/crates/openhuman-core/src/memory/brain_tests.rs
index 0c4713a4ee..5828852a70 100644
--- a/crates/openhuman-core/src/memory/brain_tests.rs
+++ b/crates/openhuman-core/src/memory/brain_tests.rs
@@ -347,3 +347,59 @@ fn legacy_nodes_map_to_their_connector() {
BrainSource::Other("googledrive".into())
);
}
+
+fn path_params(path: String) -> BrainIngestParams {
+ BrainIngestParams {
+ path: Some(path),
+ text: None,
+ source: None,
+ title: None,
+ }
+}
+
+#[tokio::test]
+async fn ingest_refuses_a_credential_store_a_system_root_traversal_and_null_bytes() {
+ let tmp = tempfile::tempdir().unwrap();
+ let config = config_in(&tmp);
+ let engine = bind_reference(&config);
+ let ssh = tmp.path().join(".ssh");
+ std::fs::create_dir_all(&ssh).unwrap();
+ std::fs::write(ssh.join("id_ed25519"), "-----BEGIN OPENSSH PRIVATE KEY-----").unwrap();
+ let notes = tmp.path().join("notes");
+ std::fs::create_dir_all(&notes).unwrap();
+ std::fs::write(tmp.path().join("guide.md"), "# Guide").unwrap();
+
+ for path in [
+ ssh.join("id_ed25519").display().to_string(),
+ "/etc/hosts".to_string(),
+ format!("{}/../guide.md", notes.display()),
+ format!("{}\0.md", tmp.path().join("guide").display()),
+ ] {
+ let result = ingest(&config, path_params(path.clone())).await;
+ assert!(
+ matches!(result, Err(MemoryError::InvalidRequest(_))),
+ "{path:?}: {result:?}"
+ );
+ }
+ assert!(engine.is_empty(), "nothing refused may be stored");
+}
+
+#[cfg(unix)]
+#[tokio::test]
+async fn ingest_refuses_a_symlink_into_a_credential_store() {
+ let tmp = tempfile::tempdir().unwrap();
+ let config = config_in(&tmp);
+ let engine = bind_reference(&config);
+ let aws = tmp.path().join(".aws");
+ std::fs::create_dir_all(&aws).unwrap();
+ std::fs::write(aws.join("credentials"), "aws_secret_access_key = x").unwrap();
+ let link = tmp.path().join("innocent.md");
+ std::os::unix::fs::symlink(aws.join("credentials"), &link).unwrap();
+
+ let result = ingest(&config, path_params(link.display().to_string())).await;
+ assert!(
+ matches!(result, Err(MemoryError::InvalidRequest(_))),
+ "{result:?}"
+ );
+ assert!(engine.is_empty());
+}
diff --git a/crates/openhuman-core/src/memory/files.rs b/crates/openhuman-core/src/memory/files.rs
new file mode 100644
index 0000000000..02301572ae
--- /dev/null
+++ b/crates/openhuman-core/src/memory/files.rs
@@ -0,0 +1,94 @@
+//! Owner-only writes of the local memory state files.
+//!
+//! The files under `<workspace>/memory/` (the job queue, channel threads,
+//! source sync state, connection roots, connector item versions, the layout
+//! migration state, the local install identity) hold ids and source
+//! settings that are the person's own. `std::fs::write` creates them with
+//! the process umask (0644 under the usual 022), readable by every local
+//! account; here, on unix, a file is created (and kept) 0600 and its
+//! directory 0700, as the config and keyring files already are. Other
+//! platforms keep their default ACLs.
+
+use std::fs::File;
+use std::io::Write;
+use std::path::Path;
+
+/// Owner read/write only.
+#[cfg(unix)]
+pub const FILE_MODE: u32 = 0o600;
+
+/// Owner read/write/search only.
+#[cfg(unix)]
+pub const DIR_MODE: u32 = 0o700;
+
+/// Creates `dir` and any missing parent, each new one 0700 on unix, and
+/// narrows `dir` itself to 0700 when it already existed.
+///
+/// # Errors
+///
+/// When a directory cannot be created or its mode set.
+pub fn create_private_dir_all(dir: &Path) -> std::io::Result<()> {
+ #[cfg(unix)]
+ {
+ use std::os::unix::fs::{DirBuilderExt, PermissionsExt};
+ std::fs::DirBuilder::new()
+ .recursive(true)
+ .mode(DIR_MODE)
+ .create(dir)?;
+ let mode = std::fs::metadata(dir)?.permissions().mode() & 0o777;
+ if mode != DIR_MODE {
+ tracing::debug!(
+ dir = %dir.display(),
+ from = format!("{mode:o}"),
+ "[memory:files] narrowing a state directory to 0700"
+ );
+ std::fs::set_permissions(dir, std::fs::Permissions::from_mode(DIR_MODE))?;
+ }
+ Ok(())
+ }
+ #[cfg(not(unix))]
+ {
+ std::fs::create_dir_all(dir)
+ }
+}
+
+/// Creates (or truncates) `path` for writing, 0600 on unix whether it is
+/// new or already existed with a wider mode.
+///
+/// # Errors
+///
+/// When the file cannot be opened or its mode set.
+pub fn create_private(path: &Path) -> std::io::Result<File> {
+ let mut options = std::fs::OpenOptions::new();
+ options.write(true).create(true).truncate(true);
+ #[cfg(unix)]
+ {
+ use std::os::unix::fs::{OpenOptionsExt, PermissionsExt};
+ options.mode(FILE_MODE);
+ let file = options.open(path)?;
+ // `mode` applies only to a file this call creates.
+ file.set_permissions(std::fs::Permissions::from_mode(FILE_MODE))?;
+ Ok(file)
+ }
+ #[cfg(not(unix))]
+ {
+ options.open(path)
+ }
+}
+
+/// Writes `bytes` to `path` owner-only ([`create_private`]), creating its
+/// directory owner-only ([`create_private_dir_all`]).
+///
+/// # Errors
+///
+/// When the directory or file cannot be created or written.
+pub fn write_private(path: &Path, bytes: &[u8]) -> std::io::Result<()> {
+ if let Some(dir) = path.parent() {
+ create_private_dir_all(dir)?;
+ }
+ create_private(path)?.write_all(bytes)
+}
+
+#[cfg(test)]
+#[path = "files_tests.rs"]
+mod tests;