-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expand file tree
/
Copy pathscript-enforcement-001.html
More file actions
351 lines (317 loc) · 14.7 KB
/
Copy pathscript-enforcement-001.html
File metadata and controls
351 lines (317 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="support/namespaces.js"></script>
<script src="support/passthroughpolicy.js"></script>
<script src="support/script-messages.js"></script>
<link rel="help" href="https://w3c.github.io/trusted-types/dist/spec/#enforcement-in-scripts">
<meta http-equiv="Content-Security-Policy" content="require-trusted-types-for 'script';">
<!-- This test modifies the source of a new (initially disconnected)
HTMLScriptElement by various DOM APIs and verifies whether it will
remain trustworthy. This can be done by checking whether the script
is actually executed after insertion, because this page enforces
Trusted Types without defining any default policy. -->
<div id="container"></div>
<script>
promise_test(async t => {
let message = await script_message_for(_ => {
let script = create_html_script_with_trusted_source_text(LOG_RUN_MESSAGE);
container.appendChild(script);
});
assert_equals(message, "RUN");
}, "The HTMLScriptElement is initially trusted.");
promise_test(async t => {
await promise_rejects_js(t, TypeError, script_messages_for(_ => {
document.createElement("script").innerText = LOG_RUN_MESSAGE;
}), "TrustedScript required.");
let message = await script_message_for(_ => {
let script = document.createElement("script");
script.innerText = passthroughpolicy.createScript(LOG_RUN_MESSAGE);
container.appendChild(script);
});
assert_equals(message, "RUN");
}, "Script source set via TrustedScript sink HTMLScriptElement.innerText keeps trustworthiness.");
promise_test(async t => {
let script = document.createElement("script");
Object.getOwnPropertyDescriptor(HTMLElement.prototype, "innerText").set.call(script, LOG_RUN_MESSAGE);
assert_equals(script.innerText, LOG_RUN_MESSAGE, "TrustedScript not required.");
await no_script_message_for(_ => {
container.appendChild(script);
});
}, "Script source set via HTMLElement.innerText drops trustworthiness.");
promise_test(async t => {
await promise_rejects_js(t, TypeError, script_messages_for(_ => {
document.createElement("script").textContent = LOG_RUN_MESSAGE;
}), "TrustedScript required.");
let message = await script_message_for(_ => {
let script = document.createElement("script");
script.textContent = passthroughpolicy.createScript(LOG_RUN_MESSAGE);
container.appendChild(script);
});
assert_equals(message, "RUN");
}, "Script source set via TrustedScript sink HTMLScriptElement.textContent keeps trustworthiness.");
promise_test(async t => {
let script = document.createElement("script");
Object.getOwnPropertyDescriptor(Node.prototype, "textContent").set.call(script, LOG_RUN_MESSAGE);
assert_equals(script.textContent, LOG_RUN_MESSAGE, "TrustedScript not required.");
await no_script_message_for(_ => {
container.appendChild(script);
});
}, "Script source set Node.textContent drops trustworthiness.");
promise_test(async t => {
await promise_rejects_js(t, TypeError, script_messages_for(_ => {
document.createElement("script").text = LOG_RUN_MESSAGE;
}), "TrustedScript required.");
let message = await script_message_for(_ => {
let script = document.createElement("script");
script.text = passthroughpolicy.createScript(LOG_RUN_MESSAGE);
container.appendChild(script);
});
assert_equals(message, "RUN");
}, "Script source set via TrustedScript sink HTMLScriptElement.text keeps trustworthiness.");
promise_test(async t => {
await promise_rejects_js(t, TypeError, script_messages_for(_ => {
document.createElement("script").innerHTML = LOG_RUN_MESSAGE;
}), "TrustedHTML required.");
await no_script_message_for(_ => {
let script = document.createElement("script");
script.innerHTML = passthroughpolicy.createHTML(LOG_RUN_MESSAGE);
container.appendChild(script);
});
}, "Script source set via TrustedHTML sink Element.innerHTML drops trustworthiness.");
promise_test(async t => {
await promise_rejects_js(t, TypeError, script_messages_for(_ => {
document.createElement("script").setHTMLUnsafe(LOG_RUN_MESSAGE);
}), "TrustedHTML required.");
await no_script_message_for(_ => {
let script = document.createElement("script");
script.setHTMLUnsafe(passthroughpolicy.createHTML(LOG_RUN_MESSAGE));
container.appendChild(script);
});
}, "Script source set via TrustedHTML sink Element.setHTMLUnsafe() drops trustworthiness.");
if (HTMLScriptElement.prototype.setHTML) {
promise_test(async t => {
// https://wicg.github.io/sanitizer-api/#set-and-filter-html
let script = document.createElement("script");
script.setHTML(LOG_RUN_MESSAGE);
assert_equals(script.text, "");
}, "Script source cannot be set via Element.setHTML().");
}
promise_test(async t => {
let message = await script_message_for(_ => {
let script = create_html_script_with_trusted_source_text(`${LOG_RUN_MESSAGE};;;`);
script.firstChild.splitText(3);
container.appendChild(script);
});
assert_equals(message, "RUN");
}, "Splitting script source via Text.splitText() keeps trustworthiness.");
promise_test(async t => {
let message = await script_message_for(_ => {
let script = create_html_script_with_trusted_source_text(`${LOG_RUN_MESSAGE};;;`);
script.firstChild.splitText(3);
script.normalize();
container.appendChild(script);
});
assert_equals(message, "RUN");
}, "Normalizing script source via Element.normalize() keeps trustworthiness.");
promise_test(async t => {
await no_script_message_for(_ => {
let script = create_html_script_with_trusted_source_text(";");
script.firstChild.nodeValue = LOG_RUN_MESSAGE;
container.appendChild(script);
});
}, "Script source set via Node.nodeValue drops trustworthiness.");
promise_test(async t => {
await no_script_message_for(_ => {
let script = create_html_script_with_trusted_source_text(";");
script.firstChild.data = LOG_RUN_MESSAGE;
container.appendChild(script);
});
}, "Setting script source via CharacterData.data drops trustworthiness.");
promise_test(async t => {
await no_script_message_for(_ => {
let script = create_html_script_with_trusted_source_text(";");
script.firstChild.appendData(LOG_RUN_MESSAGE);
container.appendChild(script);
});
}, "Setting script source via CharacterData.appendData() drops trustworthiness.");
promise_test(async t => {
await no_script_message_for(_ => {
let script = create_html_script_with_trusted_source_text(";");
script.firstChild.insertData(0, LOG_RUN_MESSAGE);
container.appendChild(script);
});
}, "Setting script source via CharacterData.insertData() drops trustworthiness.");
promise_test(async t => {
await no_script_message_for(_ => {
let script = create_html_script_with_trusted_source_text(";");
script.firstChild.replaceData(0, 0, LOG_RUN_MESSAGE);
container.appendChild(script);
});
}, "Setting script source via CharacterData.replaceData() drops trustworthiness.");
promise_test(async t => {
await no_script_message_for(_ => {
let script = create_html_script_with_trusted_source_text(`//${LOG_RUN_MESSAGE}`);
script.firstChild.deleteData(0, 2);
container.appendChild(script);
});
}, "Setting script source via CharacterData.deleteData() drops trustworthiness.");
promise_test(async t => {
await no_script_message_for(_ => {
let script = create_html_script_with_trusted_source_text(";");
script.firstChild.before(LOG_RUN_MESSAGE);
container.appendChild(script);
});
}, "Setting script source via CharacterData.before() drops trustworthiness.");
promise_test(async t => {
await no_script_message_for(_ => {
let script = create_html_script_with_trusted_source_text(";");
script.firstChild.after(LOG_RUN_MESSAGE);
container.appendChild(script);
});
}, "Setting script source via CharacterData.after() drops trustworthiness.");
promise_test(async t => {
await no_script_message_for(_ => {
let script = create_html_script_with_trusted_source_text(`;;;${LOG_RUN_MESSAGE}`);
script.firstChild.splitText(3);
script.firstChild.remove();
container.appendChild(script);
});
}, "Setting script source via CharacterData.remove() drops trustworthiness.");
promise_test(async t => {
await no_script_message_for(_ => {
let script = create_html_script_with_trusted_source_text(";");
script.firstChild.replaceWith(document.createTextNode(LOG_RUN_MESSAGE));
container.appendChild(script);
});
}, "Setting script source via CharacterData.replaceWith() drops trustworthiness.");
promise_test(async t => {
await no_script_message_for(_ => {
let script = create_html_script_with_trusted_source_text(";");
script.appendChild(document.createTextNode(LOG_RUN_MESSAGE));
container.appendChild(script);
});
}, "Setting script source via Node.appendChild() drops trustworthiness.");
promise_test(async t => {
await no_script_message_for(_ => {
let script = create_html_script_with_trusted_source_text(";");
script.insertBefore(document.createTextNode(LOG_RUN_MESSAGE), script.firstChild);
container.appendChild(script);
});
}, "Setting script source via Node.insertBefore() drops trustworthiness.");
promise_test(async t => {
await no_script_message_for(_ => {
let script = create_html_script_with_trusted_source_text(";");
script.replaceChild(document.createTextNode(LOG_RUN_MESSAGE), script.firstChild);
container.appendChild(script);
});
}, "Setting script source via Node.replaceChild() drops trustworthiness.");
promise_test(async t => {
await no_script_message_for(_ => {
let script = create_html_script_with_trusted_source_text(`;;;${LOG_RUN_MESSAGE}`);
script.firstChild.splitText(3);
script.removeChild(script.firstChild);
container.appendChild(script);
});
}, "Setting script source via Node.removeChild() drops trustworthiness.");
promise_test(async t => {
await no_script_message_for(_ => {
let script = create_html_script_with_trusted_source_text(";");
script.prepend(document.createTextNode(LOG_RUN_MESSAGE));
container.appendChild(script);
});
}, "Setting script source via Element.prepend() drops trustworthiness.");
promise_test(async t => {
await no_script_message_for(_ => {
let script = create_html_script_with_trusted_source_text(";");
script.append(document.createTextNode(LOG_RUN_MESSAGE));
container.appendChild(script);
});
}, "Setting script source via Element.append() drops trustworthiness.");
promise_test(async t => {
await no_script_message_for(_ => {
let script = create_html_script_with_trusted_source_text(";");
script.replaceChildren(document.createTextNode(LOG_RUN_MESSAGE));
container.appendChild(script);
});
}, "Setting script source via Element.replaceChildren() drops trustworthiness.");
promise_test(async t => {
await no_script_message_for(_ => {
let script = create_html_script_with_trusted_source_text(";");
let text = document.createTextNode(LOG_RUN_MESSAGE);
// Per https://dom.spec.whatwg.org/#move, step 1, moveBefore requires
// the two nodes to have the same shadow-including root.
let root = document.createElement("div");
root.appendChild(script);
root.appendChild(text);
script.moveBefore(text, script.firstChild);
container.appendChild(script);
});
}, "Setting script source via Element.moveBefore() drops trustworthiness.");
promise_test(async t => {
await promise_rejects_js(t, TypeError, script_messages_for(_ => {
document.createElement("script").insertAdjacentHTML("afterbegin", LOG_RUN_MESSAGE);
}), "TrustedHTML required.");
await no_script_message_for(_ => {
let script = create_html_script_with_trusted_source_text(";");
script.insertAdjacentHTML("afterbegin", passthroughpolicy.createHTML(LOG_RUN_MESSAGE));
container.appendChild(script);
});
await no_script_message_for(_ => {
let script = create_html_script_with_trusted_source_text(";");
script.insertAdjacentHTML("beforeend", passthroughpolicy.createHTML(LOG_RUN_MESSAGE));
container.appendChild(script);
});
}, "Setting script source via TrustedHTML sink Node.insertAdjacentHTML() drops trustworthiness.");
promise_test(async t => {
await no_script_message_for(_ => {
let script = create_html_script_with_trusted_source_text(";");
script.insertAdjacentText("afterbegin", LOG_RUN_MESSAGE);
container.appendChild(script);
});
await no_script_message_for(_ => {
let script = create_html_script_with_trusted_source_text(";");
script.insertAdjacentText("beforeend", LOG_RUN_MESSAGE);
container.appendChild(script);
});
}, "Setting script source via Node.insertAdjacentText() drops trustworthiness.");
promise_test(async t => {
await no_script_message_for(_ => {
let script = create_html_script_with_trusted_source_text(`;`);
let range = new Range();
range.selectNode(script.firstChild);
range.insertNode(document.createTextNode(LOG_RUN_MESSAGE));
container.appendChild(script);
});
}, "Setting script source via Range.insertNode() drops trustworthiness.");
promise_test(async t => {
await no_script_message_for(_ => {
let script = create_html_script_with_trusted_source_text(`//;;;${LOG_RUN_MESSAGE}`);
script.firstChild.splitText(2);
let range = new Range();
range.setStart(script.firstChild, 0);
range.setEnd(script.lastChild, 3);
range.deleteContents();
container.appendChild(script);
});
}, "Setting script source via Range.deleteContents() drops trustworthiness.");
promise_test(async t => {
await no_script_message_for(_ => {
let script = create_html_script_with_trusted_source_text(`${LOG_RUN_MESSAGE}`);
let clone = script.cloneNode(true);
container.appendChild(clone);
});
}, "Cloning a script via Node.cloneNode() drops trustworthiness.");
promise_test(async t => {
await no_script_message_for(_ => {
let div = document.createElement("div");
let script = create_html_script_with_trusted_source_text(`${LOG_RUN_MESSAGE}`);
div.appendChild(script);
let range = new Range();
range.selectNode(script);
let documentFragment = range.cloneContents();
container.appendChild(documentFragment.firstElementChild);
});
}, "Cloning a script via Range.cloneContents() drops trustworthiness.");
</script>
</body>