Skip to content

Commit

Permalink
DirEntry only allow root and simple filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
rbran committed Jul 4, 2023
1 parent ae9b3ee commit 7a4177c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
22 changes: 16 additions & 6 deletions src/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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)
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/squashfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SquashfsFileReader> = match entry.t {
// BasicDirectory, ExtendedDirectory
Expand Down

0 comments on commit 7a4177c

Please sign in to comment.