diff --git a/moli-parser/src/lib.rs b/moli-parser/src/lib.rs index 073772c1b17..c4ed0ff51c4 100644 --- a/moli-parser/src/lib.rs +++ b/moli-parser/src/lib.rs @@ -41,7 +41,7 @@ pub use script_planning::{ PreparedImportMapSource, PreparedScript, ScriptFetchMetadata, ScriptFilterSkipReason, ScriptSource, build_prepared_import_map, build_prepared_script, classify_parser_script, }; -pub use xml::XmlParser; +pub use xml::{ParsedXmlFragment, XmlFragmentParseError, XmlParser}; pub use xml_stream::XmlDocumentStream; const STREAM_CHUNK_BYTES: usize = 8 * 1024; diff --git a/moli-parser/src/xml.rs b/moli-parser/src/xml.rs index 89cc8b60c7b..b3668387b73 100644 --- a/moli-parser/src/xml.rs +++ b/moli-parser/src/xml.rs @@ -26,6 +26,12 @@ use moli_dom::native::{ Attribute as NativeAttribute, DomHost, NativeDom, NativeNodeId, Node, ParserConstruction, }; +mod fragment; +pub use fragment::{ParsedXmlFragment, XmlFragmentParseError}; + +const XML_NAMESPACE: &str = "http://www.w3.org/XML/1998/namespace"; +const XMLNS_NAMESPACE: &str = "http://www.w3.org/2000/xmlns/"; + #[derive(Debug, Clone, Default)] pub struct XmlParser; @@ -168,12 +174,15 @@ impl XmlParser { } } +type XmlNamespaceBindings = HashMap>; + fn xml_element_stack_error(xml: &str) -> Option { let mut pending_start_name = None; let mut pending_used_prefixes = Vec::new(); let mut pending_declared_prefixes = Vec::new(); + let mut pending_attributes = Vec::new(); let mut open_elements = Vec::new(); - let mut active_prefixes = HashMap::from([("xml".to_owned(), 1usize)]); + let mut active_prefixes = HashMap::from([("xml".to_owned(), vec![XML_NAMESPACE.to_owned()])]); for token in XmlTokenizer::from(xml) { match token { @@ -191,53 +200,61 @@ fn xml_element_stack_error(xml: &str) -> Option { .. }) => { let prefix = prefix.as_str(); - if prefix == "xmlns" { - if !value.as_str().is_empty() { - pending_declared_prefixes.push(local.as_str().to_owned()); + if prefix == "xmlns" || (prefix.is_empty() && local.as_str() == "xmlns") { + let Some(namespace) = xml_namespace_attribute_value(value.as_str()) else { + return Some(XmlPrepassError::namespace("Invalid XML namespace value")); + }; + let declared_prefix = if prefix == "xmlns" { + local.as_str() + } else { + "" + }; + if !xml_namespace_declaration_is_valid(declared_prefix, &namespace) { + return Some(XmlPrepassError::namespace( + "Invalid XML namespace declaration", + )); } + pending_declared_prefixes.push((declared_prefix.to_owned(), namespace)); } else if !prefix.is_empty() { pending_used_prefixes.push(prefix.to_owned()); + pending_attributes.push((prefix.to_owned(), local.as_str().to_owned())); } } Ok(XmlTokenizerToken::ElementEnd { end, .. }) => match end { - XmlElementEnd::Open => { + end @ (XmlElementEnd::Open | XmlElementEnd::Empty) => { + let is_open = matches!(end, XmlElementEnd::Open); let Some(name) = pending_start_name.take() else { - return Some(XmlPrepassError::tree("XML start tag is missing a name")); + return Some(XmlPrepassError::tree(if is_open { + "XML start tag is missing a name" + } else { + "XML empty-element tag is missing a name" + })); }; - for prefix in &pending_declared_prefixes { - *active_prefixes.entry(prefix.clone()).or_default() += 1; + for (prefix, namespace) in &pending_declared_prefixes { + active_prefixes + .entry(prefix.clone()) + .or_default() + .push(namespace.clone()); } - if let Some(prefix) = pending_used_prefixes - .iter() - .find(|prefix| !active_prefixes.contains_key(*prefix)) - { - return Some(XmlPrepassError::namespace(format!( - "XML namespace prefix `{prefix}` is not declared" - ))); + if let Some(error) = xml_start_namespace_error( + &active_prefixes, + &pending_used_prefixes, + &pending_attributes, + ) { + return Some(error); } pending_used_prefixes.clear(); - open_elements.push((name, std::mem::take(&mut pending_declared_prefixes))); - } - XmlElementEnd::Empty => { - if pending_start_name.take().is_none() { - return Some(XmlPrepassError::tree( - "XML empty-element tag is missing a name", - )); - } - for prefix in &pending_declared_prefixes { - *active_prefixes.entry(prefix.clone()).or_default() += 1; - } - if let Some(prefix) = pending_used_prefixes - .iter() - .find(|prefix| !active_prefixes.contains_key(*prefix)) - { - return Some(XmlPrepassError::namespace(format!( - "XML namespace prefix `{prefix}` is not declared" - ))); - } - pending_used_prefixes.clear(); - for prefix in pending_declared_prefixes.drain(..) { - decrement_xml_prefix_count(&mut active_prefixes, &prefix); + pending_attributes.clear(); + if is_open { + let prefixes = pending_declared_prefixes + .drain(..) + .map(|(prefix, _)| prefix) + .collect::>(); + open_elements.push((name, prefixes)); + } else { + for (prefix, _) in pending_declared_prefixes.drain(..) { + pop_xml_namespace_binding(&mut active_prefixes, &prefix); + } } } XmlElementEnd::Close(prefix, local) => { @@ -253,7 +270,7 @@ fn xml_element_stack_error(xml: &str) -> Option { ))); } for prefix in declared_prefixes { - decrement_xml_prefix_count(&mut active_prefixes, &prefix); + pop_xml_namespace_binding(&mut active_prefixes, &prefix); } } }, @@ -273,16 +290,86 @@ fn xml_element_stack_error(xml: &str) -> Option { } } -fn decrement_xml_prefix_count(active_prefixes: &mut HashMap, prefix: &str) { - let Some(count) = active_prefixes.get_mut(prefix) else { - return; - }; - *count -= 1; - if *count == 0 { - active_prefixes.remove(prefix); +fn xml_namespace_declaration_is_valid(prefix: &str, namespace: &str) -> bool { + match prefix { + "xmlns" => false, + "xml" => namespace == XML_NAMESPACE, + _ => { + namespace != XML_NAMESPACE + && namespace != XMLNS_NAMESPACE + && (prefix.is_empty() || !namespace.is_empty()) + } } } +fn xml_start_namespace_error( + namespaces: &XmlNamespaceBindings, + used_prefixes: &[String], + attributes: &[(String, String)], +) -> Option { + for prefix in used_prefixes { + if namespaces + .get(prefix) + .and_then(|values| values.last()) + .is_none_or(String::is_empty) + { + return Some(XmlPrepassError::namespace(format!( + "XML namespace prefix `{prefix}` is not declared" + ))); + } + } + // xml5ever drops attributes with duplicate expanded names without an + // error. Validate before that recovery loses their original identities. + let mut expanded_names = std::collections::HashSet::new(); + for (prefix, local) in attributes { + let namespace = namespaces + .get(prefix) + .and_then(|values| values.last()) + .map(String::as_str) + .unwrap_or(""); + if !expanded_names.insert((namespace, local)) { + return Some(XmlPrepassError::namespace(format!( + "Duplicate XML attribute {{{namespace}}}{local}" + ))); + } + } + None +} + +fn pop_xml_namespace_binding(namespaces: &mut XmlNamespaceBindings, prefix: &str) { + if let Some(values) = namespaces.get_mut(prefix) { + values.pop(); + if values.is_empty() { + namespaces.remove(prefix); + } + } +} + +fn xml_namespace_attribute_value(mut input: &str) -> Option { + let mut value = String::new(); + while let Some(character) = input.chars().next() { + if character == '&' { + let mut stream = xmlparser::Stream::from(input); + let xmlparser::Reference::Char(character) = stream.consume_reference().ok()? else { + return None; + }; + value.push(character); + input = &input[stream.pos()..]; + } else { + input = &input[character.len_utf8()..]; + match character { + '\r' => { + input = input.strip_prefix('\n').unwrap_or(input); + value.push(' '); + } + '\n' | '\t' => value.push(' '), + _ => value.push(character), + } + } + } + Some(value) +} + fn qualified_xml_name(prefix: &str, local_name: &str) -> String { if prefix.is_empty() { local_name.to_owned() diff --git a/moli-parser/src/xml/fragment.rs b/moli-parser/src/xml/fragment.rs new file mode 100644 index 00000000000..dea9c688a4d --- /dev/null +++ b/moli-parser/src/xml/fragment.rs @@ -0,0 +1,453 @@ +use std::{collections::BTreeMap, error::Error, fmt}; + +use moli_dom::native::{DomHost, DomStringValue, NativeDom, NativeNodeId, Node}; + +use super::{XML_NAMESPACE, XMLNS_NAMESPACE, XmlParser}; + +#[derive(Debug)] +pub struct XmlFragmentParseError(String); + +impl fmt::Display for XmlFragmentParseError { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(formatter) + } +} + +impl Error for XmlFragmentParseError {} + +/// A validated XML fragment. The context wrapper is private and is never +/// imported into the receiving Document. +#[derive(Debug)] +pub struct ParsedXmlFragment { + document: NativeDom, + children: Vec, +} + +impl ParsedXmlFragment { + pub fn document(&self) -> &NativeDom { + &self.document + } + + pub fn children(&self) -> &[NativeNodeId] { + &self.children + } +} + +impl XmlParser { + /// Parse an XML fragment without mutating its context Document. The XML + /// Standard's context wrapper carries namespace bindings, never its DTD. + pub fn parse_fragment( + &self, + host: &DomHost, + context: NativeNodeId, + markup: &DomStringValue, + ) -> Result { + let element = host + .node(context) + .and_then(Node::as_element) + .ok_or_else(|| XmlFragmentParseError("XML context must be an Element.".into()))?; + let document = host + .owner_document_handle(context) + .and_then(|document| host.node(document)) + .and_then(Node::as_document) + .ok_or_else(|| XmlFragmentParseError("XML context has no Document.".into()))?; + let markup = markup.as_str().ok_or_else(|| { + XmlFragmentParseError("XML markup contains an unpaired surrogate.".into()) + })?; + let name = match element.prefix().filter(|prefix| !prefix.is_empty()) { + Some(prefix) => format!("{prefix}:{}", element.local_name()), + None => element.local_name().to_owned(), + }; + let mut input = String::new(); + input.push('<'); + input.push_str(&name); + for (prefix, value) in namespaces_in_scope(host, context) { + let value = value.as_str().ok_or_else(|| { + XmlFragmentParseError("XML namespace contains an unpaired surrogate.".into()) + })?; + if prefix.is_some() && value.is_empty() { + // An empty declaration masks ancestors, but lookupNamespaceURI + // returns null, so this prefix is not declared in the wrapper. + continue; + } + input.push_str(" xmlns"); + if let Some(prefix) = prefix { + input.push(':'); + input.push_str(&prefix); + } + input.push_str("=\""); + append_namespace_value(&mut input, value); + input.push('"'); + } + input.push('>'); + input.push_str(markup); + input.push_str("'); + + let parsed = self.parse(document.url().clone(), input); + if let Some(error) = parsed.parse_errors().first() { + return Err(XmlFragmentParseError(error.clone())); + } + let mut parsed = DomHost::from_dom(parsed); + let root = { + let mut document_children = parsed.child_handles(parsed.document_handle()); + let root = document_children + .next() + .filter(|root| parsed.node(*root).is_some_and(Node::is_element)) + .ok_or_else(|| { + XmlFragmentParseError("XML fragment has no context element.".into()) + })?; + // A context name containing "--" can let a trailing comment swallow + // the generated end tag. XML well-formedness alone does not catch it. + if document_children.next().is_some() { + return Err(XmlFragmentParseError( + "XML fragment escapes its context element.".into(), + )); + } + root + }; + let content = parsed + .node(root) + .and_then(Node::as_element) + .and_then(|element| element.template_contents()) + .unwrap_or(root); + let children = parsed.child_handles(content).collect(); + let scripts = parsed + .nodes() + .iter() + .filter(|node| node.is_script_element()) + .map(Node::id) + .collect::>(); + for script in scripts { + // XML fragments stay inert even when subsequently adopted into + // an HTML Document, including scripts inside template contents. + parsed.set_script_already_started(script, true); + } + Ok(ParsedXmlFragment { + document: parsed.into_dom(), + children, + }) + } +} + +fn namespaces_in_scope( + host: &DomHost, + context: NativeNodeId, +) -> BTreeMap, DomStringValue> { + let mut namespaces = BTreeMap::from([(Some("xml".into()), XML_NAMESPACE.into())]); + let mut current = Some(context); + while let Some(node) = current.and_then(|handle| host.node(handle)) { + let Some(element) = node.as_element() else { + break; + }; + if !element.namespace().is_empty() { + namespaces + .entry( + element + .prefix() + .filter(|prefix| !prefix.is_empty()) + .map(str::to_owned), + ) + .or_insert_with(|| element.namespace().into()); + } + for attribute in element.attributes() { + if attribute.namespace() != XMLNS_NAMESPACE { + continue; + } + let prefix = match attribute.prefix().filter(|prefix| !prefix.is_empty()) { + Some("xmlns") if attribute.local_name() != "xmlns" => { + Some(attribute.local_name().to_owned()) + } + None if attribute.local_name() == "xmlns" => None, + _ => continue, + }; + // Keep an empty declaration: it masks an ancestor binding. + namespaces.entry(prefix).or_insert_with(|| { + element + .attribute_ns_utf16_units(XMLNS_NAMESPACE, attribute.local_name()) + .map(DomStringValue::from_utf16) + .unwrap_or_else(|| attribute.value().into()) + }); + } + current = node.parent_node(); + } + namespaces +} + +fn append_namespace_value(output: &mut String, value: &str) { + for character in value.chars() { + output.push_str(match character { + '&' => "&", + '<' => "<", + '>' => ">", + '"' => """, + '\t' => " ", + '\n' => " ", + '\r' => " ", + _ => { + output.push(character); + continue; + } + }); + } +} + +#[cfg(test)] +mod tests { + use super::*; + use moli_dom::native::NodeType; + use url::Url; + + fn context(source: &str) -> (DomHost, NativeNodeId) { + let document = XmlParser.parse( + Url::parse("https://xml-fragment.test/context.xml").unwrap(), + source.into(), + ); + assert!( + document.parse_errors().is_empty(), + "{:?}", + document.parse_errors() + ); + let host = DomHost::from_dom(document); + let context = host + .nodes() + .iter() + .find(|node| node.local_name() == Some("Context")) + .map(Node::id) + .unwrap_or_else(|| host.dom().document_element_node_id().unwrap()); + (host, context) + } + + #[test] + fn xml_fragments_preserve_namespaces_case_and_xml_node_kinds() { + let (host, context) = context( + "", + ); + let parsed = XmlParser + .parse_fragment( + &host, + context, + &"tail".into(), + ) + .unwrap(); + let nodes = parsed + .children() + .iter() + .map(|handle| parsed.document().node(*handle).unwrap()) + .collect::>(); + assert_eq!( + nodes + .iter() + .map(|node| node.node_type()) + .collect::>(), + [ + NodeType::Element, + NodeType::Element, + NodeType::Text, + NodeType::CDataSection, + NodeType::Comment, + NodeType::ProcessingInstruction + ] + ); + assert_eq!(nodes[0].local_name(), Some("Mixed")); + assert_eq!(nodes[0].namespace(), Some("urn:outer")); + assert_eq!( + nodes[0] + .as_element() + .unwrap() + .attribute_ns("urn:local", "Token"), + Some("v") + ); + assert_eq!(nodes[1].namespace(), Some("urn:local")); + assert_eq!(nodes[1].prefix(), Some("p")); + assert_eq!(nodes[3].node_value(), Some("a"); + let failures = [ + "", + "", + "&unknown;", + "", + "", + "", + "", + "", + "\0", + "\u{b}", + "\u{fffe}", + "�", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + ] + .into_iter() + .filter(|markup| { + XmlParser + .parse_fragment(&host, context, &(*markup).into()) + .is_ok() + }) + .collect::>(); + assert!(failures.is_empty(), "accepted malformed XML: {failures:?}"); + for units in [vec![0xd800], vec![0xdc00]] { + assert!( + XmlParser + .parse_fragment(&host, context, &DomStringValue::from_utf16(&units)) + .is_err() + ); + } + let (host, context) = self::context(""); + assert!( + XmlParser + .parse_fragment(&host, context, &"x'); + return nodes.length === 6 && nodes.map(node => node.nodeType).join() === '1,1,3,4,8,7' && + nodes[0].localName === 'MixedCase' && nodes[0].namespaceURI === 'urn:outer' && + nodes[0].getAttributeNS('urn:local', 'Token') === 'v' && nodes[1].localName === 'Item' && + nodes[1].prefix === 'p' && nodes[1].namespaceURI === 'urn:local' && + nodes[2].data === 'tail' && nodes[3].data === 'a node.ownerDocument === doc) && + (!fragment || Object.getPrototypeOf(fragment) === owner.DocumentFragment.prototype); + }); + sample('context prefix wins', 'valid', ({invoke}) => { + const {nodes} = invoke(''); + return nodes[0].namespaceURI === 'urn:context' && nodes[0].prefix === 'p'; + }, {prefixed: true}); + sample('default namespace reset', 'valid', ({invoke}) => { + const {nodes} = invoke(''); return nodes[0].namespaceURI === null && nodes[0].localName === 'Item'; + }, {reset: true}); + sample('namespace declaration escaping', 'valid', ({invoke}) => { + const {nodes} = invoke(''); return nodes[0].namespaceURI === 'urn:a&b"ce'; + }, {prefixUri: 'urn:a&b"ce'}); + sample('predefined and numeric entities', 'valid', ({invoke}) => { + const {nodes} = invoke('<&"'>🦊'); + return nodes.length === 1 && nodes[0].data === '<&\"\'>🦊'; + }); + sample('empty markup', 'valid', ({invoke}) => invoke('').nodes.length === 0); + sample('conversion changes namespace', 'conversion', ({invoke}, context) => { + let conversions = 0; + const {nodes} = invoke({toString() {conversions++; context.setAttributeNS(N, 'xmlns:q', 'urn:changed'); return '';}}); + return conversions === 1 && nodes[0].namespaceURI === 'urn:changed'; + }); + sample('conversion exception identity', 'conversion', ({invoke}) => { + const sentinel = {}; let conversions = 0; + try {invoke({toString() {conversions++; throw sentinel;}}); return false;} + catch (error) {return error === sentinel && conversions === 1;} + }); + sample('native context ignores public getters', 'native', ({invoke}, context) => { + const names = ['nodeType', 'localName', 'namespaceURI', 'prefix', 'parentNode', 'ownerDocument', 'attributes', 'lookupNamespaceURI']; + let reads = 0; + for (const name of names) Object.defineProperty(context, name, {configurable: true, get() {reads++; throw Error('public '+name);}}); + try {const {nodes} = invoke(''); return reads === 0 && nodes[0].namespaceURI === 'urn:local';} + finally {for (const name of names) delete context[name];} + }); + for (const [name, markup] of [ + ['unclosed', ''], ['mismatched', ''], ['unknown entity', '&unknown;'], + ['duplicate attribute', ''], + ['duplicate expanded attribute', ''], + ['unquoted attribute', ''], ['xml declaration', ''], + ['doctype', ''], ['nul', '\u0000'], ['vertical tab', '\u000b'], + ['high surrogate', '\ud800'], ['low surrogate', '\udc00'], ['noncharacter', '\ufffe'], + ['invalid character reference', '�'], ['root escape', ''] + ]) sample('reject '+name, 'malformed', sample => sample.rejects(markup)); + sample('reject swallowed context end tag', 'malformed', sample => sample.rejects('x