mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Reject bare ampersands in Image Processor presets
This commit is contained in:
1 parent
2f1f612b56
commit
13924e2591
2 files changed
+44
-6
No files matched your search
@@ -94,6 +94,9 @@ fn invalid_settings_are_rejected() {
|
||||
"<ImageProcessor><tiff>tr<something/>ue</tiff></ImageProcessor>",
|
||||
"<ImageProcessor><icc>false</icc><icc>true</icc></ImageProcessor>",
|
||||
"<ImageProcessor><source>&bad;</source></ImageProcessor>",
|
||||
"<ImageProcessor><dest>A & B</dest></ImageProcessor>",
|
||||
"<ImageProcessor><dest>A &</dest></ImageProcessor>",
|
||||
"<ImageProcessor><future label='A & B'/></ImageProcessor>",
|
||||
"<ImageProcessor/><Another/>",
|
||||
] {
|
||||
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}<ImageProcessor><!-- A & B --><?note A & B?>",
|
||||
"<future label='A & B'/>",
|
||||
"<dest>A & B & C &amp;</dest>",
|
||||
"<source><![CDATA[A & B]]></source></ImageProcessor>",
|
||||
);
|
||||
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<PhotocraftApp>>;
|
||||
type Written = Rc<RefCell<Vec<(String, Vec<u8>)>>>;
|
||||
|
||||
@@ -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"<ImageProcessor><useopen>true</useopen><q>wrong</q></ImageProcessor>").unwrap();
|
||||
std::fs::write(path.path(), b"<ImageProcessor><useopen>true</useopen><dest>A & B</dest></ImageProcessor>").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]
|
||||
|
||||
@@ -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<DialogFields, String> {
|
||||
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<DialogFields, String> {
|
||||
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<FieldValue> {
|
||||
let trimmed = text.trim();
|
||||
Some(match kind {
|
||||
|
||||
Reference in new issue
Block a user