|
| 1 | +<!DOCTYPE html> |
| 2 | +<head> |
| 3 | +<script src="/resources/testharness.js"></script> |
| 4 | +<script src="/resources/testharnessreport.js"></script> |
| 5 | +<meta http-equiv="Content-Security-Policy" content="trusted-types *"> |
| 6 | +</head> |
| 7 | +<body> |
| 8 | +<script> |
| 9 | + // This is a set of tests that try to make sure various ways of setting |
| 10 | + // attributes (directly; via an attribute node; etc.) are all being treated |
| 11 | + // identically. For background, se:: |
| 12 | + // https://github.com/w3c/webappsec-trusted-types/issues/47 |
| 13 | + |
| 14 | + const elems = ["div", "script", "embed", "iframe"]; |
| 15 | + const attrs = ["src", "srcdoc", "onclick", "bla"]; |
| 16 | + |
| 17 | + const policy_callback = s => ("" + s + " [policy]"); |
| 18 | + const policy = trustedTypes.createPolicy("policy", { |
| 19 | + "createHTML": policy_callback, |
| 20 | + "createScript": policy_callback, |
| 21 | + "createScriptURL": policy_callback, |
| 22 | + }); |
| 23 | + |
| 24 | + // Returns either the string-ified result value of fn, or an exception type. |
| 25 | + function try_get(fn) { |
| 26 | + try { |
| 27 | + return "" + fn(); |
| 28 | + } catch(e) { |
| 29 | + return "" + e.name; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + // Returns a 'trusted' version of value, if element.attribute requires it. |
| 34 | + function trusted(element, attribute, value) { |
| 35 | + switch (trustedTypes.getAttributeType(element, attribute)) { |
| 36 | + case 'TrustedHTML': value = policy.createHTML(value); break; |
| 37 | + case 'TrustedScript': value = policy.createScript(value); break; |
| 38 | + case 'TrustedScriptURL': value = policy.createScriptURL(value); break; |
| 39 | + } |
| 40 | + return value; |
| 41 | + } |
| 42 | + |
| 43 | + for (elem of elems) { |
| 44 | + for (attr of attrs) { |
| 45 | + const reference = try_get(_ => { |
| 46 | + const e = document.createElement(elem); |
| 47 | + e.setAttribute(attr, "hello"); |
| 48 | + return e.getAttribute(attr); |
| 49 | + }); |
| 50 | + |
| 51 | + // Event handlers (like "onclick") apply to all elements, so we don't |
| 52 | + // really have 'harmless' element we can attach them to. Hence this test |
| 53 | + // case doesn't apply. |
| 54 | + if (attr != "onclick") { |
| 55 | + test(t => { |
| 56 | + const harmless = document.createElement("div"); |
| 57 | + harmless.setAttribute(attr, "hello"); |
| 58 | + const node = harmless.getAttributeNode(attr); |
| 59 | + harmless.removeAttributeNode(node); |
| 60 | + |
| 61 | + const actual = try_get(_ => { |
| 62 | + const e = document.createElement(elem); |
| 63 | + e.setAttributeNode(node); |
| 64 | + return e.getAttribute(attr); |
| 65 | + }); |
| 66 | + assert_equals(actual, reference); |
| 67 | + }, `Set attribute via attribute node on ${elem}.${attr}.`); |
| 68 | + } |
| 69 | + |
| 70 | + test(t => { |
| 71 | + const e = document.createElement(elem); |
| 72 | + e.setAttribute(attr, trusted(elem, attr, "123")); |
| 73 | + const actual = try_get(_ => { |
| 74 | + e.getAttributeNode(attr).value = "hello"; |
| 75 | + return e.getAttribute(attr); |
| 76 | + }); |
| 77 | + assert_equals(actual, reference); |
| 78 | + }, `Set attribute via attributenode.value on ${elem}.${attr}.`); |
| 79 | + |
| 80 | + test(t => { |
| 81 | + const e = document.createElement(elem); |
| 82 | + const attrnode = document.createAttribute(attr); |
| 83 | + attrnode.value = "hello"; |
| 84 | + const actual = try_get(_ => { |
| 85 | + e.attributes.setNamedItem(attrnode); |
| 86 | + return e.getAttribute(attr); |
| 87 | + }); |
| 88 | + assert_equals(actual, reference); |
| 89 | + }, `Set attribute via NamedNodeMap.setNamedItem on ${elem}.${attr}.`); |
| 90 | + } |
| 91 | + } |
| 92 | +</script> |
| 93 | +</body> |
0 commit comments