Source code
Revision control
Copy as Markdown
Other Tools
Test Info: Warnings
- This test has a WPT meta file that expects 36 subtest issues.
- This WPT test may be referenced by the following Test IDs:
- /domparsing/tentative/stream-html-script-safe.html - WPT Dashboard Interop Dashboard
<!doctype html>
<meta charset="utf-8" />
<title>Sanitizer API safe streaming setters should not execute scripts when targeting script elements</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<body>
<script>
const containerMethods = [
"streamHTML",
"streamAppendHTML",
"streamPrependHTML",
"streamHTMLUnsafe",
"streamAppendHTMLUnsafe",
"streamPrependHTMLUnsafe",
];
const positionalMethods = [
"streamBeforeHTML",
"streamAfterHTML",
"streamReplaceWithHTML",
"streamBeforeHTMLUnsafe",
"streamAfterHTMLUnsafe",
"streamReplaceWithHTMLUnsafe",
];
function test_setter_on_script(method, is_positional) {
test((t) => {
const script = document.createElement("script");
const initialChild = document.createTextNode("/* setup */");
script.appendChild(initialChild);
document.body.appendChild(script);
t.add_cleanup(() => script.remove());
const target = is_positional ? initialChild : script;
const var_name = `executed_${method}`;
window[var_name] = false;
assert_throws_dom("NotSupportedError", () => {
target[method]();
});
assert_false(window[var_name], "Script should not execute");
assert_equals(script.childNodes.length, 1, "Existing children of <script> must remain untouched");
assert_equals(script.firstChild, initialChild, "Initial child node is preserved");
assert_equals(script.textContent, "/* setup */");
}, `Streaming setter ${method} targeting an HTML <script> element throws NotSupportedError and preserves children`);
}
for (const method of containerMethods) {
test_setter_on_script(method, false);
}
for (const method of positionalMethods) {
test_setter_on_script(method, true);
}
function test_setter_on_svg_script(method, is_positional) {
test((t) => {
const initialChild = document.createTextNode("/* setup */");
script.appendChild(initialChild);
svg.appendChild(script);
document.body.appendChild(svg);
t.add_cleanup(() => svg.remove());
const target = is_positional ? initialChild : script;
const var_name = `executed_svg_${method}`;
window[var_name] = false;
assert_throws_dom("NotSupportedError", () => {
target[method]();
});
assert_false(window[var_name], "Script should not execute");
assert_equals(script.childNodes.length, 1, "Existing children of SVG <script> must remain untouched");
assert_equals(script.firstChild, initialChild, "Initial child node is preserved");
assert_equals(script.textContent, "/* setup */");
}, `Streaming setter ${method} targeting an SVG <script> element throws NotSupportedError and preserves children`);
}
for (const method of containerMethods) {
test_setter_on_svg_script(method, false);
}
for (const method of positionalMethods) {
test_setter_on_svg_script(method, true);
}
// B4: NotSupportedError on <script> is thrown before invalid-sanitizer TypeError
for (const method of containerMethods) {
test((t) => {
const script = document.createElement("script");
const initialChild = document.createTextNode("/* setup */");
script.appendChild(initialChild);
document.body.appendChild(script);
t.add_cleanup(() => script.remove());
assert_throws_dom("NotSupportedError", () => {
script[method]({ sanitizer: { elements: ["div"], removeElements: ["div"] } });
});
assert_equals(script.firstChild, initialChild);
}, `${method} on <script> throws NotSupportedError before validating sanitizer config`);
}
for (const method of positionalMethods) {
test((t) => {
const script = document.createElement("script");
const initialChild = document.createTextNode("/* setup */");
script.appendChild(initialChild);
document.body.appendChild(script);
t.add_cleanup(() => script.remove());
assert_throws_dom("NotSupportedError", () => {
initialChild[method]({ sanitizer: { elements: ["div"], removeElements: ["div"] } });
});
assert_equals(script.firstChild, initialChild);
}, `${method} on child of <script> throws NotSupportedError before validating sanitizer config`);
}
</script>
</body>