Skip to content

Truly independent web browser

LibWeb: Cover default policy use for inline script text

Andreas Klingff0ee7750aa9
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index b6a8edca7fb..b942804e238 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -40,6 +40,7 @@ Text/input/cookie-change-events-skip-pending-navigation-url.html
Text/input/wpt-import/cookies/attributes/domain.sub.html
; Import modules by URL, which needs an HTTP origin.
+Text/input/wpt-import/trusted-types/script-enforcement-006.html
Text/input/wpt-import/html/semantics/scripting-1/the-script-element/module/dynamic-import/propagate-nonce-external-classic.html
Text/input/wpt-import/html/semantics/scripting-1/the-script-element/module/dynamic-import/propagate-nonce-external-module.html
Text/input/wpt-import/html/semantics/scripting-1/the-script-element/module/dynamic-import/propagate-nonce-inline-classic.html
diff --git a/Tests/LibWeb/Text/expected/wpt-import/trusted-types/script-enforcement-002.txt b/Tests/LibWeb/Text/expected/wpt-import/trusted-types/script-enforcement-002.txt
new file mode 100644
index 00000000000..6d8732ae9d7
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/trusted-types/script-enforcement-002.txt
@@ -0,0 +1,39 @@
+Harness status: OK
+
+Found 33 tests
+
+5 Pass
+28 Fail
+Pass Default policy's calls when setting script source via HTMLScriptElement.innerText.
+Fail Verify when the default policy is called for script source via HTMLElement.innerText.
+Pass Default policy's calls when setting script source via HTMLScriptElement.textContent.
+Fail Verify when the default policy is called for script source set via Node.textContent.
+Pass Default policy's calls when setting script source via HTMLScriptElement.text.
+Fail Default policy's calls when setting script source via Element.innerHTML.
+Fail Default policy's calls when setting script source via Element.setHTMLUnsafe().
+Pass Default policy when splitting script source via Text.splitText().
+Pass Default policy when normalizing script source via Element.normalize().
+Fail Default policy's calls when setting script source via Node.nodeValue.
+Fail Default policy's calls when setting script source via CharacterData.data.
+Fail Default policy's calls when setting script source via CharacterData.appendData().
+Fail Default policy's calls when setting script source via CharacterData.insertData().
+Fail Default policy's calls when setting script source via CharacterData.replaceData().
+Fail Default policy's calls when setting script source via CharacterData.deleteData().
+Fail Default policy's calls when setting script source via CharacterData.before().
+Fail Default policy's calls when setting script source via CharacterData.after().
+Fail Default policy's calls when setting script source via CharacterData.remove().
+Fail Default policy's calls when setting script source via CharacterData.replaceWith().
+Fail Default policy's calls when setting script source via Node.appendChild().
+Fail Default policy's calls when setting script source via Node.insertBefore().
+Fail Default policy's calls when setting script source via Node.replaceChild().
+Fail Default policy's calls when setting script source via Node.removeChild().
+Fail Default policy's calls when setting script source via Element.prepend().
+Fail Default policy's calls when setting script source via Element.append().
+Fail Default policy's calls when setting script source via Element.replaceChildren().
+Fail Default policy's calls when setting script source via Element.moveBefore().
+Fail Default policy's calls when setting script source via Node.insertAdjacentText().
+Fail Default policy's calls when setting script source via Node.insertAdjacentHTML().
+Fail Default policy's calls when setting source via Range.insertNode().
+Fail Default policy's calls when setting script source via Range.deleteContents().
+Fail Default policy's calls when cloning a script via Node.cloneNode().
+Fail Default policy's calls when cloning a script via Range.cloneContents().
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/trusted-types/script-enforcement-005.txt b/Tests/LibWeb/Text/expected/wpt-import/trusted-types/script-enforcement-005.txt
new file mode 100644
index 00000000000..928d20aea19
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/trusted-types/script-enforcement-005.txt
@@ -0,0 +1,9 @@
+Harness status: OK
+
+Found 4 tests
+
+4 Fail
+Fail Empty HTMLScriptElement is executed if the default policy makes it non-empty.
+Fail Non-empty HTMLScriptElement is not executed if the default policy makes it empty.
+Fail Empty SVGScriptElement is executed if the default policy makes it non-empty.
+Fail Non-empty SVGScriptElement is not executed if the default policy makes it empty.
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/trusted-types/script-enforcement-006.txt b/Tests/LibWeb/Text/expected/wpt-import/trusted-types/script-enforcement-006.txt
new file mode 100644
index 00000000000..a1d3c87d2ab
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/trusted-types/script-enforcement-006.txt
@@ -0,0 +1,9 @@
+Harness status: OK
+
+Found 4 tests
+
+4 Fail
+Fail Untrusted HTMLScriptElement with classic type uses the source text returned by the default policy.
+Fail Untrusted HTMLScriptElement of importmap type uses the source text returned by the default policy.
+Fail Untrusted HTMLScriptElement of module type uses the source text returned by the default policy.
+Fail Untrusted HTMLScriptElement of two importmap type use the source text returned by the default policy.
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/wpt-import/trusted-types/script-enforcement-002.html b/Tests/LibWeb/Text/input/wpt-import/trusted-types/script-enforcement-002.html
new file mode 100644
index 00000000000..8779be80a86
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/trusted-types/script-enforcement-002.html
@@ -0,0 +1,426 @@
+<!DOCTYPE html>
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+<script src="support/namespaces.js"></script>
+<script src="support/passthroughpolicy.js"></script>
+<script src="support/script-messages.js"></script>
+<link rel="help" href="https://w3c.github.io/trusted-types/dist/spec/#enforcement-in-scripts">
+<meta http-equiv="Content-Security-Policy" content="require-trusted-types-for 'script';">
+<!-- This test is similar to script-enforcement-001 but it defines a
+ default policy, so we can track precisely when its callbacks
+ are called, the sink names used as well as their effect on
+ the original script's source. -->
+<div id="container"></div>
+<script>
+ trustedTypes.createPolicy("default", {
+ createHTML: (value, _, sink) => {
+ window.log_message("CREATE_HTML");
+ window.log_message(sink);
+ return value.replace("RUN", "RUN_TRUSTED_HTML");
+ },
+ createScript: (value, _, sink) => {
+ window.log_message("CREATE_SCRIPT");
+ window.log_message(sink);
+ return value.replace("RUN", "RUN_TRUSTED_SCRIPT");
+ },
+ });
+
+ promise_test(async t => {
+ let messages = await script_messages_for(_ => {
+ let script = document.createElement("script");
+ window.log_message("SET");
+ script.innerText = LOG_RUN_MESSAGE;
+ window.log_message("APPEND");
+ container.appendChild(script);
+ });
+ assert_array_equals(messages, ["SET", "CREATE_SCRIPT", "HTMLScriptElement innerText", "APPEND", "RUN_TRUSTED_SCRIPT"]);
+ }, "Default policy's calls when setting script source via HTMLScriptElement.innerText.");
+
+ promise_test(async t => {
+ let messages = await script_messages_for(_ => {
+ let script = document.createElement("script");
+ window.log_message("SET");
+ Object.getOwnPropertyDescriptor(HTMLElement.prototype, "innerText").set.call(script, LOG_RUN_MESSAGE);
+ window.log_message("APPEND");
+ container.appendChild(script);
+ });
+ assert_array_equals(messages, ["SET", "APPEND", "CREATE_SCRIPT", "HTMLScriptElement text", "RUN_TRUSTED_SCRIPT"]);
+ }, "Verify when the default policy is called for script source via HTMLElement.innerText.");
+
+ promise_test(async t => {
+ let messages = await script_messages_for(_ => {
+ let script = document.createElement("script");
+ window.log_message("SET");
+ script.textContent = LOG_RUN_MESSAGE;
+ window.log_message("APPEND");
+ container.appendChild(script);
+ });
+ assert_array_equals(messages, ["SET", "CREATE_SCRIPT", "HTMLScriptElement textContent", "APPEND", "RUN_TRUSTED_SCRIPT"]);
+ }, "Default policy's calls when setting script source via HTMLScriptElement.textContent.");
+
+ promise_test(async t => {
+ let messages = await script_messages_for(_ => {
+ let script = document.createElement("script");
+ window.log_message("SET");
+ Object.getOwnPropertyDescriptor(Node.prototype, "textContent").set.call(script, LOG_RUN_MESSAGE);
+ window.log_message("APPEND");
+ container.appendChild(script);
+ });
+ assert_array_equals(messages, ["SET", "APPEND", "CREATE_SCRIPT", "HTMLScriptElement text", "RUN_TRUSTED_SCRIPT"]);
+ }, "Verify when the default policy is called for script source set via Node.textContent.");
+
+ promise_test(async t => {
+ let messages = await script_messages_for(_ => {
+ let script = document.createElement("script");
+ window.log_message("SET");
+ script.text = LOG_RUN_MESSAGE;
+ window.log_message("APPEND");
+ container.appendChild(script);
+ });
+ assert_array_equals(messages, ["SET", "CREATE_SCRIPT", "HTMLScriptElement text", "APPEND", "RUN_TRUSTED_SCRIPT"]);
+ }, "Default policy's calls when setting script source via HTMLScriptElement.text.");
+
+ promise_test(async t => {
+ let messages = await script_messages_for(_ => {
+ let script = document.createElement("script");
+ window.log_message("SET");
+ script.innerHTML = LOG_RUN_MESSAGE;
+ window.log_message("APPEND");
+ container.appendChild(script);
+ });
+ assert_array_equals(messages, ["SET", "CREATE_HTML", "Element innerHTML", "APPEND", "CREATE_SCRIPT", "HTMLScriptElement text", "RUN_TRUSTED_SCRIPT_TRUSTED_HTML"]);
+ }, "Default policy's calls when setting script source via Element.innerHTML.");
+
+ promise_test(async t => {
+ let messages = await script_messages_for(_ => {
+ let script = document.createElement("script");
+ window.log_message("SET");
+ script.setHTMLUnsafe(LOG_RUN_MESSAGE);
+ window.log_message("APPEND");
+ container.appendChild(script);
+ });
+ assert_array_equals(messages, ["SET", "CREATE_HTML", "Element setHTMLUnsafe", "APPEND", "CREATE_SCRIPT", "HTMLScriptElement text", "RUN_TRUSTED_SCRIPT_TRUSTED_HTML"]);
+ }, "Default policy's calls when setting script source via Element.setHTMLUnsafe().");
+
+ promise_test(async t => {
+ let messages = await script_messages_for(_ => {
+ let script = create_html_script_with_trusted_source_text(`${LOG_RUN_MESSAGE};;;`);
+ window.log_message("SET");
+ script.firstChild.splitText(3);
+ window.log_message("APPEND");
+ container.appendChild(script);
+ });
+ assert_array_equals(messages, ["SET", "APPEND", "RUN"]);
+ }, "Default policy when splitting script source via Text.splitText().");
+
+ promise_test(async t => {
+ let messages = await script_messages_for(_ => {
+ let script = create_html_script_with_trusted_source_text(`${LOG_RUN_MESSAGE};;;`);
+ script.firstChild.splitText(3);
+ window.log_message("SET");
+ script.normalize();
+ window.log_message("APPEND");
+ container.appendChild(script);
+ });
+ assert_array_equals(messages, ["SET", "APPEND", "RUN"]);
+ }, "Default policy when normalizing script source via Element.normalize().");
+
+ promise_test(async t => {
+ let messages = await script_messages_for(_ => {
+ let script = create_html_script_with_trusted_source_text(";");
+ window.log_message("SET");
+ script.firstChild.nodeValue = LOG_RUN_MESSAGE;
+ window.log_message("APPEND");
+ container.appendChild(script);
+ });
+ assert_array_equals(messages, ["SET", "APPEND", "CREATE_SCRIPT", "HTMLScriptElement text", "RUN_TRUSTED_SCRIPT"]);
+ }, "Default policy's calls when setting script source via Node.nodeValue.");
+
+ promise_test(async t => {
+ let messages = await script_messages_for(_ => {
+ let script = create_html_script_with_trusted_source_text(";");
+ window.log_message("SET");
+ script.firstChild.data = LOG_RUN_MESSAGE;
+ window.log_message("APPEND");
+ container.appendChild(script);
+ });
+ assert_array_equals(messages, ["SET", "APPEND", "CREATE_SCRIPT", "HTMLScriptElement text", "RUN_TRUSTED_SCRIPT"]);
+ }, "Default policy's calls when setting script source via CharacterData.data.");
+
+ promise_test(async t => {
+ let messages = await script_messages_for(_ => {
+ let script = create_html_script_with_trusted_source_text(";");
+ window.log_message("SET");
+ script.firstChild.appendData(LOG_RUN_MESSAGE);
+ window.log_message("APPEND");
+ container.appendChild(script);
+ });
+ assert_array_equals(messages, ["SET", "APPEND", "CREATE_SCRIPT", "HTMLScriptElement text", "RUN_TRUSTED_SCRIPT"]);
+ }, "Default policy's calls when setting script source via CharacterData.appendData().");
+
+ promise_test(async t => {
+ let messages = await script_messages_for(_ => {
+ let script = create_html_script_with_trusted_source_text(";");
+ window.log_message("SET");
+ script.firstChild.insertData(0, LOG_RUN_MESSAGE);
+ window.log_message("APPEND");
+ container.appendChild(script);
+ });
+ assert_array_equals(messages, ["SET", "APPEND", "CREATE_SCRIPT", "HTMLScriptElement text", "RUN_TRUSTED_SCRIPT"]);
+ }, "Default policy's calls when setting script source via CharacterData.insertData().");
+
+ promise_test(async t => {
+ let messages = await script_messages_for(_ => {
+ let script = create_html_script_with_trusted_source_text(";");
+ window.log_message("SET");
+ script.firstChild.replaceData(0, 0, LOG_RUN_MESSAGE);
+ window.log_message("APPEND");
+ container.appendChild(script);
+ });
+ assert_array_equals(messages, ["SET", "APPEND", "CREATE_SCRIPT", "HTMLScriptElement text", "RUN_TRUSTED_SCRIPT"]);
+ }, "Default policy's calls when setting script source via CharacterData.replaceData().");
+
+ promise_test(async t => {
+ let messages = await script_messages_for(_ => {
+ let script = create_html_script_with_trusted_source_text(`//${LOG_RUN_MESSAGE}`);
+ window.log_message("SET");
+ script.firstChild.deleteData(0, 2);
+ window.log_message("APPEND");
+ container.appendChild(script);
+ });
+ assert_array_equals(messages, ["SET", "APPEND", "CREATE_SCRIPT", "HTMLScriptElement text", "RUN_TRUSTED_SCRIPT"]);
+ }, "Default policy's calls when setting script source via CharacterData.deleteData().");
+
+ promise_test(async t => {
+ let messages = await script_messages_for(_ => {
+ let script = create_html_script_with_trusted_source_text(";");
+ window.log_message("SET");
+ script.firstChild.before(LOG_RUN_MESSAGE);
+ window.log_message("APPEND");
+ container.appendChild(script);
+ });
+ assert_array_equals(messages, ["SET", "APPEND", "CREATE_SCRIPT", "HTMLScriptElement text", "RUN_TRUSTED_SCRIPT"]);
+ }, "Default policy's calls when setting script source via CharacterData.before().");
+
+ promise_test(async t => {
+ let messages = await script_messages_for(_ => {
+ let script = create_html_script_with_trusted_source_text(";");
+ window.log_message("SET");
+ script.firstChild.after(LOG_RUN_MESSAGE);
+ window.log_message("APPEND");
+ container.appendChild(script);
+ });
+ assert_array_equals(messages, ["SET", "APPEND", "CREATE_SCRIPT", "HTMLScriptElement text", "RUN_TRUSTED_SCRIPT"]);
+ }, "Default policy's calls when setting script source via CharacterData.after().");
+
+ promise_test(async t => {
+ let messages = await script_messages_for(_ => {
+ let script = create_html_script_with_trusted_source_text(`;;;${LOG_RUN_MESSAGE}`);
+ script.firstChild.splitText(3);
+ window.log_message("SET");
+ script.firstChild.remove();
+ window.log_message("APPEND");
+ container.appendChild(script);
+ });
+ assert_array_equals(messages, ["SET", "APPEND", "CREATE_SCRIPT", "HTMLScriptElement text", "RUN_TRUSTED_SCRIPT"]);
+ }, "Default policy's calls when setting script source via CharacterData.remove().");
+
+ promise_test(async t => {
+ let messages = await script_messages_for(_ => {
+ let script = create_html_script_with_trusted_source_text(";");
+ window.log_message("SET");
+ script.firstChild.replaceWith(document.createTextNode(LOG_RUN_MESSAGE));
+ window.log_message("APPEND");
+ container.appendChild(script);
+ });
+ assert_array_equals(messages, ["SET", "APPEND", "CREATE_SCRIPT", "HTMLScriptElement text", "RUN_TRUSTED_SCRIPT"]);
+ }, "Default policy's calls when setting script source via CharacterData.replaceWith().");
+
+ promise_test(async t => {
+ let messages = await script_messages_for(_ => {
+ let script = create_html_script_with_trusted_source_text(";");
+ window.log_message("SET");
+ script.appendChild(document.createTextNode(LOG_RUN_MESSAGE));
+ window.log_message("APPEND");
+ container.appendChild(script);
+ });
+ assert_array_equals(messages, ["SET", "APPEND", "CREATE_SCRIPT", "HTMLScriptElement text", "RUN_TRUSTED_SCRIPT"]);
+ }, "Default policy's calls when setting script source via Node.appendChild().");
+
+ promise_test(async t => {
+ let messages = await script_messages_for(_ => {
+ let script = create_html_script_with_trusted_source_text(";");
+ window.log_message("SET");
+ script.insertBefore(document.createTextNode(LOG_RUN_MESSAGE), script.firstChild);
+ window.log_message("APPEND");
+ container.appendChild(script);
+ });
+ assert_array_equals(messages, ["SET", "APPEND", "CREATE_SCRIPT", "HTMLScriptElement text", "RUN_TRUSTED_SCRIPT"]);
+ }, "Default policy's calls when setting script source via Node.insertBefore().");
+
+ promise_test(async t => {
+ let messages = await script_messages_for(_ => {
+ let script = create_html_script_with_trusted_source_text(";");
+ window.log_message("SET");
+ script.replaceChild(document.createTextNode(LOG_RUN_MESSAGE), script.firstChild);
+ window.log_message("APPEND");
+ container.appendChild(script);
+ });
+ assert_array_equals(messages, ["SET", "APPEND", "CREATE_SCRIPT", "HTMLScriptElement text", "RUN_TRUSTED_SCRIPT"]);
+ }, "Default policy's calls when setting script source via Node.replaceChild().");
+
+ promise_test(async t => {
+ let messages = await script_messages_for(_ => {
+ let script = create_html_script_with_trusted_source_text(`;;;${LOG_RUN_MESSAGE}`);
+ script.firstChild.splitText(3);
+ window.log_message("SET");
+ script.removeChild(script.firstChild);
+ window.log_message("APPEND");
+ container.appendChild(script);
+ });
+ assert_array_equals(messages, ["SET", "APPEND", "CREATE_SCRIPT", "HTMLScriptElement text", "RUN_TRUSTED_SCRIPT"]);
+ }, "Default policy's calls when setting script source via Node.removeChild().");
+
+ promise_test(async t => {
+ let messages = await script_messages_for(_ => {
+ let script = create_html_script_with_trusted_source_text(";");
+ window.log_message("SET");
+ script.prepend(document.createTextNode(LOG_RUN_MESSAGE));
+ window.log_message("APPEND");
+ container.appendChild(script);
+ });
+ assert_array_equals(messages, ["SET", "APPEND", "CREATE_SCRIPT", "HTMLScriptElement text", "RUN_TRUSTED_SCRIPT"]);
+ }, "Default policy's calls when setting script source via Element.prepend().");
+
+ promise_test(async t => {
+ let messages = await script_messages_for(_ => {
+ let script = create_html_script_with_trusted_source_text(";");
+ window.log_message("SET");
+ script.append(document.createTextNode(LOG_RUN_MESSAGE));
+ window.log_message("APPEND");
+ container.appendChild(script);
+ });
+ assert_array_equals(messages, ["SET", "APPEND", "CREATE_SCRIPT", "HTMLScriptElement text", "RUN_TRUSTED_SCRIPT"]);
+ }, "Default policy's calls when setting script source via Element.append().");
+
+ promise_test(async t => {
+ let messages = await script_messages_for(_ => {
+ let script = create_html_script_with_trusted_source_text(";");
+ window.log_message("SET");
+ script.replaceChildren(document.createTextNode(LOG_RUN_MESSAGE));
+ window.log_message("APPEND");
+ container.appendChild(script);
+ });
+ assert_array_equals(messages, ["SET", "APPEND", "CREATE_SCRIPT", "HTMLScriptElement text", "RUN_TRUSTED_SCRIPT"]);
+ }, "Default policy's calls when setting script source via Element.replaceChildren().");
+
+ promise_test(async t => {
+ let messages = await script_messages_for(_ => {
+ let script = create_html_script_with_trusted_source_text(";");
+ window.log_message("SET");
+ // Per https://dom.spec.whatwg.org/#move, step 1, moveBefore requires
+ // the two nodes to have the same shadow-including root.
+ let text = document.createTextNode(LOG_RUN_MESSAGE);
+ let root = document.createElement("div");
+ root.appendChild(text);
+ root.appendChild(script);
+ script.moveBefore(text, script.firstChild);
+ window.log_message("APPEND");
+ container.appendChild(script);
+ });
+ assert_array_equals(messages, ["SET", "APPEND", "CREATE_SCRIPT", "HTMLScriptElement text", "RUN_TRUSTED_SCRIPT"]);
+ }, "Default policy's calls when setting script source via Element.moveBefore().");
+
+ promise_test(async t => {
+ let messages1 = await script_messages_for(_ => {
+ let script = create_html_script_with_trusted_source_text(";");
+ window.log_message("SET");
+ script.insertAdjacentText("afterbegin", LOG_RUN_MESSAGE);
+ window.log_message("APPEND");
+ container.appendChild(script);
+ });
+ assert_array_equals(messages1, ["SET", "APPEND", "CREATE_SCRIPT", "HTMLScriptElement text", "RUN_TRUSTED_SCRIPT"]);
+ let messages2 = await script_messages_for(_ => {
+ let script = create_html_script_with_trusted_source_text(";");
+ window.log_message("SET");
+ script.insertAdjacentText("beforeend", LOG_RUN_MESSAGE);
+ window.log_message("APPEND");
+ container.appendChild(script);
+ });
+ assert_array_equals(messages2, ["SET", "APPEND", "CREATE_SCRIPT", "HTMLScriptElement text", "RUN_TRUSTED_SCRIPT"]);
+ }, "Default policy's calls when setting script source via Node.insertAdjacentText().");
+
+ promise_test(async t => {
+ let messages1 = await script_messages_for(_ => {
+ let script = create_html_script_with_trusted_source_text(";");
+ window.log_message("SET");
+ script.insertAdjacentHTML("afterbegin", LOG_RUN_MESSAGE);
+ window.log_message("APPEND");
+ container.appendChild(script);
+ });
+ assert_array_equals(messages1, ["SET", "CREATE_HTML", "Element insertAdjacentHTML", "APPEND", "CREATE_SCRIPT", "HTMLScriptElement text", "RUN_TRUSTED_SCRIPT_TRUSTED_HTML"]);
+ let messages2 = await script_messages_for(_ => {
+ let script = create_html_script_with_trusted_source_text(";");
+ window.log_message("SET");
+ script.insertAdjacentHTML("beforeend", LOG_RUN_MESSAGE);
+ window.log_message("APPEND");
+ container.appendChild(script);
+ });
+ assert_array_equals(messages2, ["SET", "CREATE_HTML", "Element insertAdjacentHTML", "APPEND", "CREATE_SCRIPT", "HTMLScriptElement text", "RUN_TRUSTED_SCRIPT_TRUSTED_HTML"]);
+ }, "Default policy's calls when setting script source via Node.insertAdjacentHTML().");
+
+ promise_test(async t => {
+ let messages = await script_messages_for(_ => {
+ let script = create_html_script_with_trusted_source_text(`;`);
+ let range = new Range();
+ range.selectNode(script.firstChild);
+ window.log_message("SET");
+ range.insertNode(document.createTextNode(LOG_RUN_MESSAGE));
+ window.log_message("APPEND");
+ container.appendChild(script);
+ });
+ assert_array_equals(messages, ["SET", "APPEND", "CREATE_SCRIPT", "HTMLScriptElement text", "RUN_TRUSTED_SCRIPT"]);
+ }, "Default policy's calls when setting source via Range.insertNode().");
+
+ promise_test(async t => {
+ let messages = await script_messages_for(_ => {
+ let script = create_html_script_with_trusted_source_text(`//;;;${LOG_RUN_MESSAGE}`);
+ script.firstChild.splitText(2);
+ let range = new Range();
+ range.setStart(script.firstChild, 0);
+ range.setEnd(script.lastChild, 3);
+ window.log_message("SET");
+ range.deleteContents();
+ window.log_message("APPEND");
+ container.appendChild(script);
+ });
+ assert_array_equals(messages, ["SET", "APPEND", "CREATE_SCRIPT", "HTMLScriptElement text", "RUN_TRUSTED_SCRIPT"]);
+ }, "Default policy's calls when setting script source via Range.deleteContents().");
+
+ promise_test(async t => {
+ let messages = await script_messages_for(_ => {
+ let script = create_html_script_with_trusted_source_text(`${LOG_RUN_MESSAGE}`);
+ window.log_message("CLONE");
+ let clone = script.cloneNode(true);
+ window.log_message("APPEND");
+ container.appendChild(clone);
+ });
+ assert_array_equals(messages, ["CLONE", "APPEND", "CREATE_SCRIPT", "HTMLScriptElement text", "RUN_TRUSTED_SCRIPT"]);
+ }, "Default policy's calls when cloning a script via Node.cloneNode().");
+
+ promise_test(async t => {
+ let messages = await script_messages_for(_ => {
+ let div = document.createElement("div");
+ let script = create_html_script_with_trusted_source_text(`${LOG_RUN_MESSAGE}`);
+ div.appendChild(script);
+ let range = new Range();
+ range.selectNode(script);
+ window.log_message("CLONE");
+ let documentFragment = range.cloneContents();
+ window.log_message("APPEND");
+ container.appendChild(documentFragment.firstElementChild);
+ });
+ assert_array_equals(messages, ["CLONE", "APPEND", "CREATE_SCRIPT", "HTMLScriptElement text", "RUN_TRUSTED_SCRIPT"]);
+ }, "Default policy's calls when cloning a script via Range.cloneContents().");
+</script>
+</body>
diff --git a/Tests/LibWeb/Text/input/wpt-import/trusted-types/script-enforcement-005.html b/Tests/LibWeb/Text/input/wpt-import/trusted-types/script-enforcement-005.html
new file mode 100644
index 00000000000..54433d43d2b
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/trusted-types/script-enforcement-005.html
@@ -0,0 +1,78 @@
+<!DOCTYPE html>
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+<script src="support/namespaces.js"></script>
+<script src="support/passthroughpolicy.js"></script>
+<script src="support/script-messages.js"></script>
+<link rel="help" href="https://w3c.github.io/trusted-types/dist/spec/#enforcement-in-scripts">
+<link rel="help" href="https://html.spec.whatwg.org/#prepare-the-script-element">
+<meta http-equiv="Content-Security-Policy" content="require-trusted-types-for 'script';">
+<!-- This test covers the following step from the "prepare the script element"
+ algorithm, verifying that "source text" is the one after application of
+ the default policy: "If el has no src attribute, and source text is the
+ empty string, then return." -->
+<div id="htmlContainer">
+ <script id="scriptToCreateNonEmptyHTMLScript" type="unknown">;</script>
+</div>
+<svg id="svgContainer">
+ <script id="scriptToCreateNonEmptySVGScript" type="unknown">;</script>
+</svg>
+<script>
+ // Define a default policy that transforms empty script string to some source
+ // logging a RUN message and other script strings to empty.
+ trustedTypes.createPolicy("default", {
+ createScript: (value, _, sink) => {
+ window.log_message("CREATE_SCRIPT");
+ window.log_message(sink);
+ return value.length ? "" : LOG_RUN_MESSAGE;
+ }
+ });
+
+ promise_test(async t => {
+ let messages = await script_messages_for(_ => {
+ // Current version of the specification requires the script text to change
+ // in order to force a call to the default policy callback with sink
+ // "HTMLScriptElement text". If the following PR is accepted, this could
+ // be simplified to create_html_script_with_untrusted_source_text("").
+ // https://github.com/w3c/trusted-types/pull/579
+ let script = document.getElementById("scriptToCreateNonEmptyHTMLScript");
+ script.remove();
+ script.removeAttribute("type");
+ script.firstChild.remove();
+ htmlContainer.appendChild(script);
+ });
+ assert_array_equals(messages, ["CREATE_SCRIPT", "HTMLScriptElement text", "RUN"]);
+ }, "Empty HTMLScriptElement is executed if the default policy makes it non-empty.");
+
+ promise_test(async t => {
+ let messages = await script_messages_for(_ => {
+ let script = create_html_script_with_untrusted_source_text(LOG_RUN_MESSAGE);
+ htmlContainer.appendChild(script);
+ });
+ assert_array_equals(messages, ["CREATE_SCRIPT", "HTMLScriptElement text"]);
+
+ }, "Non-empty HTMLScriptElement is not executed if the default policy makes it empty.");
+
+ promise_test(async t => {
+ let messages = await script_messages_for(_ => {
+ // Note: Using create_html_script_with_untrusted_source_text("") may not
+ // guarantee the script to be untrusted for implementations using a
+ // script-based enforcement mechanism. So make sure we do modify the text.
+ let script = document.getElementById("scriptToCreateNonEmptySVGScript");
+ script.remove();
+ script.removeAttribute("type");
+ script.firstChild.remove();
+ svgContainer.appendChild(script);
+ });
+ assert_array_equals(messages, ["CREATE_SCRIPT", "SVGScriptElement text", "RUN"]);
+ }, "Empty SVGScriptElement is executed if the default policy makes it non-empty.");
+
+ promise_test(async t => {
+ let messages = await script_messages_for(_ => {
+ let script = create_svg_script_with_untrusted_source_text(LOG_RUN_MESSAGE);
+ svgContainer.appendChild(script);
+ });
+ assert_array_equals(messages, ["CREATE_SCRIPT", "SVGScriptElement text"]);
+
+ }, "Non-empty SVGScriptElement is not executed if the default policy makes it empty.");
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/trusted-types/script-enforcement-006.html b/Tests/LibWeb/Text/input/wpt-import/trusted-types/script-enforcement-006.html
new file mode 100644
index 00000000000..9517e143e70
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/trusted-types/script-enforcement-006.html
@@ -0,0 +1,104 @@
+<!DOCTYPE html>
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+<script src="support/namespaces.js"></script>
+<script src="support/passthroughpolicy.js"></script>
+<script src="support/script-messages.js"></script>
+<link rel="help" href="https://w3c.github.io/trusted-types/dist/spec/#enforcement-in-scripts">
+<link rel="help" href="https://html.spec.whatwg.org/#prepare-the-script-element">
+<meta http-equiv="Content-Security-Policy" content="require-trusted-types-for 'script'">
+<!-- This test covers the following step from the "prepare the script element"
+ algorithm, verifying that "source text" is the one after application of
+ the default policy: "If el does not have a src content attribute: ...
+ Switch on el's type:" -->
+<div id="container"></div>
+<script>
+ let modulePolicyCalled = false;
+ let moduleScriptExecuted;
+ const logMessageModulePath = "./support/logMessage-module.sub.js";
+
+ // Define a default policy that transforms the script's type to some valid
+ // source content.
+ trustedTypes.createPolicy("default", {
+ createScript: (value, _, sink) => {
+ window.log_message("CREATE_SCRIPT");
+ window.log_message(sink);
+ switch (value) {
+ case "classic":
+ return `window.log_message('CLASSIC');`;
+ case "module":
+ modulePolicyCalled = true;
+ return `window.log_message('MODULE'); moduleScriptExecuted();`;
+ case "importmap":
+ return `{ "imports": { "${logMessageModulePath}?message=UNMAPPED": "${logMessageModulePath}?message=IMPORTMAP" }}`;
+ case "importmap2":
+ return `{ "imports": { "${logMessageModulePath}?message=UNMAPPED2": "${logMessageModulePath}?message=IMPORTMAP2" }}`;
+ case "importmap3":
+ return `{ "imports": { "${logMessageModulePath}?message=UNMAPPED3": "${logMessageModulePath}?message=IMPORTMAP3" }}`;
+ }
+ }
+ });
+
+ promise_test(async t => {
+ let messages = await script_messages_for(_ => {
+ let script = create_html_script_with_untrusted_source_text("classic");
+ script.setAttribute("type", "application/ecmascript");
+ // Appending the script will log "CLASSIC".
+ container.appendChild(script);
+ });
+ assert_array_equals(messages, ["CREATE_SCRIPT", "HTMLScriptElement text", "CLASSIC"]);
+ }, "Untrusted HTMLScriptElement with classic type uses the source text returned by the default policy.");
+
+ promise_test(async t => {
+ let messages = await script_messages_for(async _ => {
+ let script = create_html_script_with_untrusted_source_text("importmap");
+ script.setAttribute("type", "importmap");
+
+ // Appending the script sets up an import map for logMessageModulePath.
+ container.appendChild(script);
+
+ // Importing logMessageModulePath will log message "IMPORTMAP"
+ await import(`${logMessageModulePath}?message=UNMAPPED`);
+ });
+ assert_array_equals(messages, ["CREATE_SCRIPT", "HTMLScriptElement text", "IMPORTMAP"]);
+ }, "Untrusted HTMLScriptElement of importmap type uses the source text returned by the default policy.");
+
+ promise_test(async t => {
+ let messages = await script_messages_for(async _ => {
+ const executed = new Promise(resolve => { moduleScriptExecuted = resolve; });
+ let script = create_html_script_with_untrusted_source_text("module");
+ script.setAttribute("type", "module");
+
+ // Appending the script will log message "MODULE". Wait for its execution.
+ container.appendChild(script);
+ assert_true(modulePolicyCalled);
+ await executed;
+ });
+ assert_array_equals(messages, ["CREATE_SCRIPT", "HTMLScriptElement text", "MODULE"]);
+ }, "Untrusted HTMLScriptElement of module type uses the source text returned by the default policy.");
+
+ promise_test(async t => {
+ let messages = await script_messages_for(async _ => {
+ let script2 = create_html_script_with_untrusted_source_text("importmap2");
+ script2.setAttribute("type", "importmap");
+
+ // Appending the script sets up an import map for logMessageModulePath.
+ container.appendChild(script2);
+
+ // Importing logMessageModulePath will log message "IMPORTMAP2"
+ await import(`${logMessageModulePath}?message=UNMAPPED2`);
+
+ let script3 = create_html_script_with_untrusted_source_text("importmap3");
+ script3.setAttribute("type", "importmap");
+
+ // Appending the script sets up an import map for logMessageModulePath.
+ container.appendChild(script3);
+
+ // Importing logMessageModulePath will log message "IMPORTMAP3"
+ await import(`${logMessageModulePath}?message=UNMAPPED3`);
+ });
+ assert_array_equals(messages,
+ ["CREATE_SCRIPT", "HTMLScriptElement text", "IMPORTMAP2",
+ "CREATE_SCRIPT", "HTMLScriptElement text", "IMPORTMAP3"]);
+ }, "Untrusted HTMLScriptElement of two importmap type use the source text returned by the default policy.");
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/trusted-types/support/logMessage-module.sub.js b/Tests/LibWeb/Text/input/wpt-import/trusted-types/support/logMessage-module.sub.js
new file mode 100644
index 00000000000..e886bd54b31
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/trusted-types/support/logMessage-module.sub.js
@@ -0,0 +1 @@
+window.log_message("{{GET[message]}}");