diff --git a/crates/engine/src/cmd/fileinfo.rs b/crates/engine/src/cmd/fileinfo.rs index f4f38855dc..b6b1643ea4 100644 --- a/crates/engine/src/cmd/fileinfo.rs +++ b/crates/engine/src/cmd/fileinfo.rs @@ -257,6 +257,9 @@ fn template_bytes(s: &Session, p: &Value) -> Result> { .min_by_key(|f| (f != name, *f != own)); if let Some(file) = listed { path = dir.join(file).to_string_lossy().into_owned(); + } else if std::path::Path::new(&path).file_stem().map(|s| s.to_string_lossy()).as_deref() != Some(name) { + // A name that is not a file name as it is never opens another template. + return Err(bad(APPLY, "the template name isn't usable as a file name")); } let plain_file = |path: &str| std::fs::symlink_metadata(path).is_ok_and(|m| m.is_file()); if std::fs::symlink_metadata(&path).is_ok() && !plain_file(&path) { diff --git a/crates/engine/src/tests/fileinfo.rs b/crates/engine/src/tests/fileinfo.rs index ba706e43c0..54482826b3 100644 --- a/crates/engine/src/tests/fileinfo.rs +++ b/crates/engine/src/tests/fileinfo.rs @@ -779,6 +779,15 @@ fn the_templates_folder_lists_only_xmp_files_and_can_be_made() { assert_eq!(r["author"], "Grace"); let r = s.execute("file.info.applyTemplate", &json!({"name": "b.XMP.xmp", "mode": "replace", "apply": false})).unwrap(); assert_eq!(r["author"], "Ada"); + // A name that is not a file name never opens the template its safe form names. + let grace = vectorcraft_pdf::metadata_template(&vectorcraft_pdf::FileInfo { author: Some("Grace".into()), ..Default::default() }); + std::fs::write(folder.join("a-b.xmp"), grace).unwrap(); + let r = s.execute("file.info.applyTemplate", &json!({"name": "a-b", "mode": "replace", "apply": false})).unwrap(); + assert_eq!(r["author"], "Grace"); + for name in ["a/b", "a\\b", "a:b"] { + let e = s.execute("file.info.applyTemplate", &json!({"name": name, "mode": "replace", "apply": false})).unwrap_err(); + assert!(e.to_string().contains("isn't usable as a file name"), "{name}: {e}"); + } } #[test] diff --git a/docs/mcp.md b/docs/mcp.md index f8c8be380b..549797968f 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -7608,7 +7608,7 @@ A PDF, `.ai` or `.ait` also opens with the `authorTitle` and `rating` its XMP pa `xmp:Rating`, a whole number from 0 to 5). Metadata templates (the menu at the top of File Info: Save Metadata Template…, Import…, the saved templates and -Templates Folder; https://help.adobe.com/archive/en_US/illustrator/cs5/illustrator_cs5_help.pdf#page=260) keep File +Templates Folder; https://help.adobe.com/archive/en_US/illustrator/cs5/illustrator_cs5_help.pdf#page=278) keep File Info fields in an XMP file. `file.info.saveTemplate {name?, path?, fields?}` writes the `title` (when it is not the document's name), `author`, `authorTitle`, `description`, `keywords`, `rating` (from 1), `copyrightStatus`, `copyrightNotice` and `copyrightUrl` that have a value as an XMP packet, using the properties listed above; the dates