use anyhow::{Result, bail};
pub fn validate_key(key: &str, allowed_prefixes: &[String]) -> Result<()> {
if key.is_empty() {
bail!("empty key");
}
if key.starts_with('/') {
bail!("key must not start with /");
}
if key.split('/').any(|seg| seg == "..") {
bail!("key must not contain ..");
}
if allowed_prefixes.is_empty() {
return Ok(());
}
let allowed = allowed_prefixes
.iter()
.any(|p| key == p || key.starts_with(&format!("{p}/")));
if !allowed {
bail!("key {key} is outside allowed prefixes {allowed_prefixes:?}");
}
Ok(())
}