Skip to content
Merged
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
70 changes: 65 additions & 5 deletions src/quarantine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,22 @@ fn restore_in(id: &str, root: &Path) -> Result<PathBuf, Error> {
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
Comment on lines +210 to +213

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Action required

1. Parent-relative files cannot be restored 🐞 Bug ≡ Correctness

restore_in rejects every ParentDir component, including one in a path that quarantine_add
recorded from a legitimate source. When a user adds an existing file as ../dir/file.bin, the add
succeeds, but restoring its ID returns an error and leaves the file in quarantine.
Agent Prompt
## Issue description
Legitimate files added through paths containing `..` cannot be restored because add records the path unchanged while restore rejects it.

## Fix Focus Areas
- src/quarantine.rs[80-86]
- src/quarantine.rs[210-213]

## Recommended Fix
Resolve legitimate source paths to safe, absolute paths when adding files, and store the resolved path as `original_path`. Keep forged traversal paths rejected during restore; account for previously recorded legitimate paths containing `..` without treating arbitrary tampered entries as trusted.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Dismiss ↗ | View ↗

{
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| {
Expand Down Expand Up @@ -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.
Expand Down
Loading