Skip to content

Commit e9f7038

Browse files
yutakahiranomoz-wptsync-bot
authored andcommitted
Bug 1774540 [wpt PR 34462] - Fix up wpt/preload, a=testonly
Automatic update from web-platform-tests Fix up wpt/preload - Some tests in wpt/preload use Resource Timing entries to make sure that no requests are made. We're changing that (Resource Timing entries should be created even when blocked by CSP - see whatwg/fetch#1215). Stop using Resource Timing entries and check that with server side scripts. - http/tests/preload/preload-csp.html is covered by some WPTs. Let's remove it. Change-Id: I3c2cdfa2459d212657be7569c5290c48b39d6f05 Bug: 1275564 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3708287 Reviewed-by: Yoav Weiss <[email protected]> Commit-Queue: Yutaka Hirano <[email protected]> Cr-Commit-Position: refs/heads/main@{#1019490} -- wpt-commits: ba22f229dfafa51c637aa02957f8b9f330f1cfa3 wpt-pr: 34462
1 parent 66a52bf commit e9f7038

10 files changed

+269
-198
lines changed

testing/web-platform/tests/preload/dynamic-adding-preload-nonce.html

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,50 @@
11
<!DOCTYPE html>
22
<script nonce="abc" src="/resources/testharness.js"></script>
33
<script nonce="abc" src="/resources/testharnessreport.js"></script>
4+
<script nonce="abc" src="/common/utils.js"></script>
45
<script nonce="abc" src="/preload/resources/preload_helper.js"></script>
56
<body>
67
<script nonce="abc">
78

8-
async_test(function(t) {
9-
verifyPreloadAndRTSupport();
10-
var link = document.createElement("link");
11-
link.as = "script";
12-
link.rel = "preload";
13-
link.href = "resources/dummy.js?with-nonce";
14-
link.nonce = "abc";
15-
link.onload = link.onerror = t.step_func(function() {
16-
t.step_timeout(function() {
17-
verifyNumberOfResourceTimingEntries("resources/dummy.js?with-nonce", 1);
18-
t.done();
19-
}, 0);
20-
});
21-
document.body.appendChild(link);
9+
promise_test(async (t) => {
10+
verifyPreloadAndRTSupport();
11+
const id = token();
12+
const link = document.createElement("link");
13+
link.as = "script";
14+
link.rel = "preload";
15+
link.href = stashPutUrl(id);
16+
link.nonce = "abc";
17+
18+
const load = new Promise((resolve) => {
19+
link.onload = resolve;
20+
});
21+
link.onerror = t.unreached_func("link.onerror");
22+
23+
document.body.appendChild(link);
24+
await load;
25+
26+
const arrived = await hasArrivedAtServer(id);
27+
assert_true(arrived, "The preload should've arrived at the server.");
2228
}, "link preload with nonce attribute");
2329

24-
async_test(function(t) {
25-
verifyPreloadAndRTSupport();
26-
var link = document.createElement("link");
27-
link.as = "script";
28-
link.rel = "preload";
29-
link.href = "resources/dummy.js?without-nonce";
30-
link.onload = link.onerror = t.step_func(function() {
31-
t.step_timeout(function() {
32-
verifyNumberOfResourceTimingEntries("resources/dummy.js?without-nonce", 0);
33-
t.done();
34-
}, 0);
35-
});
36-
document.body.appendChild(link);
30+
promise_test(async (t) => {
31+
verifyPreloadAndRTSupport();
32+
const id = token();
33+
const link = document.createElement("link");
34+
link.as = "script";
35+
link.rel = "preload";
36+
link.href = stashPutUrl(id);
37+
38+
const error = new Promise((resolve) => {
39+
link.onerror = resolve;
40+
});
41+
link.onload = t.unreached_func("link.onload");
42+
43+
document.body.appendChild(link);
44+
await error;
45+
46+
const arrived = await hasArrivedAtServer(id);
47+
assert_false(arrived, "The preload should've arrived at the server.");
3748
}, "link preload without nonce attribute");
3849

3950
</script>
Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,53 @@
11
<!DOCTYPE html>
2-
<title>Makes sure that Link headers preload resources with CSP nonce</title>
3-
<script nonce="abc" src="/resources/testharness.js"></script>
4-
<script nonce="abc" src="/resources/testharnessreport.js"></script>
5-
<script nonce="abc" src="/preload/resources/preload_helper.js"></script>
2+
<script src="/resources/testharness.js"></script>
3+
<script src="/resources/testharnessreport.js"></script>
4+
<script src="/common/utils.js"></script>
5+
<script src="/preload/resources/preload_helper.js"></script>
66
<body>
7-
<script nonce="abc">
8-
setup({single_test: true});
7+
<script>
98

10-
var iterations = 0;
9+
async_test(t => {
10+
const id = token();
11+
const pageUrl =
12+
'/common/blank.html?pipe=' +
13+
'|header(content-security-policy, script-src \'nonce-abc\')' +
14+
`|header(link, <${encodedStashPutUrl(id)}>;rel=preload;as=script)`;
1115

12-
function check_finished() {
13-
if (numberOfResourceTimingEntries("resources/dummy.js?from-header&without-nonce") == 0 &&
14-
numberOfResourceTimingEntries("resources/dummy.js?from-header&with-nonce") == 1) {
15-
done();
16-
}
17-
iterations++;
18-
if (iterations == 10) {
19-
// At least one is expected to fail, but this should give details to the exact failure(s).
20-
verifyNumberOfResourceTimingEntries("resources/dummy.js?from-header&without-nonce", 0);
21-
verifyNumberOfResourceTimingEntries("resources/dummy.js?from-header&with-nonce", 1);
22-
done();
23-
} else {
24-
step_timeout(check_finished, 500);
25-
}
16+
const w = window.open(pageUrl);
17+
t.add_cleanup(() => w.close());
18+
19+
step_timeout(async () => {
20+
try {
21+
const arrived = await hasArrivedAtServer(id);
22+
assert_false(arrived, 'The preload should be blocked.');
23+
t.done();
24+
} catch (e) {
25+
t.step(() => {throw e;});
26+
}
27+
}, 3000);
28+
}, 'without nonce');
29+
30+
async_test(t => {
31+
const id = token();
32+
const pageUrl =
33+
'/common/blank.html?pipe=' +
34+
'|header(content-security-policy, script-src \'nonce-az\')' +
35+
`|header(link, <${encodedStashPutUrl(id)}>;rel=preload;as=script;nonce=az)`;
36+
const w = window.open(pageUrl);
37+
t.add_cleanup(() => w.close());
38+
39+
// TODO: Use step_wait after
40+
// https://github.com/web-platform-tests/wpt/pull/34289 is merged.
41+
step_timeout(async () => {
42+
try {
43+
const arrived = await hasArrivedAtServer(id);
44+
assert_true(arrived, 'The preload should have arrived at the server.');
45+
t.done();
46+
} catch (e) {
47+
t.step(() => {throw e;});
2648
}
49+
}, 3000);
50+
}, 'with nonce');
2751

28-
window.addEventListener("load", function() {
29-
verifyPreloadAndRTSupport();
30-
step_timeout(check_finished, 500);
31-
});
3252
</script>
3353
</body>

testing/web-platform/tests/preload/link-header-preload-nonce.html.headers

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 25 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,35 @@
11
<!DOCTYPE html>
2-
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline'; font-src 'none'; style-src 'none'; img-src 'none'; media-src 'none'; connect-src 'none'">
2+
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline'; font-src 'none'; style-src 'none'; img-src 'none'; media-src 'none';">
33
<title>Makes sure that preload requests respect CSP</title>
44
<script src="/resources/testharness.js"></script>
55
<script src="/resources/testharnessreport.js"></script>
6+
<script src="/common/utils.js"></script>
67
<script src="/preload/resources/preload_helper.js"></script>
7-
<link rel=preload href="{{host}}:{{ports[http][1]}}/preload/resources/dummy.js" as=style>
8-
<link rel=preload href="resources/dummy.css" as=style>
9-
<link rel=preload href="resources/square.png" as=image>
10-
<link rel=preload href="/fonts/CanvasTest.ttf" as=font crossorigin>
11-
<link rel=preload href="resources/white.mp4" as=video>
12-
<link rel=preload href="resources/sound_5.oga" as=audio>
13-
<link rel=preload href="resources/foo.vtt" as=track>
14-
<link rel=preload href="resources/dummy.xml?foo=bar" as=foobarxmlthing>
15-
<link rel=preload href="resources/dummy.xml">
8+
<link rel=preload href="http://{{host}}:{{ports[http][1]}}/preload/resources/stash-put.py?key={{uuid()}}" as=style>
9+
<link rel=preload href="/preload/resources/stash-put.py?key={{uuid()}}" as=style>
10+
<link rel=preload href="/preload/resources/stash-put.py?key={{uuid()}}" as=image>
11+
<link rel=preload href="/preload/resources/stash-put.py?key={{uuid()}}" as=font crossorigin>
12+
<link rel=preload href="/preload/resources/stash-put.py?key={{uuid()}}" as=video>
13+
<link rel=preload href="/preload/resources/stash-put.py?key={{uuid()}}" as=audio>
14+
<link rel=preload href="/preload/resources/stash-put.py?key={{uuid()}}" as=track>
15+
<link rel=preload href="/preload/resources/stash-put.py?key={{uuid()}}" as=foobarxmlthing>
16+
<link rel=preload href="/preload/resources/stash-put.py?key={{uuid()}}">
1617
<body>
1718
<script>
18-
setup({single_test: true});
19-
20-
var iterations = 0;
21-
22-
function check_finished() {
23-
if (numberOfResourceTimingEntries("{{host}}:{{ports[http][1]}}/preload/resources/dummy.js") == 0 &&
24-
numberOfResourceTimingEntries("resources/dummy.css") == 0 &&
25-
numberOfResourceTimingEntries("resources/square.png") == 0 &&
26-
numberOfResourceTimingEntries("/fonts/CanvasTest.ttf") == 0 &&
27-
numberOfResourceTimingEntries("resources/white.mp4") == 0 &&
28-
numberOfResourceTimingEntries("resources/sound_5.oga") == 0 &&
29-
numberOfResourceTimingEntries("resources/foo.vtt") == 0 &&
30-
numberOfResourceTimingEntries("resources/dummy.xml") == 0) {
31-
done();
32-
}
33-
iterations++;
34-
if (iterations == 10) {
35-
// At least one is expected to fail, but this should give details to the exact failure(s).
36-
verifyNumberOfResourceTimingEntries("{{host}}:{{ports[http][1]}}/preload/resources/dummy.js", 0);
37-
verifyNumberOfResourceTimingEntries("resources/dummy.css", 0);
38-
verifyNumberOfResourceTimingEntries("resources/square.png", 0);
39-
verifyNumberOfResourceTimingEntries("/fonts/CanvasTest.ttf", 0);
40-
verifyNumberOfResourceTimingEntries("resources/white.mp4", 0);
41-
verifyNumberOfResourceTimingEntries("resources/sound_5.oga", 0);
42-
verifyNumberOfResourceTimingEntries("resources/foo.vtt", 0);
43-
verifyNumberOfResourceTimingEntries("resources/dummy.xml", 0);
44-
done();
45-
} else {
46-
step_timeout(check_finished, 500);
47-
}
19+
promise_test(async (t) => {
20+
verifyPreloadAndRTSupport();
21+
const keys = [];
22+
const links = document.querySelectorAll('link');
23+
for (const link of links) {
24+
if (link.rel === 'preload') {
25+
const r = /\?key=([a-zA-Z0-9\-]+)$/;
26+
keys.push(link.href.match(r)[1]);
4827
}
28+
}
29+
await new Promise((resolve) => step_timeout(resolve, 3000));
4930

50-
window.addEventListener("load", function() {
51-
verifyPreloadAndRTSupport();
52-
step_timeout(check_finished, 500);
53-
});
31+
for (const key of keys) {
32+
assert_false(await hasArrivedAtServer(key));
33+
}
34+
}, 'Preload requests are blocked by CSP.');
5435
</script>
55-
Lines changed: 24 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,35 @@
11
<!DOCTYPE html>
2-
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline'; default-src 'none'">
2+
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline'; default-src 'none'; connect-src 'self';">
33
<title>Makes sure that preload requests respect CSP</title>
44
<script src="/resources/testharness.js"></script>
55
<script src="/resources/testharnessreport.js"></script>
66
<script src="/preload/resources/preload_helper.js"></script>
7-
<link rel=preload href="{{host}}:{{ports[http][1]}}/preload/resources/dummy.js" as=style>
8-
<link rel=preload href="resources/dummy.css" as=style>
9-
<link rel=preload href="resources/square.png" as=image>
10-
<link rel=preload href="/fonts/CanvasTest.ttf" as=font crossorigin>
11-
<link rel=preload href="resources/white.mp4" as=video>
12-
<link rel=preload href="resources/sound_5.oga" as=audio>
13-
<link rel=preload href="resources/foo.vtt" as=track>
14-
<link rel=preload href="resources/dummy.xml?foo=bar" as=foobarxmlthing>
15-
<link rel=preload href="resources/dummy.xml">
7+
<link rel=preload href="http://{{host}}:{{ports[http][1]}}/preload/resources/stash-put.py?key={{uuid()}}" as=style>
8+
<link rel=preload href="/preload/resources/stash-put.py?key={{uuid()}}" as=style>
9+
<link rel=preload href="/preload/resources/stash-put.py?key={{uuid()}}" as=image>
10+
<link rel=preload href="/preload/resources/stash-put.py?key={{uuid()}}" as=font crossorigin>
11+
<link rel=preload href="/preload/resources/stash-put.py?key={{uuid()}}" as=video>
12+
<link rel=preload href="/preload/resources/stash-put.py?key={{uuid()}}" as=audio>
13+
<link rel=preload href="/preload/resources/stash-put.py?key={{uuid()}}" as=track>
14+
<link rel=preload href="/preload/resources/stash-put.py?key={{uuid()}}" as=foobarxmlthing>
15+
<link rel=preload href="/preload/resources/stash-put.py?key={{uuid()}}">
1616
<body>
1717
<script>
18-
setup({single_test: true});
19-
20-
var iterations = 0;
21-
22-
function check_finished() {
23-
if (numberOfResourceTimingEntries("{{host}}:{{ports[http][1]}}/preload/resources/dummy.js") == 0 &&
24-
numberOfResourceTimingEntries("resources/dummy.css") == 0 &&
25-
numberOfResourceTimingEntries("resources/square.png") == 0 &&
26-
numberOfResourceTimingEntries("/fonts/CanvasTest.ttf") == 0 &&
27-
numberOfResourceTimingEntries("resources/white.mp4") == 0 &&
28-
numberOfResourceTimingEntries("resources/sound_5.oga") == 0 &&
29-
numberOfResourceTimingEntries("resources/foo.vtt") == 0 &&
30-
numberOfResourceTimingEntries("resources/dummy.xml") == 0) {
31-
done();
32-
}
33-
iterations++;
34-
if (iterations == 10) {
35-
// At least one is expected to fail, but this should give details to the exact failure(s).
36-
verifyNumberOfResourceTimingEntries("{{host}}:{{ports[http][1]}}/preload/resources/dummy.js", 0);
37-
verifyNumberOfResourceTimingEntries("resources/dummy.css", 0);
38-
verifyNumberOfResourceTimingEntries("resources/square.png", 0);
39-
verifyNumberOfResourceTimingEntries("/fonts/CanvasTest.ttf", 0);
40-
verifyNumberOfResourceTimingEntries("resources/white.mp4", 0);
41-
verifyNumberOfResourceTimingEntries("resources/sound_5.oga", 0);
42-
verifyNumberOfResourceTimingEntries("resources/foo.vtt", 0);
43-
verifyNumberOfResourceTimingEntries("resources/dummy.xml", 0);
44-
done();
45-
} else {
46-
step_timeout(check_finished, 500);
47-
}
18+
promise_test(async (t) => {
19+
verifyPreloadAndRTSupport();
20+
const keys = [];
21+
const links = document.querySelectorAll('link');
22+
for (const link of links) {
23+
if (link.rel === 'preload') {
24+
const r = /\?key=([a-zA-Z0-9\-]+)$/;
25+
keys.push(link.href.match(r)[1]);
4826
}
27+
}
28+
await new Promise((resolve) => step_timeout(resolve, 3000));
4929

50-
window.addEventListener("load", function() {
51-
verifyPreloadAndRTSupport();
52-
step_timeout(check_finished, 500);
53-
});
30+
for (const key of keys) {
31+
assert_false(await hasArrivedAtServer(key));
32+
}
33+
}, 'Preload requests are blocked by CSP ("default-src \'none\').');
5434
</script>
5535

testing/web-platform/tests/preload/preload-strict-dynamic.html

Lines changed: 0 additions & 54 deletions
This file was deleted.

0 commit comments

Comments
 (0)