Skip to content

Commit 930052c

Browse files
TimvdLippeservo-wpt-sync
authored andcommitted
Forward CSP violations from load_whole_resource to parent event loop
Any CSP violations happening when loading a worker should be reported on the global of the document that creates the worker. Since workers run in different threads, we can't pass in this parent global into the worker global scope. Instead, we need to send a message to the parent event loop to report it on the correct global. Part of #4577 Fixes #37027 Signed-off-by: Tim van der Lippe <tvanderlippe@gmail.com>
1 parent a6a515e commit 930052c

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

content-security-policy/generic/test-case.sub.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ function TestCase(scenarios, sanityChecker) {
5757
// https://bugzilla.mozilla.org/show_bug.cgi?id=1808911
5858
// In Firefox sometimes violations from Worklets are delayed.
5959
timeout = 10;
60+
} else if (scenario.subresource.startsWith('worker-') &&
61+
navigator.userAgent.includes("Servo/")) {
62+
// In Servo, worker violations are also delayed, as they are
63+
// sent via IPC. However, they typically arrive relatively
64+
// quickly after that.
65+
timeout = 1;
6066
}
6167
await new Promise(resolve => setTimeout(resolve, timeout));
6268

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<script src="/resources/testharness.js"></script>
3+
<script src="/resources/testharnessreport.js"></script>
4+
<!-- Test the 'worker-src' directive on nested dedicated workers -->
5+
<script>
6+
const w = new Worker(
7+
`./support/worker-src-none.sub.js?` +
8+
`pipe=sub|header(Content-Security-Policy,` +
9+
`worker-src 'none')`);
10+
// Forward 'securitypolicyviolation' events from the document into the
11+
// worker (we shouldn't actually see any, so the worker will assert that
12+
// none are fired).
13+
document.addEventListener('securitypolicyviolation', _ => {
14+
w.postMessage("SecurityPolicyViolation from Document");
15+
});
16+
// Nested workers are disallowed and don't send violations to document
17+
fetch_tests_from_worker(w);
18+
</script>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
importScripts("{{location[server]}}/resources/testharness.js");
2+
importScripts("{{location[server]}}/content-security-policy/support/testharness-helper.js");
3+
4+
let cspEventFiredInDocument = false;
5+
self.addEventListener("message", e => {
6+
if (e.data == "SecurityPolicyViolation from Document") {
7+
cspEventFiredInDocument = true;
8+
}
9+
});
10+
11+
async_test(t => {
12+
const url = new URL("{{location[server]}}/content-security-policy/support/ping.js").toString();
13+
const w = new Worker(url);
14+
w.onmessage = t.unreached_func("Ping should not be sent.");
15+
Promise.all([
16+
waitUntilCSPEventForURL(t, url)
17+
.then(t.step_func_done(e => {
18+
assert_equals(e.blockedURI, url);
19+
assert_equals(e.violatedDirective, "worker-src");
20+
assert_equals(e.effectiveDirective, "worker-src");
21+
assert_false(cspEventFiredInDocument, "Should not have fired event on document");
22+
})),
23+
waitUntilEvent(w, "error"),
24+
]);
25+
}, "Nested worker with worker-src is disallowed.");
26+
27+
done();

0 commit comments

Comments
 (0)