From 8773c1393e47a0f3ca5339c53dd26630136d836f Mon Sep 17 00:00:00 2001 From: Claude Code Date: Sun, 18 Oct 2026 00:49:17 +0300 Subject: [PATCH] fix(quarantine): reject path-traversal in restore target The quarantine index (JSONL) is untrusted and attacker-tamperable. A forged entry with original_path containing '..' traversal components or a bare filename could make quarantine_restore write the quarantined file to an arbitrary location. Reject any original_path with a ParentDir component or fewer than 2 components (bare filename). Legitimate absolute paths (e.g. Windows C:\Users\...\file.txt) are still restored correctly. Add restore_rejects_traversal_path and restore_rejects_traversal_relative integration tests. Co-Authored-By: Claude Code --- src/quarantine.rs | 70 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 65 insertions(+), 5 deletions(-) diff --git a/src/quarantine.rs b/src/quarantine.rs index a8b857f..d3447c1 100644 --- a/src/quarantine.rs +++ b/src/quarantine.rs @@ -202,13 +202,22 @@ fn restore_in(id: &str, root: &Path) -> Result { n += 1; } } - // Refuse a restore target that escapes a trusted directory: the - // recorded original path must have at least two components (i.e. a - // directory plus a file name). A bare "a.txt" is rejected. - if e.original_path.components().count() < 2 { + // Refuse a restore target that could escape via traversal. The index + // is untrusted/attacker-tamperable, so `original_path` from a forged + // entry must not escape the path it specifies: any `..` (ParentDir) + // component is rejected. A bare filename is also rejected (fewer than + // 2 components) since we cannot determine a safe restore directory. + if e.original_path + .components() + .any(|c| c == std::path::Component::ParentDir) + || e.original_path.components().count() < 2 + { return Err(Error::new( ErrorKind::Quarantine, - "original path is not valid for restore", + format!( + "original path {:?} is not safe for restore (no '..' traversal, needs a directory + filename)", + e.original_path + ), )); } fs::rename(&src, &dest).map_err(|e| { @@ -343,6 +352,57 @@ mod tests { fs::remove_dir_all(&tmp).ok(); } + #[test] + fn restore_rejects_traversal_path() { + // A forged index entry with a `..`-traversal original_path must not + // be restored to an arbitrary location. The restore should error, + // not move the quarantined file outside the expected tree. + let tmp = std::env::temp_dir().join(format!("os-qtest-traversal-{}", new_id())); + let q = tmp.join("q"); + fs::create_dir_all(&q).unwrap(); + let original = tmp.join("payload.bin"); + std::fs::write(&original, b"data").unwrap(); + let entry = quarantine_add(&original, &q).unwrap(); + + // Tamper the index: rewrite the entry's original_path to traverse. + let index = q.join("index.jsonl"); + let bad = format!( + r#"{{"id":"{}","original_path":"../../../../evil.txt","sha256":"{}","quarantined_at":"2026-01-01T00:00:00Z"}}"#, + entry.id, entry.sha256 + ); + std::fs::write(&index, bad).unwrap(); + + let r = restore_in(&entry.id, &q); + assert!(r.is_err(), "traversal original_path must be rejected"); + // The quarantined file must still be in the store. + assert!(q.join(&entry.id).is_file(), "file must not have moved"); + std::fs::remove_dir_all(&tmp).ok(); + } + + #[test] + fn restore_rejects_traversal_relative() { + // A relative path that tries to climb out via `..` must be rejected + // even if it has multiple components. + let tmp = std::env::temp_dir().join(format!("os-qtest-reltrav-{}", new_id())); + let q = tmp.join("q"); + fs::create_dir_all(&q).unwrap(); + let original = tmp.join("payload.bin"); + std::fs::write(&original, b"data").unwrap(); + let entry = quarantine_add(&original, &q).unwrap(); + + let index = q.join("index.jsonl"); + let bad = format!( + r#"{{"id":"{}","original_path":"../../evil.txt","sha256":"{}","quarantined_at":"2026-01-01T00:00:00Z"}}"#, + entry.id, entry.sha256 + ); + std::fs::write(&index, bad).unwrap(); + + let r = restore_in(&entry.id, &q); + assert!(r.is_err(), "traversal original_path must be rejected"); + assert!(q.join(&entry.id).is_file(), "file must not have moved"); + std::fs::remove_dir_all(&tmp).ok(); + } + #[test] fn component_count_guard() { // A path with fewer than 2 components (e.g. "a.txt") is rejected.