Skip to content

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

fix(artifacts): fail closed when a recorded file cannot be resolved

M3gA-Mind2d2e2e112cbb
diff --git a/crates/openhuman-core/src/agent/artifacts/files.rs b/crates/openhuman-core/src/agent/artifacts/files.rs
index f495dc6afb..b47e9b916f 100644
--- a/crates/openhuman-core/src/agent/artifacts/files.rs
+++ b/crates/openhuman-core/src/agent/artifacts/files.rs
@@ -132,11 +132,25 @@ fn check_within(root: &Path, file: &Path) -> Result<(), &'static str> {
if SecurityPolicy::is_always_forbidden(file) {
return Err("file is in a protected location");
}
- // A symlink inside the folder must not lead out of it.
- if let (Ok(canon_file), Ok(canon_root)) = (file.canonicalize(), root.canonicalize()) {
- if !canon_file.starts_with(&canon_root) || SecurityPolicy::is_always_forbidden(&canon_file)
- {
- return Err("file resolves outside its files folder");
+ // A symlink inside the folder must not lead out of it. Fail closed: when
+ // anything is at the path, both sides must resolve, so a dangling or
+ // looping symlink (or an unresolvable folder) is refused rather than
+ // skipped — a skipped check would let a later read follow whatever the
+ // link comes to point at. Only a path with nothing at it passes here; the
+ // caller reports that as "file missing".
+ match file.symlink_metadata() {
+ Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
+ Err(_) => return Err("file could not be resolved"),
+ Ok(_) => {
+ let (Ok(canon_file), Ok(canon_root)) = (file.canonicalize(), root.canonicalize())
+ else {
+ return Err("file could not be resolved");
+ };
+ if !canon_file.starts_with(&canon_root)
+ || SecurityPolicy::is_always_forbidden(&canon_file)
+ {
+ return Err("file resolves outside its files folder");
+ }
}
}
Ok(())
diff --git a/crates/openhuman-core/src/agent/artifacts/files_tests.rs b/crates/openhuman-core/src/agent/artifacts/files_tests.rs
index 208e582388..5c2c597e76 100644
--- a/crates/openhuman-core/src/agent/artifacts/files_tests.rs
+++ b/crates/openhuman-core/src/agent/artifacts/files_tests.rs
@@ -304,3 +304,59 @@ async fn a_legacy_path_with_parent_components_is_rejected() {
.unwrap_err();
assert!(err.contains("escapes artifacts root"), "{err}");
}
+
+/// A dangling symlink must be refused by the guard, not skipped: if its target
+/// appears later, an unchecked read would follow it out of the folder.
+#[cfg(unix)]
+#[tokio::test]
+async fn the_escape_guard_rejects_a_dangling_symlink() {
+ let tmp = TempDir::new().unwrap();
+ let files_dir = tmp.path().join("Files");
+ std::fs::create_dir_all(&files_dir).unwrap();
+ let link = files_dir.join("dangling.txt");
+ std::os::unix::fs::symlink(tmp.path().join("not-yet-there.txt"), &link).unwrap();
+
+ let err = tampered(tmp.path(), &link, &files_dir).await;
+ assert!(err.contains("could not be resolved"), "{err}");
+}
+
+#[cfg(unix)]
+#[tokio::test]
+async fn the_escape_guard_rejects_a_symlink_loop() {
+ let tmp = TempDir::new().unwrap();
+ let files_dir = tmp.path().join("Files");
+ std::fs::create_dir_all(&files_dir).unwrap();
+ let a = files_dir.join("a.txt");
+ let b = files_dir.join("b.txt");
+ std::os::unix::fs::symlink(&b, &a).unwrap();
+ std::os::unix::fs::symlink(&a, &b).unwrap();
+
+ let err = tampered(tmp.path(), &a, &files_dir).await;
+ assert!(err.contains("could not be resolved"), "{err}");
+}
+
+/// A files folder that cannot be resolved (here, itself a symlink loop) must
+/// not let the check through either.
+#[cfg(unix)]
+#[tokio::test]
+async fn the_escape_guard_rejects_an_unresolvable_folder() {
+ let tmp = TempDir::new().unwrap();
+ let files_dir = tmp.path().join("Files");
+ let other = tmp.path().join("Other");
+ std::os::unix::fs::symlink(&other, &files_dir).unwrap();
+ std::os::unix::fs::symlink(&files_dir, &other).unwrap();
+
+ let err = tampered(tmp.path(), &files_dir.join("doc.txt"), &files_dir).await;
+ assert!(err.contains("could not be resolved"), "{err}");
+}
+
+/// A path with nothing at it passes the guard and is reported as missing.
+#[tokio::test]
+async fn a_recorded_file_that_does_not_exist_reads_as_missing() {
+ let tmp = TempDir::new().unwrap();
+ let files_dir = tmp.path().join("Files");
+ std::fs::create_dir_all(&files_dir).unwrap();
+
+ let err = tampered(tmp.path(), &files_dir.join("gone.txt"), &files_dir).await;
+ assert!(err.contains("file missing"), "{err}");
+}