From c153d95e5587c26655ba543f6315dc7fadf2adcf Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 16 Nov 2026 22:22:29 -0500 Subject: [PATCH] craft-plist: explicit legacy binary minor-version compatibility (#1980) --- crates/plist/src/binary.rs | 22 ++++++++++++++++++---- crates/plist/src/lib.rs | 2 +- crates/plist/tests/plist_marker_widths.rs | 23 +++++++++++++++++++++++ 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/crates/plist/src/binary.rs b/crates/plist/src/binary.rs index 3fe6aed92..241c83ba3 100644 --- a/crates/plist/src/binary.rs +++ b/crates/plist/src/binary.rs @@ -68,6 +68,15 @@ struct Bin<'a> { limits: Limits, } +/// Explicit legacy version-admission policy. Structural validation is never relaxed. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct BinaryOptions { + /// Accept any eighth header byte after `bplist0`, matching historical inspection readers. + /// This is not support for a new binary encoding: all objects still obey the bplist00 + /// grammar and bounds. The default accepts only the exact `bplist00` header. + pub allow_unknown_minor_version: bool, +} + /// Parse a `bplist00` binary property list. pub fn parse_binary(data: &[u8]) -> Result { parse_binary_with_limits(data, Limits::default()) @@ -75,19 +84,24 @@ pub fn parse_binary(data: &[u8]) -> Result { /// Parse a binary property list, with caller-selected input, object, expansion and nesting limits. pub fn parse_binary_with_limits(data: &[u8], limits: Limits) -> Result { - let (mut b, top) = reader(data, limits)?; + parse_binary_with_options(data, limits, BinaryOptions::default()) +} + +/// Parse with explicit version admission and the same structural/resource checks as the strict reader. +pub fn parse_binary_with_options(data: &[u8], limits: Limits, options: BinaryOptions) -> Result { + let (mut b, top) = reader(data, limits, options)?; let mut stack = reserved(limits.max_depth.min(256))?; b.object(top, &mut stack) } -fn reader(data: &[u8], limits: Limits) -> Result<(Bin<'_>, usize), FormatError> { +fn reader(data: &[u8], limits: Limits, options: BinaryOptions) -> Result<(Bin<'_>, usize), FormatError> { if data.len() > limits.max_input_bytes { return Err(FormatError::TooLarge("input bytes".into())); } if !data.starts_with(b"bplist") { return Err(FormatError::Malformed("not a binary plist".into())); } - if !data.starts_with(b"bplist00") { + if !data.starts_with(if options.allow_unknown_minor_version { b"bplist0".as_slice() } else { b"bplist00".as_slice() }) { return Err(FormatError::Unsupported("unknown binary plist version".into())); } let len = data.len(); @@ -344,7 +358,7 @@ fn reserved_string(n: usize) -> Result { /// selected objects and resource exhaustion return errors. Unselected value graphs are not /// validated. This permits preference readers to ignore unrelated future object kinds. pub fn first_string_in_array(data: &[u8], key: &str, limits: Limits) -> Result, FormatError> { - let (mut b, top) = reader(data, limits)?; + let (mut b, top) = reader(data, limits, BinaryOptions::default())?; let (kind, n, start) = b.header(top)?; if kind != 0xd { return Ok(None); diff --git a/crates/plist/src/lib.rs b/crates/plist/src/lib.rs index d84980398..826bbe4b2 100644 --- a/crates/plist/src/lib.rs +++ b/crates/plist/src/lib.rs @@ -5,7 +5,7 @@ mod binary; mod bytes; mod xml; -pub use binary::{DateValue, Value, first_string_in_array, parse_binary, parse_binary_with_limits}; +pub use binary::{BinaryOptions, DateValue, Value, first_string_in_array, parse_binary, parse_binary_with_limits, parse_binary_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. diff --git a/crates/plist/tests/plist_marker_widths.rs b/crates/plist/tests/plist_marker_widths.rs index 5873371e7..d636c0703 100644 --- a/crates/plist/tests/plist_marker_widths.rs +++ b/crates/plist/tests/plist_marker_widths.rs @@ -45,3 +45,26 @@ fn supported_maximum_widths_keep_their_values() { empty.extend_from_slice(&0u64.to_be_bytes()); assert!(matches!(parse_binary(&single_object(&empty)), Ok(Value::Data(d)) if d.is_empty())); } + +#[test] +fn minor_version_compatibility_is_explicit_and_keeps_bounds() { + use craft_plist::{BinaryOptions, FormatError, Limits, parse_binary_with_options}; + let compat = BinaryOptions { allow_unknown_minor_version: true }; + for minor in [b'0', b'1', b'9', 0xff] { + let mut data = single_object(&[0x00]); + data[7] = minor; + assert!(matches!(parse_binary_with_options(&data, Limits::default(), compat), Ok(Value::Null))); + assert_eq!(parse_binary(&data).is_ok(), minor == b'0'); + let limited = Limits { max_nodes: 0, ..Limits::default() }; + assert!(matches!(parse_binary_with_options(&data, limited, compat), Err(FormatError::TooLarge(_)))); + data[6] = b'1'; + assert!(matches!(parse_binary_with_options(&data, Limits::default(), compat), Err(FormatError::Unsupported(_)))); + } + let mut invalid = single_object(&[0x15]); + invalid[7] = b'1'; + assert!(matches!(parse_binary_with_options(&invalid, Limits::default(), compat), Err(FormatError::Malformed(_)))); + let mut cycle = single_object(&[0xa1, 0]); + cycle[7] = b'1'; + assert!(matches!(parse_binary_with_options(&cycle, Limits::default(), compat), Err(FormatError::Malformed(_)))); + assert!(parse_binary_with_options(b"bplist01", Limits::default(), compat).is_err()); +}