From 3c33f5e6dcf6af8e8ee1af45e7891165c8bfbfea Mon Sep 17 00:00:00 2001 From: Rubens Brandao Date: Thu, 23 Nov 2023 14:12:49 -0300 Subject: [PATCH 1/3] not allow invalid file path --- backhand/src/filesystem/node.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/backhand/src/filesystem/node.rs b/backhand/src/filesystem/node.rs index 90bb6360..114ad781 100644 --- a/backhand/src/filesystem/node.rs +++ b/backhand/src/filesystem/node.rs @@ -158,8 +158,13 @@ impl Nodes { let path = &node.fullpath; let parent = node.fullpath.parent().ok_or(BackhandError::InvalidFilePath)?; - //check the parent exists - let _parent = self.node_mut(parent).ok_or(BackhandError::InvalidFilePath)?; + //check if the parent exists and is a dir + let parent = self.node_mut(parent).ok_or(BackhandError::InvalidFilePath)?; + match &parent.inner { + InnerNode::Dir(_) => {} + _ => return Err(BackhandError::InvalidFilePath), + } + match self.nodes.binary_search_by(|node| node.fullpath.as_path().cmp(path)) { //file with this fullpath already exists From 32c4c0dc61065207ddc33d04c66f4d77855b218b Mon Sep 17 00:00:00 2001 From: Rubens Brandao Date: Thu, 23 Nov 2023 14:13:10 -0300 Subject: [PATCH 2/3] add test for issue 363 --- backhand-test/tests/issues.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/backhand-test/tests/issues.rs b/backhand-test/tests/issues.rs index e6d67aca..0cfa8944 100644 --- a/backhand-test/tests/issues.rs +++ b/backhand-test/tests/issues.rs @@ -17,3 +17,21 @@ fn issue_359() { fs.push_dir_all("a/b/c/d/e/f/g", header).unwrap(); fs.write(&mut writer).unwrap(); } + +/// https://github.com/wcampbell0x2a/backhand/issues/363 +#[test] +#[cfg(feature = "xz")] +fn issue_363() { + let dummy_file = std::io::Cursor::new(&[]); + let dummy_header = backhand::NodeHeader::default(); + let mut fs = backhand::FilesystemWriter::default(); + // create a files + fs.push_file(dummy_file.clone(), "a", dummy_header).unwrap(); + // try to put a file inside the first file + match fs.push_file(dummy_file, "a/b", dummy_header) { + // correct result: InvalidFilePath (or equivalent error?) + Err(backhand::BackhandError::InvalidFilePath) => {}, + Ok(_) => panic!("Invalid result"), + x => x.unwrap(), + }; +} From 570e0e06e3db5b9720b3097be3c89a763c2d2cdc Mon Sep 17 00:00:00 2001 From: Rubens Brandao Date: Thu, 23 Nov 2023 14:18:51 -0300 Subject: [PATCH 3/3] cargo fmt --- backhand-test/tests/issues.rs | 2 +- backhand/src/filesystem/node.rs | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/backhand-test/tests/issues.rs b/backhand-test/tests/issues.rs index 0cfa8944..bf621cf1 100644 --- a/backhand-test/tests/issues.rs +++ b/backhand-test/tests/issues.rs @@ -30,7 +30,7 @@ fn issue_363() { // try to put a file inside the first file match fs.push_file(dummy_file, "a/b", dummy_header) { // correct result: InvalidFilePath (or equivalent error?) - Err(backhand::BackhandError::InvalidFilePath) => {}, + Err(backhand::BackhandError::InvalidFilePath) => {} Ok(_) => panic!("Invalid result"), x => x.unwrap(), }; diff --git a/backhand/src/filesystem/node.rs b/backhand/src/filesystem/node.rs index 114ad781..ed33657c 100644 --- a/backhand/src/filesystem/node.rs +++ b/backhand/src/filesystem/node.rs @@ -165,7 +165,6 @@ impl Nodes { _ => return Err(BackhandError::InvalidFilePath), } - match self.nodes.binary_search_by(|node| node.fullpath.as_path().cmp(path)) { //file with this fullpath already exists Ok(_index) => Err(BackhandError::DuplicatedFileName),