diff --git a/src/dir.rs b/src/dir.rs index cab05fd0..8f16890e 100644 --- a/src/dir.rs +++ b/src/dir.rs @@ -4,13 +4,14 @@ //! with references back to the inodes that describe those entries. use core::fmt; -use std::ffi::OsString; -use std::os::unix::prelude::OsStringExt; -use std::path::PathBuf; +use std::ffi::OsStr; +use std::os::unix::prelude::OsStrExt; +use std::path::{Component, Path}; use deku::prelude::*; use crate::inode::InodeId; +use crate::BackhandError; #[derive(Debug, DekuRead, DekuWrite, Clone, PartialEq, Eq)] #[deku(ctx = "type_endian: deku::ctx::Endian")] @@ -78,9 +79,18 @@ impl fmt::Debug for DirEntry { } impl DirEntry { - pub fn name(&self) -> PathBuf { - let name = OsString::from_vec(self.name.clone()); - PathBuf::from(name) + pub fn name(&self) -> Result<&Path, BackhandError> { + // allow root and nothing else + if self.name == Component::RootDir.as_os_str().as_bytes() { + return Ok(Path::new(Component::RootDir.as_os_str())); + } + let path = Path::new(OsStr::from_bytes(&self.name)); + // if not a simple filename, return an error + let filename = path.file_name().map(OsStrExt::as_bytes); + if filename != Some(&self.name) { + return Err(BackhandError::InvalidFilePath); + } + Ok(path) } } diff --git a/src/squashfs.rs b/src/squashfs.rs index 1720b914..8f0314f8 100644 --- a/src/squashfs.rs +++ b/src/squashfs.rs @@ -528,8 +528,7 @@ impl Squashfs { .unwrap(); let found_inode = &self.inodes[&inode_key]; let header = found_inode.header; - let new_path = entry.name(); - fullpath.push(&new_path); + fullpath.push(entry.name()?); let inner: InnerNode = match entry.t { // BasicDirectory, ExtendedDirectory