Skip to content

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

fix(artifacts): refuse protected paths as artifact files

Sanilfc2d7857e56a
diff --git a/crates/openhuman-core/src/agent/artifacts/preview.rs b/crates/openhuman-core/src/agent/artifacts/preview.rs
index f086e3a144..a1a31ede4c 100644
--- a/crates/openhuman-core/src/agent/artifacts/preview.rs
+++ b/crates/openhuman-core/src/agent/artifacts/preview.rs
@@ -76,7 +76,12 @@ pub async fn read_body(config: &Config, id: &str) -> Result<ArtifactBody, String
}
};
let converter = DocumentConverter::for_config(config);
- Ok(body_of(&record, &tiny_artifacts::FileRoots::new(roots), &converter).await)
+ Ok(body_of(
+ &record,
+ &super::tiny_store::guarded_roots(roots),
+ &converter,
+ )
+ .await)
}
/// A one-version file record for an artifact that has only its legacy
diff --git a/crates/openhuman-core/src/agent/artifacts/tiny_store.rs b/crates/openhuman-core/src/agent/artifacts/tiny_store.rs
index 03157bb620..30af11ac2f 100644
--- a/crates/openhuman-core/src/agent/artifacts/tiny_store.rs
+++ b/crates/openhuman-core/src/agent/artifacts/tiny_store.rs
@@ -245,8 +245,14 @@ pub fn file_roots(
action_dir: Option<&Path>,
) -> Vec<PathBuf> {
// A relative path resolves against the first root: the agent's working
- // folder when there is one, else the current files folder.
- let mut roots: Vec<PathBuf> = action_dir.map(Path::to_path_buf).into_iter().collect();
+ // folder when there is one, else the current files folder. A working
+ // folder that contains OpenHuman's workspace (the home directory, say) is
+ // left out: it would make OpenHuman's own data referenceable.
+ let mut roots: Vec<PathBuf> = action_dir
+ .filter(|action| !contains(action, workspace_dir))
+ .map(Path::to_path_buf)
+ .into_iter()
+ .collect();
for folder in files.trusted() {
if !roots.contains(folder) {
roots.push(folder.clone());
@@ -256,6 +262,26 @@ pub fn file_roots(
roots
}
+/// Whether `folder` is `inside` or one of its parents, compared canonically
+/// where the paths exist.
+fn contains(folder: &Path, inside: &Path) -> bool {
+ let canon = |p: &Path| p.canonicalize().unwrap_or_else(|_| p.to_path_buf());
+ canon(inside).starts_with(canon(folder))
+}
+
+/// Why a resolved path may not be an artifact's file, if it may not: it is in
+/// a location OpenHuman never lets the agent reach (credential stores, system
+/// roots), however broad the folders around it.
+pub fn refused_path(path: &Path) -> Option<String> {
+ crate::security::SecurityPolicy::is_always_forbidden(path)
+ .then(|| "the file is in a protected location".to_string())
+}
+
+/// [`file_roots`] as the crate's folders, with [`refused_path`] as their guard.
+pub fn guarded_roots(roots: Vec<PathBuf>) -> tiny_artifacts::FileRoots {
+ tiny_artifacts::FileRoots::new(roots).with_guard(refused_path)
+}
+
/// The artifact service for one workspace: OpenHuman's store and UI bridge,
/// with file artifacts allowed from [`file_roots`]. Cards are announced to
/// `chat`, the turn making the change (`None` outside a chat turn).
@@ -268,7 +294,7 @@ pub fn artifacts_service(
let roots = file_roots(workspace_dir, &files, action_dir);
Artifacts::new(Arc::new(versioned_store(workspace_dir)))
.with_observer(Arc::new(LegacyUiBridge::new(workspace_dir, files, chat)))
- .with_file_roots(tiny_artifacts::FileRoots::new(roots))
+ .with_file_roots(guarded_roots(roots))
.with_version_files(version_files_dir(workspace_dir))
}
diff --git a/crates/openhuman-core/src/agent/artifacts/tiny_store_tests.rs b/crates/openhuman-core/src/agent/artifacts/tiny_store_tests.rs
index 7c64e95788..d84e9532b5 100644
--- a/crates/openhuman-core/src/agent/artifacts/tiny_store_tests.rs
+++ b/crates/openhuman-core/src/agent/artifacts/tiny_store_tests.rs
@@ -238,3 +238,38 @@ async fn a_file_in_an_earlier_files_folder_can_still_be_revised() {
.unwrap();
assert_eq!(saved.version, 2);
}
+
+#[tokio::test]
+async fn a_protected_file_inside_the_working_folder_is_refused() {
+ let (workspace, files) = temp_dirs();
+ let action = workspace.parent().unwrap().join("home");
+ let ssh = action.join(".ssh");
+ std::fs::create_dir_all(&ssh).unwrap();
+ std::fs::write(ssh.join("id_ed25519"), b"PRIVATE").unwrap();
+ let artifacts = artifacts_service(&workspace, FileRoots::new(&files), Some(&action), None);
+
+ let err = artifacts
+ .create(
+ ArtifactKind::File,
+ "Key",
+ Content::File(".ssh/id_ed25519".into()),
+ &Author::agent("agent"),
+ None,
+ )
+ .await
+ .unwrap_err();
+ assert!(err.to_string().contains("protected location"), "{err}");
+}
+
+#[test]
+fn a_working_folder_that_contains_the_workspace_is_not_trusted() {
+ let (workspace, files) = temp_dirs();
+ let home = workspace.parent().unwrap().to_path_buf();
+ let roots = file_roots(&workspace, &FileRoots::new(&files), Some(&home));
+ assert!(!roots.contains(&home), "{roots:?}");
+ assert!(roots.contains(&files));
+ // A working folder beside the workspace stays the first root.
+ let beside = home.join("projects");
+ let roots = file_roots(&workspace, &FileRoots::new(&files), Some(&beside));
+ assert_eq!(roots.first(), Some(&beside));
+}
diff --git a/crates/openhuman-core/src/integrations/file_storage/artifact_publisher.rs b/crates/openhuman-core/src/integrations/file_storage/artifact_publisher.rs
index 7f8c60376c..4b949e693a 100644
--- a/crates/openhuman-core/src/integrations/file_storage/artifact_publisher.rs
+++ b/crates/openhuman-core/src/integrations/file_storage/artifact_publisher.rs
@@ -76,7 +76,15 @@ async fn upload_body(
.as_ref()
.map_or(version.body.as_str(), |f| f.path.as_str());
let path = Path::new(path);
- let bytes = tokio::fs::read(path)
+ // The record was checked when written; check again on what the path
+ // resolves to now, so nothing protected leaves the machine.
+ let resolved = tokio::fs::canonicalize(path)
+ .await
+ .map_err(|e| anyhow::anyhow!("cannot read the artifact's file: {e}"))?;
+ if let Some(reason) = crate::agent::artifacts::tiny_store::refused_path(&resolved) {
+ anyhow::bail!("[policy-blocked] cannot share this artifact: {reason}");
+ }
+ let bytes = tokio::fs::read(&resolved)
.await
.map_err(|e| anyhow::anyhow!("cannot read the artifact's file: {e}"))?;
// Named after the artifact: the file read may be a kept version copy
diff --git a/crates/openhuman-core/src/integrations/file_storage/artifact_publisher_tests.rs b/crates/openhuman-core/src/integrations/file_storage/artifact_publisher_tests.rs
index baa19620af..b7d0afad23 100644
--- a/crates/openhuman-core/src/integrations/file_storage/artifact_publisher_tests.rs
+++ b/crates/openhuman-core/src/integrations/file_storage/artifact_publisher_tests.rs
@@ -338,3 +338,33 @@ async fn a_server_failure_on_the_link_is_not_retried() {
.unwrap_err();
assert!(err.to_string().contains("link generation failed"), "{err}");
}
+
+#[tokio::test]
+async fn a_file_in_a_protected_location_is_never_uploaded() {
+ let dir = tempfile::tempdir().unwrap();
+ let ssh = dir.path().join(".ssh");
+ std::fs::create_dir_all(&ssh).unwrap();
+ let key = ssh.join("id_ed25519");
+ std::fs::write(&key, b"PRIVATE").unwrap();
+ let mut version = ArtifactVersion::new(
+ 1,
+ key.display().to_string(),
+ tiny_artifacts::ArtifactAuthor::Agent,
+ "agent",
+ 1,
+ );
+ version.file = Some(FileRef {
+ path: key.display().to_string(),
+ size_bytes: 7,
+ media_type: None,
+ });
+ let mut record = ArtifactRecord::pending("k-1", "Key", ArtifactKind::File, 1);
+ record.versions.push(version.clone());
+
+ // An unreachable backend: an upload attempt would fail differently.
+ let err = publisher("http://127.0.0.1:9".into(), AutonomyLevel::Full)
+ .publish(&record, &version, &PublishOptions::default())
+ .await
+ .unwrap_err();
+ assert!(err.to_string().contains("protected location"), "{err}");
+}
diff --git a/vendor/tiny-artifacts b/vendor/tiny-artifacts
index ea9ad1db2c..3f43558c63 160000
--- a/vendor/tiny-artifacts
+++ b/vendor/tiny-artifacts
@@ -1 +1 @@
-Subproject commit ea9ad1db2c308852c60c1891447af30b731013d6
+Subproject commit 3f43558c63d82a52c1deded6f29edbe798a537a0