Skip to content

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

refactor(attachments): improve native attachment reading logic

Steven Enamakelaa66d72207d2
diff --git a/crates/openhuman-core/src/channels/runtime/dispatch/host_agent/mod.rs b/crates/openhuman-core/src/channels/runtime/dispatch/host_agent/mod.rs
index dd089b10c1..e9af65a90b 100644
--- a/crates/openhuman-core/src/channels/runtime/dispatch/host_agent/mod.rs
+++ b/crates/openhuman-core/src/channels/runtime/dispatch/host_agent/mod.rs
@@ -57,7 +57,11 @@ pub(crate) async fn configured_agent(
);
config.default_model = Some(route.model.clone());
config.default_temperature = ctx.temperature;
- config.multimodal_files = crate::config::MultimodalFileConfig::for_untrusted_channel_input();
+ // Keep the operator's actual count, MIME, and byte limits for the
+ // host-issued opaque media grants. External marker/path resolution remains
+ // disabled by the ExternalChannel attachment scope, and remote fetch stays
+ // off; the zero sentinel here would also reject these exact trusted refs.
+ config.multimodal_files.allow_remote_fetch = false;
HostAgent::from_config(&dispatch, config, &agent_id).map_err(|error| error.to_string())
}
diff --git a/crates/openhuman-core/src/threads/attachments/staged.rs b/crates/openhuman-core/src/threads/attachments/staged.rs
index 8ce314c5a3..2e3270fb14 100644
--- a/crates/openhuman-core/src/threads/attachments/staged.rs
+++ b/crates/openhuman-core/src/threads/attachments/staged.rs
@@ -472,9 +472,11 @@ pub(crate) async fn read_authorized_native_attachment(
.content_type
.clone()
.ok_or_else(|| "native attachment MIME is unavailable".to_owned())?;
- let bytes = tokio::fs::read(path)
+ let root = tokio::fs::canonicalize(&workspace)
.await
- .map_err(|_| "native attachment bytes are unavailable".to_owned())?;
+ .map_err(|_| "native attachment workspace is unavailable".to_owned())?;
+ let limit = native_attachment_byte_limit(&record.attachment.part, config)?;
+ let bytes = read_staged_file_bounded(&path, &root, limit).await?;
validate_native_attachment_size(&record.attachment.part, bytes.len(), config)?;
Ok(ResolvedNativeAttachment {
path: entry.path.clone(),
@@ -483,6 +485,47 @@ pub(crate) async fn read_authorized_native_attachment(
})
}
+fn native_attachment_byte_limit(
+ part: &ContentPart,
+ config: &crate::config::Config,
+) -> Result<usize, String> {
+ let is_image = matches!(part, ContentPart::Image { .. });
+ let mb = if is_image {
+ config.multimodal.effective_limits().1
+ } else {
+ config.multimodal_files.effective_limits().1
+ };
+ Ok(mb
+ .saturating_mul(1024 * 1024)
+ .min(tinychannels_bus::module_media::MEDIA_UPLOAD_MAX_BYTES))
+}
+
+async fn read_staged_file_bounded(
+ path: &std::path::Path,
+ root: &std::path::Path,
+ limit: usize,
+) -> Result<Vec<u8>, String> {
+ let file = crate::agent::attachments::secure_open(path, root)
+ .map_err(|_| "native attachment bytes are unavailable".to_owned())?;
+ let metadata = file
+ .metadata()
+ .map_err(|_| "native attachment bytes are unavailable".to_owned())?;
+ if !metadata.is_file() || metadata.len() > limit as u64 {
+ return Err("native attachment exceeds its configured byte limit".into());
+ }
+ use tokio::io::AsyncReadExt;
+ let file = tokio::fs::File::from_std(file);
+ let mut bytes = Vec::new();
+ file.take(limit.saturating_add(1) as u64)
+ .read_to_end(&mut bytes)
+ .await
+ .map_err(|_| "native attachment bytes are unavailable".to_owned())?;
+ if bytes.len() > limit {
+ return Err("native attachment exceeds its configured byte limit".into());
+ }
+ Ok(bytes)
+}
+
fn validate_native_attachment_policy(
entry: &StoredAttachment,
config: &crate::config::Config,
diff --git a/crates/openhuman-core/src/threads/attachments_tests.rs b/crates/openhuman-core/src/threads/attachments_tests.rs
index b4cad70acb..dc4e8720b5 100644
--- a/crates/openhuman-core/src/threads/attachments_tests.rs
+++ b/crates/openhuman-core/src/threads/attachments_tests.rs
@@ -302,6 +302,44 @@ async fn native_attachment_refs_are_bound_to_profile_agent_account_and_turn() {
);
}
+#[tokio::test]
+async fn native_staged_file_reader_enforces_limit_and_rejects_symlinks() {
+ let temp = tempfile::tempdir().unwrap();
+ let root = temp.path().join("workspace");
+ let outside = temp.path().join("outside");
+ std::fs::create_dir_all(&root).unwrap();
+ std::fs::write(root.join("small.txt"), b"small").unwrap();
+ std::fs::write(root.join("large.txt"), b"larger than bound").unwrap();
+
+ assert_eq!(
+ super::staged::read_staged_file_bounded(&root.join("small.txt"), &root, 16)
+ .await
+ .unwrap(),
+ b"small"
+ );
+ assert!(
+ super::staged::read_staged_file_bounded(&root.join("large.txt"), &root, 4)
+ .await
+ .unwrap_err()
+ .contains("configured byte limit")
+ );
+
+ #[cfg(unix)]
+ {
+ use std::os::unix::fs::symlink;
+
+ std::fs::write(&outside, b"outside workspace").unwrap();
+ symlink(&outside, root.join("swapped.txt")).unwrap();
+ assert!(super::staged::read_staged_file_bounded(
+ &root.join("swapped.txt"),
+ &root,
+ 128,
+ )
+ .await
+ .is_err());
+ }
+}
+
#[tokio::test]
async fn multipart_claim_failure_keeps_prior_claim_owned_by_retry() {
let dir = tempfile::tempdir().unwrap();