JavaScript: XML namespace lookups take fuel for each ancestor they climb

`inScopeNamespaces()`, `namespaceDeclarations()` and `namespace()` walk
from the node up to its root. The walk cloned each ancestor's whole node,
children and attributes included, to read its namespaces, and charged
fuel only for the namespaces it found, so a deep tree with no declared
namespaces cost no fuel. It now takes one unit of fuel per ancestor,
copies only the namespace list and parent link, and checks prefixes
already seen with a set. `namespaceDeclarations()` compares the node's
declarations with the ancestors' through a map instead of a scan.
This commit is contained in:
dotCooCoo committed 2026-11-11 14:54:26 -08:00
1 parent 7f3a046b18
commit a354503683
2 files changed
+25 -9

No files matched your search

+15 -9
View File
@@ -1,6 +1,7 @@
//! The E4X built-ins (ECMA-357 sections 13.1 to 13.5): `XML`, `XMLList`, `Namespace`, `QName`,
//! `isXMLName` and `setDefaultXMLNamespace`. A realm has them only with [`crate::Options::e4x`].
use std::collections::{HashMap, HashSet};
use std::rc::Rc;
use crate::builtins::{arg, constructor, data, method, native};
@@ -662,19 +663,25 @@ fn has_simple_content(interp: &mut Interp, this: Value, _: &[Value], _: bool) ->
}
}
/// The namespaces in scope at `x`, nearest declarations first, one for each prefix.
/// The namespaces in scope at `x`, nearest declarations first, one for each prefix. Takes one unit
/// of fuel for each node it climbs through and for each declaration it reads.
fn scope_of(interp: &mut Interp, x: Option<ObjId>) -> JsResult<Vec<XmlNs>> {
let mut out: Vec<XmlNs> = Vec::new();
let mut seen: HashSet<Option<Key>> = HashSet::new();
let mut cur = x;
while let Some(y) = cur {
let node = interp.xml_node(y)?.clone();
for ns in &node.ns {
interp.use_fuel()?;
let (list, parent) = {
let node = interp.xml_node(y)?;
(node.ns.clone(), node.parent)
};
for ns in list {
interp.use_fuel()?;
if !out.iter().any(|n| n.prefix == ns.prefix) {
out.push(ns.clone());
if seen.insert(ns.prefix.clone()) {
out.push(ns);
}
}
cur = node.parent;
cur = parent;
}
Ok(out)
}
@@ -702,9 +709,8 @@ fn namespace_declarations(interp: &mut Interp, this: Value, _: &[Value], _: bool
return namespaces_array(interp, Vec::new());
}
let parent = interp.xml_node(x)?.parent;
let ancestors = scope_of(interp, parent)?;
let declared: Vec<XmlNs> =
interp.xml_node(x)?.ns.iter().filter(|ns| !ancestors.iter().any(|a| a.prefix == ns.prefix && a.uri == ns.uri)).cloned().collect();
let ancestors: HashMap<Option<Key>, Key> = scope_of(interp, parent)?.into_iter().map(|a| (a.prefix, a.uri)).collect();
let declared: Vec<XmlNs> = interp.xml_node(x)?.ns.iter().filter(|ns| ancestors.get(&ns.prefix) != Some(&ns.uri)).cloned().collect();
namespaces_array(interp, declared)
}
+10
View File
@@ -740,6 +740,16 @@ fn insert_child_before_and_after_take_fuel_for_each_child_they_scan() {
}
}
#[test]
fn namespace_lookups_take_fuel_for_each_ancestor_they_climb() {
let shallow = "var x = <a><a/></a>; var leaf = x.children()[0];";
let deep = "var x = <a/>; var leaf = x; for (var i = 0; i < 150; i++) { var c = <a/>; leaf.appendChild(c); leaf = c; }";
for action in ["leaf.inScopeNamespaces()", "leaf.namespaceDeclarations()", "leaf.namespace()", "leaf.namespace('p')"] {
let (s, d) = (fuel_of(shallow, action), fuel_of(deep, action));
assert!(d >= s + 149, "{action}: {s} one level down, {d} 150 levels down");
}
}
#[test]
fn limits_cannot_be_caught_by_the_script() {
assert_eq!(kind_of(TIGHT, "try { for (;;) (<a/>).toXMLString(); } catch (e) { 1 }"), RunErrorKind::Limit(LimitKind::Fuel));