From 13924e259159d608506531dca2fd985f9faaab59 Mon Sep 17 00:00:00 2001 From: Dan Date: Fri, 20 Nov 2026 11:49:45 +0000 Subject: [PATCH] Reject bare ampersands in Image Processor presets --- .../dialogs/image_processor_settings_tests.rs | 20 +++++++++++-- .../dialogs/image_processor_ui/settings.rs | 30 ++++++++++++++++--- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/crates/ui-egui/src/dialogs/image_processor_settings_tests.rs b/crates/ui-egui/src/dialogs/image_processor_settings_tests.rs index bb470ecdc4..a925c45b69 100644 --- a/crates/ui-egui/src/dialogs/image_processor_settings_tests.rs +++ b/crates/ui-egui/src/dialogs/image_processor_settings_tests.rs @@ -94,6 +94,9 @@ fn invalid_settings_are_rejected() { "true", "falsetrue", "&bad;", + "A & B", + "A &", + "", "", ] { assert!(settings::decode(invalid.as_bytes()).is_err(), "{invalid}"); @@ -103,6 +106,19 @@ fn invalid_settings_are_rejected() { assert!(settings::encode(&DialogFields::from_pairs([("input", "bad\0path")])).is_err()); } +#[test] +fn escaped_ampersands_and_literal_cdata_remain_valid() { + let xml = concat!( + "\u{feff}", + "", + "A & B & C &amp;", + "", + ); + let fields = settings::decode(xml.as_bytes()).unwrap(); + assert_eq!(fields.text("output"), Some("A & B & C &")); + assert_eq!(fields.text("input"), Some("A & B")); +} + type AppHarness = Harness<'static, Box>; type Written = Rc)>>>; @@ -228,14 +244,14 @@ fn malformed_and_cancelled_picks_leave_the_form_unchanged() { assert_eq!(h.state().framework.dialogs.stack().top(), Some(id)); let path = tempfile::NamedTempFile::new().unwrap(); - std::fs::write(path.path(), b"truewrong").unwrap(); + std::fs::write(path.path(), b"trueA & B").unwrap(); click(&mut h, "Load…"); files.answer(Picked::Paths(vec![path.path().to_path_buf()])).unwrap(); h.run_steps(3); assert_eq!(fields(&h, id), &original); let alert = h.state().framework.dialogs.stack().top().unwrap(); assert_ne!(alert, id, "an error is shown over the form"); - assert!(h.state().ui.status.contains("Invalid Image Processor setting: q")); + assert!(h.state().ui.status.contains("unescaped ampersand")); } #[test] diff --git a/crates/ui-egui/src/dialogs/image_processor_ui/settings.rs b/crates/ui-egui/src/dialogs/image_processor_ui/settings.rs index 5cb7cb6c3b..267c863e1c 100644 --- a/crates/ui-egui/src/dialogs/image_processor_ui/settings.rs +++ b/crates/ui-egui/src/dialogs/image_processor_ui/settings.rs @@ -4,7 +4,7 @@ use std::borrow::Cow; use std::fmt::Write; use craft::ui::patterns::form_dialogs::{DialogFields, FieldValue}; -use craft_xml::{Doctype, Limits, Node, Options}; +use craft_xml::{Doctype, Event, Limits, Node, Options}; pub(super) const MAX_BYTES: usize = 256 * 1024; @@ -118,15 +118,17 @@ pub(super) fn decode(bytes: &[u8]) -> Result { if bytes.len() > MAX_BYTES { return Err(too_large()); } - std::str::from_utf8(bytes).map_err(|_| tl!("Image Processor settings must be UTF-8.").to_string())?; + let text = std::str::from_utf8(bytes).map_err(|_| tl!("Image Processor settings must be UTF-8.").to_string())?; let options = Options { doctype: Doctype::Reject, pis: true, ..Options::default() }.strict().with_limits(Limits { max_input: MAX_BYTES, max_depth: 32, max_nodes: 4096, ..Limits::default() }); - let root = - options.parse(bytes).map_err(|error| crate::i18n::fmt(tl!("Could not read Image Processor settings: {error}"), &[("error", &error.to_string())]))?; + if text.contains('&') { + validate_references(text.strip_prefix('\u{feff}').unwrap_or(text), &options)?; + } + let root = options.parse(bytes).map_err(xml_error)?; if root.name != ROOT || root.attr("xmlns").is_some_and(|ns| !ns.is_empty()) { return Err(tl!("This file does not contain Image Processor settings.").into()); } @@ -152,6 +154,26 @@ pub(super) fn decode(bytes: &[u8]) -> Result { Ok(updates) } +fn xml_error(error: craft_xml::Error) -> String { + crate::i18n::fmt(tl!("Could not read Image Processor settings: {error}"), &[("error", &error.to_string())]) +} + +/// The shared reader accepts bare ampersands even in strict mode. Its event spans let us +/// check the original text and attributes without treating CDATA, comments or instructions as XML text. +fn validate_references(text: &str, options: &Options<'_>) -> Result<(), String> { + let mut events = options.events(text.as_bytes()); + while let Some(event) = events.next().map_err(xml_error)? { + if !matches!(event, Event::Text(_) | Event::Start { .. }) { + continue; + } + let raw = text.get(events.span()).ok_or_else(|| invalid(ROOT))?; + if raw.split('&').skip(1).any(|tail| tail.split_once(';').and_then(|(name, _)| craft_xml::entity(name)).is_none()) { + return Err(tl!("Image Processor settings contain an unescaped ampersand.").into()); + } + } + Ok(()) +} + fn parse_value(kind: Kind, text: Cow<'_, str>, current: bool) -> Option { let trimmed = text.trim(); Some(match kind {