Skip to content

Truly independent web browser

LibWeb: Do not run scripts in `use` element shadow trees

Tim Ledbetter0e34089efa21
diff --git a/Libraries/LibWeb/HTML/HTMLOrSVGScriptElement.cpp b/Libraries/LibWeb/HTML/HTMLOrSVGScriptElement.cpp
index addd225d117..54b420bb7bb 100644
--- a/Libraries/LibWeb/HTML/HTMLOrSVGScriptElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLOrSVGScriptElement.cpp
@@ -25,6 +25,7 @@
#include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/HighResolutionTime/TimeOrigin.h>
+#include <LibWeb/SVG/SVGUseElement.h>
#include <LibWeb/TrustedTypes/RequireTrustedTypesForDirective.h>
#include <LibWeb/TrustedTypes/TrustedTypePolicy.h>
#include <LibWebCommon/Infra/CharacterTypes.h>
@@ -38,6 +39,15 @@ static GC::Ref<DOM::Event> create_event_for_element(DOM::Element& element, Utf16
return DOM::Event::create(event_name, HighResolutionTime::current_high_resolution_time(relevant_global_object(element)));
}
+static bool is_in_a_use_element_shadow_tree(DOM::Node const& node)
+{
+ for (auto const* shadow_root = as_if<DOM::ShadowRoot>(node.root()); shadow_root && shadow_root->host(); shadow_root = as_if<DOM::ShadowRoot>(shadow_root->host()->root())) {
+ if (is<SVG::SVGUseElement>(*shadow_root->host()))
+ return true;
+ }
+ return false;
+}
+
void HTMLOrSVGScriptElement::visit_script_element_edges(GC::Cell::Visitor& visitor)
{
m_result.visit(
@@ -226,6 +236,11 @@ void HTMLOrSVGScriptElement::prepare_script()
if (m_already_started)
return;
+ // AD-HOC: Scripts that a use element copies into its shadow tree are inert, matching other engines.
+ // See https://github.com/w3c/svgwg/issues/876
+ if (is_in_a_use_element_shadow_tree(element))
+ return;
+
// 2. Let parser document be el's parser document.
GC::Ptr<DOM::Document> parser_document = m_parser_document;
diff --git a/Tests/LibWeb/Text/expected/wpt-import/svg/struct/scripted/use-script-not-executed.txt b/Tests/LibWeb/Text/expected/wpt-import/svg/struct/scripted/use-script-not-executed.txt
new file mode 100644
index 00000000000..2bab06d6299
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/svg/struct/scripted/use-script-not-executed.txt
@@ -0,0 +1,12 @@
+Harness status: OK
+
+Found 6 tests
+
+5 Pass
+1 Fail
+Fail Not-yet-started svg-script cloned by a use element doesn't run
+Pass Not-yet-started html-script cloned by a use element doesn't run
+Pass Not-yet-started shadow-script cloned by a use element doesn't run
+Pass Not-yet-started root-html-script cloned by a use element doesn't run
+Pass Not-yet-started root-shadow-script cloned by a use element doesn't run
+Pass Scripts cloned by a use element while being parsed don't bypass 'strict-dynamic'
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/wpt-import/svg/struct/scripted/support/use-script-strict-dynamic.js b/Tests/LibWeb/Text/input/wpt-import/svg/struct/scripted/support/use-script-strict-dynamic.js
new file mode 100644
index 00000000000..6b8777fbfbf
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/svg/struct/scripted/support/use-script-strict-dynamic.js
@@ -0,0 +1 @@
+strictDynamicRuns++;
diff --git a/Tests/LibWeb/Text/input/wpt-import/svg/struct/scripted/use-script-not-executed.html b/Tests/LibWeb/Text/input/wpt-import/svg/struct/scripted/use-script-not-executed.html
new file mode 100644
index 00000000000..34bc4ebcbdd
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/svg/struct/scripted/use-script-not-executed.html
@@ -0,0 +1,76 @@
+<!doctype html>
+<meta charset="utf-8">
+<meta http-equiv="Content-Security-Policy" content="script-src 'nonce-abc' 'strict-dynamic'">
+<title>Scripts cloned by an SVG use element don't execute</title>
+<link rel="help" href="https://svgwg.org/svg2-draft/struct.html#UseElement">
+<link rel="help" href="https://github.com/w3c/svgwg/issues/876">
+<script nonce="abc" src="../../../resources/testharness.js"></script>
+<script nonce="abc" src="../../../resources/testharnessreport.js"></script>
+<script nonce="abc">
+ window.typeChangedRuns = {
+ "svg-script": 0,
+ "html-script": 0,
+ "shadow-script": 0,
+ "root-html-script": 0,
+ "root-shadow-script": 0,
+ };
+ window.strictDynamicRuns = 0;
+</script>
+<svg>
+ <g id="g">
+ <script id="svg-script" type="text/x-not-js">typeChangedRuns["svg-script"]++</script>
+ <foreignObject width="100" height="100">
+ <script id="html-script" type="text/x-not-js">typeChangedRuns["html-script"]++</script>
+ </foreignObject>
+ <foreignObject width="100" height="100">
+ <div id="shadow-host">
+ <template shadowrootmode="open" shadowrootclonable>
+ <script id="shadow-script" type="text/x-not-js">typeChangedRuns["shadow-script"]++</script>
+ </template>
+ </div>
+ </foreignObject>
+ </g>
+ <foreignObject id="fo" width="100" height="100">
+ <script id="root-html-script" type="text/x-not-js">typeChangedRuns["root-html-script"]++</script>
+ <div id="root-shadow-host">
+ <template shadowrootmode="open" shadowrootclonable>
+ <script id="root-shadow-script" type="text/x-not-js">typeChangedRuns["root-shadow-script"]++</script>
+ </template>
+ </div>
+ </foreignObject>
+</svg>
+<script nonce="abc">
+ document.write('<svg><use href="#written-svg"/><g id="written-svg"><script href="support/use-script-strict-dynamic.js">');
+ document.documentElement.getBoundingClientRect();
+ document.write('</scr' + 'ipt></g></svg>');
+</script>
+<script nonce="abc">
+ document.write('<svg><use href="#written-html"/><g id="written-html"><foreignObject><script src="support/use-script-strict-dynamic.js">');
+ document.documentElement.getBoundingClientRect();
+ document.write('</scr' + 'ipt></foreignObject></g></svg>');
+</script>
+<script nonce="abc">
+ const loaded = new Promise(resolve => window.addEventListener("load", resolve));
+
+ for (const kind of Object.keys(typeChangedRuns)) {
+ promise_test(async function(t) {
+ const script = document.getElementById(kind) ||
+ document.getElementById("shadow-host").shadowRoot.getElementById(kind) ||
+ document.getElementById("root-shadow-host").shadowRoot.getElementById(kind);
+ script.setAttribute("type", "text/javascript");
+ const use = document.createElementNS("http://www.w3.org/2000/svg", "use");
+ use.setAttribute("href", kind.startsWith("root-") ? "#fo" : "#g");
+ document.querySelector("svg").appendChild(use);
+ t.add_cleanup(() => use.remove());
+ await new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)));
+ await new Promise(resolve => step_timeout(resolve, 0));
+ assert_equals(typeChangedRuns[kind], 0, "Cloned script shouldn't run");
+ }, `Not-yet-started ${kind} cloned by a use element doesn't run`);
+ }
+
+ promise_test(async function() {
+ await loaded;
+ await new Promise(resolve => step_timeout(resolve, 0));
+ assert_equals(strictDynamicRuns, 0, "Cloned scripts shouldn't run");
+ }, "Scripts cloned by a use element while being parsed don't bypass 'strict-dynamic'");
+</script>