From 59f87982005fd43fd2e2fd21b3b1961d4fc859c9 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Sat, 28 Nov 2026 22:38:02 +0100 Subject: [PATCH] fix(platform): hold the zone's target open, without delete sharing, until the stream is written Addresses review: between the existence check and the stream write, another process could delete or rename the target, and the write would then recreate it as an empty file. The target is now opened without FILE_SHARE_DELETE, so it can't be deleted or renamed until the zone is written, and the check reads that handle's metadata. Refs #8622 --- crates/platform/src/zone.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/crates/platform/src/zone.rs b/crates/platform/src/zone.rs index ace0ad650d..d0d26b6b15 100644 --- a/crates/platform/src/zone.rs +++ b/crates/platform/src/zone.rs @@ -18,15 +18,23 @@ const MAX_ZONE_STREAM: u64 = 64 * 1024; /// Internet zone when `source` is `None` or has no zone. Windows only; elsewhere nothing is /// written. Fails where the volume keeps no alternate data streams (FAT32, exFAT, some network /// shares), so callers treat the mark as best effort. Fails with `NotFound` when `target` isn't an -/// existing file: writing `target:Zone.Identifier` would otherwise create an empty `target`. +/// existing file: writing `target:Zone.Identifier` would otherwise create an empty `target`. The +/// target is held open, without delete sharing, until the stream is written. pub fn mark_from(target: &Path, source: Option<&Path>) -> std::io::Result<()> { #[cfg(windows)] { - if !std::fs::metadata(target)?.is_file() { + use std::os::windows::fs::OpenOptionsExt; + // FILE_SHARE_READ | FILE_SHARE_WRITE, without FILE_SHARE_DELETE: while this handle is open + // the target can't be deleted or renamed, so the stream write below can't recreate it. + const SHARE_READ_WRITE: u32 = 0x1 | 0x2; + let held = std::fs::OpenOptions::new().read(true).share_mode(SHARE_READ_WRITE).open(target)?; + if !held.metadata()?.is_file() { return Err(std::io::Error::new(std::io::ErrorKind::NotFound, "the zone's target is not a file")); } let zone = source.and_then(read_zone).unwrap_or_else(|| INTERNET_ZONE.as_bytes().to_vec()); - std::fs::write(zone_stream(target), zone) + let written = std::fs::write(zone_stream(target), zone); + drop(held); + written } #[cfg(not(windows))] {