Skip to content

Truly independent web browser

LibWeb: Set the script text of scripts that the XML parser inserts

Tim Ledbetter1d0bf7cca21f
diff --git a/Libraries/LibWeb/HTML/HTMLOrSVGScriptElement.cpp b/Libraries/LibWeb/HTML/HTMLOrSVGScriptElement.cpp
index b781dd479a9..addd225d117 100644
--- a/Libraries/LibWeb/HTML/HTMLOrSVGScriptElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLOrSVGScriptElement.cpp
@@ -667,7 +667,7 @@ void HTMLOrSVGScriptElement::ParserChildrenChangeScope::begin_changing_children(
// https://html.spec.whatwg.org/multipage/scripting.html#script-processing-model:children-changed-steps
void HTMLOrSVGScriptElement::script_element_children_changed(DOM::Node::ChildrenChangedMetadata const& metadata)
{
- if (!m_parser_is_changing_children)
+ if (!exchange(m_parser_is_changing_children, false))
m_children_changed_by_api = true;
// AD-HOC: Avoid running script on child removal, matching the behaviour of other browsers.
diff --git a/Libraries/LibWeb/XML/XMLDocumentBuilder.cpp b/Libraries/LibWeb/XML/XMLDocumentBuilder.cpp
index fcfedc40bdf..94b8270a2fd 100644
--- a/Libraries/LibWeb/XML/XMLDocumentBuilder.cpp
+++ b/Libraries/LibWeb/XML/XMLDocumentBuilder.cpp
@@ -151,6 +151,7 @@ void XMLDocumentBuilder::element_start(Utf16FlyString const& name, Vector<XML::L
link_element->set_was_enabled_when_created_by_parser(Badge<XMLDocumentBuilder> {}, !has_disabled_attribute);
}
+ HTML::HTMLOrSVGScriptElement::ParserChildrenChangeScope script_children_change { *m_current_node };
if (m_current_node->is_html_template_element()) {
// When an XML parser would append a node to a template element, it must instead append it to the template element's template contents (a DocumentFragment node).
MUST(static_cast<HTML::HTMLTemplateElement&>(*m_current_node).content()->append_child(node));
@@ -208,6 +209,8 @@ void XMLDocumentBuilder::element_end(Utf16FlyString const& name)
HTML::perform_a_microtask_checkpoint();
// and then prepare the script element.
auto& script_element = static_cast<HTML::HTMLScriptElement&>(*m_current_node);
+ // AD-HOC: Set the script text value for Trusted Types as the HTML parser does, matching other engines.
+ script_element.set_script_text_from_parser();
script_element.prepare_script(Badge<XMLDocumentBuilder> {});
// If this causes there to be a pending parsing-blocking script, then the user agent must run the following steps:
@@ -253,6 +256,7 @@ void XMLDocumentBuilder::text(StringView data)
if (m_has_error)
return;
+ HTML::HTMLOrSVGScriptElement::ParserChildrenChangeScope script_children_change { *m_current_node };
if (auto* last = m_current_node->last_child(); last && last->is_text()) {
auto& text_node = static_cast<DOM::Text&>(*last);
m_text_builder.append(text_node.data());
@@ -270,6 +274,7 @@ void XMLDocumentBuilder::comment(StringView data)
if (m_has_error || !m_current_node)
return;
+ HTML::HTMLOrSVGScriptElement::ParserChildrenChangeScope script_children_change { *m_current_node };
MUST(m_current_node->append_child(m_document->create_comment(Utf16String::from_utf8(data))));
}
@@ -279,6 +284,7 @@ void XMLDocumentBuilder::cdata_section(StringView data)
return;
auto section = MUST(m_document->create_cdata_section(Utf16String::from_utf8(data)));
+ HTML::HTMLOrSVGScriptElement::ParserChildrenChangeScope script_children_change { *m_current_node };
MUST(m_current_node->append_child(section));
}
@@ -288,6 +294,7 @@ void XMLDocumentBuilder::processing_instruction(Utf16FlyString const& target, Ut
return;
auto processing_instruction = MUST(m_document->create_processing_instruction(target, data));
+ HTML::HTMLOrSVGScriptElement::ParserChildrenChangeScope script_children_change { *m_current_node };
MUST(m_current_node->append_child(processing_instruction));
}
diff --git a/Tests/LibWeb/Text/expected/XML/inline-scripts-under-require-trusted-types-for.txt b/Tests/LibWeb/Text/expected/XML/inline-scripts-under-require-trusted-types-for.txt
new file mode 100644
index 00000000000..fe52317d709
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/XML/inline-scripts-under-require-trusted-types-for.txt
@@ -0,0 +1,4 @@
+Inline HTML script of an XHTML document under require-trusted-types-for: ran
+Inline SVG script of an XHTML document under require-trusted-types-for: ran
+Inline script whose text a mutation observer replaced while parsing an XHTML document: blocked
+Inline script whose text a load event listener replaced while the XML parser inserted its child: blocked
diff --git a/Tests/LibWeb/Text/input/XML/inline-scripts-under-require-trusted-types-for.html b/Tests/LibWeb/Text/input/XML/inline-scripts-under-require-trusted-types-for.html
new file mode 100644
index 00000000000..695634d9bbf
--- /dev/null
+++ b/Tests/LibWeb/Text/input/XML/inline-scripts-under-require-trusted-types-for.html
@@ -0,0 +1,73 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ promiseTest(async () => {
+ const server = httpTestServer();
+ const observerURL = await server.createEcho("GET", "/inline-scripts-under-require-trusted-types-for-observer.js", {
+ status: 200,
+ headers: { "Content-Type": "text/javascript" },
+ body: `new MutationObserver((records, observer) => {
+ const script = document.getElementById("target");
+ if (script && script.firstChild) {
+ observer.disconnect();
+ script.firstChild.data = 'parent.postMessage("replaced", "*");';
+ }
+ }).observe(document.documentElement, { subtree: true, childList: true });`,
+ });
+ const listenerURL = await server.createEcho("GET", "/inline-scripts-under-require-trusted-types-for-listener.js", {
+ status: 200,
+ headers: { "Content-Type": "text/javascript" },
+ body: `document.addEventListener("load", event => {
+ if (event.target.localName === "iframe")
+ document.getElementById("iframe-target").firstChild.data = 'parent.postMessage("replaced by a load event listener", "*");';
+ }, true);`,
+ });
+ const doneURL = await server.createEcho("GET", "/inline-scripts-under-require-trusted-types-for-done.js", {
+ status: 200,
+ headers: { "Content-Type": "text/javascript" },
+ body: `parent.postMessage("done", "*");`,
+ });
+ const scriptStartTag = "<scr" + "ipt";
+ const scriptEndTag = "</scr" + "ipt>";
+ const documentURL = await server.createEcho("GET", "/inline-scripts-under-require-trusted-types-for.xhtml", {
+ status: 200,
+ headers: {
+ "Content-Type": "application/xhtml+xml",
+ "Content-Security-Policy": "require-trusted-types-for 'script'",
+ },
+ body: `<html xmlns="http://www.w3.org/1999/xhtml"><body>
+ ${scriptStartTag}>parent.postMessage("HTML script", "*");${scriptEndTag}
+ <svg xmlns="http://www.w3.org/2000/svg">${scriptStartTag}>parent.postMessage("SVG script", "*");${scriptEndTag}</svg>
+ ${scriptStartTag} src="${observerURL}">${scriptEndTag}
+ ${scriptStartTag} id="target">parent.postMessage("original", "*");${scriptEndTag}
+ ${scriptStartTag} src="${listenerURL}">${scriptEndTag}
+ ${scriptStartTag} id="iframe-target">parent.postMessage("original", "*");<iframe/>${scriptEndTag}
+ ${scriptStartTag} src="${doneURL}">${scriptEndTag}
+ </body></html>`,
+ });
+
+ const received = [];
+ const finished = new Promise(resolve => {
+ window.addEventListener("message", event => {
+ if (event.data === "done") {
+ resolve();
+ } else {
+ received.push(event.data);
+ }
+ });
+ });
+ const frame = document.createElement("iframe");
+ frame.src = documentURL;
+ document.body.appendChild(frame);
+ await finished;
+
+ for (const label of ["HTML script", "SVG script"]) {
+ const result = received.includes(label) ? "ran" : "blocked";
+ println(`Inline ${label} of an XHTML document under require-trusted-types-for: ${result}`);
+ }
+ const replaced = received.includes("replaced") ? "ran" : "blocked";
+ println(`Inline script whose text a mutation observer replaced while parsing an XHTML document: ${replaced}`);
+ const replacedByListener = received.includes("replaced by a load event listener") ? "ran" : "blocked";
+ println(`Inline script whose text a load event listener replaced while the XML parser inserted its child: ${replacedByListener}`);
+ });
+</script>