Skip to content

Commit 60d46da

Browse files
committed
Bug 1596058 [wpt PR 20228] - [Trusted Types] Cover attribute node manipulation with Trusted Types checks., a=testonly
Automatic update from web-platform-tests [Trusted Types] Cover attribute node manipulation with Trusted Types checks. Element::setAttribute will perform trusted types checks, which (currently) can be circumvented by obtaining the DOM's attribute node and setting the value directly. This fixes this bypass, by performing identical checks when the attribute node values are set, and/or the attribute node is attached to an element. Bug: 1008012 Bug: w3c/trusted-types#47 Change-Id: I1d8ead85b3fa11821c329e1f4af60c1e85ea8298 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1911215 Commit-Queue: Daniel Vogelheim <vogelheimchromium.org> Reviewed-by: Mike West <mkwstchromium.org> Cr-Commit-Position: refs/heads/master{#716193} -- wpt-commits: 36362f1a77faf18831c8b596e7b5bee081629817 wpt-pr: 20228 UltraBlame original commit: 5765ef6d9f1a1d561d37a365cda5c2084245145f
1 parent 010c2fd commit 60d46da

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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

Comments
 (0)