mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
Address CodeRabbit review: refuse a non-regular write target before opening it
An existing FIFO at the write target reached holds_image (and the Unix xattr copy), whose read-only open blocks until a writer appears, stalling every request behind the Headless lock. Check the target's type first, following links, and refuse directories, FIFOs, sockets and devices. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
fa4269af98
commit
52359ada45
1 file changed
+48
-1
@@ -92,7 +92,14 @@ impl AuthorizedWorkspace {
|
||||
// only a file of a kind PhotoCraft writes is replaced: never source, settings or notes
|
||||
// that share the root (`src/main.rs`, `.env`; #9439). A new file may have any name, and
|
||||
// an image saved under one (`out` with `format: png`) can be saved over again.
|
||||
if root.dir.symlink_metadata(&relative).is_ok() && !written_kind(&leaf) && !holds_image(&root.dir, &relative) {
|
||||
let exists = root.dir.symlink_metadata(&relative).is_ok();
|
||||
// Only a regular file is ever opened below (its head here, its attributes on Unix): a
|
||||
// FIFO would block that open until a writer appears, and with it every other request.
|
||||
// Links are followed for this check; a dangling one falls through to the name rule.
|
||||
if exists && root.dir.metadata(&relative).is_ok_and(|m| !m.is_file()) {
|
||||
return Err(AutomationError::BadRequest(format!("automation write refused: `{path}` already exists and is not a regular file")));
|
||||
}
|
||||
if exists && !written_kind(&leaf) && !holds_image(&root.dir, &relative) {
|
||||
return Err(AutomationError::BadRequest(format!(
|
||||
"automation write refused: `{path}` already exists and is not an image or document file PhotoCraft writes; save to a new name"
|
||||
)));
|
||||
@@ -634,6 +641,46 @@ mod tests {
|
||||
assert!(temp_files(&inside).is_empty());
|
||||
}
|
||||
|
||||
/// A directory at the target is refused before anything is opened, even under an image name.
|
||||
#[test]
|
||||
fn write_refuses_a_directory_target_before_opening_it() {
|
||||
let (inside, _, workspace) = roots("dir-target");
|
||||
for name in ["adir", "adir.png"] {
|
||||
std::fs::create_dir_all(inside.join(name)).unwrap();
|
||||
let error = workspace.write(name, b"x").unwrap_err().to_string();
|
||||
assert!(error.contains("is not a regular file"), "{name}: {error}");
|
||||
assert!(inside.join(name).is_dir());
|
||||
}
|
||||
assert!(temp_files(&inside).is_empty());
|
||||
}
|
||||
|
||||
/// A FIFO at the target is refused at once: opening it to sniff its head or copy its
|
||||
/// attributes would block until a writer appears, stalling every other request.
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn write_refuses_a_fifo_target_without_blocking() {
|
||||
let (inside, _, workspace) = roots("fifo-target");
|
||||
for name in ["pipe", "out.png"] {
|
||||
let made = std::process::Command::new("mkfifo").arg(inside.join(name)).status();
|
||||
if !made.is_ok_and(|s| s.success()) {
|
||||
return; // no mkfifo on this system: nothing to assert
|
||||
}
|
||||
}
|
||||
std::os::unix::fs::symlink("pipe", inside.join("link")).unwrap();
|
||||
let (tx, rx) = std::sync::mpsc::channel();
|
||||
std::thread::spawn(move || {
|
||||
for name in ["pipe", "out.png", "link"] {
|
||||
let _ = tx.send((name, workspace.write(name, b"x").map_err(|e| e.to_string())));
|
||||
}
|
||||
});
|
||||
for _ in 0..3 {
|
||||
let (name, result) = rx.recv_timeout(std::time::Duration::from_secs(5)).expect("write blocked on a FIFO");
|
||||
let error = result.unwrap_err();
|
||||
assert!(error.contains("is not a regular file"), "{name}: {error}");
|
||||
}
|
||||
assert!(temp_files(&inside).is_empty());
|
||||
}
|
||||
|
||||
/// #7095: replacing a file through the automation path keeps the old file's extended
|
||||
/// attributes, the same as a save through `atomic_write` does.
|
||||
#[cfg(any(target_os = "linux", target_os = "android", target_vendor = "apple"))]
|
||||
|
||||
Reference in new issue
Block a user