PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
OCR: Recognize text in multiple files refuses paths outside the root (#795)
mlkonradf9ed8b264ab0
diff --git a/crates/automation/src/a11y.rs b/crates/automation/src/a11y.rsindex 2a853083c..b58b2d587 100644--- a/crates/automation/src/a11y.rs+++ b/crates/automation/src/a11y.rs@@ -156,10 +156,16 @@ impl Automation {pub(crate) fn ocr_recognize_files(&mut self, a: &Args) -> Result<Value> {let settings = self.ocr_settings(a)?;let folder = self.resolve(a.str("folder")?, true)?;+ // A path that leaves the root refuses the whole call before any work starts, like the+ // other tools (the write check passes missing files, which stay per-file errors below).+ let paths = a.strs("paths")?;+ for p in &paths {+ self.resolve(p, true)?;+ }std::fs::create_dir_all(&folder).map_err(|e| failed(e.to_string()))?;let ocr = pdfcraft_engine::ocr::engine().map_err(failed)?;let mut out = Vec::new();- for p in a.strs("paths")? {+ for p in paths {let name = std::path::Path::new(p).file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_else(|| "document.pdf".into());let result =self.resolve(p, false).map_err(|e| e.to_string()).and_then(|src| std::fs::read(&src).map_err(|e| e.to_string())).and_then(|bytes| {diff --git a/crates/automation/tests/automation.rs b/crates/automation/tests/automation.rsindex e83e2125c..f31ffc2a2 100644--- a/crates/automation/tests/automation.rs+++ b/crates/automation/tests/automation.rs@@ -2829,6 +2829,18 @@ fn ocr_tools_make_a_scan_searchable() {assert!(matches!(a.call("ocr_recognize", &json!({ "doc": scan, "language": "xx" })), Err(ToolError::InvalidArgs(_))));}+#[test]+fn ocr_recognize_files_refuses_paths_outside_the_root() {+ let dir = workdir("ocr-files-root");+ let mut a = auto(&dir);+ // Refused before anything else, with or without the OCR models installed.+ for path in ["../outside.pdf", "sub/../../outside.pdf", "/outside.pdf"] {+ let r = a.call("ocr_recognize_files", &json!({ "paths": ["a.pdf", path], "folder": "out" }));+ assert!(matches!(&r, Err(e) if e.to_string().contains("outside the allowed directory")), "{path}: {r:?}");+ }+ assert!(!dir.join("out").exists(), "nothing was started");+}+#[test]fn ocr_recognize_files_writes_searchable_copies() {let dir = workdir("ocr-files");