Skip to content

Commit 3cf7839

Browse files
fix(core): reject stale authority before creating quarantine directories
1 parent 7ddd050 commit 3cf7839

1 file changed

Lines changed: 30 additions & 5 deletions

File tree

‎crates/diskern-core/src/actions.rs‎

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ fn quarantine_checked(
9898
verdict: Verdict,
9999
quarantine_dir: &Path,
100100
stamp: i64,
101-
before_move: impl FnOnce() -> Result<()>,
101+
before_move: impl Fn() -> Result<()>,
102102
) -> Result<QuarantineRecord> {
103103
match verdict {
104104
Verdict::Protected | Verdict::Risky => {
@@ -110,12 +110,14 @@ fn quarantine_checked(
110110
Verdict::Safe | Verdict::Review => {}
111111
}
112112

113-
std::fs::create_dir_all(quarantine_dir).map_err(|e| io_err(quarantine_dir, e))?;
114-
115113
// Hold the same lock as restore/purge through selection, movement and
116114
// manifest append. Locking only the append lets two tasks select the
117115
// same flattened name before either one has reserved it.
118116
let _guard = manifest_lock();
117+
// Reject an already stale request before making any filesystem changes.
118+
// Repeat this check below in case authority changes during preparation.
119+
before_move()?;
120+
std::fs::create_dir_all(quarantine_dir).map_err(|e| io_err(quarantine_dir, e))?;
119121
let record = QuarantineRecord {
120122
original: file.to_path_buf(),
121123
quarantined_to: unique_dest(quarantine_dir, stamp, file),
@@ -167,8 +169,8 @@ pub fn quarantine_finding(
167169
/// Report-bound quarantine with a final authority check supplied by the
168170
/// caller. Tauri uses this to reject a lease invalidated by a newer scan
169171
/// without holding the report mutex over filesystem I/O. The callback runs
170-
/// under the manifest lock immediately before moving, so it must not call
171-
/// another action that acquires that lock.
172+
/// under the manifest lock before creating the directory and again immediately
173+
/// before moving, so it must not call another action that acquires that lock.
172174
pub fn quarantine_finding_with_authorization<F>(
173175
path: &Path,
174176
report: &report::Report,
@@ -769,6 +771,29 @@ mod tests {
769771
});
770772
assert!(result.is_err());
771773
assert_eq!(std::fs::read(&target).unwrap(), b"cache");
774+
assert!(
775+
!q.exists(),
776+
"rejected requests must not create a quarantine directory"
777+
);
778+
assert!(list(&q).unwrap().is_empty());
779+
}
780+
781+
#[test]
782+
fn authority_invalidated_during_preparation_still_prevents_the_move() {
783+
let dir = tempfile::tempdir().unwrap();
784+
let target = dir.path().join("cache.bin");
785+
let q = dir.path().join("quarantine");
786+
std::fs::write(&target, b"cache").unwrap();
787+
let report = report_for(&target, Verdict::Review);
788+
let rules = rules_with_verdict("**/cache.bin", Verdict::Review);
789+
let checks = std::cell::Cell::new(0);
790+
let result = quarantine_finding_with_authorization(&target, &report, &rules, &q, || {
791+
checks.set(checks.get() + 1);
792+
checks.get() == 1
793+
});
794+
assert!(result.is_err());
795+
assert_eq!(checks.get(), 2);
796+
assert_eq!(std::fs::read(&target).unwrap(), b"cache");
772797
assert!(list(&q).unwrap().is_empty());
773798
}
774799

0 commit comments

Comments
 (0)