-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathtrusted-types-navigation.html
More file actions
238 lines (216 loc) · 11.8 KB
/
trusted-types-navigation.html
File metadata and controls
238 lines (216 loc) · 11.8 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
<!DOCTYPE html>
<head>
<meta name="timeout" content="long">
<meta name="variant" content="?01-05">
<meta name="variant" content="?06-10">
<meta name="variant" content="?11-15">
<meta name="variant" content="?16-20">
<meta name="variant" content="?21-25">
<meta name="variant" content="?26-30">
<meta name="variant" content="?31-35">
<meta name="variant" content="?36-last">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="support/navigation-support.js"></script>
<script src="/common/subset-tests.js"></script>
</head>
<body>
<script>
"use strict";
function violationMessage(sample) {
return e => {
const result = (e.data.effectiveDirective == "require-trusted-types-for" &&
(!sample || e.data.sample.startsWith(sample)));
if (result) {
assert_true(true, "Expected violation as message: " + sample);
}
return result;
};
}
function loadedMessage(searchParams, originAndPathName) {
return e => {
if (e.data.type != "DOMContentLoaded") {
return false;
}
let url = new URL(e.data.uri);
if (!url.pathname.endsWith(originAndPathName)) {
return false;
}
let keys = Object.keys(searchParams);
if (url.searchParams.size != keys.length ||
!keys.every(key =>
url.searchParams.get(key) === searchParams[key].toString())) {
return false;
}
assert_true(true, `Expected loaded as message: ${joinToHref(searchParams, originAndPathName)}`);
return true;
};
}
function openWindowAndWaitMessages(test, uri, expectedMessages) {
const win = window.open(uri);
let messageSeen = (new Array(expectedMessages.length)).fill(false);
return new Promise((resolve, reject) => {
function listener(e) {
let receivedUnexpectedMessage = true;
expectedMessages.forEach((message, index) => {
if (message(e)) {
receivedUnexpectedMessage = false;
if (!messageSeen[index]) {
messageSeen[index] = true;
if (messageSeen.every(seen => seen)) {
resolve();
}
}
}
});
if (receivedUnexpectedMessage) {
reject(`Unexpected message received: ${JSON.stringify(e.data)}`);
}
}
test.add_cleanup(_ => {
window.removeEventListener("message", listener);
win.close();
});
window.addEventListener("message", listener);
});
}
// When adding more elements, adapt all functions consuming the existing elements.
const kNavigationElements =
[
"anchor",
"area",
"form-submission",
];
function maybeAddNavigationElementToSearchParams(navigationElement, searchParams) {
if (navigationElement == "anchor") {
return searchParams;
}
let extraParam = {};
extraParam[navigationElement] = 1;
return Object.assign(extraParam, searchParams);
}
function joinToHref(searchParams, originAndPathName) {
let urlSearchParams = new URLSearchParams(searchParams);
if (urlSearchParams.size > 0) {
return originAndPathName + "?" + urlSearchParams.toString();
}
return originAndPathName;
}
const kNavigationSupport = "navigation-support.html";
const kNavigationSupportReportOnly = "navigation-report-only-support.html";
for (const navigationElement of kNavigationElements) {
subsetTest(promise_test, t => {
const params = maybeAddNavigationElementToSearchParams(navigationElement, {});
return openWindowAndWaitMessages(t, `support/${joinToHref(params, kNavigationSupport)}`, [
loadedMessage(params, kNavigationSupport),
violationMessage("Location href"),
]);
}, `Navigate a window via ${navigationElement} with javascript:-urls in enforcing mode.`);
subsetTest(promise_test, t => {
const params = maybeAddNavigationElementToSearchParams(navigationElement, {defaultpolicy: 'replace'});
return openWindowAndWaitMessages(t, `support/${joinToHref(params, kNavigationSupport)}`, [
loadedMessage(params, kNavigationSupport),
loadedMessage(Object.assign({navigationattempted: 1, defaultpolicywashere: 1}, params), kNavigationSupport),
]);
}, `Navigate a window via ${navigationElement} with javascript:-urls w/ default policy in enforcing mode.`);
subsetTest(promise_test, t => {
const params = maybeAddNavigationElementToSearchParams(navigationElement, {});
return openWindowAndWaitMessages(t, `support/${joinToHref(params, kNavigationSupportReportOnly)}`, [
loadedMessage(params, kNavigationSupportReportOnly),
violationMessage("Location href"),
loadedMessage(Object.assign({navigationattempted: 1, continue: 1}, params), kNavigationSupport),
]);
}, `Navigate a window via ${navigationElement} with javascript:-urls in report-only mode.`);
subsetTest(promise_test, t => {
const params = maybeAddNavigationElementToSearchParams(navigationElement, {defaultpolicy: 'replace'});
return openWindowAndWaitMessages(t, `support/${joinToHref(params, kNavigationSupportReportOnly)}`, [
loadedMessage(params, kNavigationSupportReportOnly),
loadedMessage(Object.assign({navigationattempted: 1, defaultpolicywashere: 1}, params), kNavigationSupport),
]);
}, `Navigate a window via ${navigationElement} with javascript:-urls w/ default policy in report-only mode.`);
subsetTest(promise_test, t => {
const params = maybeAddNavigationElementToSearchParams(navigationElement, {frame: 1});
return openWindowAndWaitMessages(t, `support/${joinToHref(params, kNavigationSupport)}`, [
loadedMessage(params, kNavigationSupport),
violationMessage("Location href"),
]);
}, `Navigate a frame via ${navigationElement} with javascript:-urls in enforcing mode.`);
subsetTest(promise_test, t => {
const params = maybeAddNavigationElementToSearchParams(navigationElement,
{defaultpolicy: 'replace', frame: 1});
return openWindowAndWaitMessages(t, `support/${joinToHref(params, kNavigationSupport)}`, [
loadedMessage(params, kNavigationSupport),
loadedMessage(Object.assign({navigationattempted: 1, defaultpolicywashere: 1}, params), kNavigationSupport),
]);
}, `Navigate a frame via ${navigationElement} with javascript:-urls w/ default policy in enforcing mode.`);
subsetTest(promise_test, t => {
const params = maybeAddNavigationElementToSearchParams(navigationElement, {frame: 1})
return openWindowAndWaitMessages(t, `support/${joinToHref(params, kNavigationSupportReportOnly)}`, [
loadedMessage(params, kNavigationSupportReportOnly),
violationMessage("Location href"),
loadedMessage(Object.assign({navigationattempted: 1, continue: 1}, params), kNavigationSupport),
]);
}, `Navigate a frame via ${navigationElement} with javascript:-urls in report-only mode.`);
subsetTest(promise_test, t => {
const params = maybeAddNavigationElementToSearchParams(navigationElement,
{defaultpolicy: 'replace', frame: 1});
return openWindowAndWaitMessages(t, `support/${joinToHref(params, kNavigationSupportReportOnly)}`, [
loadedMessage(params, kNavigationSupportReportOnly),
loadedMessage(Object.assign({navigationattempted: 1, defaultpolicywashere: 1}, params), kNavigationSupport),
]);
}, `Navigate a frame via ${navigationElement} with javascript:-urls w/ default policy in report-only mode.`);
subsetTest(promise_test, t => {
// This test navigates to a 'javascript:location.href=...' URL with a CSP
// policy in enforcing mode and a default policy throwing an exception.
// "require-trusted-types-for Pre-Navigation check" returns "Blocked"
// per step 4 of https://w3c.github.io/trusted-types/dist/spec/#require-trusted-types-for-pre-navigation-check
// Then a violation is reported and the navigation is blocked per steps 4 and 5 of https://w3c.github.io/webappsec-csp/#should-block-navigation-request
const params = maybeAddNavigationElementToSearchParams(navigationElement, {defaultpolicy: 'throw'});
return openWindowAndWaitMessages(t, `support/${joinToHref(params, kNavigationSupport)}`, [
loadedMessage(params, kNavigationSupport),
violationMessage("Location href"),
]);
}, `Navigate a window via ${navigationElement} with javascript:-urls w/ a default policy throwing an exception in enforcing mode.`);
subsetTest(promise_test, t => {
// This test navigates to a 'javascript:location.href=...' URL with a CSP
// policy in report-only mode and a default policy throwing an exception.
// "require-trusted-types-for Pre-Navigation check" returns "Blocked"
// per step 4 of https://w3c.github.io/trusted-types/dist/spec/#require-trusted-types-for-pre-navigation-check
// Then a violation is reported per step 4 of https://w3c.github.io/webappsec-csp/#should-block-navigation-request
// The "location.href=..." is executed, causing a second navigation.
const params = maybeAddNavigationElementToSearchParams(navigationElement, {defaultpolicy: 'throw'});
return openWindowAndWaitMessages(t, `support/${joinToHref(params, kNavigationSupportReportOnly)}`, [
loadedMessage(params, kNavigationSupportReportOnly),
violationMessage("Location href"),
loadedMessage(Object.assign({navigationattempted: 1, continue: 1}, params), kNavigationSupport),
]);
}, `Navigate a window via ${navigationElement} with javascript:-urls w/ a default policy throwing an exception in report-only mode.`);
subsetTest(promise_test, t => {
// This test navigates to a 'javascript:location.href=...' URL with a CSP
// policy in enforcing mode and a default policy making the URL invalid.
// "require-trusted-types-for Pre-Navigation check" returns "Blocked"
// per step 6 of https://w3c.github.io/trusted-types/dist/spec/#require-trusted-types-for-pre-navigation-check
// Then a violation is reported and the navigation is blocked per steps 4 and 5 of https://w3c.github.io/webappsec-csp/#should-block-navigation-request
const params = maybeAddNavigationElementToSearchParams(navigationElement, {defaultpolicy: 'make-invalid'});
return openWindowAndWaitMessages(t, `support/${joinToHref(params, kNavigationSupport)}`, [
loadedMessage(params, kNavigationSupport),
violationMessage("Location href"),
]);
}, `Navigate a window via ${navigationElement} with javascript:-urls w/ a default policy making the URL invalid in enforcing mode.`);
subsetTest(promise_test, t => {
// This test navigates to a 'javascript:location.href=...' URL with a CSP
// policy in report-only mode and a default policy making the URL invalid.
// "require-trusted-types-for Pre-Navigation check" returns "Blocked"
// per step 6 of https://w3c.github.io/trusted-types/dist/spec/#require-trusted-types-for-pre-navigation-check
// Then a violation is reported per step 4 of https://w3c.github.io/webappsec-csp/#should-block-navigation-request
// The "location.href=..." is executed, causing a second navigation.
const params = maybeAddNavigationElementToSearchParams(navigationElement, {defaultpolicy: 'make-invalid'});
return openWindowAndWaitMessages(t, `support/${joinToHref(params, kNavigationSupportReportOnly)}`, [
loadedMessage(params, kNavigationSupportReportOnly),
violationMessage("Location href"),
loadedMessage(Object.assign({navigationattempted: 1, continue: 1}, params), kNavigationSupport),
]);
}, `Navigate a window via ${navigationElement} with javascript:-urls w/ a default policy making the URL invalid in report-only mode.`);
}
</script>
</body>