Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Stored paths: refuse Windows drive and share paths outside Windows without a document

aiongg37463ae532ee
diff --git a/crates/io/src/doc_path.rs b/crates/io/src/doc_path.rs
index ce6cc0fdc..8c3c36c93 100644
--- a/crates/io/src/doc_path.rs
+++ b/crates/io/src/doc_path.rs
@@ -37,11 +37,20 @@ pub fn resolve(stored: &str, document: Option<&Path>) -> Result<PathBuf, NotFoll
let document = document.map(|d| if d.is_relative() { std::path::absolute(d).unwrap_or_else(|_| d.to_path_buf()) } else { d.to_path_buf() });
craft_docpath::resolve_absolute(stored, document.as_deref()).map_err(|r| match r {
// Without a document, a relative path resolves but stays relative.
- Refused::Invalid if document.is_none() && craft_docpath::resolve(stored, None).is_ok() => NotFollowed::NoDocumentFolder,
+ Refused::Invalid if document.is_none() && craft_docpath::resolve(stored, None).is_ok_and(|p| cfg!(windows) || !is_windows_form(&p)) => {
+ NotFollowed::NoDocumentFolder
+ }
r => NotFollowed::Refused(r),
})
}
+/// Whether `path`, as `craft_docpath` writes it, has a Windows drive or share root (`C:x`, `C:\x`,
+/// `\\server\share\x`). Outside Windows no document folder makes such a path absolute.
+fn is_windows_form(path: &Path) -> bool {
+ let s = path.as_os_str().as_encoded_bytes();
+ s.starts_with(b"\\") || matches!(s, [l, b':', ..] if l.is_ascii_alphabetic())
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@ -62,8 +71,13 @@ mod tests {
assert_eq!(resolve("img/fig:1.png", Some(&doc)), Ok(std::env::temp_dir().join("art").join("img").join("fig:1.png")));
assert_eq!(resolve("chapter1:intro.png", None), Err(NotFollowed::NoDocumentFolder));
}
- // On Unix a Windows path names no file: it is refused, not reported as relative.
- #[cfg(unix)]
- assert_eq!(resolve(r"C:\art\logo.png", Some(&doc)), Err(NotFollowed::Refused(Refused::Invalid)));
+ // Outside Windows a Windows path names no file: it is refused, not reported as relative.
+ if !cfg!(windows) {
+ for stored in [r"C:\art\logo.png", "C:logo.png"] {
+ assert_eq!(resolve(stored, Some(&doc)), Err(NotFollowed::Refused(Refused::Invalid)), "{stored}");
+ assert_eq!(resolve(stored, None), Err(NotFollowed::Refused(Refused::Invalid)), "{stored}");
+ }
+ assert_eq!(resolve("../logo.png", None), Err(NotFollowed::NoDocumentFolder));
+ }
}
}