The headless browser for AI agents and web scraping
fix(js): keep DOMParser XML check independent of page startsWith
Shahzad Ali738ec02250e6
diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.jsindex 2c2e262f..016f658b 100644--- a/crates/obscura-js/js/bootstrap.js+++ b/crates/obscura-js/js/bootstrap.js@@ -12639,6 +12639,12 @@ const _checkXmlWellFormed = (html) => {// "well-formed" whenever the scan is ambiguous, so valid XML is never falsely// flagged. Quoted attribute regions, comments, CDATA, PIs and the doctype are// skipped; a literal '<' in text (invalid in XML) reads as a bad tag.+// Pages replace String.prototype.startsWith with polyfills that ignore the+// position argument (`this.substr(0, a.length) === a`; iasWorld county sites+// ship one). Every `startsWith(x, i)` below then tested the start of the+// input, so any XML beginning with `<?xml` read as a parse error.+const _startsWithAt = Function.prototype.call.bind(String.prototype.startsWith);+function _xmlWellFormed(src) {const s = String(src);const stack = [];@@ -12649,10 +12655,10 @@ function _xmlWellFormed(src) {const lt = s.indexOf('<', i);if (lt === -1) break;i = lt;- if (s.startsWith('<!--', i)) { const e = s.indexOf('-->', i + 4); if (e === -1) return false; i = e + 3; continue; }- if (s.startsWith('<![CDATA[', i)) { const e = s.indexOf(']]>', i + 9); if (e === -1) return false; i = e + 3; continue; }- if (s.startsWith('<?', i)) { const e = s.indexOf('?>', i + 2); if (e === -1) return false; i = e + 2; continue; }- if (s.startsWith('<!', i)) { const e = s.indexOf('>', i + 2); if (e === -1) return false; i = e + 1; continue; }+ if (_startsWithAt(s, '<!--', i)) { const e = s.indexOf('-->', i + 4); if (e === -1) return false; i = e + 3; continue; }+ if (_startsWithAt(s, '<![CDATA[', i)) { const e = s.indexOf(']]>', i + 9); if (e === -1) return false; i = e + 3; continue; }+ if (_startsWithAt(s, '<?', i)) { const e = s.indexOf('?>', i + 2); if (e === -1) return false; i = e + 2; continue; }+ if (_startsWithAt(s, '<!', i)) { const e = s.indexOf('>', i + 2); if (e === -1) return false; i = e + 1; continue; }// A start/end/self-closing tag: find its '>' while skipping quoted regions.let j = i + 1, quote = null;while (j < n) {diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rsindex b470bf1a..3a8800d6 100644--- a/crates/obscura-js/src/runtime.rs+++ b/crates/obscura-js/src/runtime.rs@@ -7686,6 +7686,23 @@ mod tests {assert_eq!(has_err, serde_json::json!(true));}+ #[test]+ fn dom_parser_ignores_page_startswith_polyfill() {+ // iasWorld county sites replace startsWith with a polyfill that ignores+ // the position argument; their SOAP responses start with `<?xml`.+ let mut rt = setup_runtime("<html><body></body></html>");+ let result = rt+ .evaluate(r#"(function(){+ String.prototype.startsWith = function(a){ return this.substr(0, a.length) === a; };+ var xml = '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><R xmlns="http://tempuri.org/"><GetSearchExistsResult>true</GetSearchExistsResult></R></soap:Body></soap:Envelope>';+ var d = new DOMParser().parseFromString(xml, 'text/xml');+ if (d.querySelector('parsererror')) return 'ERR';+ return d.getElementsByTagName('GetSearchExistsResult')[0].textContent;+ })()"#)+ .unwrap();+ assert_eq!(result, serde_json::json!("true"));+ }+#[test]fn dom_parser_accepts_well_formed_xml() {let mut rt = setup_runtime("<html><body></body></html>");