Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions rc-zip-sync/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand All @@ -21,6 +22,8 @@ fn check_case<F: HasCursor>(test: &Case, archive: Result<ArchiveHandle<'_, F>, 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()[..])
}
Expand Down Expand Up @@ -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>(R);
Expand Down
41 changes: 40 additions & 1 deletion rc-zip-tokio/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<F: HasCursor>(test: &Case, archive: Result<ArchiveHandle<'_, F>, Error>) {
rc_zip_corpus::check_case(test, archive.as_ref().map(|ar| -> &Archive { ar }));
Expand Down Expand Up @@ -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>(R);
Expand Down
17 changes: 4 additions & 13 deletions rc-zip/src/parse/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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('/'))
}
}
}
Expand Down
Binary file added testdata/zip_slip.zip
Binary file not shown.
Loading