From 756dad1c55210bb6f5732276b83d865b167f0818 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 17 Oct 2026 07:22:39 +0000 Subject: [PATCH 1/5] deps: move zip to 9.x for RUSTSEC-2026-0367/0368/0369 cargo-audit fails on every PR: zip 2.4.2 in elastos/Cargo.lock and in capsules/object-provider/Cargo.lock (through its elastos-server path dependency) has three advisories against ZipArchive::extract and the stream extract API. The runtime calls neither; it reads each entry itself. zip 9.0.4 is the patched release. - elastos-server uses zip 9.0.4, still without default features. 9.x has no separate flate2 feature; deflate-flate2 now enables the dependency. - Both lockfiles change only for zip: nix 0.31.3 and typed-path come in, derive_arbitrary goes. `cargo update -p zip` also moved unrelated range dependencies (windows-sys, getrandom); those lines stay as they were, and `cargo metadata --locked` accepts both files. - ZipFile::name now returns a Result. The ZIP readers report an error as "invalid ZIP archive entry". - Whole-archive extract keeps its rule for entry names. zip 9 enclosed_name turns "/a.txt" into "a.txt" and folds "x/../a.txt" into "a.txt", so an entry that extract refuses today would be written. The runtime now checks the name with the rule zip 2.x applied: no NUL byte, no root or drive prefix, no climb above the archive root. test_library_provider_extract_zip_refuses_absolute_dotdot_and_symlink_entries covers absolute, "..", inner "..", backslash and symlink entries. It passes on zip 2.4.2 and with this change, and fails on the Absolute.zip case when extract uses zip 9 enclosed_name. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01W6Qn9RX7LDqrwiAfNC2F2K --- capsules/object-provider/Cargo.lock | 50 ++--- elastos/Cargo.lock | 50 ++--- elastos/crates/elastos-server/Cargo.toml | 2 +- .../src/api/gateway_tests/library.rs | 199 +++++++++++++++++- elastos/crates/elastos-server/src/library.rs | 44 +++- 5 files changed, 290 insertions(+), 55 deletions(-) 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.lock b/elastos/Cargo.lock index ac82b721d..dc06da84e 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", @@ -3925,6 +3911,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" @@ -6164,6 +6162,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" @@ -7703,18 +7707,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 84788f1d5..e54e95885 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..0253c229f 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,203 @@ 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); 5] = [ + ( + "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", + "", + "invalid library object name", + ), + ( + "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() + ); + } +} + #[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..84c791d0a 100644 --- a/elastos/crates/elastos-server/src/library.rs +++ b/elastos/crates/elastos-server/src/library.rs @@ -4239,7 +4239,10 @@ 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 raw_name = file + .name() + .map_err(|err| anyhow!("invalid ZIP archive entry: {err}"))? + .into_owned(); let Ok(normalized) = normalized_archive_entry_path(Path::new(&raw_name)) else { continue; }; @@ -4457,7 +4460,10 @@ 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 raw_name = file + .name() + .map_err(|err| anyhow!("invalid ZIP archive entry: {err}"))? + .into_owned(); let path = Path::new(&raw_name); let normalized = match normalized_archive_entry_path(path) { Ok(path) => path, @@ -4939,7 +4945,10 @@ 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 raw_name = file + .name() + .map_err(|err| anyhow!("invalid ZIP archive entry: {err}"))? + .into_owned(); let normalized = match normalized_archive_entry_path(Path::new(&raw_name)) { Ok(path) => path, Err(err) => { @@ -5429,8 +5438,10 @@ 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() + let raw_name = file + .name() + .map_err(|err| anyhow!("invalid ZIP archive entry: {err}"))?; + let entry_path = zip_entry_enclosed_path(&raw_name) .ok_or_else(|| anyhow!("library archive entry path must be relative and safe"))?; let entry_uri = archive_entry_uri(&destination.uri, &entry_path)?; if entry_uri == destination.uri { @@ -5461,6 +5472,29 @@ fn extract_zip_archive( Ok(()) } +/// The path a whole-archive ZIP extract writes for an entry name, or `None` +/// when the name holds a NUL byte, is absolute (a root or a drive prefix), or +/// climbs above the archive root. `archive_entry_uri` then refuses any `..` +/// that is left. This keeps the rule in the runtime: zip 9 `enclosed_name` +/// strips a leading `/` or drive prefix and folds `..` instead, which would +/// write entries that this extract refuses. +fn zip_entry_enclosed_path(name: &str) -> Option { + if name.contains('\0') { + return None; + } + let path = PathBuf::from(name); + let mut depth = 0usize; + for component in path.components() { + match component { + Component::Prefix(_) | Component::RootDir => return None, + Component::ParentDir => depth = depth.checked_sub(1)?, + Component::Normal(_) => depth += 1, + Component::CurDir => {} + } + } + Some(path) +} + fn archive_entry_uri(destination_uri: &str, path: &Path) -> anyhow::Result { let mut uri = destination_uri.trim_end_matches('/').to_string(); for component in path.components() { From aaa1c69ceceda47deca27cbb311101b6eea78a10 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 17 Oct 2026 10:07:12 +0000 Subject: [PATCH 2/5] model-provider: every held-lock journal test takes its lock with the bounded retry 50515055 fixed one test. Four other test sites still took the journal directory lock with one try_lock(). A child forked on another test thread can briefly hold a copy of an earlier lock fd (#381), so they failed with "WouldBlock" in CI (live_transaction_creator_preserves_partial_files_and_bindings, journal.rs:4336, run 39018319086). They now use lock_journal_directory, the provider's own bounded retry. Test code only. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01W6Qn9RX7LDqrwiAfNC2F2K --- capsules/model-provider/src/journal.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/capsules/model-provider/src/journal.rs b/capsules/model-provider/src/journal.rs index b711b092d..8433b1de8 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" @@ -4257,7 +4261,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); @@ -4330,7 +4336,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()); From bb8afd7d1f05f60cc1dc5f9ce2a48b81f89d8191 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 17 Oct 2026 10:09:47 +0000 Subject: [PATCH 3/5] cargo-audit: remove the zip exception now that zip is 9.0.4 (#414) zip 9.0.4 is past the first fixed release for RUSTSEC-2026-0367/0368/0369 (upstream GHSA-4684-h2q9-2vcp names 9.0.1). The three IDs were the only entries in elastos/.cargo/audit.toml, so the file goes, as #414 asks. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01W6Qn9RX7LDqrwiAfNC2F2K --- elastos/.cargo/audit.toml | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 elastos/.cargo/audit.toml 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", -] From 5873d2f7c89c258a229a8ffe338712d173338e6d Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 17 Oct 2026 13:54:54 +0000 Subject: [PATCH 4/5] library: refuse Windows root, drive and UNC names in ZIP extract on every platform zip_entry_enclosed_path read the entry name with std Path, so on Linux and macOS a drive letter is not a prefix and `\` is not a separator. `C:/x` passed the guard and extract wrote a folder named "C:". `C:\x`, `\\server\share\x` and `\\?\C:\x` were refused only later, by the object name check, because they hold a backslash. The guard now reads the name the same way on every platform: a leading `/` or `\` (this includes UNC and `\\?\` names) or a drive letter (`C:\x`, `C:/x`, `C:x`) is refused, and `\` counts as a separator when it counts the depth. A name that starts with a letter and a colon, such as `a:b.txt`, is now refused too. The tamper test adds `C:\x`, `C:/x`, `\\server\share\x` and `\\?\C:\x`, and the backslash case now expects the guard's message. A new unit test checks the guard alone. With the old guard, the unit test lists 10 accepted hostile names and the tamper test writes `C:/drive-slash-escape.txt`. With zip 9 enclosed_name in place of the guard, all four Windows names are written. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01W6Qn9RX7LDqrwiAfNC2F2K --- .../src/api/gateway_tests/library.rs | 34 ++++++++- elastos/crates/elastos-server/src/library.rs | 73 +++++++++++++++---- 2 files changed, 91 insertions(+), 16 deletions(-) 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 0253c229f..c6e4b8d3e 100644 --- a/elastos/crates/elastos-server/src/api/gateway_tests/library.rs +++ b/elastos/crates/elastos-server/src/api/gateway_tests/library.rs @@ -3921,7 +3921,7 @@ async fn test_library_provider_extract_zip_refuses_absolute_dotdot_and_symlink_e // 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); 5] = [ + let cases: [(&str, &str, &str, &str, &str); 9] = [ ( "Absolute.zip", "file", @@ -3948,7 +3948,37 @@ async fn test_library_provider_extract_zip_refuses_absolute_dotdot_and_symlink_e "file", "..\\backslash-escape.txt", "", - "invalid library object name", + "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", diff --git a/elastos/crates/elastos-server/src/library.rs b/elastos/crates/elastos-server/src/library.rs index 84c791d0a..f902da5df 100644 --- a/elastos/crates/elastos-server/src/library.rs +++ b/elastos/crates/elastos-server/src/library.rs @@ -5473,26 +5473,31 @@ fn extract_zip_archive( } /// The path a whole-archive ZIP extract writes for an entry name, or `None` -/// when the name holds a NUL byte, is absolute (a root or a drive prefix), or -/// climbs above the archive root. `archive_entry_uri` then refuses any `..` -/// that is left. This keeps the rule in the runtime: zip 9 `enclosed_name` -/// strips a leading `/` or drive prefix and folds `..` instead, which would -/// write entries that this extract refuses. +/// when the name holds a NUL byte, is absolute, or climbs above the archive +/// root. Absolute means a leading `/` or `\` (this includes `\\server\share` +/// and `\\?\` names) or a drive letter (`C:\x`, `C:/x`, `C:x`). The rule is +/// the same on every platform: `\` counts as a separator, and a drive letter +/// is refused also where `Path` has no drive prefix (Linux and macOS). +/// `archive_entry_uri` then refuses any `..` or `\` that is left. This keeps +/// the rule in the runtime: zip 9 `enclosed_name` strips a root or drive +/// prefix and folds `..` instead, which would write entries that this +/// extract refuses. fn zip_entry_enclosed_path(name: &str) -> Option { - if name.contains('\0') { + let bytes = name.as_bytes(); + let rooted = matches!(bytes.first(), Some(b'/' | b'\\')); + let drive = bytes.len() >= 2 && bytes[0].is_ascii_alphabetic() && bytes[1] == b':'; + if name.contains('\0') || rooted || drive { return None; } - let path = PathBuf::from(name); let mut depth = 0usize; - for component in path.components() { - match component { - Component::Prefix(_) | Component::RootDir => return None, - Component::ParentDir => depth = depth.checked_sub(1)?, - Component::Normal(_) => depth += 1, - Component::CurDir => {} + for part in name.split(['/', '\\']) { + match part { + "" | "." => {} + ".." => depth = depth.checked_sub(1)?, + _ => depth += 1, } } - Some(path) + Some(PathBuf::from(name)) } fn archive_entry_uri(destination_uri: &str, path: &Path) -> anyhow::Result { @@ -7212,6 +7217,46 @@ mod tests { use super::*; + #[test] + fn zip_entry_enclosed_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", + "a\0.txt", + ]; + let accepted: Vec<&str> = refused + .into_iter() + .filter(|name| zip_entry_enclosed_path(name).is_some()) + .collect(); + assert!(accepted.is_empty(), "accepted hostile names: {accepted:?}"); + + for name in [ + "a.txt", + "./a.txt", + "Nested/a.txt", + "Nested/", + "Nested/C:a.txt", + ] { + assert_eq!( + zip_entry_enclosed_path(name), + Some(PathBuf::from(name)), + "{name:?}" + ); + } + } + #[test] fn object_provider_exposes_object_scheme_only() { let registry = Arc::new(ProviderRegistry::new()); From 16a132a8f2a32d27c20e2597ecd0bcfad4e1a785 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 18 Oct 2026 03:11:18 +0000 Subject: [PATCH 5/5] library: one entry-name rule for every archive reader, ZIP and tar Decision on #417: list, preview and selected extract use the same path rule as whole-archive extract, so `C:/x` is refused by every archive reader. Before, list, preview and selected extract read an entry name with std Path (normalized_archive_entry_path). On Linux and macOS that reads `C:/x` as a folder named "C:": the list showed the entry as safe, preview returned its bytes, and selected extract wrote Imports/C:/x. Whole-archive tar extract had no guard: it wrote `C:/x`, and refused `C:\x` and `\\server\share\x` only through the object name check ("invalid library object name"). archive_entry_path is now the one rule. All ten call sites use it: the ZIP and tar readers for list, preview, selected extract and whole extract, and the entry a person asks to preview or to extract. It is the same on every platform: a NUL byte, any `\`, a leading `/`, a drive letter and any `..` part are refused with "library archive entry path must be relative and safe". It replaces zip_entry_enclosed_path. Tar names are read from the raw header bytes (path_bytes), so the rule sees the same text on every platform. The list still shows the archive. A refused entry is a row with kind "blocked", safety status "blocked" and the rule's reason. It cannot be previewed or extracted: the request is refused by the rule, and each reader skips or blocks the entry by the same rule. Behaviour change: a name with a `\` anywhere is refused with the rule's message by every reader. Before, whole extract refused it on Linux with "invalid library object name"; on Windows, std Path read `\` as a separator. Tests: test_library_provider_every_archive_reader_refuses_root_drive_unc_and_climb_names (gateway, ZIP and tar, five hostile names, every reader) and archive_readers_block_root_drive_unc_and_climb_entry_names (readers called past the request check). Before this change they list 14 and 7 failed checks. With any one of the ten call sites routed back to the old code, at least one of them fails. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01W6Qn9RX7LDqrwiAfNC2F2K --- .../src/api/gateway_tests/library.rs | 205 +++++++++++ elastos/crates/elastos-server/src/library.rs | 337 +++++++++++------- 2 files changed, 407 insertions(+), 135 deletions(-) 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 c6e4b8d3e..78a2f46bf 100644 --- a/elastos/crates/elastos-server/src/api/gateway_tests/library.rs +++ b/elastos/crates/elastos-server/src/api/gateway_tests/library.rs @@ -4097,6 +4097,211 @@ async fn test_library_provider_extract_zip_refuses_absolute_dotdot_and_symlink_e } } +/// 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 f902da5df..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 { @@ -4243,7 +4243,7 @@ fn preview_zip_archive_entry( .name() .map_err(|err| anyhow!("invalid ZIP archive entry: {err}"))? .into_owned(); - let Ok(normalized) = normalized_archive_entry_path(Path::new(&raw_name)) else { + let Ok(normalized) = normalized_archive_entry_path(&raw_name) else { continue; }; if normalized != selected { @@ -4284,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 { @@ -4395,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() { @@ -4464,8 +4450,7 @@ fn list_zip_archive_entries(bytes: Vec) -> anyhow::Result<(Vec, bool) .name() .map_err(|err| anyhow!("invalid ZIP archive entry: {err}"))? .into_owned(); - let path = Path::new(&raw_name); - let normalized = match normalized_archive_entry_path(path) { + let normalized = match normalized_archive_entry_path(&raw_name) { Ok(path) => path, Err(err) => { entries.push(blocked_archive_entry_listing( @@ -4506,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 { @@ -4853,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; } @@ -4949,7 +4942,7 @@ fn collect_selected_zip_writes( .name() .map_err(|err| anyhow!("invalid ZIP archive entry: {err}"))? .into_owned(); - let normalized = match normalized_archive_entry_path(Path::new(&raw_name)) { + let normalized = match normalized_archive_entry_path(&raw_name) { Ok(path) => path, Err(err) => { outcome.blocked.push(json!({ @@ -5395,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; } @@ -5441,9 +5434,8 @@ fn extract_zip_archive( let raw_name = file .name() .map_err(|err| anyhow!("invalid ZIP archive entry: {err}"))?; - let entry_path = zip_entry_enclosed_path(&raw_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 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; } @@ -5472,34 +5464,6 @@ fn extract_zip_archive( Ok(()) } -/// The path a whole-archive ZIP extract writes for an entry name, or `None` -/// when the name holds a NUL byte, is absolute, or climbs above the archive -/// root. Absolute means a leading `/` or `\` (this includes `\\server\share` -/// and `\\?\` names) or a drive letter (`C:\x`, `C:/x`, `C:x`). The rule is -/// the same on every platform: `\` counts as a separator, and a drive letter -/// is refused also where `Path` has no drive prefix (Linux and macOS). -/// `archive_entry_uri` then refuses any `..` or `\` that is left. This keeps -/// the rule in the runtime: zip 9 `enclosed_name` strips a root or drive -/// prefix and folds `..` instead, which would write entries that this -/// extract refuses. -fn zip_entry_enclosed_path(name: &str) -> Option { - let bytes = name.as_bytes(); - let rooted = matches!(bytes.first(), Some(b'/' | b'\\')); - let drive = bytes.len() >= 2 && bytes[0].is_ascii_alphabetic() && bytes[1] == b':'; - if name.contains('\0') || rooted || drive { - return None; - } - let mut depth = 0usize; - for part in name.split(['/', '\\']) { - match part { - "" | "." => {} - ".." => depth = depth.checked_sub(1)?, - _ => depth += 1, - } - } - Some(PathBuf::from(name)) -} - fn archive_entry_uri(destination_uri: &str, path: &Path) -> anyhow::Result { let mut uri = destination_uri.trim_end_matches('/').to_string(); for component in path.components() { @@ -7218,7 +7182,7 @@ mod tests { use super::*; #[test] - fn zip_entry_enclosed_path_refuses_root_drive_and_climb_on_every_platform() { + fn archive_entry_path_refuses_root_drive_and_climb_on_every_platform() { let refused = [ "/a.txt", "\\a.txt", @@ -7233,30 +7197,133 @@ mod tests { "../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| zip_entry_enclosed_path(name).is_some()) + .filter(|name| archive_entry_path(name).is_ok()) .collect(); assert!(accepted.is_empty(), "accepted hostile names: {accepted:?}"); - for name in [ - "a.txt", - "./a.txt", - "Nested/a.txt", - "Nested/", - "Nested/C:a.txt", + 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!( - zip_entry_enclosed_path(name), - Some(PathBuf::from(name)), - "{name:?}" - ); + 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());