mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 22:14:30 +00:00
Merge pull request #1633 from storytold/extract/plist-xml
Share bounded XML property-list reader from Craft Anything
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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
|
||||
|
||||
+10
-2
@@ -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; `<data>` 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 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.
|
||||
|
||||
@@ -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::{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)]
|
||||
|
||||
@@ -0,0 +1,561 @@
|
||||
//! 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<Vec<u8>> {
|
||||
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<Vec<u8>> {
|
||||
decode_base64(s, false)
|
||||
}
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
enum Tok {
|
||||
Open(String),
|
||||
Close(String),
|
||||
Empty(String),
|
||||
Text(String),
|
||||
}
|
||||
|
||||
// Ported from LightCraft f786b875: scan one entity-name run, never the remaining document.
|
||||
fn entity_end(rest: &str) -> Option<usize> {
|
||||
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();
|
||||
}
|
||||
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) = entity_end(after) 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::<u32>().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("<!--") {
|
||||
rest = comment.split_once("-->").ok_or_else(|| FormatError::Truncated("comment in declaration".into()))?.1;
|
||||
continue;
|
||||
}
|
||||
if let Some(pi) = rest.strip_prefix("<?") {
|
||||
rest = pi.split_once("?>").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, max_tokens: usize) -> Result<Vec<Tok>, FormatError> {
|
||||
let mut toks = Vec::new();
|
||||
let mut rest = text;
|
||||
while !rest.is_empty() {
|
||||
if toks.len() >= max_tokens {
|
||||
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("<!--") {
|
||||
let e = r.find("-->").ok_or_else(|| FormatError::Truncated("comment".into()))?;
|
||||
rest = r.get(e + 3..).unwrap_or("");
|
||||
continue;
|
||||
}
|
||||
if let Some(r) = rest.strip_prefix("<![CDATA[") {
|
||||
let e = r.find("]]>").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("<?") {
|
||||
let end = r.find("?>").ok_or_else(|| FormatError::Truncated("processing instruction".into()))?;
|
||||
rest = r.get(end + 2..).unwrap_or("");
|
||||
continue;
|
||||
}
|
||||
if let Some(declaration) = rest.strip_prefix("<!") {
|
||||
rest = after_declaration(declaration)?;
|
||||
continue;
|
||||
}
|
||||
let mut quote = None;
|
||||
let e = rest
|
||||
.find(|ch| match (quote, ch) {
|
||||
(Some(q), c) if q == c => {
|
||||
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()));
|
||||
}
|
||||
}
|
||||
if toks.len() > max_tokens {
|
||||
return Err(FormatError::TooLarge("too many XML tokens".into()));
|
||||
}
|
||||
Ok(toks)
|
||||
}
|
||||
|
||||
struct Xml {
|
||||
toks: Vec<Tok>,
|
||||
pos: usize,
|
||||
nodes: usize,
|
||||
limits: Limits,
|
||||
bytes_left: usize,
|
||||
options: XmlOptions,
|
||||
}
|
||||
|
||||
impl Xml {
|
||||
fn next(&mut self) -> Option<Tok> {
|
||||
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<String, FormatError> {
|
||||
let mut s = String::new();
|
||||
loop {
|
||||
match self.next() {
|
||||
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"))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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<Value, FormatError> {
|
||||
self.nodes += 1;
|
||||
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();
|
||||
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::<i128>().ok()
|
||||
};
|
||||
v.map(Value::Integer).ok_or_else(|| FormatError::Malformed(format!("bad integer {t:?}")))
|
||||
}
|
||||
"real" => {
|
||||
let t = self.text_until("real")?;
|
||||
t.trim()
|
||||
.parse::<f64>()
|
||||
.ok()
|
||||
.filter(|v| v.is_finite())
|
||||
.map(Value::Real)
|
||||
.ok_or_else(|| FormatError::Malformed(format!("bad real {t:?}")))
|
||||
}
|
||||
"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 <data>".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("<array> 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 <key> in <dict>, found {other:?}"))),
|
||||
None => return Err(FormatError::Truncated("<dict> 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<String, FormatError> {
|
||||
if !data.len().is_multiple_of(2) {
|
||||
return Err(FormatError::Truncated("UTF-16 code unit".into()));
|
||||
}
|
||||
let units: Vec<u16> = 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<Value, FormatError> {
|
||||
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<Value, FormatError> {
|
||||
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<Value, FormatError> {
|
||||
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<Value, FormatError> {
|
||||
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
|
||||
// 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(),
|
||||
};
|
||||
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, 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");
|
||||
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 </plist> 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 {
|
||||
#![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"<string><![CDATA[a]]><![CDATA[b]]></string>";
|
||||
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"<true/>", limits, XmlOptions::default(), 0), Err(FormatError::TooLarge(_))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn xml_has_one_complete_root_with_only_markup_space_around_values() {
|
||||
for invalid in [
|
||||
"<plist><true/>",
|
||||
"<plist><true/></dict>",
|
||||
"<plist><true/><false/></plist>",
|
||||
"<plist><true/></plist><dict/>",
|
||||
"<false/>junk",
|
||||
"text<true/>",
|
||||
"<array>junk<true/></array>",
|
||||
"<dict><key>x</key>garbage<true/></dict>",
|
||||
"<true>no</true>",
|
||||
"<false>yes</false>",
|
||||
"<array><key/></array>",
|
||||
] {
|
||||
assert!(parse_xml(invalid.as_bytes()).is_err(), "{invalid}");
|
||||
}
|
||||
let xml = b"<?example content > <false/> ?>\n<plist><dict><key/><string> </string></dict></plist><!-- end -->";
|
||||
assert_eq!(parse_xml(xml).unwrap().get("").and_then(Value::as_str), Some(" "));
|
||||
assert_eq!(parse_xml(b" <true></true> ").unwrap(), Value::Bool(true));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn xml_rejects_partial_code_units_and_preserves_the_utf8_bom() {
|
||||
assert_eq!(parse_xml(b"\xef\xbb\xbf<string>ok</string>").unwrap().as_str(), Some("ok"));
|
||||
assert!(parse_xml(b"<string>\xff</string>").is_err());
|
||||
for be in [false, true] {
|
||||
let mut bytes = if be { vec![0xfe, 0xff] } else { vec![0xff, 0xfe] };
|
||||
for unit in "<true/>".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#"<!DOCTYPE plist [<!ELEMENT plist (string)><!ENTITY text "x]>y"><!-- ]> --><?note ]> ?>]><plist><string>ok</string></plist>"#;
|
||||
assert_eq!(parse_xml(doc).unwrap().as_str(), Some("ok"));
|
||||
assert!(parse_xml(b"<!DOCTYPE plist [<!ELEMENT plist ANY><plist><true/></plist>").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn xml_char_refs_require_plain_digits() {
|
||||
let text = |s: &str| {
|
||||
let doc = format!("<plist version=\"1.0\"><string>{s}</string></plist>");
|
||||
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;"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn xml_real_must_be_finite() {
|
||||
for bad in ["NaN", "1e999"] {
|
||||
let doc = format!("<dict><key>k</key><real>{bad}</real></dict>");
|
||||
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!("<dict><key>k</key><real>{text}</real></dict>");
|
||||
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}");
|
||||
}
|
||||
}
|
||||
|
||||
#[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");
|
||||
assert_eq!(decode_entities("Canon A"), "Canon 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"<true/>", limits), Err(FormatError::TooLarge(_))));
|
||||
let limits = Limits { max_nodes: 1, ..Limits::default() };
|
||||
assert!(matches!(parse_xml_with_limits(b"<array><true/></array>", limits), Err(FormatError::TooLarge(_))));
|
||||
let limits = Limits { max_depth: 0, ..Limits::default() };
|
||||
assert!(matches!(parse_xml_with_limits(b"<array><true/></array>", limits), Err(FormatError::TooLarge(_))));
|
||||
let limits = Limits { max_expanded_bytes: 8, ..Limits::default() };
|
||||
assert!(matches!(parse_xml_with_limits(b"<string>abc</string>", limits), Err(FormatError::TooLarge(_))));
|
||||
assert!(parse_xml_with_limits(b"<true/>", 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!("<data>{s}</data>");
|
||||
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"<dict><key>k</key><string>v</string></dict>", limits).is_ok());
|
||||
}
|
||||
#[test]
|
||||
fn callers_can_preserve_opaque_date_whitespace() {
|
||||
let xml = b"<date> 2026-01-01T00:00:00Z </date>";
|
||||
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()))
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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 = "<plist><dict><key>a\rb</key><string>a\r\nb\rc\nd e<![CDATA[f\r\ng]]></string></dict></plist>";
|
||||
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"<plist><string>a\r\nb</dict></plist>").is_err());
|
||||
}
|
||||
@@ -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!(
|
||||
"<plist note={quote}a>b{quote}><dict><key note={quote}/>界{quote}>key</key><string note={quote}>/{quote}>value</string><key>empty</key><string note={quote}>/{quote}/></dict></plist>"
|
||||
);
|
||||
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 ["<plist><string note=\"oops>value</string></plist>", "<plist><string note='oops>value</string></plist>"] {
|
||||
assert!(plist::parse_xml(xml.as_bytes()).is_err(), "{xml}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user