diff --git a/rc-zip-sync/tests/integration_tests.rs b/rc-zip-sync/tests/integration_tests.rs index 5d0e391..121180c 100644 --- a/rc-zip-sync/tests/integration_tests.rs +++ b/rc-zip-sync/tests/integration_tests.rs @@ -3,6 +3,7 @@ use rc_zip_corpus::{zips_dir, Case, Files}; use rc_zip_sync::{ArchiveHandle, HasCursor, ReadZip, ReadZipStreaming, ReadZipWithSize}; use std::{ + collections::HashMap, fs::File, io::{self, Read}, }; @@ -21,6 +22,8 @@ fn check_case(test: &Case, archive: Result, E .by_name(file.name) .unwrap_or_else(|| panic!("entry {} should exist", file.name)); + assert_eq!(Some(entry.name.as_str()), entry.sanitized_name()); + tracing::info!("got entry for {}", file.name); rc_zip_corpus::check_file_against(file, &entry, &entry.bytes().unwrap()[..]) } @@ -121,6 +124,45 @@ fn streaming() { } } +#[test] +fn zip_slip() { + rc_zip_corpus::install_test_subscriber(); + + let file = File::open(zips_dir().join("zip_slip.zip")).unwrap(); + let archive = file.read_zip().unwrap(); + assert_eq!(archive.entries().count(), 13); + + let expected = HashMap::from([ + ("..", None), + ("/..", None), + ("./..", None), + ("../../etc/passwd", None), + ("../escape.txt", None), + ("a/../../../etc/shadow", None), + (".", Some(".")), + ("...", Some("...")), + ("..hidden", Some("..hidden")), + ("fo..o/notes.txt", Some("fo..o/notes.txt")), + ("backup..2020/config.yaml", Some("backup..2020/config.yaml")), + ( + "release..archive/README.md", + Some("release..archive/README.md"), + ), + ( + "./..././.../a.b..c...d..txt", + Some("./..././.../a.b..c...d..txt"), + ), + ]); + assert_eq!(archive.entries().count(), expected.len()); + + for entry in archive.entries() { + let exp = expected + .get(entry.name.as_str()) + .unwrap_or_else(|| panic!("Path '{}' not found in expected values", entry.name)); + assert_eq!(&entry.sanitized_name(), exp); + } +} + // This helps find bugs in state machines! struct OneByteReadWrapper(R); diff --git a/rc-zip-tokio/tests/integration_tests.rs b/rc-zip-tokio/tests/integration_tests.rs index 7b20460..76ceb29 100644 --- a/rc-zip-tokio/tests/integration_tests.rs +++ b/rc-zip-tokio/tests/integration_tests.rs @@ -4,7 +4,7 @@ use rc_zip_corpus::{zips_dir, Case, Files}; use rc_zip_tokio::{ArchiveHandle, HasCursor, ReadZip, ReadZipStreaming, ReadZipWithSize}; use tokio::io::{AsyncRead, AsyncReadExt, ReadBuf}; -use std::{pin::Pin, sync::Arc, task}; +use std::{collections::HashMap, pin::Pin, sync::Arc, task}; async fn check_case(test: &Case, archive: Result, Error>) { rc_zip_corpus::check_case(test, archive.as_ref().map(|ar| -> &Archive { ar })); @@ -118,6 +118,45 @@ async fn streaming() { } } +#[tokio::test] +async fn zip_slip() { + rc_zip_corpus::install_test_subscriber(); + + let file = Arc::new(RandomAccessFile::open(zips_dir().join("zip_slip.zip")).unwrap()); + let archive = file.read_zip().await.unwrap(); + assert_eq!(archive.entries().count(), 13); + + let expected = HashMap::from([ + ("..", None), + ("/..", None), + ("./..", None), + ("../../etc/passwd", None), + ("../escape.txt", None), + ("a/../../../etc/shadow", None), + (".", Some(".")), + ("...", Some("...")), + ("..hidden", Some("..hidden")), + ("fo..o/notes.txt", Some("fo..o/notes.txt")), + ("backup..2020/config.yaml", Some("backup..2020/config.yaml")), + ( + "release..archive/README.md", + Some("release..archive/README.md"), + ), + ( + "./..././.../a.b..c...d..txt", + Some("./..././.../a.b..c...d..txt"), + ), + ]); + assert_eq!(archive.entries().count(), expected.len()); + + for entry in archive.entries() { + let exp = expected + .get(entry.name.as_str()) + .unwrap_or_else(|| panic!("Path '{}' not found in expected values", entry.name)); + assert_eq!(&entry.sanitized_name(), exp); + } +} + // This helps find bugs in state machines! struct OneByteReadWrapper(R); diff --git a/rc-zip/src/parse/archive.rs b/rc-zip/src/parse/archive.rs index 0d4836e..1a8ae2d 100644 --- a/rc-zip/src/parse/archive.rs +++ b/rc-zip/src/parse/archive.rs @@ -291,31 +291,22 @@ impl Entry { /// /// Other than that, it will strip any leading slashes on non-Windows OSes. pub fn sanitized_name(&self) -> Option<&str> { - let name = self.name.as_str(); - // refuse entries with traversed/absolute path to mitigate zip slip - if name.contains("..") { + if self.name.split(['/', '\\']).any(|part| part == "..") { return None; } #[cfg(windows)] { - if name.contains(":\\") || name.starts_with("\\") { + if self.name.contains(":\\") || self.name.starts_with("\\") { return None; } - Some(name) + Some(&self.name) } #[cfg(not(windows))] { - // strip absolute prefix on entries pointing to root path - let mut entry_chars = name.chars(); - let mut name = name; - while name.starts_with('/') { - entry_chars.next(); - name = entry_chars.as_str() - } - Some(name) + Some(self.name.trim_start_matches('/')) } } } diff --git a/testdata/zip_slip.zip b/testdata/zip_slip.zip new file mode 100644 index 0000000..85a2e1b Binary files /dev/null and b/testdata/zip_slip.zip differ