From 0fc19936f8b22a3ee8b120afb3e2bee37cd72773 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 22 Nov 2026 13:49:08 -0500 Subject: [PATCH] android-res: report partial XML at event and nesting limits --- crates/android-res/src/axml.rs | 52 +++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 11 deletions(-) diff --git a/crates/android-res/src/axml.rs b/crates/android-res/src/axml.rs index e76617940..3aea1f7ff 100644 --- a/crates/android-res/src/axml.rs +++ b/crates/android-res/src/axml.rs @@ -313,6 +313,10 @@ pub fn framework_attr(id: u32) -> Option<&'static str> { /// Decode a binary XML file. pub fn parse(data: &[u8]) -> Result { + parse_with_limits(data, MAX_NODES, MAX_DEPTH) +} + +fn parse_with_limits(data: &[u8], max_nodes: usize, max_depth: usize) -> Result { let r = Reader::new(data); let (t, h, size) = chunk(&r, 0).ok_or_else(|| Error::Truncated("binary XML header".into()))?; if t != RES_XML { @@ -342,6 +346,11 @@ pub fn parse(data: &[u8]) -> Result { doc.warnings.push(format!("incomplete XML node at offset {pos}")); break; } + if matches!(ct, XML_START_NS | XML_START_ELEMENT | XML_END_ELEMENT | XML_CDATA) && doc.nodes.len() >= max_nodes { + doc.truncated = true; + doc.warnings.push("XML node limit reached; the remaining events were skipped".into()); + break; + } match ct { RES_STRING_POOL => match StringPool::parse(data, pos) { Ok(p) => { @@ -368,16 +377,15 @@ pub fn parse(data: &[u8]) -> Result { if ns_stack.len() < MAX_DEPTH { ns_stack.push((prefix.clone(), uri.clone())); } - if doc.nodes.len() < MAX_NODES { - doc.nodes.push(Node::Namespace { prefix, uri }); - } + doc.nodes.push(Node::Namespace { prefix, uri }); } else { ns_stack.pop(); } } XML_START_ELEMENT => { - if doc.nodes.len() >= MAX_NODES { + if depth >= max_depth { doc.truncated = true; + doc.warnings.push("XML nesting limit reached; the remaining events were skipped".into()); break; } let line = r.u32_le(pos + 8).unwrap_or(0); @@ -433,20 +441,16 @@ pub fn parse(data: &[u8]) -> Result { }); } doc.nodes.push(Node::Start { depth, name, namespace: ns, line, attributes }); - depth = (depth + 1).min(MAX_DEPTH); + depth += 1; } XML_END_ELEMENT => { depth = depth.saturating_sub(1); let name = r.u32_le(pos + ch + 4).and_then(|i| doc.pool.get(i)).unwrap_or("?").to_string(); - if doc.nodes.len() < MAX_NODES { - doc.nodes.push(Node::End { depth, name }); - } + doc.nodes.push(Node::End { depth, name }); } XML_CDATA => { let text = r.u32_le(pos + ch).and_then(|i| doc.pool.get(i)).unwrap_or("").to_string(); - if doc.nodes.len() < MAX_NODES { - doc.nodes.push(Node::Text { depth, text }); - } + doc.nodes.push(Node::Text { depth, text }); } _ => {} } @@ -750,6 +754,32 @@ mod tests { assert_eq!(pairs, [("package", fx::PACKAGE), ("versionCode", "42"), ("versionName", "1.2.3")]); } + #[test] + fn dropping_a_close_event_marks_the_document_partial() { + let bytes = fx::axml(&[fx::AxNode::Start("root", vec![]), fx::AxNode::End("root")]); + let complete = parse_with_limits(&bytes, 3, 1).unwrap(); + assert!(!complete.truncated); + assert!(matches!(complete.nodes.last(), Some(Node::End { depth: 0, .. }))); + + let partial = parse_with_limits(&bytes, 2, 1).unwrap(); + assert!(partial.truncated); + assert_eq!(partial.nodes, complete.nodes[..2]); + } + + #[test] + fn nesting_limit_keeps_an_exact_prefix_instead_of_saturating_depth() { + let bytes = + fx::axml(&[fx::AxNode::Start("root", vec![]), fx::AxNode::Start("child", vec![]), fx::AxNode::End("child"), fx::AxNode::End("root")]); + let complete = parse_with_limits(&bytes, 5, 2).unwrap(); + assert!(!complete.truncated); + assert!(matches!(complete.nodes[3], Node::End { depth: 1, .. })); + assert!(matches!(complete.nodes[4], Node::End { depth: 0, .. })); + + let partial = parse_with_limits(&bytes, 5, 1).unwrap(); + assert!(partial.truncated); + assert_eq!(partial.nodes, complete.nodes[..2]); + } + #[test] fn values_format() { let p = StringPool { strings: vec!["x".into()], utf8: false, declared: 1 };