diff --git a/capsules/model-provider/src/journal.rs b/capsules/model-provider/src/journal.rs index d3c6878f2..9cb7b04ca 100644 --- a/capsules/model-provider/src/journal.rs +++ b/capsules/model-provider/src/journal.rs @@ -3941,13 +3941,17 @@ mod tests { } let root_writer = if hold_root { let held = open_journal_directory(&root).unwrap(); - held.try_lock().unwrap(); + // Take the lock with the provider's bounded retry: a child forked on + // another test thread can briefly hold a copy of an earlier lock fd (#381). + lock_journal_directory(&held).unwrap(); Some(held) } else { None }; let runs_writer = open_journal_directory(&root.join("runs")).unwrap(); - runs_writer.try_lock().unwrap(); + // Take the lock with the provider's bounded retry: a child forked on + // another test thread can briefly hold a copy of an earlier lock fd (#381). + lock_journal_directory(&runs_writer).unwrap(); assert_eq!( RunJournal::open(root.clone()).err().unwrap().code(), "selection_unavailable" @@ -4260,7 +4264,9 @@ mod tests { let root = temp_root("briefly-held-writer"); let journal = RunJournal::open(root.clone()).unwrap(); let holder = open_journal_directory(&root.join("runs")).unwrap(); - holder.try_lock().unwrap(); + // Take the lock with the provider's bounded retry: a child forked on + // another test thread can briefly hold a copy of an earlier lock fd (#381). + lock_journal_directory(&holder).unwrap(); let release = std::thread::spawn(move || { std::thread::sleep(std::time::Duration::from_millis(100)); drop(holder); @@ -4333,7 +4339,9 @@ mod tests { #[cfg(unix)] let _writer = { let writer = open_journal_directory(temp.parent().unwrap()).unwrap(); - writer.try_lock().unwrap(); + // Take the lock with the provider's bounded retry: a child forked on + // another test thread can briefly hold a copy of an earlier lock fd (#381). + lock_journal_directory(&writer).unwrap(); writer }; assert!(RunJournal::open(root).is_err()); diff --git a/capsules/object-provider/Cargo.lock b/capsules/object-provider/Cargo.lock index d5114091d..914d1dc39 100644 --- a/capsules/object-provider/Cargo.lock +++ b/capsules/object-provider/Cargo.lock @@ -167,9 +167,6 @@ name = "arbitrary" version = "1.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3d036a3c4ab069c7b410a2ce876bd74808d2d0888a82667669f8e783a898bf1" -dependencies = [ - "derive_arbitrary", -] [[package]] name = "arc-swap" @@ -1190,7 +1187,7 @@ dependencies = [ "elastos-protected-content-provider-contracts", "elastos-protected-content-rights", "getrandom 0.2.17", - "nix", + "nix 0.29.0", "serde", "serde_json", "sha2 0.10.9", @@ -1274,17 +1271,6 @@ version = "0.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7cd812cc2bc1d69d4764bd80df88b4317eaef9e773c75226407d9bc0876b211c" -[[package]] -name = "derive_arbitrary" -version = "1.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e567bd82dcff979e4b03460c307b3cdc9e96fde3d73bed1496d2bc75d9dd62a" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.119", -] - [[package]] name = "derive_more" version = "2.1.1" @@ -1589,7 +1575,7 @@ dependencies = [ "elastos-common", "elastos-compute", "libc", - "nix", + "nix 0.29.0", "serde", "serde_json", "thiserror 1.0.69", @@ -1689,7 +1675,7 @@ dependencies = [ "ghash", "hkdf", "hpke", - "nix", + "nix 0.29.0", "rand 0.10.2", "rand 0.9.5", "rand_chacha 0.3.1", @@ -1734,7 +1720,7 @@ dependencies = [ "elastos-protected-content-provider-contracts", "elastos-wallet-contract", "hex", - "nix", + "nix 0.29.0", "serde", "serde_json", "sha2 0.10.9", @@ -3855,6 +3841,18 @@ dependencies = [ "libc", ] +[[package]] +name = "nix" +version = "0.31.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf20d2fde8ff38632c426f1165ed7436270b44f199fc55284c38276f9db47c3d" +dependencies = [ + "bitflags", + "cfg-if", + "cfg_aliases", + "libc", +] + [[package]] name = "nom" version = "7.1.3" @@ -6053,6 +6051,12 @@ dependencies = [ "utf-8", ] +[[package]] +name = "typed-path" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e28f89b80c87b8fb0cf04ab448d5dd0dd0ade2f8891bae878de66a75a28600e" + [[package]] name = "typenum" version = "1.20.1" @@ -7446,18 +7450,16 @@ dependencies = [ [[package]] name = "zip" -version = "2.4.2" +version = "9.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fabe6324e908f85a1c52063ce7aa26b68dcb7eb6dbc83a2d148403c9bc3eba50" +checksum = "a954114b47a54df49f83d2722814e86b23d4bb1b54a1b90fb7d92b41619ddc22" dependencies = [ - "arbitrary", "crc32fast", - "crossbeam-utils", - "displaydoc", "flate2", "indexmap", "memchr", - "thiserror 2.0.20", + "nix 0.31.3", + "typed-path", ] [[package]] diff --git a/elastos/.cargo/audit.toml b/elastos/.cargo/audit.toml deleted file mode 100644 index 31b16f606..000000000 --- a/elastos/.cargo/audit.toml +++ /dev/null @@ -1,15 +0,0 @@ -# Reviewed cargo-audit exceptions. The CI job `cargo-audit` runs from elastos/ and reads this file -# for every tracked Cargo.lock. Each entry says why it does not apply and which issue removes it. -[advisories] -ignore = [ - # zip 2.4.2 (elastos-server): the zip crate's own ZipArchive::extract and - # ZipStreamReader::extract write entries, symlinks and Unix permission bits to disk. ElastOS - # never calls them. Library reads each entry with ZipArchive::by_index; its own extraction - # (extract_zip_archive, write_selected_archive_file in library.rs) refuses symlink entries and - # writes bytes without archive permission bits; ZipWriter builds archives in memory. - # Upgrade to zip >= 9.0.1 (upstream GHSA-4684-h2q9-2vcp is fixed in 9.0.1, not 9.0.0) and - # remove these three lines: #414. - "RUSTSEC-2026-0367", - "RUSTSEC-2026-0368", - "RUSTSEC-2026-0369", -] diff --git a/elastos/Cargo.lock b/elastos/Cargo.lock index 324256b60..eda3abe91 100644 --- a/elastos/Cargo.lock +++ b/elastos/Cargo.lock @@ -167,9 +167,6 @@ name = "arbitrary" version = "1.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3d036a3c4ab069c7b410a2ce876bd74808d2d0888a82667669f8e783a898bf1" -dependencies = [ - "derive_arbitrary", -] [[package]] name = "arc-swap" @@ -1197,7 +1194,7 @@ dependencies = [ "elastos-protected-content-provider-contracts", "elastos-protected-content-rights", "getrandom 0.2.17", - "nix", + "nix 0.29.0", "serde", "serde_json", "sha2 0.10.9", @@ -1281,17 +1278,6 @@ version = "0.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7cd812cc2bc1d69d4764bd80df88b4317eaef9e773c75226407d9bc0876b211c" -[[package]] -name = "derive_arbitrary" -version = "1.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e567bd82dcff979e4b03460c307b3cdc9e96fde3d73bed1496d2bc75d9dd62a" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.119", -] - [[package]] name = "derive_more" version = "2.1.1" @@ -1598,7 +1584,7 @@ dependencies = [ "elastos-common", "elastos-compute", "libc", - "nix", + "nix 0.29.0", "serde", "serde_json", "tempfile", @@ -1706,7 +1692,7 @@ dependencies = [ "hkdf", "hpke", "k256", - "nix", + "nix 0.29.0", "rand 0.10.2", "rand 0.9.5", "rand_chacha 0.3.1", @@ -1765,7 +1751,7 @@ dependencies = [ "elastos-wallet-contract", "hex", "k256", - "nix", + "nix 0.29.0", "serde", "serde_json", "sha2 0.10.9", @@ -3926,6 +3912,18 @@ dependencies = [ "libc", ] +[[package]] +name = "nix" +version = "0.31.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf20d2fde8ff38632c426f1165ed7436270b44f199fc55284c38276f9db47c3d" +dependencies = [ + "bitflags", + "cfg-if", + "cfg_aliases", + "libc", +] + [[package]] name = "nom" version = "7.1.3" @@ -6165,6 +6163,12 @@ dependencies = [ "utf-8", ] +[[package]] +name = "typed-path" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e28f89b80c87b8fb0cf04ab448d5dd0dd0ade2f8891bae878de66a75a28600e" + [[package]] name = "typenum" version = "1.20.1" @@ -7704,18 +7708,16 @@ dependencies = [ [[package]] name = "zip" -version = "2.4.2" +version = "9.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fabe6324e908f85a1c52063ce7aa26b68dcb7eb6dbc83a2d148403c9bc3eba50" +checksum = "a954114b47a54df49f83d2722814e86b23d4bb1b54a1b90fb7d92b41619ddc22" dependencies = [ - "arbitrary", "crc32fast", - "crossbeam-utils", - "displaydoc", "flate2", "indexmap", "memchr", - "thiserror 2.0.20", + "nix 0.31.3", + "typed-path", ] [[package]] diff --git a/elastos/crates/elastos-server/Cargo.toml b/elastos/crates/elastos-server/Cargo.toml index 56bf3b4ac..18fb4371a 100644 --- a/elastos/crates/elastos-server/Cargo.toml +++ b/elastos/crates/elastos-server/Cargo.toml @@ -104,7 +104,7 @@ tempfile = "3.10" # Archive extraction (for capsule artifacts) flate2 = "1.0" tar = "0.4" -zip = { version = "2.4.2", default-features = false, features = ["deflate-flate2", "flate2"] } +zip = { version = "9.0.4", default-features = false, features = ["deflate-flate2"] } libc = "0.2" # Built-in Carrier transport (P2P for updates, gossip messaging, DHT discovery). diff --git a/elastos/crates/elastos-server/src/api/gateway_tests/library.rs b/elastos/crates/elastos-server/src/api/gateway_tests/library.rs index d8477c0db..78a2f46bf 100644 --- a/elastos/crates/elastos-server/src/api/gateway_tests/library.rs +++ b/elastos/crates/elastos-server/src/api/gateway_tests/library.rs @@ -892,7 +892,7 @@ fn zip_text_files(bytes: &[u8]) -> std::collections::BTreeMap { if entry.is_dir() { continue; } - let name = entry.name().to_string(); + let name = entry.name().unwrap().into_owned(); let mut body = String::new(); entry.read_to_string(&mut body).unwrap(); files.insert(name, body); @@ -3870,6 +3870,438 @@ async fn test_library_provider_rejects_unsafe_zip_entries() { .contains("relative and safe")); } +/// Every path under `root` (files, folders and symlinks, not followed). +fn walk_paths(root: &std::path::Path) -> Vec { + let mut out = Vec::new(); + let mut stack = vec![root.to_path_buf()]; + while let Some(dir) = stack.pop() { + for entry in std::fs::read_dir(&dir).unwrap() { + let entry = entry.unwrap(); + let path = entry.path(); + if entry.file_type().unwrap().is_dir() { + stack.push(path.clone()); + } + out.push(path); + } + } + out +} + +#[tokio::test] +async fn test_library_provider_extract_zip_refuses_absolute_dotdot_and_symlink_entries() { + use std::io::Write as _; + + let dir = tempfile::tempdir().unwrap(); + let app = gateway_router(library_test_state(dir.path()).await); + let authority = passkey_authority_with_name(dir.path(), Some("admin")); + let token = app_token_for_authority(dir.path(), LIBRARY_CAPSULE_ID, &authority); + let root = crate::auth::principal_localhost_root(&authority.principal_id); + let documents_uri = format!("{root}/Documents"); + let imports_uri = format!("{documents_uri}/Imports"); + + let (status, _) = post_library( + app.clone(), + &token, + "mkdir", + json!({ "parent_uri": root, "name": "Documents" }), + ) + .await; + assert_eq!(status, StatusCode::OK); + let (status, _) = post_library( + app.clone(), + &token, + "mkdir", + json!({ "parent_uri": documents_uri, "name": "Imports" }), + ) + .await; + assert_eq!(status, StatusCode::OK); + + let options = + zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored); + // Each archive holds one hostile entry. The whole-archive extract must + // refuse it and write nothing for it, inside or outside the Library. + // (archive name, entry kind, entry name, symlink target, expected message) + let cases: [(&str, &str, &str, &str, &str); 9] = [ + ( + "Absolute.zip", + "file", + "/absolute-escape.txt", + "", + "relative and safe", + ), + ( + "DotDot.zip", + "file", + "../dotdot-escape.txt", + "", + "relative and safe", + ), + ( + "Inner.zip", + "file", + "Nested/../inner-escape.txt", + "", + "relative and safe", + ), + ( + "Backslash.zip", + "file", + "..\\backslash-escape.txt", + "", + "relative and safe", + ), + // Windows path forms. Linux and macOS read each as an ordinary name, + // so the extract must refuse them by its own rule. + ( + "DriveBackslash.zip", + "file", + "C:\\drive-backslash-escape.txt", + "", + "relative and safe", + ), + ( + "DriveSlash.zip", + "file", + "C:/drive-slash-escape.txt", + "", + "relative and safe", + ), + ( + "Unc.zip", + "file", + "\\\\server\\share\\unc-escape.txt", + "", + "relative and safe", + ), + ( + "Verbatim.zip", + "file", + "\\\\?\\C:\\verbatim-escape.txt", + "", + "relative and safe", + ), + ( + "Symlink.zip", + "symlink", + "symlink-escape.txt", + "/etc/passwd", + "non-file", + ), + ]; + for (archive_name, kind, entry_name, link_target, expected) in cases { + let archive_bytes = { + let mut writer = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + if kind == "symlink" { + writer + .add_symlink(entry_name, link_target, options) + .unwrap(); + } else { + writer.start_file(entry_name, options).unwrap(); + writer.write_all(b"escape").unwrap(); + } + writer.finish().unwrap().into_inner() + }; + let zip_uri = format!("{documents_uri}/{archive_name}"); + let (write_status, write) = post_library( + app.clone(), + &token, + "write", + json!({ + "uri": zip_uri, + "mime": "application/zip", + "data": base64::engine::general_purpose::STANDARD.encode(archive_bytes), + }), + ) + .await; + assert_eq!(write_status, StatusCode::OK, "{archive_name}"); + assert_eq!(write["status"], "ok", "{archive_name}: {write}"); + + let (extract_status, extracted) = post_library( + app.clone(), + &token, + "extract_archive", + json!({ "uri": zip_uri }), + ) + .await; + assert_eq!(extract_status, StatusCode::OK, "{archive_name}"); + assert_eq!( + extracted["status"], "error", + "{archive_name}: hostile entry {entry_name:?} was not refused: {extracted}" + ); + assert!( + extracted["message"].as_str().unwrap().contains(expected), + "{archive_name}: {extracted}" + ); + + if kind == "symlink" { + // The listing marks it blocked and a selected extract refuses it. + let (list_status, listed) = post_library( + app.clone(), + &token, + "archive_entries", + json!({ "uri": zip_uri }), + ) + .await; + assert_eq!(list_status, StatusCode::OK); + let rows = listed["data"]["entries"].as_array().unwrap(); + assert!( + rows.iter().any(|entry| entry["path"] == entry_name + && entry["kind"] == "blocked" + && entry["safety"]["reason"] + .as_str() + .unwrap() + .contains("non-file")), + "{listed}" + ); + let (select_status, selected) = post_library( + app.clone(), + &token, + "archive_extract_entries", + json!({ + "uri": zip_uri, + "destination_uri": imports_uri, + "entries": [entry_name], + }), + ) + .await; + assert_eq!(select_status, StatusCode::OK); + assert_eq!(selected["status"], "ok", "{selected}"); + assert_eq!( + selected["data"]["receipt"]["progress"]["blocked_entries"], + 1 + ); + assert!(selected["data"]["blocked"][0]["reason"] + .as_str() + .unwrap() + .contains("non-file")); + } + } + + // Nothing from any hostile entry exists anywhere under the data dir, and + // no symlink was created. + for path in walk_paths(dir.path()) { + let name = path.file_name().unwrap().to_string_lossy().to_string(); + assert!( + !name.contains("escape"), + "hostile entry was written: {}", + path.display() + ); + assert!( + !std::fs::symlink_metadata(&path) + .unwrap() + .file_type() + .is_symlink(), + "symlink was created: {}", + path.display() + ); + } +} + +/// A tar archive whose entry names are kept byte for byte. `tar::Builder` +/// refuses `..` and absolute names, so the header name is written directly. +fn tar_with_raw_entry_names(entries: &[(&str, &[u8])]) -> Vec { + let mut builder = tar::Builder::new(Vec::new()); + for (name, body) in entries { + let mut header = tar::Header::new_gnu(); + header.as_gnu_mut().unwrap().name[..name.len()].copy_from_slice(name.as_bytes()); + header.set_entry_type(tar::EntryType::Regular); + header.set_size(body.len() as u64); + header.set_mode(0o644); + header.set_cksum(); + builder.append(&header, *body).unwrap(); + } + builder.into_inner().unwrap() +} + +#[tokio::test] +async fn test_library_provider_every_archive_reader_refuses_root_drive_unc_and_climb_names() { + use std::io::Write as _; + + let dir = tempfile::tempdir().unwrap(); + let app = gateway_router(library_test_state(dir.path()).await); + let authority = passkey_authority_with_name(dir.path(), Some("admin")); + let token = app_token_for_authority(dir.path(), LIBRARY_CAPSULE_ID, &authority); + let root = crate::auth::principal_localhost_root(&authority.principal_id); + let documents_uri = format!("{root}/Documents"); + let imports_uri = format!("{documents_uri}/Imports"); + for (parent_uri, name) in [ + (root.as_str(), "Documents"), + (documents_uri.as_str(), "Imports"), + ] { + let (status, _) = post_library( + app.clone(), + &token, + "mkdir", + json!({ "parent_uri": parent_uri, "name": name }), + ) + .await; + assert_eq!(status, StatusCode::OK); + } + + // One rule for every reader: list, preview, selected extract and + // whole-archive extract, for ZIP and for tar. Each archive holds one + // hostile entry and one safe entry. + let hostile = [ + "C:/drive-slash-escape.txt", + "C:\\drive-backslash-escape.txt", + "\\\\server\\share\\unc-escape.txt", + "/etc/absolute-escape.txt", + "../dotdot-escape.txt", + ]; + let unsafe_reason = "library archive entry path must be relative and safe"; + let mut failures = Vec::new(); + for (case, entry_name) in hostile.into_iter().enumerate() { + let leaf = entry_name.rsplit(['/', '\\']).next().unwrap(); + for format in ["zip", "tar"] { + let label = format!("{format} {entry_name:?}"); + let (bytes, mime) = if format == "zip" { + let options = zip::write::SimpleFileOptions::default() + .compression_method(zip::CompressionMethod::Stored); + let mut writer = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + writer.start_file(entry_name, options).unwrap(); + writer.write_all(b"escape").unwrap(); + writer.start_file("safe.txt", options).unwrap(); + writer.write_all(b"safe").unwrap(); + (writer.finish().unwrap().into_inner(), "application/zip") + } else { + ( + tar_with_raw_entry_names(&[(entry_name, b"escape"), ("safe.txt", b"safe")]), + "application/x-tar", + ) + }; + let archive_uri = format!("{documents_uri}/Case{case}.{format}"); + let (status, write) = post_library( + app.clone(), + &token, + "write", + json!({ + "uri": archive_uri, + "mime": mime, + "data": base64::engine::general_purpose::STANDARD.encode(bytes), + }), + ) + .await; + assert_eq!(status, StatusCode::OK, "{label}"); + assert_eq!(write["status"], "ok", "{label}: {write}"); + + // List: the archive is still shown; the hostile entry is a + // blocked row with the rule's reason, never a safe row. + let (_, listed) = post_library( + app.clone(), + &token, + "archive_entries", + json!({ "uri": archive_uri }), + ) + .await; + let rows = listed["data"]["entries"] + .as_array() + .cloned() + .unwrap_or_default(); + let hostile_rows: Vec<_> = rows + .iter() + .filter(|row| row["path"] != "safe.txt") + .collect(); + let blocked_by_rule = hostile_rows.len() == 1 + && hostile_rows[0]["kind"] == "blocked" + && hostile_rows[0]["safety"]["status"] == "blocked" + && hostile_rows[0]["safety"]["reason"] == unsafe_reason; + if !blocked_by_rule { + failures.push(format!("{label} list: {hostile_rows:?}")); + } + + // Preview: the name is refused as a request, and its last part + // matches nothing; neither gives the bytes. + for (requested, message) in [ + (entry_name, unsafe_reason), + (leaf, "library archive preview entry not found"), + ] { + let (_, preview) = post_library( + app.clone(), + &token, + "archive_preview_entry", + json!({ "uri": archive_uri, "entry": requested }), + ) + .await; + if preview["status"] != "error" || preview["message"] != message { + failures.push(format!( + "{label} preview {requested:?}: status={} message={} text={}", + preview["status"], preview["message"], preview["data"]["preview"]["text"] + )); + } + } + + // Selected extract: the name is refused as a selection, and the + // last part matches nothing; the entry is reported as blocked. + let (_, selected) = post_library( + app.clone(), + &token, + "archive_extract_entries", + json!({ + "uri": archive_uri, + "destination_uri": imports_uri, + "entries": [entry_name], + }), + ) + .await; + if selected["status"] != "error" || selected["message"] != unsafe_reason { + failures.push(format!( + "{label} selected {entry_name:?}: status={} message={} written={}", + selected["status"], selected["message"], selected["data"]["written"] + )); + } + let (_, selected) = post_library( + app.clone(), + &token, + "archive_extract_entries", + json!({ + "uri": archive_uri, + "destination_uri": imports_uri, + "entries": [leaf], + }), + ) + .await; + let blocked = &selected["data"]["blocked"]; + if selected["data"]["receipt"]["progress"]["written_entries"] != 0 + || blocked.as_array().map(Vec::len) != Some(1) + || blocked[0]["reason"] != unsafe_reason + { + failures.push(format!( + "{label} selected {leaf:?}: written={} blocked={blocked}", + selected["data"]["written"] + )); + } + + // Whole-archive extract: refused with the rule's message. + let (_, extracted) = post_library( + app.clone(), + &token, + "extract_archive", + json!({ "uri": archive_uri }), + ) + .await; + if extracted["status"] != "error" || extracted["message"] != unsafe_reason { + failures.push(format!( + "{label} extract: status={} message={}", + extracted["status"], extracted["message"] + )); + } + } + } + + for path in walk_paths(dir.path()) { + let name = path.file_name().unwrap().to_string_lossy().to_string(); + if name.contains("escape") { + failures.push(format!("hostile entry was written: {}", path.display())); + } + } + assert!( + failures.is_empty(), + "{} reader checks failed:\n{}", + failures.len(), + failures.join("\n") + ); +} + #[tokio::test] async fn test_library_gateway_reads_webspace_files_through_runtime_provider() { let dir = tempfile::tempdir().unwrap(); diff --git a/elastos/crates/elastos-server/src/library.rs b/elastos/crates/elastos-server/src/library.rs index 9e537d229..62cf92157 100644 --- a/elastos/crates/elastos-server/src/library.rs +++ b/elastos/crates/elastos-server/src/library.rs @@ -4173,7 +4173,7 @@ fn archive_preview_entry_for_object( if !is_extractable_archive_name(archive_name) { bail!("library archive preview only supports .tar, .tar.gz, .tgz, and .zip archives"); } - let normalized = normalized_archive_entry_path(Path::new(entry))?; + let normalized = normalized_archive_entry_path(entry)?; let preview = if archive_name.to_ascii_lowercase().ends_with(".zip") { preview_zip_archive_entry(bytes, &normalized)? } else { @@ -4239,8 +4239,11 @@ fn preview_zip_archive_entry( let mut file = archive .by_index(index) .map_err(|err| anyhow!("invalid ZIP archive entry: {err}"))?; - let raw_name = file.name().to_string(); - let Ok(normalized) = normalized_archive_entry_path(Path::new(&raw_name)) else { + let raw_name = file + .name() + .map_err(|err| anyhow!("invalid ZIP archive entry: {err}"))? + .into_owned(); + let Ok(normalized) = normalized_archive_entry_path(&raw_name) else { continue; }; if normalized != selected { @@ -4281,11 +4284,9 @@ fn preview_tar_archive_entry( let mut archive = tar::Archive::new(reader); for entry in archive.entries()? { let mut entry = entry?; - let path = match entry.path() { - Ok(path) => path.to_path_buf(), - Err(_) => continue, - }; - let Ok(normalized) = normalized_archive_entry_path(&path) else { + let Ok(normalized) = + tar_entry_name(&entry).and_then(|name| normalized_archive_entry_path(&name)) + else { continue; }; if normalized != selected { @@ -4392,35 +4393,23 @@ fn list_tar_archive_entries(name: &str, bytes: Vec) -> anyhow::Result<(Vec path.to_path_buf(), - Err(err) => { - entries.push(blocked_archive_entry_listing( - index, - format!("entry-{index}"), - None, - None, - modified_at, - format!("invalid archive entry path: {err}"), - )); - continue; - } - }; - let display_path = archive_entry_display_path(&path, index); - let normalized = match normalized_archive_entry_path(&path) { - Ok(path) => path, - Err(err) => { - entries.push(blocked_archive_entry_listing( - index, - display_path, - size, - None, - modified_at, - err.to_string(), - )); - continue; - } - }; + let display_path = + archive_entry_display_name(&String::from_utf8_lossy(&entry.path_bytes()), index); + let normalized = + match tar_entry_name(&entry).and_then(|name| normalized_archive_entry_path(&name)) { + Ok(path) => path, + Err(err) => { + entries.push(blocked_archive_entry_listing( + index, + display_path, + size, + None, + modified_at, + err.to_string(), + )); + continue; + } + }; let kind = if entry_type.is_dir() { "directory" } else if entry_type.is_file() { @@ -4457,9 +4446,11 @@ fn list_zip_archive_entries(bytes: Vec) -> anyhow::Result<(Vec, bool) let file = archive .by_index(index) .map_err(|err| anyhow!("invalid ZIP archive entry: {err}"))?; - let raw_name = file.name().to_string(); - let path = Path::new(&raw_name); - let normalized = match normalized_archive_entry_path(path) { + let raw_name = file + .name() + .map_err(|err| anyhow!("invalid ZIP archive entry: {err}"))? + .into_owned(); + let normalized = match normalized_archive_entry_path(&raw_name) { Ok(path) => path, Err(err) => { entries.push(blocked_archive_entry_listing( @@ -4500,42 +4491,56 @@ fn list_zip_archive_entries(bytes: Vec) -> anyhow::Result<(Vec, bool) Ok((entries, truncated)) } -fn normalized_archive_entry_path(path: &Path) -> anyhow::Result { +const UNSAFE_ARCHIVE_ENTRY_PATH: &str = "library archive entry path must be relative and safe"; + +/// The one rule for an archive entry name. Every Library archive reader +/// passes each entry name through it: list, preview, selected extract and +/// whole-archive extract, for ZIP and for tar. The entry names a person asks +/// to preview or to extract go through it too. The rule is the same on every +/// platform. It refuses a name that holds a NUL byte or a `\`, starts with +/// `/` (this includes `//server/share`), starts with a drive letter (`C:/x`, +/// `C:x`), or has a `..` part. `\` is refused everywhere, so `C:\x`, +/// `\\server\share\x` and `..\x` are refused as well. It returns the +/// path with `/` separators and no empty or `.` parts; an empty path is the +/// archive root (for example `./`). +/// +/// It is not `Path::components()`: on Linux and macOS that reads `C:/x` as a +/// folder `C:` and `\` as an ordinary character. It is not zip 9 +/// `enclosed_name` either: that strips a root or drive prefix and folds `..`, +/// so a hostile entry would be written under another name. +fn archive_entry_path(name: &str) -> anyhow::Result { + let bytes = name.as_bytes(); + let drive = bytes.len() >= 2 && bytes[0].is_ascii_alphabetic() && bytes[1] == b':'; + if name.starts_with('/') || drive || name.contains(['\\', '\0']) { + bail!(UNSAFE_ARCHIVE_ENTRY_PATH); + } let mut parts = Vec::new(); - for component in path.components() { - match component { - Component::CurDir => {} - Component::Normal(name) => { - let name = name - .to_str() - .ok_or_else(|| anyhow!("library archive entry path must be UTF-8"))?; - if name.is_empty() - || name.contains('/') - || name.contains('\\') - || name.contains('\0') - || name == "." - || name == ".." - { - bail!("library archive entry path must be relative and safe"); - } - parts.push(name); - } - _ => bail!("library archive entry path must be relative and safe"), + for part in name.split('/') { + match part { + "" | "." => {} + ".." => bail!(UNSAFE_ARCHIVE_ENTRY_PATH), + part => parts.push(part), } } - if parts.is_empty() { - bail!("library archive entry path must not be empty"); - } Ok(parts.join("/")) } -fn archive_entry_display_path(path: &Path, index: usize) -> String { - let display = path.to_string_lossy().trim().to_string(); - if display.is_empty() { - format!("entry-{index}") - } else { - display +/// `archive_entry_path` for a reader that names one entry (list, preview, +/// selected extract): the archive root is not an entry. +fn normalized_archive_entry_path(name: &str) -> anyhow::Result { + let path = archive_entry_path(name)?; + if path.is_empty() { + bail!("library archive entry path must not be empty"); } + Ok(path) +} + +/// A tar entry name as text (GNU long names and PAX `path` included), before +/// `archive_entry_path`. The raw bytes are read, so the result is the same +/// on every platform. +fn tar_entry_name(entry: &tar::Entry<'_, R>) -> anyhow::Result { + String::from_utf8(entry.path_bytes().into_owned()) + .map_err(|_| anyhow!("library archive entry path must be UTF-8")) } fn archive_entry_display_name(name: &str, index: usize) -> String { @@ -4847,7 +4852,7 @@ fn selected_archive_entries(entries: &[String]) -> anyhow::Result path.to_path_buf(), - Err(err) => { - outcome.blocked.push(json!({ - "path": "invalid-entry-path", - "reason": format!("invalid archive entry path: {err}"), - })); - continue; - } - }; - let normalized = match normalized_archive_entry_path(&path) { - Ok(path) => path, - Err(err) => { - outcome.blocked.push(json!({ - "path": archive_entry_display_path(&path, outcome.processed_entries), - "reason": err.to_string(), - })); - continue; - } - }; + let normalized = + match tar_entry_name(&entry).and_then(|name| normalized_archive_entry_path(&name)) { + Ok(path) => path, + Err(err) => { + outcome.blocked.push(json!({ + "path": archive_entry_display_name( + &String::from_utf8_lossy(&entry.path_bytes()), + outcome.processed_entries, + ), + "reason": err.to_string(), + })); + continue; + } + }; if !outcome.is_selected(&normalized) { continue; } @@ -4939,8 +4938,11 @@ fn collect_selected_zip_writes( let mut file = archive .by_index(index) .map_err(|err| anyhow!("invalid ZIP archive entry: {err}"))?; - let raw_name = file.name().to_string(); - let normalized = match normalized_archive_entry_path(Path::new(&raw_name)) { + let raw_name = file + .name() + .map_err(|err| anyhow!("invalid ZIP archive entry: {err}"))? + .into_owned(); + let normalized = match normalized_archive_entry_path(&raw_name) { Ok(path) => path, Err(err) => { outcome.blocked.push(json!({ @@ -5386,8 +5388,8 @@ fn extract_tar_archive( let mut archive = tar::Archive::new(reader); for entry in archive.entries()? { let mut entry = entry?; - let entry_path = entry.path()?.to_path_buf(); - let entry_uri = archive_entry_uri(&destination.uri, &entry_path)?; + let entry_path = archive_entry_path(&tar_entry_name(&entry)?)?; + let entry_uri = archive_entry_uri(&destination.uri, Path::new(&entry_path))?; if entry_uri == destination.uri { continue; } @@ -5429,10 +5431,11 @@ fn extract_zip_archive( let mut file = archive .by_index(index) .map_err(|err| anyhow!("invalid ZIP archive entry: {err}"))?; - let entry_path = file - .enclosed_name() - .ok_or_else(|| anyhow!("library archive entry path must be relative and safe"))?; - let entry_uri = archive_entry_uri(&destination.uri, &entry_path)?; + let raw_name = file + .name() + .map_err(|err| anyhow!("invalid ZIP archive entry: {err}"))?; + let entry_path = archive_entry_path(&raw_name)?; + let entry_uri = archive_entry_uri(&destination.uri, Path::new(&entry_path))?; if entry_uri == destination.uri { continue; } @@ -7178,6 +7181,149 @@ mod tests { use super::*; + #[test] + fn archive_entry_path_refuses_root_drive_and_climb_on_every_platform() { + let refused = [ + "/a.txt", + "\\a.txt", + "C:\\a.txt", + "C:/a.txt", + "C:a.txt", + "c:", + "\\\\server\\share\\a.txt", + "//server/share/a.txt", + "\\\\?\\C:\\a.txt", + "\\\\.\\C:\\a.txt", + "../a.txt", + "..\\a.txt", + "Nested/../../a.txt", + "Nested/../a.txt", + "Nested\\..\\..\\a.txt", + "a\0.txt", + ]; + let accepted: Vec<&str> = refused + .into_iter() + .filter(|name| archive_entry_path(name).is_ok()) + .collect(); + assert!(accepted.is_empty(), "accepted hostile names: {accepted:?}"); + + for (name, path) in [ + ("a.txt", "a.txt"), + ("./a.txt", "a.txt"), + ("Nested/a.txt", "Nested/a.txt"), + ("Nested/", "Nested"), + ("Nested/C:a.txt", "Nested/C:a.txt"), + ("./", ""), + ] { + assert_eq!(archive_entry_path(name).unwrap(), path, "{name:?}"); + } + } + + /// The list, preview and selected-extract readers, called directly (past + /// the check on the entry a person asks for): an entry whose name is + /// rooted, has a drive or UNC prefix, or climbs is shown as blocked and is + /// never matched, previewed or written, in ZIP and in tar. + #[test] + fn archive_readers_block_root_drive_unc_and_climb_entry_names() { + use std::io::Write as _; + + let reason = "library archive entry path must be relative and safe"; + let mut failures = Vec::new(); + for entry_name in [ + "C:/drive-slash-escape.txt", + "C:\\drive-backslash-escape.txt", + "\\\\server\\share\\unc-escape.txt", + "/etc/absolute-escape.txt", + "../dotdot-escape.txt", + ] { + let leaf = entry_name.rsplit(['/', '\\']).next().unwrap(); + for archive_name in ["Case.zip", "Case.tar"] { + let label = format!("{archive_name} {entry_name:?}"); + let bytes = if archive_name.ends_with(".zip") { + let options = zip::write::SimpleFileOptions::default() + .compression_method(zip::CompressionMethod::Stored); + let mut writer = ZipWriter::new(Cursor::new(Vec::new())); + writer.start_file(entry_name, options).unwrap(); + writer.write_all(b"escape").unwrap(); + writer.start_file("safe.txt", options).unwrap(); + writer.write_all(b"safe").unwrap(); + writer.finish().unwrap().into_inner() + } else { + // `tar::Builder` refuses these names, so write the header + // name byte for byte. + let mut builder = tar::Builder::new(Vec::new()); + for (name, body) in [(entry_name, b"escape".as_slice()), ("safe.txt", b"safe")] + { + let mut header = tar::Header::new_gnu(); + header.as_gnu_mut().unwrap().name[..name.len()] + .copy_from_slice(name.as_bytes()); + header.set_entry_type(tar::EntryType::Regular); + header.set_size(body.len() as u64); + header.set_mode(0o644); + header.set_cksum(); + builder.append(&header, body).unwrap(); + } + builder.into_inner().unwrap() + }; + let zip = archive_name.ends_with(".zip"); + + let (rows, _) = if zip { + list_zip_archive_entries(bytes.clone()) + } else { + list_tar_archive_entries(archive_name, bytes.clone()) + } + .unwrap(); + let hostile_rows: Vec<&Value> = rows + .iter() + .filter(|row| row["path"] != "safe.txt") + .collect(); + if hostile_rows.len() != 1 + || hostile_rows[0]["kind"] != "blocked" + || hostile_rows[0]["safety"]["reason"] != reason + { + failures.push(format!("{label} list: {hostile_rows:?}")); + } + + for selected in [entry_name, leaf] { + let preview = if zip { + preview_zip_archive_entry(bytes.clone(), selected) + } else { + preview_tar_archive_entry(archive_name, bytes.clone(), selected) + }; + if let Ok(preview) = preview { + failures.push(format!( + "{label} preview {selected:?}: {:?}", + String::from_utf8_lossy(&preview.bytes) + )); + } + } + + let requested = BTreeSet::from([entry_name.to_string(), leaf.to_string()]); + let mut outcome = ArchiveExtractOutcome::new(requested); + let writes = + collect_selected_archive_writes(archive_name, bytes, &mut outcome).unwrap(); + if !writes.is_empty() + || outcome.blocked.len() != 1 + || outcome.blocked[0]["reason"] != reason + { + failures.push(format!( + "{label} selected: writes={writes:?} blocked={:?}", + outcome.blocked + )); + } + } + if selected_archive_entries(&[entry_name.to_string()]).is_ok() { + failures.push(format!("selection {entry_name:?} accepted")); + } + } + assert!( + failures.is_empty(), + "{} reader checks failed:\n{}", + failures.len(), + failures.join("\n") + ); + } + #[test] fn object_provider_exposes_object_scheme_only() { let registry = Arc::new(ProviderRegistry::new());