Skip to content

Commit 42add81

Browse files
authored
1 parent 695796e commit 42add81

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!DOCTYPE html>
2+
<script src="/resources/testharness.js"></script>
3+
<script src="/resources/testharnessreport.js"></script>
4+
<script src="/common/rendering-utils.js"></script>
5+
<script src="./support/csp-violations.js"></script>
6+
<script src="./support/navigation-support.js"></script>
7+
<script>
8+
promise_test(async _ => {
9+
await new Promise(resolve => window.addEventListener("load", resolve));
10+
const result = await setLocationToJavaScriptURL("throw" /* defaultpolicy */);
11+
// This is odd but CSP pre-navigation checks on report-only directives cannot block navigation.
12+
assert_true(result.javaScriptExecuted, "JavaScript should have been executed");
13+
assert_equals(result.violations.length, 1);
14+
const violation = result.violations[0];
15+
assert_equals(violation.violatedDirective, "require-trusted-types-for");
16+
assert_equals(violation.disposition, "report");
17+
assert_equals(violation.sample, `Location href|${clipSampleIfNeeded(kJavaScriptURLCode)}`);
18+
assert_equals(result.exception, null, "No exception");
19+
}, "Setting window.location to a javascript: URL with a default policy that throws should report a CSP violation without rethrowing the exception.");
20+
</script>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
Content-Security-Policy: connect-src 'none';
3+
Content-Security-Policy-Report-Only: require-trusted-types-for 'script'

trusted-types/support/navigation-support.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ async function setLocationToJavaScriptURL(defaultpolicy) {
6060
// pre-navigation check CSP violation.
6161
if (window.requestIdleCallback) {
6262
await new Promise(resolve => requestIdleCallback(resolve));
63+
} else {
64+
await new Promise(resolve => requestAnimationFrame(_ => requestAnimationFrame(resolve)));
6365
}
6466
});
6567

@@ -188,6 +190,11 @@ function navigateToJavascriptURL(reportOnly) {
188190
requestIdleCallback(resolve);
189191
window.addEventListener("beforeunload", resolve);
190192
});
193+
} else {
194+
await new Promise(resolve => requestAnimationFrame(() => {
195+
requestAnimationFrame(resolve);
196+
window.addEventListener("beforeunload", resolve);
197+
}));
191198
}
192199
});
193200
if (exception) {

0 commit comments

Comments
 (0)