Skip to content

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

feat(multimodal): return canonical path from managed attachment check

Steven Enamakel899bd7d8a6cf
diff --git a/src/openhuman/agent/multimodal.rs b/src/openhuman/agent/multimodal.rs
index 1d31a7fc2b..7a8f6ed529 100644
--- a/src/openhuman/agent/multimodal.rs
+++ b/src/openhuman/agent/multimodal.rs
@@ -589,14 +589,17 @@ fn attachments_dir() -> PathBuf {
/// Whether a provider image reference points inside this process' managed
/// attachment stash. Raw channel-supplied filesystem paths are never trusted.
pub fn is_managed_attachment_path(path: &str) -> bool {
+ managed_attachment_path(path).is_some()
+}
+
+/// Return the canonical path when `path` resolves inside the managed stash.
+/// Callers should use this returned path for subsequent reads so the checked
+/// path, rather than an attacker-controlled spelling, is what gets opened.
+pub fn managed_attachment_path(path: &str) -> Option<PathBuf> {
let candidate = Path::new(path);
- let Ok(candidate) = candidate.canonicalize() else {
- return false;
- };
- let Ok(root) = attachments_dir().canonicalize() else {
- return false;
- };
- candidate.starts_with(root)
+ let candidate = candidate.canonicalize().ok()?;
+ let root = attachments_dir().canonicalize().ok()?;
+ candidate.starts_with(root).then_some(candidate)
}
#[cfg(test)]
diff --git a/src/openhuman/inference/provider/claude_code/input_builder.rs b/src/openhuman/inference/provider/claude_code/input_builder.rs
index 1bcc04a2e8..cd6737a44d 100644
--- a/src/openhuman/inference/provider/claude_code/input_builder.rs
+++ b/src/openhuman/inference/provider/claude_code/input_builder.rs
@@ -24,7 +24,7 @@ use serde_json::{json, Value};
use crate::openhuman::agent::messages::ChatMessage;
use crate::openhuman::agent::multimodal::{
- is_managed_attachment_path, rehydrate_image_placeholders,
+ managed_attachment_path, rehydrate_image_placeholders,
};
/// Build the bytes to write to claude's stdin. Returns an empty `Vec`
@@ -122,6 +122,12 @@ fn image_block(reference: &str) -> Option<Value> {
let (media_type, data) = if let Some(rest) = reference.strip_prefix("data:") {
let (metadata, payload) = rest.split_once(',')?;
let mime = metadata.split(';').next()?.to_string();
+ if !matches!(
+ mime.to_ascii_lowercase().as_str(),
+ "image/png" | "image/jpeg" | "image/gif" | "image/webp"
+ ) {
+ return None;
+ }
let (bytes, encoded) = if metadata
.split(';')
.any(|flag| flag.eq_ignore_ascii_case("base64"))
@@ -140,15 +146,13 @@ fn image_block(reference: &str) -> Option<Value> {
}
(mime, encoded)
} else {
- if !is_managed_attachment_path(reference) {
- return None;
- }
- let metadata = std::fs::metadata(reference).ok()?;
+ let path = managed_attachment_path(reference)?;
+ let metadata = std::fs::metadata(&path).ok()?;
if metadata.len() > 20 * 1024 * 1024 {
return None;
}
- let bytes = std::fs::read(reference).ok()?;
- let lower = reference.to_ascii_lowercase();
+ let bytes = std::fs::read(&path).ok()?;
+ let lower = path.to_string_lossy().to_ascii_lowercase();
let mime = if lower.ends_with(".png") {
"image/png"
} else if lower.ends_with(".gif") {