···1717};
1818use tar::{Archive, Entry};
19192020-use camino::{Utf8Path, Utf8PathBuf};
2020+use camino::{Utf8Component, Utf8Path, Utf8PathBuf};
2121+2222+/// Validates that a path is safe to use as a relative path.
2323+pub fn validate_safe_relative_path(path: &Utf8Path) -> Result<(), &'static str> {
2424+ // Absolute paths are not permitted.
2525+ // On Windows paths starting with \\ are drive-relative, so absolute as
2626+ // far as we are concerned.
2727+ if path.is_absolute() || (cfg!(windows) && path.starts_with("\\")) {
2828+ return Err("paths must be relative");
2929+ }
3030+3131+ for component in path.components() {
3232+ if component == Utf8Component::ParentDir {
3333+ return Err("paths must not contain .. segments");
3434+ }
3535+ }
3636+3737+ Ok(())
3838+}
21392240/// Takes in a source path and a target path and determines a relative path
2341/// from source -> target.