craft-plist: explicit legacy binary minor-version compatibility (#1980)

This commit is contained in:
Dylan M. Taylor authored and GitHub committed 2026-11-16 22:22:29 -05:00
1 parent 08e922c020
commit c153d95e55
3 files changed
+42 -5

No files matched your search

+18 -4
View File
@@ -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<Value, FormatError> {
parse_binary_with_limits(data, Limits::default())
@@ -75,19 +84,24 @@ pub fn parse_binary(data: &[u8]) -> Result<Value, FormatError> {
/// 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<Value, FormatError> {
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<Value, FormatError> {
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<String, FormatError> {
/// 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<Option<String>, 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);
+1 -1
View File
@@ -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.
+23
View File
@@ -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());
}