diff --git a/crates/engine/src/cmd/blocks/library.rs b/crates/engine/src/cmd/blocks/library.rs index b7d7ba001..905a0e509 100644 --- a/crates/engine/src/cmd/blocks/library.rs +++ b/crates/engine/src/cmd/blocks/library.rs @@ -70,6 +70,8 @@ fn stamp(path: &str) -> Option<(u64, Option)> { /// A library drawing, read once and kept while the file is as it was: placing a block must not /// parse the whole file again. fn cached_drawing(path: &str) -> Result> { + // Before the cache too: a drawing read earlier must not be handed out past the roots. + crate::file_access::check_read(path).map_err(|e| bad("blocklibrary", e))?; // The reader sniffs the bytes, so a DXF named `part.bin` would load: the contract is DWG/DXF by name. if !is_drawing(Path::new(path)) { return Err(bad("blocklibrary", format!("{path}: expected a DWG or DXF file"))); @@ -186,6 +188,9 @@ pub fn read_library_file(path: &str) -> Result> { /// What BLOCKLIBRARY answers for `path`. It needs no session, so the palette reads big drawings /// and slow folders on a worker thread. pub fn list_library(path: &str) -> Result { + // Before anything looks at the path: a folder's listing, or whether it is one, says what is + // outside the automation roots. + crate::file_access::check_read(path).map_err(|e| bad("blocklibrary", e))?; if is_folder(path) { let (files, truncated) = folder_files(Path::new(path))?; let list: Vec = files.iter().map(|(name, file)| json!({ "name": name, "file": file })).collect(); @@ -356,6 +361,26 @@ fn run_libraries(_s: &mut Session, p: &Value) -> Result { mod tests { use super::*; + /// Under the automation roots a library folder or file outside them is refused before it is + /// listed or read. + #[test] + fn the_automation_roots_hold_library_paths() { + let base = std::env::temp_dir().join(format!("cadcraft-library-roots-{}", std::process::id())); + let (inside, outside) = (base.join("in"), base.join("outside")); + std::fs::create_dir_all(&inside).unwrap(); + std::fs::create_dir_all(&outside).unwrap(); + std::fs::write(outside.join("parts.dxf"), b"0\nEOF\n").unwrap(); + let roots = crate::file_access::AutomationRoots::new(Some(&inside), None).unwrap(); + crate::file_access::confine(roots.as_ref(), || { + let folder = list_library(&outside.to_string_lossy()).map(|v| v.to_string()); + assert!(folder.as_ref().is_err_and(|e| e.to_string().contains("automation")), "{folder:?}"); + let file = list_library(&outside.join("parts.dxf").to_string_lossy()).map(|v| v.to_string()); + assert!(file.as_ref().is_err_and(|e| e.to_string().contains("automation")), "{file:?}"); + assert!(list_library(&inside.to_string_lossy()).is_ok()); + }); + let _ = std::fs::remove_dir_all(&base); + } + /// A list that could not be written stays the list of this session, whatever the file says. #[test] fn a_failed_write_keeps_the_list_in_memory() { diff --git a/crates/engine/src/file_access/commands.rs b/crates/engine/src/file_access/commands.rs index 4ff6b992c..8d628449d 100644 --- a/crates/engine/src/file_access/commands.rs +++ b/crates/engine/src/file_access/commands.rs @@ -14,6 +14,8 @@ pub const FILES: &[&str] = &[ "aliasedit", "appload", "attach", + "blocklibimport", + "blocklibrary", "audit", "compare", "compareexport", @@ -61,10 +63,19 @@ pub const FILES: &[&str] = &[ ]; /// Commands that can't be held to the roots, and why. -pub const REFUSED: &[(&str, &str)] = &[("drawingrecovery", "it works on the autosave folder (SAVEFILEPATH), which is the server's own")]; +pub const REFUSED: &[(&str, &str)] = &[ + ("drawingrecovery", "it works on the autosave folder (SAVEFILEPATH), which is the server's own"), + ("blocklibraries", "it keeps its list of libraries in the settings folder, which is the server's own"), +]; /// Commands that touch no file. pub const NO_FILES: &[&str] = &[ + "blockspalette", + "dimoverride.clear", + "-dimstyle", + "dimstyle.overrides", + "dimstyle.saveas", + "properties.describe", "close", "closeall", "document.switch", @@ -532,6 +543,7 @@ mod tests { confine(roots.as_ref(), || { assert!(refusal("made.up").unwrap().contains("not classified")); assert!(refusal("drawingrecovery").unwrap().contains("autosave")); + assert!(refusal("blocklibraries").unwrap().contains("settings folder")); assert_eq!(refusal("open"), None); assert_eq!(refusal("line"), None); });