use std::path::{Component, Path};
pub fn is_safe_relative(rel: &str) -> bool {
!rel.is_empty()
&& !rel.contains('\0')
&& Path::new(rel)
.components()
.all(|c| matches!(c, Component::Normal(_) | Component::CurDir))
}
#[cfg(test)]
mod tests {
use super::is_safe_relative;
#[test]
fn accepts_nested_relative() {
assert!(is_safe_relative("a/b.png"));
assert!(is_safe_relative("./a"));
}
#[test]
fn rejects_escapes() {
for bad in ["", "../x", "a/../../x", "/etc/passwd", "a\0b"] {
assert!(!is_safe_relative(bad), "{bad:?}");
}
}
}