Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve test coverage for Trusted Types and SVGScriptElement #49950

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 88 additions & 20 deletions trusted-types/trusted-types-svg-script-set-href.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="./support/resolve-spv.js"></script>
<script src="./support/namespaces.js"></script>
<meta http-equiv="Content-Security-Policy"
content="require-trusted-types-for 'script'">
</head>
Expand All @@ -14,8 +15,7 @@
createScript: x => x, createHTML: x => x, createScriptURL: x => x });

promise_test(t => {
const elem = document.createElementNS(
"http://www.w3.org/2000/svg", "script");
const elem = document.createElementNS(NSURI_SVG, "script");
assert_throws_js(TypeError, _ => {
elem.href.baseVal = "about:blank";
});
Expand All @@ -24,16 +24,16 @@
}, "Assign string to SVGScriptElement.href.baseVal.");

promise_test(t => {
const elem = document.createElementNS(
"http://www.w3.org/2000/svg", "script");
const elem = document.createElementNS(NSURI_SVG, "script");
elem.href.baseVal = policy.createScriptURL("about:blank");
assert_equals(elem.href.baseVal, "about:blank");
assert_equals(elem.getAttribute("href"), "about:blank");
document.getElementById("svg").appendChild(elem);
return Promise.resolve();
}, "Assign TrustedScriptURL to SVGScriptElement.href.baseVal.");

promise_test(t => {
const elem = document.createElementNS(
"http://www.w3.org/2000/svg", "script");
const elem = document.createElementNS(NSURI_SVG, "script");
assert_throws_js(TypeError, _ => {
elem.setAttribute("href", "about:blank");
});
Expand All @@ -42,16 +42,16 @@
}, "Assign string to non-attached SVGScriptElement.href via setAttribute.");

promise_test(t => {
const elem = document.createElementNS(
"http://www.w3.org/2000/svg", "script");
const elem = document.createElementNS(NSURI_SVG, "script");
elem.setAttribute("href", policy.createScriptURL("about:blank"));
assert_equals(elem.href.baseVal, "about:blank");
assert_equals(elem.getAttribute("href"), "about:blank");
document.getElementById("svg").appendChild(elem);
return Promise.resolve();
}, "Assign TrustedScriptURL to non-attached SVGScriptElement.href via setAttribute.");

promise_test(t => {
const elem = document.createElementNS(
"http://www.w3.org/2000/svg", "script");
const elem = document.createElementNS(NSURI_SVG, "script");
document.getElementById("svg").appendChild(elem);
assert_throws_js(TypeError, _ => {
elem.setAttribute("href", "about:blank");
Expand All @@ -60,15 +60,48 @@
}, "Assign string to attached SVGScriptElement.href via setAttribute.");

promise_test(t => {
const elem = document.createElementNS(
"http://www.w3.org/2000/svg", "script");
const elem = document.createElementNS(NSURI_SVG, "script");
document.getElementById("svg").appendChild(elem);
elem.setAttribute("href", policy.createScriptURL("about:blank"));
assert_equals(elem.href.baseVal, "about:blank");
assert_equals(elem.getAttribute("href"), "about:blank");
return Promise.resolve();
}, "Assign TrustedScriptURL to attached SVGScriptElement.href via setAttribute.");

// Default policy test: We repate the string assignment tests above,
// but now expect all of them to pass.
promise_test(t => {
const elem = document.createElementNS(NSURI_SVG, "script");
elem.className.baseVal = "myClass";
assert_equals(elem.className.baseVal, "myClass");
assert_equals(elem.getAttribute("class"), "myClass");
return Promise.resolve();
}, "Assign string to SVGScriptElement.className.baseVal.");

promise_test(t => {
const elem = document.createElementNS(NSURI_SVG, "script");
elem.className.baseVal = policy.createScriptURL("myClass");
assert_equals(elem.className.baseVal, "myClass");
assert_equals(elem.getAttribute("class"), "myClass");
return Promise.resolve();
}, "Assign TrustedScriptURL to SVGScriptElement.className.baseVal.");

promise_test(t => {
const elem = document.createElementNS(NSURI_SVG, "use");
elem.href.baseVal = "about:blank";
assert_equals(elem.href.baseVal, "about:blank");
assert_equals(elem.getAttribute("href"), "about:blank");
return Promise.resolve();
}, "Assign string to SVGUseElement.href.baseVal.");

promise_test(t => {
const elem = document.createElementNS(NSURI_SVG, "use");
elem.href.baseVal = policy.createScriptURL("about:blank");
assert_equals(elem.href.baseVal, "about:blank");
assert_equals(elem.getAttribute("href"), "about:blank");
return Promise.resolve();
}, "Assign TrustedScriptURL to SVGUseElement.href.baseVal.");

// Default policy test: We repeat the string assignment tests above,
// but now expect all assignments to succeed.
promise_test(t => {
trustedTypes.createPolicy("default", {
createScript: (x, _, sink) => {
Expand All @@ -93,27 +126,62 @@
}, "Assign String to SVGScriptElement.innerHTML w/ default policy.");

promise_test(t => {
const elem = document.createElementNS(
"http://www.w3.org/2000/svg", "script");
const elem = document.createElementNS(NSURI_SVG, "script");
elem.href.baseVal = "about:blank";
assert_equals(elem.href.baseVal, "about:blank");
assert_equals(elem.getAttribute("href"), "about:blank");
document.getElementById("svg").appendChild(elem);
return Promise.resolve();
}, "Assign string to SVGScriptElement.href.baseVal w/ default policy.");

promise_test(t => {
const elem = document.createElementNS(
"http://www.w3.org/2000/svg", "script");
const elem = document.createElementNS(NSURI_SVG, "script");
elem.setAttribute("href", "about:blank");
assert_equals(elem.href.baseVal, "about:blank");
assert_equals(elem.getAttribute("href"), "about:blank");
document.getElementById("svg").appendChild(elem);
return Promise.resolve();
}, "Assign string to non-attached SVGScriptElement.href via setAttribute w/ default policy.");

promise_test(t => {
const elem = document.createElementNS(
"http://www.w3.org/2000/svg", "script");
const elem = document.createElementNS(NSURI_SVG, "script");
document.getElementById("svg").appendChild(elem);
elem.setAttribute("href", "about:blank");
assert_equals(elem.href.baseVal, "about:blank");
assert_equals(elem.getAttribute("href"), "about:blank");
return Promise.resolve();
}, "Assign string to attached SVGScriptElement.href via setAttribute w/ default policy.");

promise_test(t => {
const elem = document.createElementNS(NSURI_SVG, "script");
elem.className.baseVal = "myClass";
assert_equals(elem.className.baseVal, "myClass");
assert_equals(elem.getAttribute("class"), "myClass");
return Promise.resolve();
}, "Assign string to SVGScriptElement.className.baseVal w/ default policy.");

promise_test(t => {
const elem = document.createElementNS(NSURI_SVG, "script");
elem.className.baseVal = policy.createScriptURL("myClass");
assert_equals(elem.className.baseVal, "myClass");
assert_equals(elem.getAttribute("class"), "myClass");
return Promise.resolve();
}, "Assign TrustedScriptURL to SVGScriptElement.className.baseVal w/ default policy.");

promise_test(t => {
const elem = document.createElementNS(NSURI_SVG, "use");
elem.href.baseVal = "about:blank";
assert_equals(elem.href.baseVal, "about:blank");
assert_equals(elem.getAttribute("href"), "about:blank");
return Promise.resolve();
}, "Assign string to SVGUseElement.href.baseVal w/ default policy.");

promise_test(t => {
const elem = document.createElementNS(NSURI_SVG, "use");
elem.href.baseVal = policy.createScriptURL("about:blank");
assert_equals(elem.href.baseVal, "about:blank");
assert_equals(elem.getAttribute("href"), "about:blank");
return Promise.resolve();
}, "Assign TrustedScriptURL to SVGUseElement.href.baseVal w/ default policy.");
</script>
</body>