Merge pull request #1684 from storytold/codex/xml-strict-attributes

Reject duplicate attributes in strict XML reads
This commit is contained in:
FredApps authored and GitHub committed 2026-11-16 01:15:55 +01:00
commit 4444eaa13c
3 files changed
+99 -6

No files matched your search

+3 -3
View File
@@ -46,7 +46,7 @@ assert_eq!((rows, sheet.map(|s| s.children.len())), (3, Ok(0)));
| `max_input` | 256 MiB | `TooLarge` |
| `max_depth` | 256 levels (the root is 1) | `TooDeep` |
| `max_nodes` | 2,000,000 nodes kept in the tree: elements, text stretches, kept CDATA, instructions and comments | `TooManyNodes` |
| `max_attrs` | 1024 on one start tag, namespace declarations and malformed ones included; with canonical names, also the declarations in scope at once | `TooManyAttrs` |
| `max_attrs` | 1024 on one start tag, namespace declarations and malformed ones included; with canonical names or strict reads, also the declarations in scope at once | `TooManyAttrs` |
| `max_attrs_total` | 4,000,000 attributes kept in the tree, over all its elements | `TooManyAttrsTotal` |
A limit exceeded is an error, never a panic and never a truncated tree. `parse_streaming_partial`
@@ -73,9 +73,9 @@ A lenient read keeps what a damaged document had:
- reading stops when the root element ends: nothing after it is looked at;
- a malformed attribute is skipped; bytes that are not UTF-8 become U+FFFD.
A strict read makes each of those an error, and also a second root element and text outside the
A strict read rejects duplicate raw or expanded attribute names (while allowing undeclared prefixes in fragments). It makes each of those an error, and also a second root element and text outside the
root, in the pull reader as in the tree. Markup the tokenizer cannot read (`<a b='1`, an unterminated comment) is an error either
way. A repeated attribute is kept each time in both.
way. A lenient read keeps repeated attributes.
### Text
+53 -2
View File
@@ -1,6 +1,7 @@
//! Reading: the pull reader over `quick-xml`, the tree builder on top of it, and streaming.
use std::borrow::Cow;
use std::collections::BTreeSet;
use std::ops::Range;
use quick_xml::Reader;
@@ -12,6 +13,10 @@ use crate::write::is_xml_char;
/// The namespace the `xml` prefix is always bound to.
pub const XML_NS: &str = "http://www.w3.org/XML/1998/namespace";
/// [`Events::scope_ids`] id of the XML namespace, bound to `xml:` without a declaration.
const XML_NS_ID: usize = usize::MAX;
/// Expanded-name id of an attribute in no namespace (or with an undeclared prefix).
const NO_NS_ID: usize = usize::MAX - 1;
/// What the reader refuses to exceed.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -469,6 +474,10 @@ pub struct Events<'o, 'i> {
end_names: bool,
open: Vec<Open>,
scope: Vec<(String, String)>,
/// For each binding in `scope`, an id shared by every binding of an equal URI (the index of
/// the first such binding, or [`XML_NS_ID`]), with the URI's hash. Strict reads compare
/// expanded attribute names by this id instead of copying the URI for each attribute.
scope_ids: Vec<(u64, usize)>,
/// The end of an empty element, due after its start.
pending: bool,
/// Whether a root element has started.
@@ -500,7 +509,7 @@ impl<'o, 'i> Events<'o, 'i> {
cfg.trim_text_start = false;
cfg.trim_text_end = false;
cfg.expand_empty_elements = false;
Events { reader, opts, end_names, open: Vec::new(), scope: Vec::new(), pending: false, rooted: false, span: 0..0 }
Events { reader, opts, end_names, open: Vec::new(), scope: Vec::new(), scope_ids: Vec::new(), pending: false, rooted: false, span: 0..0 }
}
/// The bytes of the input the last event was read from. Both events of an empty element
@@ -614,6 +623,7 @@ impl<'o, 'i> Events<'o, 'i> {
fn end(&mut self) -> Event {
let (scope, name) = self.open.pop().map_or((0, String::new()), |o| (o.scope, o.name));
self.scope.truncate(scope);
self.scope_ids.truncate(scope);
Event::End { name }
}
@@ -669,6 +679,7 @@ impl<'o, 'i> Events<'o, 'i> {
if is_ns_decl(key) {
let prefix = self.decode(key.get(6..).unwrap_or_default())?.into_owned();
let uri = self.attr_value(&a.value)?;
self.scope_ids.push(self.uri_id(&uri));
self.scope.push((prefix, uri));
}
}
@@ -680,6 +691,20 @@ impl<'o, 'i> Events<'o, 'i> {
Ok(())
}
/// The id an expanded name uses for `uri`: one hash and at most one full comparison per
/// declaration, so a long URI costs its length once however many attributes use it.
fn uri_id(&self, uri: &str) -> (u64, usize) {
use std::hash::{Hash, Hasher};
let mut h = std::collections::hash_map::DefaultHasher::new();
uri.hash(&mut h);
let hash = h.finish();
if uri == XML_NS {
return (hash, XML_NS_ID);
}
let first = self.scope_ids.iter().zip(&self.scope).position(|((h, _), (_, u))| *h == hash && u == uri);
(hash, first.map_or(self.scope.len(), |i| self.scope_ids.get(i).map_or(i, |(_, id)| *id)))
}
/// In a strict read: is the reader before or after the root element?
fn outside_root(&self) -> bool {
self.opts.strict && self.open.is_empty()
@@ -699,7 +724,7 @@ impl<'o, 'i> Events<'o, 'i> {
_ => None,
};
let scope = self.scope.len();
if canonical.is_some() {
if canonical.is_some() || self.opts.strict {
self.declare(e)?;
}
let raw = e.name();
@@ -707,6 +732,8 @@ impl<'o, 'i> Events<'o, 'i> {
let mut out = Vec::new();
let mut attrs = e.attributes();
attrs.with_checks(false);
let mut raw_names = BTreeSet::new();
let mut expanded_names = BTreeSet::new();
for (seen, a) in attrs.enumerate() {
if seen >= limits.max_attrs {
return Err(Error::TooManyAttrs(limits.max_attrs));
@@ -718,6 +745,30 @@ impl<'o, 'i> Events<'o, 'i> {
};
let key = a.key.as_ref();
let decl = is_ns_decl(key);
if self.opts.strict {
let raw_key = self.decode(key)?;
if !raw_names.insert(raw_key.to_string()) {
return self.syntax(format!("duplicate attribute `{raw_key}`"));
}
if !decl {
// Default namespaces do not apply to attributes. Keep undeclared prefixes
// distinct so standalone fragments retain the reader's existing behaviour.
let expanded = match raw_key.split_once(':') {
Some(("xml", local)) => (XML_NS_ID, local),
Some((prefix, local)) => self
.scope
.iter()
.zip(&self.scope_ids)
.rev()
.find(|((p, _), _)| p == prefix)
.map_or((NO_NS_ID, raw_key.as_ref()), |(_, (_, id))| (*id, local)),
None => (NO_NS_ID, raw_key.as_ref()),
};
if !expanded_names.insert((expanded.0, expanded.1.to_string())) {
return self.syntax(format!("duplicate expanded attribute `{raw_key}`"));
}
}
}
if decl && !self.opts.ns_decls {
continue;
}
+43 -1
View File
@@ -117,7 +117,11 @@ fn an_attribute_flood_is_refused_at_the_limit() {
let opts = opts.with_limits(limits);
for (k, kind) in kinds.iter().enumerate() {
let _ = opts.parse(tag(16, kind).as_bytes());
assert_eq!(opts.parse(tag(17, kind).as_bytes()).err().map(|e| e.is_limit() || (opts.strict && k == 3)), Some(true), "kind {k}");
assert_eq!(
opts.parse(tag(17, kind).as_bytes()).err().map(|e| e.is_limit() || (opts.strict && (k == 1 || k == 3))),
Some(true),
"kind {k}"
);
assert!(opts.first_start(tag(17, kind).as_bytes(), "a").is_none());
}
assert_eq!(opts.parse(tag(17, kinds[0]).as_bytes()), Err(Error::TooManyAttrs(16)));
@@ -524,3 +528,41 @@ fn a_strict_pull_reader_refuses_what_is_outside_the_root() {
// white space, comments and instructions may stand outside the root
assert!(read(strict, b"<?xml version='1.0'?>\r\n <!-- c -->\t<a>x</a>\n<?pi?> ").is_ok());
}
#[test]
fn strict_attributes_are_unique_before_name_rewriting() {
use craft_xml::{Names, Options};
for names in [Names::AsWritten, Names::Local] {
let strict = Options { names, ..Options::default() }.strict();
for xml in [
"<r a='1' a='2'/>",
"<r xmlns:p='urn:x' xmlns:q='urn:x' p:a='1' q:a='2'/>",
"<r xml:a='1' xmlns:p='http://www.w3.org/XML/1998/namespace' p:a='2'/>",
"<r xmlns:p='urn:x'><c xmlns:q='urn:x' p:a='1' q:a='2'/></r>",
] {
assert!(strict.parse(xml.as_bytes()).is_err(), "{xml}");
let mut events = strict.events(xml.as_bytes());
assert!(
loop {
match events.next() {
Ok(Some(_)) => {}
Ok(None) => break false,
Err(_) => break true,
}
},
"{xml}"
);
}
for xml in [
"<r xmlns='urn:x' xmlns:p='urn:x' a='1' p:a='2'/>",
"<r p:a='1' q:a='2'/>",
"<r xmlns:p='urn:x'><c xmlns:p='urn:y' xmlns:q='urn:x' p:a='1' q:a='2'/></r>",
] {
assert!(strict.parse(xml.as_bytes()).is_ok(), "{xml}");
}
assert!(Options { names, ..Options::default() }.parse(b"<r a='1' a='2'/>").is_ok());
}
let strict = Options { limits: craft_xml::Limits { max_attrs: 2, ..craft_xml::Limits::DEFAULT }, ..Options::default() }.strict();
assert!(strict.parse(b"<r xmlns:a='a' xmlns:b='b'><c xmlns:c='c'/></r>").is_err());
assert!(strict.parse(b"<r a='1' b='2' c='3'/>").is_err());
}