Skip to content

Commit

Permalink
part 2) Transform the test which is commented out in <Element-setAttr…
Browse files Browse the repository at this point in the history
…ibuteNS.html> to a valid one which is executed.

Regarding the removed TODO: checking one non-null namespace should
suffice, since other non-null namespaces likely take the same codepath.

Differential Revision: https://phabricator.services.mozilla.com/D231565

bugzilla-url: https://bugzilla.mozilla.org/show_bug.cgi?id=1907849
gecko-commit: bc406a35457dd43ee7e738683b1a4817e993def1
gecko-reviewers: smaug
  • Loading branch information
mbrodesser-Igalia authored and moz-wptsync-bot committed Dec 11, 2024
1 parent 5a699cb commit 9fe5f14
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 28 deletions.
17 changes: 5 additions & 12 deletions trusted-types/Element-setAttributeNS.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/helper.sub.js"></script>
<script src="support/namespaces.js"></script>
</head>
<body>
<script>
Expand All @@ -18,17 +19,9 @@
assert_element_accepts_trusted_script_url_set_ns(window, '2', t, 'a', 'b', RESULTS.SCRIPTURL);
}, "Element.setAttributeNS assigned via policy (successful ScriptURL transformation)");

// TODO: Is there any non-URL, namespaced accessor left?
/*
test(t => {
let p = createURL_policy(window, '5');
let url = p.createURL(INPUTS.URL);
let elem = document.createElementNS("http://www.w3.org/2000/svg", "image");
elem.setAttributeNS("http://www.w3.org/1999/xlink", "href", url);
let attr_node = elem.getAttributeNodeNS("http://www.w3.org/1999/xlink", "href");
assert_equals(attr_node.value + "", RESULTS.URL);
test(t=> {
const policy = trustedTypes.createPolicy("p", { createScriptURL: s => s + s });
assert_element_accepts_value_set_ns("image", "href", policy.createScriptURL("v"), "vv",
NSURI_SVG, NSURI_XLINK);
}, "Element.setAttributeNS accepts a URL on <svg:image xlink:href/>");
*/

</script>
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="support/helper.sub.js"></script>

<script src="support/namespaces.js"></script>
<meta http-equiv="Content-Security-Policy" content="require-trusted-types-for 'script';">
</head>
<body>
Expand All @@ -25,37 +25,34 @@

// Unknown attributes should not be TT checked:
test(t => {
assert_element_accepts_non_trusted_type_set_ns('a', 'b', 'A string', 'A string', htmlNamespace, null);
assert_element_accepts_value_set_ns('a', 'b', 'A string', 'A string', htmlNamespace, null);
}, "Element.setAttributeNS accepts untrusted string for non-specced accessor");

test(t => {
assert_element_accepts_non_trusted_type_set_ns('a', 'b', null, 'null', htmlNamespace, null);
assert_element_accepts_value_set_ns('a', 'b', null, 'null', htmlNamespace, null);
}, "Element.setAttributeNS accepts null for non-specced accessor");

// Setup trusted values for use in subsequent tests.
const script_url = createScriptURL_policy(window, '5').createScriptURL(INPUTS.ScriptURL);
const html = createHTML_policy(window, '6').createHTML(INPUTS.HTML);
const script = createScript_policy(window, '7').createScript(INPUTS.Script);

const xlinkNamespace = "http://www.w3.org/1999/xlink";
const svgNamespace = "http://www.w3.org/2000/svg";

// svg:script xlink:href=... expects a TrustedScriptURL.
// Assigning a TrustedScriptURL works.
test(t => {
let elem = document.createElementNS(svgNamespace, "script");
elem.setAttributeNS(xlinkNamespace, "href", script_url);
let elem = document.createElementNS(NSURI_SVG, "script");
elem.setAttributeNS(NSURI_XLINK, "href", script_url);
assert_equals("" + RESULTS.ScriptURL,
elem.getAttributeNodeNS(xlinkNamespace, "href").value);
elem.getAttributeNodeNS(NSURI_XLINK, "href").value);
}, "Assigning TrustedScriptURL to <svg:script xlink:href=...> works");

// Assigning things that ought to not work.
test(t => {
let elem = document.createElementNS(svgNamespace, "script");
let elem = document.createElementNS(NSURI_SVG, "script");
const values = [ "abc", null, html, script ];
for (const v of values) {
assert_throws_js(TypeError, _ => {
elem.setAttributeNS(xlinkNamespace, "href", v);
elem.setAttributeNS(NSURI_XLINK, "href", v);
});
}
}, "Blocking non-TrustedScriptURL assignment to <svg:script xlink:href=...> works");
Expand All @@ -64,16 +61,16 @@
const nonLowerCaseTests = [
{ element: "iframe", attribute: "SRCDOC", elementNamespace: htmlNamespace },
{ element: "script", attribute: "SRC", elementNamespace: htmlNamespace },
{ element: "script", attribute: "HREF", elementNamespace: svgNamespace },
{ element: "script", attribute: "HREF", elementNamespace: svgNamespace,
attributeNamespace: xlinkNamespace },
{ element: "script", attribute: "HREF", elementNamespace: NSURI_SVG },
{ element: "script", attribute: "HREF", elementNamespace: NSURI_SVG,
attributeNamespace: NSURI_XLINK },
];

for (const testData of nonLowerCaseTests) {
const attributeNamespace = testData.attributeNamespace ?? null;

test(t => {
assert_element_accepts_non_trusted_type_set_ns(testData.element, testData.attribute, "v",
assert_element_accepts_value_set_ns(testData.element, testData.attribute, "v",
"v", testData.elementNamespace, attributeNamespace);
}, "Check `setAttributeNS` allows setting non-trusted string for non-lowercase attribute \"" +
testData.attribute + "\" (ns=" + attributeNamespace + ") for \"" + testData.element +
Expand Down
2 changes: 1 addition & 1 deletion trusted-types/support/helper.sub.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ function assert_throws_no_trusted_type_set_ns(tag, attribute, value) {
});
}

function assert_element_accepts_non_trusted_type_set_ns(tag, attribute, value, expected,
function assert_element_accepts_value_set_ns(tag, attribute, value, expected,
elementNamespace, attributeNamespace) {
let elem = document.createElementNS(elementNamespace, tag);
elem.setAttributeNS(attributeNamespace, attribute, value);
Expand Down

0 comments on commit 9fe5f14

Please sign in to comment.