Truly independent web browser
LibWeb: Parse XMLHttpRequest XML responses with scripting disabled
Tim Ledbetter1ef4bf6290aa
diff --git a/Libraries/LibWeb/DOM/DocumentLoading.cpp b/Libraries/LibWeb/DOM/DocumentLoading.cppindex ba733bc93a8..6141b4414a8 100644--- a/Libraries/LibWeb/DOM/DocumentLoading.cpp+++ b/Libraries/LibWeb/DOM/DocumentLoading.cpp@@ -107,7 +107,7 @@ static StringView determine_xml_encoding(ReadonlyBytes data, Optional<StringViewreturn "UTF-8"sv;}-bool build_xml_document(DOM::Document& document, ByteBuffer const& data, Optional<StringView> content_encoding)+bool build_xml_document(DOM::Document& document, ByteBuffer const& data, Optional<StringView> content_encoding, XMLScriptingSupport scripting_support){auto decoder = TextCodec::decoder_for(determine_xml_encoding(data, content_encoding));VERIFY(decoder.has_value());@@ -119,7 +119,7 @@ bool build_xml_document(DOM::Document& document, ByteBuffer const& data, Optiona}auto source = source_or_error.release_value();XML::Parser parser(source, { .resolve_named_html_entity = resolve_named_html_entity });- XMLDocumentBuilder builder { document };+ XMLDocumentBuilder builder { document, scripting_support };auto result = parser.parse_with_listener(builder);return !result.is_error() && !builder.has_error();}diff --git a/Libraries/LibWeb/DOM/DocumentLoading.h b/Libraries/LibWeb/DOM/DocumentLoading.hindex f8bab12958a..8b96ba7aafc 100644--- a/Libraries/LibWeb/DOM/DocumentLoading.h+++ b/Libraries/LibWeb/DOM/DocumentLoading.h@@ -15,7 +15,9 @@namespace Web {-bool build_xml_document(DOM::Document& document, ByteBuffer const& data, Optional<StringView> content_encoding);+enum class XMLScriptingSupport;++bool build_xml_document(DOM::Document& document, ByteBuffer const& data, Optional<StringView> content_encoding, XMLScriptingSupport);GC::Ptr<DOM::Document> load_document(HTML::NavigationParams const& navigation_params, ReadonlyBytes sniff_bytes);bool can_load_document_with_type(MimeSniff::MimeType const&);diff --git a/Libraries/LibWeb/XHR/XMLHttpRequest.cpp b/Libraries/LibWeb/XHR/XMLHttpRequest.cppindex dc459491ee7..227f598ef82 100644--- a/Libraries/LibWeb/XHR/XMLHttpRequest.cpp+++ b/Libraries/LibWeb/XHR/XMLHttpRequest.cpp@@ -67,6 +67,7 @@#include <LibWeb/XHR/ProgressEvent.h>#include <LibWeb/XHR/XMLHttpRequest.h>#include <LibWeb/XHR/XMLHttpRequestUpload.h>+#include <LibWeb/XML/XMLDocumentBuilder.h>namespace Web::XHR {@@ -465,7 +466,7 @@ void XMLHttpRequest::set_document_response()// 6. Otherwise, let document be a document that represents the result of running the XML parser with XML scripting support disabled on xhr’s received bytes. If that fails (unsupported character encoding, namespace well-formedness error, etc.), then return null.else {document = DOM::XMLDocument::create(page, *m_global_object, m_response->url().value_or({}));- if (!Web::build_xml_document(*document, m_received_bytes, get_final_encoding())) {+ if (!Web::build_xml_document(*document, m_received_bytes, get_final_encoding(), XMLScriptingSupport::Disabled)) {reset_response_object();return;}diff --git a/Tests/LibWeb/Text/expected/wpt-import/xhr/responseXML-xml-scripting-support-disabled.txt b/Tests/LibWeb/Text/expected/wpt-import/xhr/responseXML-xml-scripting-support-disabled.txtnew file mode 100644index 00000000000..75367529756--- /dev/null+++ b/Tests/LibWeb/Text/expected/wpt-import/xhr/responseXML-xml-scripting-support-disabled.txt@@ -0,0 +1,11 @@+Harness status: OK++Found 5 tests++2 Pass+3 Fail+Fail XMLHttpRequest responseXML, svg script, appendChild+Fail XMLHttpRequest responseXML, svg script, append importNode result+Fail XMLHttpRequest responseXML, svg script, append adoptNode result+Pass XMLHttpRequest responseXML, xhtml script, append importNode result+Pass XMLHttpRequest responseXML, xhtml script, append adoptNode result\ No newline at end of filediff --git a/Tests/LibWeb/Text/input/wpt-import/xhr/responseXML-xml-scripting-support-disabled.html b/Tests/LibWeb/Text/input/wpt-import/xhr/responseXML-xml-scripting-support-disabled.htmlnew file mode 100644index 00000000000..acd10f0305f--- /dev/null+++ b/Tests/LibWeb/Text/input/wpt-import/xhr/responseXML-xml-scripting-support-disabled.html@@ -0,0 +1,71 @@+<!doctype html>+<html>+<head>+<title>XMLHttpRequest: script already started</title>+<link rel="help" href="https://xhr.spec.whatwg.org/#document-response">+<script src="../resources/testharness.js"></script>+<script src="../resources/testharnessreport.js"></script>+</head>+<body>+<script>+// XMLHttpRequest.responseXML creates an XML parser with+// XML scripting support disabled. This test tests that various+// responseXML-parsed script elements are inactive ("already started").+//+// See:+// https://xhr.spec.whatwg.org/#document-response+// step 6, "with the XML scripting support disabled flag set."+test(_ => {+ window.mark = 0;+ const xhr = new XMLHttpRequest();+ xhr.open("GET", "data:text/xml,<svg xmlns='http://www.w3.org/2000/svg'><script>window.mark = 1;</sc" + "ript></svg>", false);+ xhr.send();+ assert_not_equals(xhr.responseXML, null);+ document.body.appendChild(xhr.responseXML.rootElement);+ assert_equals(window.mark, 0);+}, "XMLHttpRequest responseXML, svg script, appendChild");++test(_ => {+ window.mark = 0;+ const xhr = new XMLHttpRequest();+ xhr.open("GET", "data:text/xml,<svg xmlns='http://www.w3.org/2000/svg'><script>window.mark = 1;</sc" + "ript></svg>", false);+ xhr.send();+ assert_not_equals(xhr.responseXML, null);+ document.body.appendChild(document.importNode(xhr.responseXML.rootElement, true));+ assert_equals(window.mark, 0);+}, "XMLHttpRequest responseXML, svg script, append importNode result");++test(_ => {+ window.mark = 0;+ const xhr = new XMLHttpRequest();+ xhr.open("GET", "data:text/xml,<svg xmlns='http://www.w3.org/2000/svg'><script>window.mark = 1;</sc" + "ript></svg>", false);+ xhr.send();+ assert_not_equals(xhr.responseXML, null);+ document.body.appendChild(document.adoptNode(xhr.responseXML.rootElement));+ assert_equals(window.mark, 0);+}, "XMLHttpRequest responseXML, svg script, append adoptNode result");++test(_ => {+ window.mark = 0;+ const xhr = new XMLHttpRequest();+ xhr.open("GET", "data:application/xhtml+xml,<html xmlns='http://www.w3.org/1999/xhtml'><body><script>window.mark = 1;</sc" + "ript></body></html>", false);+ xhr.send();+ assert_not_equals(xhr.responseXML, null);+ const script = xhr.responseXML.getElementsByTagNameNS("http://www.w3.org/1999/xhtml", "script")[0];+ document.body.appendChild(document.importNode(script, true));+ assert_equals(window.mark, 0);+}, "XMLHttpRequest responseXML, xhtml script, append importNode result");++test(_ => {+ window.mark = 0;+ const xhr = new XMLHttpRequest();+ xhr.open("GET", "data:application/xhtml+xml,<html xmlns='http://www.w3.org/1999/xhtml'><body><script>window.mark = 1;</sc" + "ript></body></html>", false);+ xhr.send();+ assert_not_equals(xhr.responseXML, null);+ const script = xhr.responseXML.getElementsByTagNameNS("http://www.w3.org/1999/xhtml", "script")[0];+ document.body.appendChild(document.adoptNode(script));+ assert_equals(window.mark, 0);+}, "XMLHttpRequest responseXML, xhtml script, append adoptNode result");+</script>+</body>+</html>