From b616d590e862d9a2c63d67aaadb59dcae2e6d054 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Sun, 15 Nov 2026 11:15:32 -0500 Subject: [PATCH 1/8] Port XML property-list reader from Craft Anything Source: storytold/craft-anything at 8f3b775157d331dd31cfa4da911e26d8856b8c77, crates/formats/src/plist.rs and bytes.rs. Preserve original contributor fixes for document boundaries, UTF-16, numeric references, quoted attributes and line endings. Co-authored-by: Afloat Co-authored-by: BFlat Co-authored-by: Rahul Co-authored-by: RRFZ <247457381+Plastic-time@users.noreply.github.com> Co-authored-by: NetterSilver <133535268+Silvertree2010@users.noreply.github.com> --- crates/plist/src/xml.rs | 432 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 432 insertions(+) create mode 100644 crates/plist/src/xml.rs diff --git a/crates/plist/src/xml.rs b/crates/plist/src/xml.rs new file mode 100644 index 000000000..7b41ecc91 --- /dev/null +++ b/crates/plist/src/xml.rs @@ -0,0 +1,432 @@ +//! XML property-list reader ported from Craft Anything at 8f3b7751. +use crate::{DateValue, FormatError, Value}; +const MAX_NODES: usize = 100_000; +const MAX_DEPTH: usize = 64; +fn decode_base64(text: &str, url_safe: bool) -> Option> { + let mut out = Vec::with_capacity(text.len() / 4 * 3); + let mut acc = 0u32; + let mut bits = 0u32; + let mut digits = 0usize; + let mut padding = 0usize; + for c in text.bytes() { + match c { + b' ' | b'\t' | b'\r' | b'\n' => continue, + b'=' => { + padding += 1; + if padding > 2 { + return None; + } + continue; + } + _ if padding != 0 => return None, + _ => {} + } + let value = match c { + b'A'..=b'Z' => c - b'A', + b'a'..=b'z' => c - b'a' + 26, + b'0'..=b'9' => c - b'0' + 52, + b'+' => 62, + b'/' => 63, + b'-' if url_safe => 62, + b'_' if url_safe => 63, + _ => return None, + }; + acc = (acc << 6) | u32::from(value); + bits += 6; + digits += 1; + if bits >= 8 { + bits -= 8; + out.push((acc >> bits) as u8); + acc &= (1 << bits) - 1; + } + } + let remainder = digits % 4; + if remainder == 1 || (padding != 0 && !matches!((remainder, padding), (2, 2) | (3, 1))) { + return None; + } + Some(out) +} + +fn base64_decode(s: &str) -> Option> { decode_base64(s, false) } +#[derive(Debug, Clone, PartialEq)] +enum Tok { + Open(String), + Close(String), + Empty(String), + Text(String), +} + +fn decode_entities(s: &str) -> String { + if !s.contains('&') { + return s.to_string(); + } + let mut out = String::with_capacity(s.len()); + let mut rest = s; + while let Some(p) = rest.find('&') { + out.push_str(rest.get(..p).unwrap_or("")); + let after = rest.get(p..).unwrap_or(""); + let Some(end) = after.find(';').filter(|&e| e <= 12) else { + out.push('&'); + rest = after.get(1..).unwrap_or(""); + continue; + }; + let ent = after.get(1..end).unwrap_or(""); + let rep = match ent { + "lt" => Some('<'), + "gt" => Some('>'), + "amp" => Some('&'), + "quot" => Some('"'), + "apos" => Some('\''), + e if e.starts_with("#x") || e.starts_with("#X") => { + let d = e.get(2..).unwrap_or(""); + if d.bytes().all(|b| b.is_ascii_hexdigit()) { u32::from_str_radix(d, 16).ok().and_then(char::from_u32) } else { None } + } + e if e.starts_with('#') => { + let d = e.get(1..).unwrap_or(""); + if d.bytes().all(|b| b.is_ascii_digit()) { d.parse::().ok().and_then(char::from_u32) } else { None } + } + _ => None, + }; + match rep { + Some(c) => { + out.push(c); + rest = after.get(end + 1..).unwrap_or(""); + } + None => { + out.push('&'); + rest = after.get(1..).unwrap_or(""); + } + } + } + out.push_str(rest); + out +} + +/// Skip a declaration without interpreting or fetching any entity. A DOCTYPE's +/// internal subset can itself contain declarations, quoted delimiters, comments and PIs. +fn after_declaration(mut rest: &str) -> Result<&str, FormatError> { + let mut quote = None; + let mut brackets = 0usize; + while let Some(ch) = rest.chars().next() { + if quote.is_none() { + if let Some(comment) = rest.strip_prefix("").ok_or_else(|| FormatError::Truncated("comment in declaration".into()))?.1; + continue; + } + if let Some(pi) = rest.strip_prefix("").ok_or_else(|| FormatError::Truncated("processing instruction in declaration".into()))?.1; + continue; + } + } + rest = rest.get(ch.len_utf8()..).unwrap_or(""); + match (quote, ch) { + (Some(q), c) if q == c => quote = None, + (None, '\'' | '"') => quote = Some(ch), + (None, '[') => brackets += 1, + (None, ']') => brackets = brackets.saturating_sub(1), + (None, '>') if brackets == 0 => return Ok(rest), + _ => {} + } + } + Err(FormatError::Truncated("declaration".into())) +} + +fn tokenize(text: &str) -> Result, FormatError> { + let mut toks = Vec::new(); + let mut rest = text; + while !rest.is_empty() { + if toks.len() > MAX_NODES { + return Err(FormatError::TooLarge("too many XML tokens".into())); + } + match rest.find('<') { + None => { + if !rest.is_empty() { + toks.push(Tok::Text(decode_entities(rest))); + } + break; + } + Some(p) => { + let t = rest.get(..p).unwrap_or(""); + if !t.is_empty() { + toks.push(Tok::Text(decode_entities(t))); + } + rest = rest.get(p..).unwrap_or(""); + } + } + if let Some(r) = rest.strip_prefix("").ok_or_else(|| FormatError::Truncated("comment".into()))?; + rest = r.get(e + 3..).unwrap_or(""); + continue; + } + if let Some(r) = rest.strip_prefix("").ok_or_else(|| FormatError::Truncated("CDATA".into()))?; + toks.push(Tok::Text(r.get(..e).unwrap_or("").to_string())); + rest = r.get(e + 3..).unwrap_or(""); + continue; + } + if let Some(r) = rest.strip_prefix("").ok_or_else(|| FormatError::Truncated("processing instruction".into()))?; + rest = r.get(end + 2..).unwrap_or(""); + continue; + } + if let Some(declaration) = rest.strip_prefix(" { + quote = None; + false + } + (None, '\'' | '"') => { + quote = Some(ch); + false + } + (None, '>') => true, + _ => false, + }) + .ok_or_else(|| FormatError::Truncated("tag".into()))?; + let inner = rest.get(1..e).unwrap_or("").trim(); + rest = rest.get(e + 1..).unwrap_or(""); + if let Some(name) = inner.strip_prefix('/') { + toks.push(Tok::Close(name.trim().to_string())); + } else if let Some(body) = inner.strip_suffix('/') { + toks.push(Tok::Empty(body.split_whitespace().next().unwrap_or("").to_string())); + } else { + toks.push(Tok::Open(inner.split_whitespace().next().unwrap_or("").to_string())); + } + } + Ok(toks) +} + +struct Xml { + toks: Vec, + pos: usize, + nodes: usize, +} + +impl Xml { + fn next(&mut self) -> Option { + let t = self.toks.get(self.pos).cloned(); + self.pos += 1; + t + } + + fn peek(&self) -> Option<&Tok> { + self.toks.get(self.pos) + } + + /// Text up to the closing tag `name`. + fn text_until(&mut self, name: &str) -> Result { + let mut s = String::new(); + loop { + match self.next() { + Some(Tok::Text(t)) => s.push_str(&t), + Some(Tok::Close(n)) if n == name => return Ok(s), + Some(other) => return Err(FormatError::Malformed(format!("unexpected {other:?} inside <{name}>"))), + None => return Err(FormatError::Truncated(format!("<{name}> is not closed"))), + } + } + } + + fn skip_space(&mut self) { + while matches!(self.peek(), Some(Tok::Text(t)) if t.bytes().all(|b| matches!(b, b' ' | b'\t' | b'\r' | b'\n'))) { + self.pos += 1; + } + } + + fn value(&mut self, depth: usize) -> Result { + self.nodes += 1; + if depth > MAX_DEPTH || self.nodes > MAX_NODES { + return Err(FormatError::TooLarge("plist nesting or size limit".into())); + } + self.skip_space(); + match self.next() { + Some(Tok::Empty(n)) => Ok(match n.as_str() { + "true" => Value::Bool(true), + "false" => Value::Bool(false), + "dict" => Value::Dict(Vec::new()), + "array" => Value::Array(Vec::new()), + "string" => Value::String(String::new()), + "data" => Value::Data(Vec::new()), + other => return Err(FormatError::Malformed(format!("unexpected <{other}/>"))), + }), + Some(Tok::Open(n)) => match n.as_str() { + "string" => Ok(Value::String(self.text_until("string")?)), + "integer" => { + let t = self.text_until("integer")?; + let t = t.trim(); + let v = if let Some(h) = t.strip_prefix("0x").or_else(|| t.strip_prefix("0X")) { + i128::from_str_radix(h, 16).ok() + } else { + t.parse::().ok() + }; + v.map(Value::Integer).ok_or_else(|| FormatError::Malformed(format!("bad integer {t:?}"))) + } + "real" => { + let t = self.text_until("real")?; + t.trim().parse::().map(Value::Real).map_err(|_| FormatError::Malformed(format!("bad real {t:?}"))) + } + "date" => Ok(Value::Date(DateValue::Text(self.text_until("date")?.trim().to_string()))), + "data" => { + let t = self.text_until("data")?; + base64_decode(&t).map(Value::Data).ok_or_else(|| FormatError::Malformed("bad base64 in ".into())) + } + "true" | "false" => { + if !self.text_until(&n)?.is_empty() { + return Err(FormatError::Malformed(format!("<{n}> must be empty"))); + } + Ok(Value::Bool(n == "true")) + } + "array" => { + let mut out = Vec::new(); + loop { + self.skip_space(); + if matches!(self.peek(), Some(Tok::Close(c)) if c == "array") { + self.pos += 1; + return Ok(Value::Array(out)); + } + if self.peek().is_none() { + return Err(FormatError::Truncated(" is not closed".into())); + } + out.push(self.value(depth + 1)?); + } + } + "dict" => { + let mut out = Vec::new(); + loop { + self.skip_space(); + match self.next() { + Some(Tok::Close(c)) if c == "dict" => return Ok(Value::Dict(out)), + Some(Tok::Open(k)) if k == "key" => { + let key = self.text_until("key")?; + let v = self.value(depth + 1)?; + out.push((key, v)); + } + Some(Tok::Empty(k)) if k == "key" => { + let v = self.value(depth + 1)?; + out.push((String::new(), v)); + } + Some(other) => return Err(FormatError::Malformed(format!("expected in , found {other:?}"))), + None => return Err(FormatError::Truncated(" is not closed".into())), + } + } + } + other => Err(FormatError::Malformed(format!("unknown plist element <{other}>"))), + }, + Some(other) => Err(FormatError::Malformed(format!("expected a value, found {other:?}"))), + None => Err(FormatError::Truncated("expected a value".into())), + } + } +} + +/// Decode a UTF-16 XML property list after its byte-order mark into UTF-8 text. +/// Surrogate pairs join normally; a lone surrogate fails as malformed rather +/// than turning into replacement characters that tokenize into garbage element +/// names. +fn decode_utf16_xml(data: &[u8], big_endian: bool) -> Result { + if !data.len().is_multiple_of(2) { + return Err(FormatError::Truncated("UTF-16 code unit".into())); + } + let units: Vec = data.chunks_exact(2).map(|b| if big_endian { u16::from_be_bytes([b[0], b[1]]) } else { u16::from_le_bytes([b[0], b[1]]) }).collect(); + String::from_utf16(&units).map_err(|_| FormatError::Malformed("invalid UTF-16 in XML plist".into())) +} + +/// Parse an XML property list. +pub fn parse_xml(data: &[u8]) -> Result { + // Apple's Core Foundation reader accepts XML plists in UTF-16 as well as + // UTF-8: it recognises both byte-order marks. `from_utf8_lossy` turns a + // UTF-16 byte stream into NUL-separated text, so the encoding is settled + // here, before tokenizing. + let text = match data { + [0xFF, 0xFE, rest @ ..] => decode_utf16_xml(rest, false)?, + [0xFE, 0xFF, rest @ ..] => decode_utf16_xml(rest, true)?, + _ => std::str::from_utf8(data.strip_prefix(b"\xef\xbb\xbf").unwrap_or(data)) + .map_err(|_| FormatError::Malformed("invalid UTF-8 in XML plist".into()))? + .to_owned(), + }; + // XML normalizes literal line endings before parsing; character references such as + // must still decode to a carriage return afterwards. + let text = if text.contains('\r') { text.replace("\r\n", "\n").replace('\r', "\n") } else { text }; + let toks = tokenize(&text)?; + let mut x = Xml { toks, pos: 0, nodes: 0 }; + x.skip_space(); + let wrapped = matches!(x.peek(), Some(Tok::Open(n)) if n == "plist"); + if wrapped { + x.pos += 1; + } + let value = x.value(0)?; + x.skip_space(); + if wrapped { + if !matches!(x.next(), Some(Tok::Close(n)) if n == "plist") { + return Err(FormatError::Malformed("expected after its value".into())); + } + x.skip_space(); + } + if x.peek().is_some() { + return Err(FormatError::Malformed("unexpected content after the plist value".into())); + } + Ok(value) +} + + +#[cfg(test)] +mod tests { +use super::*; + #[test] + fn xml_has_one_complete_root_with_only_markup_space_around_values() { + for invalid in [ + "", + "", + "", + "", + "junk", + "text", + "junk", + "xgarbage", + "no", + "yes", + "", + ] { + assert!(parse_xml(invalid.as_bytes()).is_err(), "{invalid}"); + } + let xml = b" ?>\n "; + assert_eq!(parse_xml(xml).unwrap().get("").and_then(Value::as_str), Some(" ")); + assert_eq!(parse_xml(b" ").unwrap(), Value::Bool(true)); + } + + #[test] + fn xml_rejects_partial_code_units_and_preserves_the_utf8_bom() { + assert_eq!(parse_xml(b"\xef\xbb\xbfok").unwrap().as_str(), Some("ok")); + assert!(parse_xml(b"\xff").is_err()); + for be in [false, true] { + let mut bytes = if be { vec![0xfe, 0xff] } else { vec![0xff, 0xfe] }; + for unit in "".encode_utf16() { + bytes.extend(if be { unit.to_be_bytes() } else { unit.to_le_bytes() }); + } + bytes.push(0); + assert!(parse_xml(&bytes).is_err()); + } + } + + #[test] + fn a_doctype_internal_subset_is_skipped_as_one_declaration() { + let doc = br#"y"> ?>]>ok"#; + assert_eq!(parse_xml(doc).unwrap().as_str(), Some("ok")); + assert!(parse_xml(b"").is_err()); + } + + #[test] + fn xml_char_refs_require_plain_digits() { + let text = |s: &str| { + let doc = format!("{s}"); + parse_xml(doc.as_bytes()).unwrap().as_str().map(str::to_string) + }; + assert_eq!(text("a&#+65;b&#x+41;").as_deref(), Some("a&#+65;b&#x+41;")); + assert_eq!(text("AA").as_deref(), Some("AA")); + assert_eq!(text("&#;&#x;").as_deref(), Some("&#;&#x;")); + } + +} From 4c4ad5d20f03b2d566bab89678712b6eb9231194 Mon Sep 17 00:00:00 2001 From: gmrmad1 Date: Sun, 15 Nov 2026 11:15:51 -0500 Subject: [PATCH 2/8] Reject nonfinite XML property-list reals Port storytold/craft-anything#1905 at d85e104f093a334961048881bf003351e1ac4992. Retain typed-value tests; app JSON projection remains outside this codec. --- crates/plist/src/xml.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/crates/plist/src/xml.rs b/crates/plist/src/xml.rs index 7b41ecc91..54fd9f895 100644 --- a/crates/plist/src/xml.rs +++ b/crates/plist/src/xml.rs @@ -267,7 +267,7 @@ impl Xml { } "real" => { let t = self.text_until("real")?; - t.trim().parse::().map(Value::Real).map_err(|_| FormatError::Malformed(format!("bad real {t:?}"))) + t.trim().parse::().ok().filter(|v| v.is_finite()).map(Value::Real).ok_or_else(|| FormatError::Malformed(format!("bad real {t:?}"))) } "date" => Ok(Value::Date(DateValue::Text(self.text_until("date")?.trim().to_string()))), "data" => { @@ -429,4 +429,16 @@ use super::*; assert_eq!(text("&#;&#x;").as_deref(), Some("&#;&#x;")); } + #[test] + fn xml_real_must_be_finite() { + for bad in ["NaN", "1e999"] { + let doc = format!("k{bad}"); + assert!(parse_xml(doc.as_bytes()).is_err(), "{bad}"); + } + for (text, want) in [("1.5", 1.5), ("-2", -2.0), ("0", 0.0)] { + let doc = format!("k{text}"); + let v = parse_xml(doc.as_bytes()).unwrap_or_else(|e| panic!("{text} should parse: {e}")); + assert_eq!(v.get("k"), Some(&Value::Real(want)), "{text}"); + } + } } From ada2188786fd646443c4fb3326e68e52ce84c90d Mon Sep 17 00:00:00 2001 From: RRFZ <247457381+Plastic-time@users.noreply.github.com> Date: Sun, 15 Nov 2026 11:17:46 -0500 Subject: [PATCH 3/8] Preserve XML quoted-attribute and line-ending regression tests Imported from storytold/craft-anything #1738 and #1726 at 8f3b7751, adapted only to shared parse_xml entry point. --- crates/plist/tests/plist_line_endings.rs | 17 ++++++++++++++ crates/plist/tests/plist_quoted_attributes.rs | 22 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 crates/plist/tests/plist_line_endings.rs create mode 100644 crates/plist/tests/plist_quoted_attributes.rs diff --git a/crates/plist/tests/plist_line_endings.rs b/crates/plist/tests/plist_line_endings.rs new file mode 100644 index 000000000..3f8f82b50 --- /dev/null +++ b/crates/plist/tests/plist_line_endings.rs @@ -0,0 +1,17 @@ +use craft_plist::{self as plist, Value}; + +/// Literal line endings normalize before entities, including keys and CDATA. +#[test] +fn xml_line_endings_follow_xml_rules() { + let xml = "a\rba\r\nb\rc\nd e"; + for encoding in 0..3 { + let bytes = match encoding { + 1 => [vec![0xff, 0xfe], xml.encode_utf16().flat_map(u16::to_le_bytes).collect()].concat(), + 2 => [vec![0xfe, 0xff], xml.encode_utf16().flat_map(u16::to_be_bytes).collect()].concat(), + _ => xml.as_bytes().to_vec(), + }; + let value = plist::parse_xml(&bytes).unwrap(); + assert_eq!(value.get("a\nb").and_then(Value::as_str), Some("a\nb\nc\nd\ref\ng")); + } + assert!(plist::parse_xml(b"a\r\nb").is_err()); +} diff --git a/crates/plist/tests/plist_quoted_attributes.rs b/crates/plist/tests/plist_quoted_attributes.rs new file mode 100644 index 000000000..54cd042ef --- /dev/null +++ b/crates/plist/tests/plist_quoted_attributes.rs @@ -0,0 +1,22 @@ +use craft_plist::{self as plist, Value}; + +/// Quoted delimiters belong to attributes, including on empty elements. +#[test] +fn quoted_tag_delimiters_preserve_values() { + for quote in ['\'', '"'] { + let xml = format!( + "b{quote}>界{quote}>key/{quote}>valueempty/{quote}/>" + ); + let value = plist::parse_xml(xml.as_bytes()).unwrap(); + assert_eq!(value.get("key").and_then(Value::as_str), Some("value")); + assert_eq!(value.get("empty").and_then(Value::as_str), Some("")); + } +} + +/// A quote that never closes must not turn later markup into a value. +#[test] +fn unterminated_attribute_quotes_are_rejected() { + for xml in ["value", "` consumes its encoded text allowance, including whitespace. XML `max_objects` and `max_nodes` both cap values. + +The attributed Craft Anything reader includes its complete-document, declaration-skipping (no external entity fetching), quoted-tag and normalized-line-ending behavior. Nonfinite reals are refused using contributor PR #1905. Unknown or malformed character references retain legacy literal behavior; scanning is bounded before searching for a terminator. Base64 retains that reader's legacy optional-padding/ASCII-whitespace behavior, including unused trailing bits; it is not a signature/key-validation API. No writer, JSON projection, keyed-archive interpretation or app policy is added. + +XML source: Craft Anything `8f3b775157d331dd31cfa4da911e26d8856b8c77`, plus #1905 `d85e104f093a334961048881bf003351e1ac4992`. The import credits Brandon Thomas and the existing hardening contributors; #1905 is authored by gmrmad1 and the quoted-tag/line-ending tests by RRFZ. Consumer cutovers require their own policy/parity tests, especially LightCraft's preset mapping and Craft Anything's projections. diff --git a/crates/plist/src/lib.rs b/crates/plist/src/lib.rs index f3af79ddc..2547e73d8 100644 --- a/crates/plist/src/lib.rs +++ b/crates/plist/src/lib.rs @@ -1,10 +1,12 @@ -//! Bounded binary Apple property lists, independent of operating-system preferences. -//! XML and writing are intentionally not implemented in this first extraction. +//! Bounded binary and XML Apple property lists, independent of operating-system preferences. +//! Writing and application-specific projections remain with callers. //! File discovery, locale selection, bundle validation and application policy stay in callers. #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] mod binary; mod bytes; +mod xml; pub use binary::{DateValue, Value, first_string_in_array, parse_binary, parse_binary_with_limits}; +pub use xml::{parse_xml, parse_xml_with_limits}; /// Caller-selected resource limits. Zero values refuse the corresponding resource. #[derive(Clone, Copy, Debug)] diff --git a/crates/plist/src/xml.rs b/crates/plist/src/xml.rs index 54fd9f895..52f5c4586 100644 --- a/crates/plist/src/xml.rs +++ b/crates/plist/src/xml.rs @@ -1,7 +1,5 @@ //! XML property-list reader ported from Craft Anything at 8f3b7751. -use crate::{DateValue, FormatError, Value}; -const MAX_NODES: usize = 100_000; -const MAX_DEPTH: usize = 64; +use crate::{DateValue, FormatError, Limits, Value}; fn decode_base64(text: &str, url_safe: bool) -> Option> { let mut out = Vec::with_capacity(text.len() / 4 * 3); let mut acc = 0u32; @@ -47,7 +45,9 @@ fn decode_base64(text: &str, url_safe: bool) -> Option> { Some(out) } -fn base64_decode(s: &str) -> Option> { decode_base64(s, false) } +fn base64_decode(s: &str) -> Option> { + decode_base64(s, false) +} #[derive(Debug, Clone, PartialEq)] enum Tok { Open(String), @@ -65,7 +65,7 @@ fn decode_entities(s: &str) -> String { while let Some(p) = rest.find('&') { out.push_str(rest.get(..p).unwrap_or("")); let after = rest.get(p..).unwrap_or(""); - let Some(end) = after.find(';').filter(|&e| e <= 12) else { + let Some(end) = after.as_bytes().iter().take(13).position(|b| *b == b';') else { out.push('&'); rest = after.get(1..).unwrap_or(""); continue; @@ -131,11 +131,11 @@ fn after_declaration(mut rest: &str) -> Result<&str, FormatError> { Err(FormatError::Truncated("declaration".into())) } -fn tokenize(text: &str) -> Result, FormatError> { +fn tokenize(text: &str, max_tokens: usize) -> Result, FormatError> { let mut toks = Vec::new(); let mut rest = text; while !rest.is_empty() { - if toks.len() > MAX_NODES { + if toks.len() >= max_tokens { return Err(FormatError::TooLarge("too many XML tokens".into())); } match rest.find('<') { @@ -198,6 +198,9 @@ fn tokenize(text: &str) -> Result, FormatError> { toks.push(Tok::Open(inner.split_whitespace().next().unwrap_or("").to_string())); } } + if toks.len() > max_tokens { + return Err(FormatError::TooLarge("too many XML tokens".into())); + } Ok(toks) } @@ -205,6 +208,8 @@ struct Xml { toks: Vec, pos: usize, nodes: usize, + limits: Limits, + bytes_left: usize, } impl Xml { @@ -223,7 +228,10 @@ impl Xml { let mut s = String::new(); loop { match self.next() { - Some(Tok::Text(t)) => s.push_str(&t), + Some(Tok::Text(t)) => { + self.bytes_left = self.bytes_left.checked_sub(t.len()).ok_or_else(|| FormatError::TooLarge("XML text/data bytes".into()))?; + s.push_str(&t); + } Some(Tok::Close(n)) if n == name => return Ok(s), Some(other) => return Err(FormatError::Malformed(format!("unexpected {other:?} inside <{name}>"))), None => return Err(FormatError::Truncated(format!("<{name}> is not closed"))), @@ -239,7 +247,7 @@ impl Xml { fn value(&mut self, depth: usize) -> Result { self.nodes += 1; - if depth > MAX_DEPTH || self.nodes > MAX_NODES { + if depth > self.limits.max_depth.min(256) || self.nodes > self.limits.max_nodes.min(self.limits.max_objects) { return Err(FormatError::TooLarge("plist nesting or size limit".into())); } self.skip_space(); @@ -267,7 +275,12 @@ impl Xml { } "real" => { let t = self.text_until("real")?; - t.trim().parse::().ok().filter(|v| v.is_finite()).map(Value::Real).ok_or_else(|| FormatError::Malformed(format!("bad real {t:?}"))) + t.trim() + .parse::() + .ok() + .filter(|v| v.is_finite()) + .map(Value::Real) + .ok_or_else(|| FormatError::Malformed(format!("bad real {t:?}"))) } "date" => Ok(Value::Date(DateValue::Text(self.text_until("date")?.trim().to_string()))), "data" => { @@ -330,12 +343,24 @@ fn decode_utf16_xml(data: &[u8], big_endian: bool) -> Result = data.chunks_exact(2).map(|b| if big_endian { u16::from_be_bytes([b[0], b[1]]) } else { u16::from_le_bytes([b[0], b[1]]) }).collect(); + let units: Vec = data.as_chunks::<2>().0.iter().map(|b| if big_endian { u16::from_be_bytes(*b) } else { u16::from_le_bytes(*b) }).collect(); String::from_utf16(&units).map_err(|_| FormatError::Malformed("invalid UTF-16 in XML plist".into())) } /// Parse an XML property list. pub fn parse_xml(data: &[u8]) -> Result { + parse_xml_with_limits(data, Limits::default()) +} + +/// Parse UTF-8 or BOM-tagged UTF-16 XML with explicit resource allowances. +/// Input and decoded XML text are bounded before tokenization; token count, value count, +/// nesting and cumulative leaf/key text are bounded separately. `max_objects` and +/// `max_nodes` both cap values. Nesting never exceeds 256 even with a larger caller limit. +/// Data text consumes its encoded size, conservatively including whitespace. +pub fn parse_xml_with_limits(data: &[u8], limits: Limits) -> Result { + if data.len() > limits.max_input_bytes { + return Err(FormatError::TooLarge("input bytes".into())); + } // Apple's Core Foundation reader accepts XML plists in UTF-16 as well as // UTF-8: it recognises both byte-order marks. `from_utf8_lossy` turns a // UTF-16 byte stream into NUL-separated text, so the encoding is settled @@ -347,11 +372,14 @@ pub fn parse_xml(data: &[u8]) -> Result { .map_err(|_| FormatError::Malformed("invalid UTF-8 in XML plist".into()))? .to_owned(), }; + if text.len() > limits.max_expanded_bytes { + return Err(FormatError::TooLarge("decoded XML bytes".into())); + } // XML normalizes literal line endings before parsing; character references such as // must still decode to a carriage return afterwards. let text = if text.contains('\r') { text.replace("\r\n", "\n").replace('\r', "\n") } else { text }; - let toks = tokenize(&text)?; - let mut x = Xml { toks, pos: 0, nodes: 0 }; + let toks = tokenize(&text, limits.max_nodes.min(limits.max_objects).saturating_mul(8).saturating_add(8))?; + let mut x = Xml { toks, pos: 0, nodes: 0, limits, bytes_left: limits.max_expanded_bytes }; x.skip_space(); let wrapped = matches!(x.peek(), Some(Tok::Open(n)) if n == "plist"); if wrapped { @@ -371,10 +399,9 @@ pub fn parse_xml(data: &[u8]) -> Result { Ok(value) } - #[cfg(test)] mod tests { -use super::*; + use super::*; #[test] fn xml_has_one_complete_root_with_only_markup_space_around_values() { for invalid in [ @@ -441,4 +468,38 @@ use super::*; assert_eq!(v.get("k"), Some(&Value::Real(want)), "{text}"); } } + + #[test] + fn malformed_entity_scan_is_bounded_and_preserves_text() { + for n in [4096, 65536, 262144] { + let text = "&".repeat(n); + assert_eq!(decode_entities(&text), text); + assert_eq!(decode_entities(&(text.clone() + ";")), text + ";"); + } + assert_eq!(decode_entities("é&missing; & A"), "é&missing; & \r A"); + } + #[test] + fn caller_limits_refuse_before_unbounded_tree_growth() { + let limits = Limits { max_input_bytes: 3, ..Limits::default() }; + assert!(matches!(parse_xml_with_limits(b"", limits), Err(FormatError::TooLarge(_)))); + let limits = Limits { max_nodes: 1, ..Limits::default() }; + assert!(matches!(parse_xml_with_limits(b"", limits), Err(FormatError::TooLarge(_)))); + let limits = Limits { max_depth: 0, ..Limits::default() }; + assert!(matches!(parse_xml_with_limits(b"", limits), Err(FormatError::TooLarge(_)))); + let limits = Limits { max_expanded_bytes: 8, ..Limits::default() }; + assert!(matches!(parse_xml_with_limits(b"abc", limits), Err(FormatError::TooLarge(_)))); + assert!(parse_xml_with_limits(b"", Limits::default()).is_ok()); + } + #[test] + fn data_preserves_existing_whitespace_padding_and_alphabet_rules() { + for (s, expected) in [(" Z g == ", Some(vec![102])), ("Zg", Some(vec![102])), ("Zg=", None), ("Zg==junk", None), ("_w==", None)] { + let xml = format!("{s}"); + assert_eq!(parse_xml(xml.as_bytes()).ok(), expected.map(Value::Data)); + } + } + #[test] + fn small_node_allowance_accepts_one_dictionary_entry() { + let limits = Limits { max_nodes: 2, max_objects: 2, ..Limits::default() }; + assert!(parse_xml_with_limits(b"kv", limits).is_ok()); + } } From f53ed0c505008412554322fda6d5edba79d9f860 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 11:18:38 -0500 Subject: [PATCH 5/8] Scope imported XML test assertions to test-only lint policy --- crates/plist/src/xml.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/plist/src/xml.rs b/crates/plist/src/xml.rs index 52f5c4586..c6cc4d022 100644 --- a/crates/plist/src/xml.rs +++ b/crates/plist/src/xml.rs @@ -401,6 +401,7 @@ pub fn parse_xml_with_limits(data: &[u8], limits: Limits) -> Result Date: Sun, 15 Nov 2026 11:21:36 -0500 Subject: [PATCH 6/8] Preserve LightCraft zero-padded entities with linear scanning Reuse LightCraft f786b87533e58c6d31c7f9f2897814904448f39a entity_end implementation and regression. XML numeric references are not arbitrarily length limited. --- crates/plist/README.md | 2 +- crates/plist/src/xml.rs | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/crates/plist/README.md b/crates/plist/README.md index 5621ef96a..a7adf6c79 100644 --- a/crates/plist/README.md +++ b/crates/plist/README.md @@ -14,6 +14,6 @@ Source: `storytold/craft-anything` at `88d1d9cac852aec1c7fd97886db451e005e49d2a` `parse_xml` and `parse_xml_with_limits` now read UTF-8 and BOM-tagged UTF-16 XML into the same `Value` tree. XML input/decoded-text size, tokens, values, nesting and cumulative key/leaf text are bounded. The independent token ceiling is eight times the smaller node/object allowance plus eight; unusually fragmented text can exhaust it before the value limit. This is not a precise RSS limit. The decoded-XML limit also includes markup; `` consumes its encoded text allowance, including whitespace. XML `max_objects` and `max_nodes` both cap values. -The attributed Craft Anything reader includes its complete-document, declaration-skipping (no external entity fetching), quoted-tag and normalized-line-ending behavior. Nonfinite reals are refused using contributor PR #1905. Unknown or malformed character references retain legacy literal behavior; scanning is bounded before searching for a terminator. Base64 retains that reader's legacy optional-padding/ASCII-whitespace behavior, including unused trailing bits; it is not a signature/key-validation API. No writer, JSON projection, keyed-archive interpretation or app policy is added. +The attributed Craft Anything reader includes its complete-document, declaration-skipping (no external entity fetching), quoted-tag and normalized-line-ending behavior. Nonfinite reals are refused using contributor PR #1905. Unknown or malformed character references retain legacy literal behavior; scanning stops at the next non-entity byte, preserving LightCraft's valid zero-padded numeric references without suffix rescans. Base64 retains that reader's legacy optional-padding/ASCII-whitespace behavior, including unused trailing bits; it is not a signature/key-validation API. No writer, JSON projection, keyed-archive interpretation or app policy is added. XML source: Craft Anything `8f3b775157d331dd31cfa4da911e26d8856b8c77`, plus #1905 `d85e104f093a334961048881bf003351e1ac4992`. The import credits Brandon Thomas and the existing hardening contributors; #1905 is authored by gmrmad1 and the quoted-tag/line-ending tests by RRFZ. Consumer cutovers require their own policy/parity tests, especially LightCraft's preset mapping and Craft Anything's projections. diff --git a/crates/plist/src/xml.rs b/crates/plist/src/xml.rs index c6cc4d022..be225d3fb 100644 --- a/crates/plist/src/xml.rs +++ b/crates/plist/src/xml.rs @@ -56,6 +56,12 @@ enum Tok { Text(String), } +// Ported from LightCraft f786b875: scan one entity-name run, never the remaining document. +fn entity_end(rest: &str) -> Option { + let n = rest.bytes().skip(1).take_while(|b| b.is_ascii_alphanumeric() || *b == b'#').count(); + (rest.as_bytes().get(n + 1) == Some(&b';')).then_some(n + 1) +} + fn decode_entities(s: &str) -> String { if !s.contains('&') { return s.to_string(); @@ -65,7 +71,7 @@ fn decode_entities(s: &str) -> String { while let Some(p) = rest.find('&') { out.push_str(rest.get(..p).unwrap_or("")); let after = rest.get(p..).unwrap_or(""); - let Some(end) = after.as_bytes().iter().take(13).position(|b| *b == b';') else { + let Some(end) = entity_end(after) else { out.push('&'); rest = after.get(1..).unwrap_or(""); continue; @@ -478,6 +484,7 @@ mod tests { assert_eq!(decode_entities(&(text.clone() + ";")), text + ";"); } assert_eq!(decode_entities("é&missing; & A"), "é&missing; & \r A"); + assert_eq!(decode_entities("Canon A"), "Canon A"); } #[test] fn caller_limits_refuse_before_unbounded_tree_growth() { From 0dab3133a2a58d321ade446f23592e7855b79e78 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 11:24:10 -0500 Subject: [PATCH 7/8] Allow consumers to retain opaque XML date text --- crates/plist/src/lib.rs | 2 +- crates/plist/src/xml.rs | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/crates/plist/src/lib.rs b/crates/plist/src/lib.rs index 2547e73d8..d668de277 100644 --- a/crates/plist/src/lib.rs +++ b/crates/plist/src/lib.rs @@ -6,7 +6,7 @@ mod binary; mod bytes; mod xml; pub use binary::{DateValue, Value, first_string_in_array, parse_binary, parse_binary_with_limits}; -pub use xml::{parse_xml, parse_xml_with_limits}; +pub use xml::{XmlOptions, parse_xml, parse_xml_with_limits, parse_xml_with_options}; /// Caller-selected resource limits. Zero values refuse the corresponding resource. #[derive(Clone, Copy, Debug)] diff --git a/crates/plist/src/xml.rs b/crates/plist/src/xml.rs index be225d3fb..d30b938d0 100644 --- a/crates/plist/src/xml.rs +++ b/crates/plist/src/xml.rs @@ -1,5 +1,18 @@ //! XML property-list reader ported from Craft Anything at 8f3b7751. use crate::{DateValue, FormatError, Limits, Value}; + +/// XML-specific representation policy, separate from resource allowances. +#[derive(Clone, Copy, Debug)] +pub struct XmlOptions { + /// Trim surrounding whitespace in date text, as the original Craft Anything reader does. + /// Disable for consumers that retain dates as opaque strings, such as Luminar presets. + pub trim_date_text: bool, +} +impl Default for XmlOptions { + fn default() -> Self { + Self { trim_date_text: true } + } +} fn decode_base64(text: &str, url_safe: bool) -> Option> { let mut out = Vec::with_capacity(text.len() / 4 * 3); let mut acc = 0u32; @@ -216,6 +229,7 @@ struct Xml { nodes: usize, limits: Limits, bytes_left: usize, + options: XmlOptions, } impl Xml { @@ -288,7 +302,10 @@ impl Xml { .map(Value::Real) .ok_or_else(|| FormatError::Malformed(format!("bad real {t:?}"))) } - "date" => Ok(Value::Date(DateValue::Text(self.text_until("date")?.trim().to_string()))), + "date" => { + let text = self.text_until("date")?; + Ok(Value::Date(DateValue::Text(if self.options.trim_date_text { text.trim().to_string() } else { text }))) + } "data" => { let t = self.text_until("data")?; base64_decode(&t).map(Value::Data).ok_or_else(|| FormatError::Malformed("bad base64 in ".into())) @@ -364,6 +381,11 @@ pub fn parse_xml(data: &[u8]) -> Result { /// `max_nodes` both cap values. Nesting never exceeds 256 even with a larger caller limit. /// Data text consumes its encoded size, conservatively including whitespace. pub fn parse_xml_with_limits(data: &[u8], limits: Limits) -> Result { + parse_xml_with_options(data, limits, XmlOptions::default()) +} + +/// Parse bounded XML using an explicit date-text representation policy. +pub fn parse_xml_with_options(data: &[u8], limits: Limits, options: XmlOptions) -> Result { if data.len() > limits.max_input_bytes { return Err(FormatError::TooLarge("input bytes".into())); } @@ -385,7 +407,7 @@ pub fn parse_xml_with_limits(data: &[u8], limits: Limits) -> Resultkv", limits).is_ok()); } + #[test] + fn callers_can_preserve_opaque_date_whitespace() { + let xml = b" 2026-01-01T00:00:00Z "; + assert_eq!(parse_xml(xml).unwrap(), Value::Date(DateValue::Text("2026-01-01T00:00:00Z".into()))); + assert_eq!( + parse_xml_with_options(xml, Limits::default(), XmlOptions { trim_date_text: false }).unwrap(), + Value::Date(DateValue::Text(" 2026-01-01T00:00:00Z ".into())) + ); + } } From 902a722422583d051093679985793fb94b2af26e Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 13:02:56 -0500 Subject: [PATCH 8/8] Allow independent XML plist token budgets --- crates/plist/src/lib.rs | 2 +- crates/plist/src/xml.rs | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/crates/plist/src/lib.rs b/crates/plist/src/lib.rs index d668de277..d84980398 100644 --- a/crates/plist/src/lib.rs +++ b/crates/plist/src/lib.rs @@ -6,7 +6,7 @@ mod binary; mod bytes; mod xml; pub use binary::{DateValue, Value, first_string_in_array, parse_binary, parse_binary_with_limits}; -pub use xml::{XmlOptions, parse_xml, parse_xml_with_limits, parse_xml_with_options}; +pub use xml::{XmlOptions, parse_xml, parse_xml_with_limits, parse_xml_with_options, parse_xml_with_token_limit}; /// Caller-selected resource limits. Zero values refuse the corresponding resource. #[derive(Clone, Copy, Debug)] diff --git a/crates/plist/src/xml.rs b/crates/plist/src/xml.rs index d30b938d0..f4eee291d 100644 --- a/crates/plist/src/xml.rs +++ b/crates/plist/src/xml.rs @@ -386,6 +386,14 @@ pub fn parse_xml_with_limits(data: &[u8], limits: Limits) -> Result Result { + let max_tokens = limits.max_nodes.min(limits.max_objects).saturating_mul(8).saturating_add(8); + parse_xml_with_token_limit(data, limits, options, max_tokens) +} + +/// Parse XML with an independent token allowance, without reducing the value allowance. +/// This bounds token-heavy input (for example many CDATA chunks in one string). +/// Zero tokens refuses nonempty XML. Existing convenience APIs retain their default budget. +pub fn parse_xml_with_token_limit(data: &[u8], limits: Limits, options: XmlOptions, max_tokens: usize) -> Result { if data.len() > limits.max_input_bytes { return Err(FormatError::TooLarge("input bytes".into())); } @@ -406,7 +414,7 @@ pub fn parse_xml_with_options(data: &[u8], limits: Limits, options: XmlOptions) // XML normalizes literal line endings before parsing; character references such as // must still decode to a carriage return afterwards. let text = if text.contains('\r') { text.replace("\r\n", "\n").replace('\r', "\n") } else { text }; - let toks = tokenize(&text, limits.max_nodes.min(limits.max_objects).saturating_mul(8).saturating_add(8))?; + let toks = tokenize(&text, max_tokens)?; let mut x = Xml { toks, pos: 0, nodes: 0, limits, bytes_left: limits.max_expanded_bytes, options }; x.skip_space(); let wrapped = matches!(x.peek(), Some(Tok::Open(n)) if n == "plist"); @@ -431,6 +439,15 @@ pub fn parse_xml_with_options(data: &[u8], limits: Limits, options: XmlOptions) mod tests { #![allow(clippy::unwrap_used, clippy::panic)] use super::*; + #[test] + fn explicit_token_budget_is_independent_of_value_budget() { + let limits = Limits { max_nodes: 100, max_objects: 100, ..Limits::default() }; + let xml = b""; + assert!(matches!(parse_xml_with_token_limit(xml, limits, XmlOptions::default(), 3), Err(FormatError::TooLarge(_)))); + assert_eq!(parse_xml_with_token_limit(xml, limits, XmlOptions::default(), 4).unwrap(), Value::String("ab".into())); + assert!(matches!(parse_xml_with_token_limit(b"", limits, XmlOptions::default(), 0), Err(FormatError::TooLarge(_)))); + } + #[test] fn xml_has_one_complete_root_with_only_markup_space_around_values() { for invalid in [