Skip to content

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

fix(artifacts): map Windows-special filename characters instead of passing them through

M3gA-Mind90f24d01e94a
diff --git a/crates/openhuman-app/src/artifact_commands.rs b/crates/openhuman-app/src/artifact_commands.rs
index 95a52bf8da..c886d3682a 100644
--- a/crates/openhuman-app/src/artifact_commands.rs
+++ b/crates/openhuman-app/src/artifact_commands.rs
@@ -139,10 +139,13 @@ pub async fn download_artifact_to_downloads(
Ok(dest.display().to_string())
}
-/// Strip path-traversal characters from a filename hint. The
-/// renderer is expected to pass something like `"My Deck.pptx"`;
-/// reject anything that contains a separator or null byte so a
-/// malicious `ai_get_artifact` response can never escape the chosen dir.
+/// Turn a filename hint (the artifact title, e.g. `"Q3: plan.pptx"`) into a
+/// single plain path component. Separators, NUL and `.`/`..` are rejected so
+/// a malicious `ai_get_artifact` response can never escape the chosen dir.
+/// Characters Windows treats specially are replaced rather than rejected —
+/// `:` alone would otherwise make `C:evil.dll` drive-relative and
+/// `a.txt:stream` an alternate data stream — and reserved device names
+/// (`CON`, `NUL`, `COM1`, …) get a `_` prefix.
fn sanitize_filename(name: &str) -> Result<String, String> {
let trimmed = name.trim();
if trimmed.is_empty() {
@@ -159,7 +162,41 @@ fn sanitize_filename(name: &str) -> Result<String, String> {
if trimmed == "." || trimmed == ".." {
return Err(format!("filename must not be '.' or '..': {trimmed:?}"));
}
- Ok(trimmed.to_string())
+ let mapped: String = trimmed
+ .chars()
+ .map(|c| match c {
+ ':' | '<' | '>' | '"' | '|' | '?' | '*' => '_',
+ c if c.is_control() => '_',
+ c => c,
+ })
+ .collect();
+ // Windows drops trailing dots and spaces, so `evil.dll.` would land as
+ // `evil.dll`.
+ let mut name = mapped.trim_end_matches(['.', ' ']).to_string();
+ if name.is_empty() {
+ return Err(format!("filename has no usable characters: {trimmed:?}"));
+ }
+ let stem = name
+ .split('.')
+ .next()
+ .unwrap_or_default()
+ .to_ascii_uppercase();
+ let reserved = matches!(stem.as_str(), "CON" | "PRN" | "AUX" | "NUL")
+ || ((stem.starts_with("COM") || stem.starts_with("LPT"))
+ && matches!(stem.as_bytes().get(3..), Some([b'1'..=b'9'])));
+ if reserved {
+ name.insert(0, '_');
+ }
+ let mut components = Path::new(&name).components();
+ if !matches!(
+ (components.next(), components.next()),
+ (Some(std::path::Component::Normal(_)), None)
+ ) {
+ return Err(format!(
+ "filename must be a single path component: {trimmed:?}"
+ ));
+ }
+ Ok(name)
}
/// Pick a destination path under `dir` that does not exist yet.
diff --git a/crates/openhuman-app/src/artifact_commands_tests.rs b/crates/openhuman-app/src/artifact_commands_tests.rs
index ddd972d970..d854ebab8a 100644
--- a/crates/openhuman-app/src/artifact_commands_tests.rs
+++ b/crates/openhuman-app/src/artifact_commands_tests.rs
@@ -22,6 +22,33 @@ fn sanitize_accepts_plain_names() {
assert_eq!(sanitize_filename(" trim me ").unwrap(), "trim me");
}
+#[test]
+fn sanitize_maps_windows_special_names_to_one_plain_component() {
+ for (input, expected) in [
+ ("C:evil.dll", "C_evil.dll"),
+ ("notes.txt:hidden", "notes.txt_hidden"),
+ ("Q3: plan <draft>?.pptx", "Q3_ plan _draft__.pptx"),
+ ("a\"b|c*.txt", "a_b_c_.txt"),
+ ("tab\there.txt", "tab_here.txt"),
+ ("evil.dll. . ", "evil.dll"),
+ ("CON", "_CON"),
+ ("nul.txt", "_nul.txt"),
+ ("com1.log", "_com1.log"),
+ ("LPT9", "_LPT9"),
+ ("COM10.log", "COM10.log"),
+ ("Console.txt", "Console.txt"),
+ ] {
+ let out = sanitize_filename(input).unwrap();
+ assert_eq!(out, expected, "{input:?}");
+ assert_eq!(
+ std::path::Path::new(&out).components().count(),
+ 1,
+ "{input:?}"
+ );
+ }
+ assert!(sanitize_filename("...").is_err());
+}
+
async fn ready_artifact(workspace: &Path, files_dir: &Path) -> String {
use openhuman_rpc::embed::artifacts::{create_artifact, finalize_artifact, ArtifactKind};
let (meta, path) = create_artifact(