Skip to content

Commit 036e8ee

Browse files
authored
Add cross-origin-embedder-policy tests with service workers (#18559)
1 parent 92f88af commit 036e8ee

8 files changed

+364
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<!doctype html>
2+
<html>
3+
<script src=/resources/testharness.js></script>
4+
<script src=/resources/testharnessreport.js></script>
5+
<script src="/common/get-host-info.sub.js"></script>
6+
<script src="/service-workers/service-worker/resources/test-helpers.sub.js"></script>
7+
<script>
8+
const SCOPE = new URL(location.href).pathname;
9+
const SCRIPT =
10+
'resources/sw.js?' +
11+
`pipe=header(service-worker-allowed,${SCOPE})`;
12+
13+
function remote(path) {
14+
const REMOTE_ORIGIN = get_host_info().HTTPS_REMOTE_ORIGIN;
15+
return new URL(path, REMOTE_ORIGIN + '/html/cross-origin-embedder-policy/');
16+
}
17+
18+
promise_test(async (t) => {
19+
const reg = await service_worker_unregister_and_register(t, SCRIPT, SCOPE);
20+
add_completion_callback(() => {
21+
reg.unregister();
22+
});
23+
await new Promise(resolve => {
24+
navigator.serviceWorker.addEventListener('controllerchange', resolve);
25+
});
26+
}, 'setting up');
27+
28+
promise_test(async (t) => {
29+
await fetch('resources/nothing-same-origin-corp.txt', {mode: 'no-cors'});
30+
}, 'making a same-origin request for CORP: same-origin');
31+
32+
promise_test(async (t) => {
33+
await fetch('/common/blank.html', {mode: 'no-cors'});
34+
}, 'making a same-origin request for no CORP');
35+
36+
promise_test(async (t) => {
37+
await fetch('resources/nothing-cross-origin-corp.txt', {mode: 'no-cors'});
38+
}, 'making a same-origin request for CORP: cross-origin');
39+
40+
promise_test(async (t) => {
41+
await promise_rejects(
42+
t, TypeError(),
43+
fetch(remote('resources/nothing-same-origin-corp.txt'), {mode: 'no-cors'}));
44+
}, 'making a cross-origin request for CORP: same-origin');
45+
46+
promise_test(async (t) => {
47+
await fetch(remote('/common/blank.html'), {mode: 'no-cors'});
48+
}, 'making a cross-origin request for no CORP');
49+
50+
promise_test(async (t) => {
51+
await fetch(
52+
remote('resources/nothing-cross-origin-corp.txt'),
53+
{mode: 'no-cors'});
54+
}, 'making a cross-origin request for CORP: cross-origin');
55+
56+
promise_test(async (t) => {
57+
await promise_rejects(
58+
t, TypeError(),
59+
fetch(remote('resources/nothing-same-origin-corp.txt?passthrough'),
60+
{mode: 'no-cors'}));
61+
}, 'making a cross-origin request for CORP: same-origin [PASS THROUGH]');
62+
63+
promise_test(async (t) => {
64+
await fetch(remote('/common/blank.html?passthrough'), {mode: 'no-cors'});
65+
}, 'making a cross-origin request for no CORP [PASS THROUGH]');
66+
67+
promise_test(async (t) => {
68+
await fetch(
69+
remote('resources/nothing-cross-origin-corp.txt?passthrough'),
70+
{mode: 'no-cors'});
71+
}, 'making a cross-origin request for CORP: cross-origin [PASS THROUGH]');
72+
73+
promise_test(async (t) => {
74+
await promise_rejects(
75+
t, TypeError(), fetch(remote('/common/blank.html'), {mode: 'cors'}));
76+
}, 'making a cross-origin request with CORS without ACAO');
77+
78+
promise_test(async (t) => {
79+
const URL = remote(
80+
'/common/blank.html?pipe=header(access-control-allow-origin,*');
81+
await fetch(URL, {mode: 'cors'});
82+
}, 'making a cross-origin request with CORS');
83+
84+
</script>
85+
</html>
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<!doctype html>
2+
<html>
3+
<script src=/resources/testharness.js></script>
4+
<script src=/resources/testharnessreport.js></script>
5+
<script src="/common/get-host-info.sub.js"></script>
6+
<script src="/service-workers/service-worker/resources/test-helpers.sub.js"></script>
7+
<script>
8+
const SCOPE = new URL(location.href).pathname;
9+
const SCRIPT =
10+
'resources/sw.js?' +
11+
`pipe=header(service-worker-allowed,${SCOPE})`;
12+
13+
function remote(path) {
14+
const REMOTE_ORIGIN = get_host_info().HTTPS_REMOTE_ORIGIN;
15+
return new URL(path, REMOTE_ORIGIN + '/html/cross-origin-embedder-policy/');
16+
}
17+
18+
promise_test(async (t) => {
19+
const reg = await service_worker_unregister_and_register(t, SCRIPT, SCOPE);
20+
add_completion_callback(() => {
21+
reg.unregister();
22+
});
23+
await new Promise(resolve => {
24+
navigator.serviceWorker.addEventListener('controllerchange', resolve);
25+
});
26+
}, 'setting up');
27+
28+
promise_test(async (t) => {
29+
await fetch('resources/nothing-same-origin-corp.txt', {mode: 'no-cors'});
30+
}, 'making a same-origin request for CORP: same-origin');
31+
32+
promise_test(async (t) => {
33+
await fetch('/common/blank.html', {mode: 'no-cors'});
34+
}, 'making a same-origin request for no CORP');
35+
36+
promise_test(async (t) => {
37+
await fetch('resources/nothing-cross-origin-corp.txt', {mode: 'no-cors'});
38+
}, 'making a same-origin request for CORP: cross-origin');
39+
40+
promise_test(async (t) => {
41+
await promise_rejects(
42+
t, TypeError(),
43+
fetch(remote('resources/nothing-same-origin-corp.txt'), {mode: 'no-cors'}));
44+
}, 'making a cross-origin request for CORP: same-origin');
45+
46+
promise_test(async (t) => {
47+
await promise_rejects(
48+
t, TypeError(), fetch(remote('/common/blank.html'), {mode: 'no-cors'}));
49+
}, 'making a cross-origin request for no CORP');
50+
51+
promise_test(async (t) => {
52+
await fetch(
53+
remote('resources/nothing-cross-origin-corp.txt'),
54+
{mode: 'no-cors'});
55+
}, 'making a cross-origin request for CORP: cross-origin');
56+
57+
promise_test(async (t) => {
58+
await promise_rejects(
59+
t, TypeError(),
60+
fetch(remote('resources/nothing-same-origin-corp.txt?passthrough'),
61+
{mode: 'no-cors'}));
62+
}, 'making a cross-origin request for CORP: same-origin [PASS THROUGH]');
63+
64+
promise_test(async (t) => {
65+
await promise_rejects(
66+
t, TypeError(),
67+
fetch(remote('/common/blank.html?passthrough'), {mode: 'no-cors'}));
68+
}, 'making a cross-origin request for no CORP [PASS THROUGH]');
69+
70+
promise_test(async (t) => {
71+
await fetch(
72+
remote('resources/nothing-cross-origin-corp.txt?passthrough'),
73+
{mode: 'no-cors'});
74+
}, 'making a cross-origin request for CORP: cross-origin [PASS THROUGH]');
75+
76+
promise_test(async (t) => {
77+
await promise_rejects(
78+
t, TypeError(), fetch(remote('/common/blank.html'), {mode: 'cors'}));
79+
}, 'making a cross-origin request with CORS without ACAO');
80+
81+
promise_test(async (t) => {
82+
const URL = remote(
83+
'/common/blank.html?pipe=header(access-control-allow-origin,*');
84+
await fetch(URL, {mode: 'cors'});
85+
}, 'making a cross-origin request with CORS');
86+
87+
</script>
88+
</html>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cross-origin-embedder-policy: require-corp
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<!doctype html>
2+
<html>
3+
<script src=/resources/testharness.js></script>
4+
<script src=/resources/testharnessreport.js></script>
5+
<script src="/common/get-host-info.sub.js"></script>
6+
<script src="/service-workers/service-worker/resources/test-helpers.sub.js"></script>
7+
<script>
8+
const SCOPE = new URL(location.href).pathname;
9+
const SCRIPT =
10+
'resources/sw.js?' +
11+
`pipe=header(service-worker-allowed,${SCOPE})|` +
12+
'header(cross-origin-embedder-policy,require-corp)';
13+
14+
function remote(path) {
15+
const REMOTE_ORIGIN = get_host_info().HTTPS_REMOTE_ORIGIN;
16+
return new URL(path, REMOTE_ORIGIN + '/html/cross-origin-embedder-policy/');
17+
}
18+
19+
promise_test(async (t) => {
20+
const reg = await service_worker_unregister_and_register(t, SCRIPT, SCOPE);
21+
add_completion_callback(() => {
22+
reg.unregister();
23+
});
24+
await new Promise(resolve => {
25+
navigator.serviceWorker.addEventListener('controllerchange', resolve);
26+
});
27+
}, 'setting up');
28+
29+
promise_test(async (t) => {
30+
await fetch('resources/nothing-same-origin-corp.txt', {mode: 'no-cors'});
31+
}, 'making a same-origin request for CORP: same-origin');
32+
33+
promise_test(async (t) => {
34+
await fetch('/common/blank.html', {mode: 'no-cors'});
35+
}, 'making a same-origin request for no CORP');
36+
37+
promise_test(async (t) => {
38+
await fetch('resources/nothing-cross-origin-corp.txt', {mode: 'no-cors'});
39+
}, 'making a same-origin request for CORP: cross-origin');
40+
41+
promise_test(async (t) => {
42+
await promise_rejects(
43+
t, TypeError(),
44+
fetch(remote('resources/nothing-same-origin-corp.txt'), {mode: 'no-cors'}));
45+
}, 'making a cross-origin request for CORP: same-origin');
46+
47+
promise_test(async (t) => {
48+
await promise_rejects(
49+
t, TypeError(), fetch(remote('/common/blank.html'), {mode: 'no-cors'}));
50+
}, 'making a cross-origin request for no CORP');
51+
52+
promise_test(async (t) => {
53+
await fetch(
54+
remote('resources/nothing-cross-origin-corp.txt'),
55+
{mode: 'no-cors'});
56+
}, 'making a cross-origin request for CORP: cross-origin');
57+
58+
promise_test(async (t) => {
59+
await promise_rejects(
60+
t, TypeError(),
61+
fetch(remote('resources/nothing-same-origin-corp.txt?passthrough'),
62+
{mode: 'no-cors'}));
63+
}, 'making a cross-origin request for CORP: same-origin [PASS THROUGH]');
64+
65+
promise_test(async (t) => {
66+
await fetch(remote('/common/blank.html?passthrough'), {mode: 'no-cors'});
67+
}, 'making a cross-origin request for no CORP [PASS THROUGH]');
68+
69+
promise_test(async (t) => {
70+
await fetch(
71+
remote('resources/nothing-cross-origin-corp.txt?passthrough'),
72+
{mode: 'no-cors'});
73+
}, 'making a cross-origin request for CORP: cross-origin [PASS THROUGH]');
74+
75+
promise_test(async (t) => {
76+
await promise_rejects(
77+
t, TypeError(), fetch(remote('/common/blank.html'), {mode: 'cors'}));
78+
}, 'making a cross-origin request with CORS without ACAO');
79+
80+
promise_test(async (t) => {
81+
const URL = remote(
82+
'/common/blank.html?pipe=header(access-control-allow-origin,*');
83+
await fetch(URL, {mode: 'cors'});
84+
}, 'making a cross-origin request with CORS');
85+
86+
</script>
87+
</html>
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<!doctype html>
2+
<html>
3+
<script src=/resources/testharness.js></script>
4+
<script src=/resources/testharnessreport.js></script>
5+
<script src="/common/get-host-info.sub.js"></script>
6+
<script src="/service-workers/service-worker/resources/test-helpers.sub.js"></script>
7+
<script>
8+
const SCOPE = new URL(location.href).pathname;
9+
const SCRIPT =
10+
'resources/sw.js?' +
11+
`pipe=header(service-worker-allowed,${SCOPE})|` +
12+
'header(cross-origin-embedder-policy,require-corp)';
13+
14+
function remote(path) {
15+
const REMOTE_ORIGIN = get_host_info().HTTPS_REMOTE_ORIGIN;
16+
return new URL(path, REMOTE_ORIGIN + '/html/cross-origin-embedder-policy/');
17+
}
18+
19+
promise_test(async (t) => {
20+
const reg = await service_worker_unregister_and_register(t, SCRIPT, SCOPE);
21+
add_completion_callback(() => {
22+
reg.unregister();
23+
});
24+
await new Promise(resolve => {
25+
navigator.serviceWorker.addEventListener('controllerchange', resolve);
26+
});
27+
}, 'setting up');
28+
29+
promise_test(async (t) => {
30+
await fetch('resources/nothing-same-origin-corp.txt', {mode: 'no-cors'});
31+
}, 'making a same-origin request for CORP: same-origin');
32+
33+
promise_test(async (t) => {
34+
await fetch('/common/blank.html', {mode: 'no-cors'});
35+
}, 'making a same-origin request for no CORP');
36+
37+
promise_test(async (t) => {
38+
await fetch('resources/nothing-cross-origin-corp.txt', {mode: 'no-cors'});
39+
}, 'making a same-origin request for CORP: cross-origin');
40+
41+
promise_test(async (t) => {
42+
await promise_rejects(
43+
t, TypeError(),
44+
fetch(remote('resources/nothing-same-origin-corp.txt'), {mode: 'no-cors'}));
45+
}, 'making a cross-origin request for CORP: same-origin');
46+
47+
promise_test(async (t) => {
48+
await promise_rejects(
49+
t, TypeError(), fetch(remote('/common/blank.html'), {mode: 'no-cors'}));
50+
}, 'making a cross-origin request for no CORP');
51+
52+
promise_test(async (t) => {
53+
await fetch(
54+
remote('resources/nothing-cross-origin-corp.txt'),
55+
{mode: 'no-cors'});
56+
}, 'making a cross-origin request for CORP: cross-origin');
57+
58+
promise_test(async (t) => {
59+
await promise_rejects(
60+
t, TypeError(),
61+
fetch(remote('resources/nothing-same-origin-corp.txt?passthrough'),
62+
{mode: 'no-cors'}));
63+
}, 'making a cross-origin request for CORP: same-origin [PASS THROUGH]');
64+
65+
promise_test(async (t) => {
66+
await promise_rejects(
67+
t, TypeError(),
68+
fetch(remote('/common/blank.html?passthrough'), {mode: 'no-cors'}));
69+
}, 'making a cross-origin request for no CORP [PASS THROUGH]');
70+
71+
promise_test(async (t) => {
72+
await fetch(
73+
remote('resources/nothing-cross-origin-corp.txt?passthrough'),
74+
{mode: 'no-cors'});
75+
}, 'making a cross-origin request for CORP: cross-origin [PASS THROUGH]');
76+
77+
promise_test(async (t) => {
78+
await promise_rejects(
79+
t, TypeError(), fetch(remote('/common/blank.html'), {mode: 'cors'}));
80+
}, 'making a cross-origin request with CORS without ACAO');
81+
82+
promise_test(async (t) => {
83+
const URL = remote(
84+
'/common/blank.html?pipe=header(access-control-allow-origin,*');
85+
await fetch(URL, {mode: 'cors'});
86+
}, 'making a cross-origin request with CORS');
87+
88+
</script>
89+
</html>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cross-origin-embedder-policy: require-corp
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nothing with no CORP headers
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
self.addEventListener('activate', (e) => {
2+
e.waitUntil(clients.claim());
3+
});
4+
5+
self.addEventListener('fetch', (e) => {
6+
const url = new URL(e.request.url);
7+
if (url.searchParams.has('passthrough')) {
8+
return;
9+
}
10+
11+
e.respondWith(fetch(e.request));
12+
});

0 commit comments

Comments
 (0)