Skip to content
Merged
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
59 changes: 14 additions & 45 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/openab-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ tempfile = "3.27.0"
aws-sdk-secretsmanager = { version = "1", optional = true }
aws-sdk-s3 = { version = "1", optional = true }
aws-config = { version = "1", optional = true }
zip = { version = "2", default-features = false, features = ["deflate"], optional = true }
zip = { version = "9.1.2", default-features = false, features = ["deflate-flate2"], optional = true }
flate2 = { version = "1", optional = true }
tar = { version = "0.4.45", default-features = false, optional = true }
aws-sigv4 = { version = "1", optional = true }
Expand Down
48 changes: 48 additions & 0 deletions crates/openab-core/src/pre_seed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,12 @@ fn extract_zip_budgeted(
}

let mut file = archive.by_index(i)?;
// zip 9.x `enclosed_name()` strips a leading root and accepts absolute
// entry names; zip 2.x rejected them. Keep rejecting so an archive with
// absolute paths fails closed, as before the 2.x -> 9.x bump.
if matches!(file.name_raw().first(), Some(b'/' | b'\\')) {
anyhow::bail!("hooks.pre_seed: invalid zip entry name at index {i}");
}
let name = file.enclosed_name().ok_or_else(|| {
anyhow::anyhow!("hooks.pre_seed: invalid zip entry name at index {i}")
})?;
Expand Down Expand Up @@ -663,6 +669,48 @@ mod tests {
);
}

#[test]
fn extract_rejects_path_traversal() {
use std::io::Write;
// Pins the path-safety contract across the zip 2.x -> 9.x bump: every
// escaping or absolute entry name is rejected and nothing is written.
fn zip_with(name: &str) -> Vec<u8> {
let mut writer = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
writer
.start_file(name, zip::write::SimpleFileOptions::default())
.unwrap();
writer.write_all(b"pwned").unwrap();
writer.finish().unwrap().into_inner()
}
let deadline = Instant::now() + std::time::Duration::from_secs(60);

for evil in [
"../escape.txt",
"sub/../../escape.txt",
"/abs_escape.txt",
"\\abs_escape.txt",
] {
let root = tempfile::tempdir().unwrap();
let dest = root.path().join("dest");
std::fs::create_dir_all(&dest).unwrap();

let err = extract_zip_with_limits(&zip_with(evil), &dest, deadline).unwrap_err();
assert!(
err.to_string().contains("invalid zip entry name"),
"{evil}: expected rejection, got {err}"
);
assert!(
!root.path().join("escape.txt").exists(),
"{evil}: escaped dest"
);
assert_eq!(
std::fs::read_dir(&dest).unwrap().count(),
0,
"{evil}: dest not empty"
);
}
}

#[test]
fn extract_rejects_exceeding_file_count() {
use std::io::Write;
Expand Down
Loading