From f78b0ac48439354229827c50c25a50fc19c71ea8 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 11:18:07 -0500 Subject: [PATCH] Expose bounded XML plist parsing and cap malformed entity scans --- ATTRIBUTION.md | 2 + crates/plist/Cargo.toml | 2 +- crates/plist/README.md | 12 +++++- crates/plist/src/lib.rs | 6 ++- crates/plist/src/xml.rs | 91 ++++++++++++++++++++++++++++++++++------- 5 files changed, 93 insertions(+), 20 deletions(-) diff --git a/ATTRIBUTION.md b/ATTRIBUTION.md index ef3b98f31..43d543062 100644 --- a/ATTRIBUTION.md +++ b/ATTRIBUTION.md @@ -100,3 +100,5 @@ and the exact transfer revision are recorded in each vendored crate's SOURCE.jso ## Binary Apple property lists `crates/plist` is extracted from `storytold/craft-anything` (`crates/formats/src/plist.rs` and checked byte reads), at source main `88d1d9cac852aec1c7fd97886db451e005e49d2a`. MIT OR Apache-2.0; source license texts are retained in the crate. Primary import Git authors preserve Brandon Thomas, BFlat, RRFZ and Afloat. Regression imports preserve BFlat. XML, JSON projection, calendar formatting and app policy were not imported into the public API. + +The XML reader in `crates/plist/src/xml.rs` and its base64 compatibility helper are ported from Craft Anything `8f3b775157d331dd31cfa4da911e26d8856b8c77` (same MIT OR Apache-2.0 source licenses). Brandon Thomas is the primary import author; retained hardening credits include Afloat, BFlat, Rahul, RRFZ and NetterSilver. The finite-real fix preserves gmrmad1 as author from Craft Anything #1905 (`d85e104f`); quoted-attribute/line-ending regressions preserve RRFZ. Bounds/adaptation and bounded entity scanning are subsequent Dylan M. Taylor changes. App-specific JSON projections and keyed-archive interpretation remain outside this crate. diff --git a/crates/plist/Cargo.toml b/crates/plist/Cargo.toml index 7ae95aae0..7ffaf3480 100644 --- a/crates/plist/Cargo.toml +++ b/crates/plist/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "craft-plist" -description = "Bounded cross-platform binary Apple property-list reader" +description = "Bounded cross-platform binary and XML Apple property-list reader" version.workspace = true edition.workspace = true license.workspace = true diff --git a/crates/plist/README.md b/crates/plist/README.md index dc5c52007..5621ef96a 100644 --- a/crates/plist/README.md +++ b/crates/plist/README.md @@ -4,8 +4,16 @@ A cross-platform bounded binary (`bplist00`) reader, extracted from Craft Anythi `parse_binary_with_limits` accepts explicit input, object, expanded-node, nesting and decoded-byte limits. Recursion always has a hard ceiling of 256 even if a caller supplies a larger depth. Counts are checked before container reservation; allocations can return a resource error. Limits are per parse, not a promise of a precise process RSS cap. -This first PR contains reading only. XML parsing/writing and keyed-archive interpretation are not implemented. JSON projection, date formatting, OS file access and preference/locale selection are not part of this crate. Existing updater/Photo streaming `plist` users remain unchanged. +This crate contains reading only. Writing and keyed-archive interpretation are not implemented. JSON projection, date formatting, OS file access and preference/locale selection are not part of this crate. Existing updater/Photo streaming `plist` users remain unchanged. -Initial consumers: PhotoCraft's AppleLanguages binary-reader path; later Craft Anything binary parsing and LightCraft XML presets (after XML support/parity). Photo must retain its 4 MiB input policy and first-language selection. +Initial consumers: PhotoCraft's AppleLanguages binary-reader path; later Craft Anything binary parsing and LightCraft XML presets (after consumer parity). Photo must retain its 4 MiB input policy and first-language selection. Source: `storytold/craft-anything` at `88d1d9cac852aec1c7fd97886db451e005e49d2a`, `crates/formats/src/plist.rs` and checked byte reader. Source license files retained. Imported changes retain Brandon Thomas, BFlat, RRFZ and Afloat as primary Git authors; imported regression tests are BFlat's. No external specification implementation copied. + +## XML reader + +`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. + +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()); + } }