Skip to content

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

fix(mcp-ui): read show_ui files through one non-following handle

oxoxDev7e2bc2e0b672
diff --git a/crates/openhuman-core/src/mcp/ui/tools.rs b/crates/openhuman-core/src/mcp/ui/tools.rs
index faca12eb51..e1f8d9a148 100644
--- a/crates/openhuman-core/src/mcp/ui/tools.rs
+++ b/crates/openhuman-core/src/mcp/ui/tools.rs
@@ -58,6 +58,15 @@ impl ShowUiTool {
///
/// When the path is malformed, outside every root, or not an HTML file.
pub fn confine_path(roots: &[PathBuf], path: &str) -> Result<PathBuf, String> {
+ confine_path_in(roots, path).map(|(_, resolved)| resolved)
+}
+
+/// [`confine_path`], also returning the canonical root that contains the file.
+///
+/// # Errors
+///
+/// As [`confine_path`].
+pub fn confine_path_in(roots: &[PathBuf], path: &str) -> Result<(PathBuf, PathBuf), String> {
let requested = Path::new(path.trim());
if path.trim().is_empty() || path.contains('\0') {
return Err("path must not be empty".to_string());
@@ -88,12 +97,48 @@ pub fn confine_path(roots: &[PathBuf], path: &str) -> Result<PathBuf, String> {
continue;
};
if resolved.starts_with(&canonical_root) && resolved.is_file() {
- return Ok(resolved);
+ return Ok((canonical_root, resolved));
}
}
Err("the file is not inside the workspace or a skill bundle".to_string())
}
+/// Reads `resolved` through one handle opened without following links below
+/// `root`, bounded by [`MAX_RESOURCE_BYTES`].
+///
+/// # Errors
+///
+/// When the open is refused, the file is not regular, or it is too large.
+pub async fn read_confined(root: PathBuf, resolved: PathBuf) -> Result<String, String> {
+ tokio::task::spawn_blocking(move || {
+ use std::io::Read;
+ let file = crate::agent::attachments::provider::open::secure_open(&resolved, &root)
+ .map_err(|error| {
+ tracing::debug!(?error, "[mcp_ui] show_ui refused the file open");
+ "the file is not inside the workspace or a skill bundle".to_string()
+ })?;
+ let metadata = file
+ .metadata()
+ .map_err(|_| "could not read the file".to_string())?;
+ if !metadata.is_file() {
+ return Err("only regular files can be shown".to_string());
+ }
+ if metadata.len() > MAX_RESOURCE_BYTES as u64 {
+ return Err("the file is too large to show".to_string());
+ }
+ let mut html = String::new();
+ file.take(MAX_RESOURCE_BYTES as u64 + 1)
+ .read_to_string(&mut html)
+ .map_err(|_| "could not read the file".to_string())?;
+ if html.len() > MAX_RESOURCE_BYTES {
+ return Err("the file is too large to show".to_string());
+ }
+ Ok(html)
+ })
+ .await
+ .map_err(|_| "could not read the file".to_string())?
+}
+
fn clean_title(raw: Option<&str>) -> Option<String> {
let title: String = raw?
.chars()
@@ -140,15 +185,14 @@ impl Tool for ShowUiTool {
let html = match (html_arg, path_arg) {
(Some(html), None) => html.to_string(),
(None, Some(path)) => {
- let resolved = match confine_path(&self.roots, path) {
- Ok(resolved) => resolved,
+ let (root, resolved) = match confine_path_in(&self.roots, path) {
+ Ok(found) => found,
Err(error) => return Ok(ToolResult::error(format!("show_ui: {error}"))),
};
- let size = tokio::fs::metadata(&resolved).await?.len();
- if size > MAX_RESOURCE_BYTES as u64 {
- return Ok(ToolResult::error("show_ui: the file is too large to show"));
+ match read_confined(root, resolved).await {
+ Ok(html) => html,
+ Err(error) => return Ok(ToolResult::error(format!("show_ui: {error}"))),
}
- tokio::fs::read_to_string(&resolved).await?
}
_ => {
return Ok(ToolResult::error(
diff --git a/crates/openhuman-core/src/mcp/ui/tools_tests.rs b/crates/openhuman-core/src/mcp/ui/tools_tests.rs
index 309ba12bed..90acd0b15e 100644
--- a/crates/openhuman-core/src/mcp/ui/tools_tests.rs
+++ b/crates/openhuman-core/src/mcp/ui/tools_tests.rs
@@ -35,6 +35,44 @@ fn confine_path_refuses_symlink_escape() {
assert!(confine_path(&[root.path().to_path_buf()], "link.html").is_err());
}
+#[cfg(unix)]
+#[tokio::test]
+async fn read_confined_refuses_a_file_swapped_for_an_outside_symlink() {
+ let root = tempdir();
+ let outside = tempdir();
+ let target = root.path().join("view.html");
+ std::fs::write(&target, "<p>inside</p>").unwrap();
+ std::fs::write(outside.path().join("secret.html"), "<p>secret</p>").unwrap();
+ let roots = vec![root.path().to_path_buf()];
+ let (canonical_root, resolved) = confine_path_in(&roots, "view.html").unwrap();
+ assert_eq!(
+ read_confined(canonical_root.clone(), resolved.clone())
+ .await
+ .unwrap(),
+ "<p>inside</p>"
+ );
+
+ std::fs::remove_file(&target).unwrap();
+ std::os::unix::fs::symlink(outside.path().join("secret.html"), &target).unwrap();
+ assert!(read_confined(canonical_root, resolved).await.is_err());
+}
+
+#[cfg(unix)]
+#[tokio::test]
+async fn read_confined_refuses_a_directory_swapped_for_an_outside_symlink() {
+ let root = tempdir();
+ let outside = tempdir();
+ std::fs::create_dir(root.path().join("pages")).unwrap();
+ std::fs::write(root.path().join("pages/view.html"), "<p>inside</p>").unwrap();
+ std::fs::write(outside.path().join("view.html"), "<p>secret</p>").unwrap();
+ let roots = vec![root.path().to_path_buf()];
+ let (canonical_root, resolved) = confine_path_in(&roots, "pages/view.html").unwrap();
+
+ std::fs::remove_dir_all(root.path().join("pages")).unwrap();
+ std::os::unix::fs::symlink(outside.path(), root.path().join("pages")).unwrap();
+ assert!(read_confined(canonical_root, resolved).await.is_err());
+}
+
#[tokio::test]
async fn inline_html_produces_host_inline_presentation() {
let tool = ShowUiTool::new(vec![]);