use eyre::Context;
use std::path::PathBuf;
use uuid::Uuid;
pub(crate) struct ArtifactStore {
base_dir: PathBuf,
}
impl ArtifactStore {
pub fn new() -> eyre::Result<Self> {
let base_dir = std::env::temp_dir().join(format!("dora-artifacts-{}", Uuid::new_v4()));
std::fs::create_dir_all(&base_dir)
.with_context(|| format!("failed to create artifact dir {}", base_dir.display()))?;
Ok(Self { base_dir })
}
pub fn artifact_path(&self, build_id: &Uuid, node_id: &str) -> Option<PathBuf> {
sanitize_node_id(node_id).map(|s| self.base_dir.join(build_id.to_string()).join(s))
}
pub fn cleanup(&self) {
if self.base_dir.exists() {
if let Err(err) = std::fs::remove_dir_all(&self.base_dir) {
tracing::warn!(
"failed to clean up artifact store at {}: {err}",
self.base_dir.display()
);
} else {
tracing::debug!("cleaned up artifact store at {}", self.base_dir.display());
}
}
}
}
impl Drop for ArtifactStore {
fn drop(&mut self) {
self.cleanup();
}
}
fn sanitize_node_id(node_id: &str) -> Option<String> {
if node_id.is_empty()
|| node_id.contains("..")
|| node_id.contains('/')
|| node_id.contains('\\')
{
return None;
}
let sanitized: String = node_id
.chars()
.map(|c| {
if c.is_alphanumeric() || c == '-' || c == '_' {
c
} else {
'_'
}
})
.collect();
Some(sanitized)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn artifact_path_constructs_correct_path() {
let store = ArtifactStore::new().unwrap();
let build_id = Uuid::new_v4();
let path = store.artifact_path(&build_id, "my-node").unwrap();
assert!(path.ends_with("my-node"));
assert!(path.to_str().unwrap().contains(&build_id.to_string()));
}
#[test]
fn artifact_path_rejects_traversal() {
let store = ArtifactStore::new().unwrap();
let build_id = Uuid::new_v4();
assert!(store.artifact_path(&build_id, "../etc/passwd").is_none());
assert!(store.artifact_path(&build_id, "foo/bar").is_none());
assert!(store.artifact_path(&build_id, "").is_none());
}
#[test]
fn sanitizes_special_chars() {
assert_eq!(sanitize_node_id("my node!"), Some("my_node_".into()));
assert_eq!(sanitize_node_id("ok-node_1"), Some("ok-node_1".into()));
}
#[test]
fn drop_cleans_up_temp_dir() {
let dir = {
let store = ArtifactStore {
base_dir: std::env::temp_dir()
.join(format!("dora-artifacts-test-{}", Uuid::new_v4())),
};
std::fs::create_dir_all(&store.base_dir).unwrap();
let dir = store.base_dir.clone();
let build_dir = dir.join("test-build");
std::fs::create_dir_all(&build_dir).unwrap();
std::fs::write(build_dir.join("artifact.bin"), b"data").unwrap();
assert!(dir.exists());
dir
};
assert!(!dir.exists(), "artifact dir should be removed on drop");
}
#[test]
fn artifact_path_rejects_backslash() {
let store = ArtifactStore::new().unwrap();
let build_id = Uuid::new_v4();
assert!(store.artifact_path(&build_id, r"foo\..\bar").is_none());
assert!(store.artifact_path(&build_id, r"foo\bar").is_none());
}
}