Skip to content

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

fix(artifacts): legacy records go through the escape guard

M3gA-Mind549a6ca2617f
diff --git a/crates/openhuman-core/src/artifacts/files.rs b/crates/openhuman-core/src/artifacts/files.rs
index 72e57037aa..e67e557d35 100644
--- a/crates/openhuman-core/src/artifacts/files.rs
+++ b/crates/openhuman-core/src/artifacts/files.rs
@@ -188,12 +188,12 @@ pub(crate) async fn resolve_file(
let Some(file) = meta.file.as_deref() else {
let root = artifacts_root(workspace_dir).await?;
let resolved = root.join(&meta.path);
- if has_parent_component(Path::new(&meta.path)) || !resolved.starts_with(&root) {
- return Err(format!(
- "[artifacts] meta.path {:?} escapes artifacts root for id={}",
+ check_within(&root, &resolved).map_err(|reason| {
+ format!(
+ "[artifacts] meta.path {:?} escapes artifacts root for id={}: {reason}",
meta.path, meta.id
- ));
- }
+ )
+ })?;
return Ok(resolved);
};
let file = PathBuf::from(file);
@@ -221,8 +221,9 @@ fn has_parent_component(path: &Path) -> bool {
path.components().any(|c| matches!(c, Component::ParentDir))
}
-/// The escape guard for a recorded `file` against its `file_root`.
-fn check_within(root: &Path, file: &Path) -> Result<(), &'static str> {
+/// The escape guard for a recorded `file` against its `file_root` (or a
+/// legacy record's bytes against the artifacts root).
+pub(super) fn check_within(root: &Path, file: &Path) -> Result<(), &'static str> {
if !root.is_absolute() || !file.is_absolute() {
return Err("path is not absolute");
}
diff --git a/crates/openhuman-core/src/artifacts/files_tests.rs b/crates/openhuman-core/src/artifacts/files_tests.rs
index 10b160ad1b..b2dfc224e0 100644
--- a/crates/openhuman-core/src/artifacts/files_tests.rs
+++ b/crates/openhuman-core/src/artifacts/files_tests.rs
@@ -432,3 +432,24 @@ async fn a_previously_trusted_folder_still_resolves() {
.unwrap_err();
assert!(err.contains("not a files folder"), "{err}");
}
+
+/// A legacy record goes through the same escape guard: a symlink placed at its
+/// path must not serve a file from outside `artifacts/`.
+#[cfg(unix)]
+#[tokio::test]
+async fn a_legacy_record_symlinked_out_of_the_artifacts_root_is_refused() {
+ let tmp = TempDir::new().unwrap();
+ let secret = tmp.path().join("secret.txt");
+ std::fs::write(&secret, b"private").unwrap();
+ let meta = legacy_meta("legacy-3", "legacy-3/x.txt");
+ save_artifact_meta(tmp.path(), &meta).await.unwrap();
+ std::fs::create_dir_all(tmp.path().join("artifacts/legacy-3")).unwrap();
+ std::os::unix::fs::symlink(&secret, tmp.path().join("artifacts/legacy-3/x.txt")).unwrap();
+
+ let read = read_artifact_bytes(tmp.path(), tmp.path(), "legacy-3").await;
+ assert!(
+ read.is_err(),
+ "legacy symlink served: {:?}",
+ read.map(|bytes| String::from_utf8_lossy(&bytes).into_owned())
+ );
+}
diff --git a/crates/openhuman-core/src/artifacts/migrate.rs b/crates/openhuman-core/src/artifacts/migrate.rs
index 68b64d24d5..a6401ce542 100644
--- a/crates/openhuman-core/src/artifacts/migrate.rs
+++ b/crates/openhuman-core/src/artifacts/migrate.rs
@@ -131,16 +131,10 @@ pub(crate) async fn migrate_with(
/// The legacy on-disk bytes of an unmigrated record, when present and inside
/// its own artifact directory.
fn legacy_bytes(root: &Path, artifact_dir: &Path, meta: &ArtifactMeta) -> Option<PathBuf> {
- let rel = Path::new(&meta.path);
- if rel.is_absolute()
- || rel
- .components()
- .any(|c| matches!(c, std::path::Component::ParentDir))
- {
- return None;
- }
- let candidate = root.join(rel);
- (candidate.starts_with(artifact_dir) && candidate.is_file()).then_some(candidate)
+ let candidate = root.join(&meta.path);
+ // The escape guard also refuses a symlink that leads out of the directory.
+ (super::files::check_within(artifact_dir, &candidate).is_ok() && candidate.is_file())
+ .then_some(candidate)
}
/// For a migrated record, every regular file in its artifact directory other
diff --git a/crates/openhuman-core/src/artifacts/migrate_tests.rs b/crates/openhuman-core/src/artifacts/migrate_tests.rs
index 4bd54c0111..4f2080688f 100644
--- a/crates/openhuman-core/src/artifacts/migrate_tests.rs
+++ b/crates/openhuman-core/src/artifacts/migrate_tests.rs
@@ -325,3 +325,24 @@ async fn an_orphaned_partial_from_an_earlier_run_is_removed() {
assert_eq!(partials(&files_dir), 0);
assert_eq!(visible(&files_dir), vec!["notes.docx"]);
}
+
+/// The migration must not copy a symlink's target out of its artifact directory
+/// into the visible Files folder.
+#[cfg(unix)]
+#[tokio::test]
+async fn a_legacy_file_symlinked_out_of_its_directory_is_not_migrated() {
+ let tmp = TempDir::new().unwrap();
+ let ws = tmp.path().join("ws");
+ let files_dir = tmp.path().join("Files");
+ let secret = tmp.path().join("secret.txt");
+ std::fs::write(&secret, b"private").unwrap();
+ legacy(&ws, "s1", "x.txt", b"placeholder", ArtifactStatus::Ready).await;
+ let link = legacy_path(&ws, "s1", "x.txt");
+ std::fs::remove_file(&link).unwrap();
+ std::os::unix::fs::symlink(&secret, &link).unwrap();
+
+ let report = migrate_legacy_artifacts(&ws, &files_dir).await;
+
+ assert_eq!(report.moved, 0);
+ assert!(visible(&files_dir).is_empty(), "{:?}", visible(&files_dir));
+}