Commit d5cd6b98 authored by Eriko Kurimoto's avatar Eriko Kurimoto Committed by Commit Bot

SharedWorker: Assign unique names to SharedWorkers to avoid unintentional matching

This CL assigns unique names to SharedWorkers in WPTs for referrer
policy and csp of module shared workers to avoid unintentinal matching.

Before this CL, the flakiness is reported for shared-worker-import-csp.html.
See https://github.com/web-platform-tests/wpt/pull/21591 for more detail.

Bug: 1051779
Change-Id: Id4f7e02510189320366862499f81a20a01089c76
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2055913
Commit-Queue: Eriko Kurimoto <elkurin@google.com>
Reviewed-by: default avatarHiroki Nakagawa <nhiroki@chromium.org>
Reviewed-by: default avatarHiroshige Hayashizaki <hiroshige@chromium.org>
Cr-Commit-Position: refs/heads/master@{#741849}
parent 2f57e742
...@@ -7,7 +7,8 @@ let worker; ...@@ -7,7 +7,8 @@ let worker;
// Create a new shared worker for a given script url. // Create a new shared worker for a given script url.
window.onmessage = e => { window.onmessage = e => {
worker = new SharedWorker(e.data, { type: 'module' }); worker = new SharedWorker(e.data.scriptURL,
{ name: e.data.name, type: 'module' });
worker.port.onmessage = msg => window.opener.postMessage(msg.data, '*'); worker.port.onmessage = msg => window.opener.postMessage(msg.data, '*');
worker.onerror = err => { worker.onerror = err => {
window.opener.postMessage(['ERROR'], '*'); window.opener.postMessage(['ERROR'], '*');
......
...@@ -4,6 +4,9 @@ ...@@ -4,6 +4,9 @@
<script src="/resources/testharnessreport.js"></script> <script src="/resources/testharnessreport.js"></script>
<script> <script>
// This Set is for checking a shared worker in each test is newly created.
const existingWorkers = new Set();
async function openWindow(url) { async function openWindow(url) {
const win = window.open(url, '_blank'); const win = window.open(url, '_blank');
add_result_callback(() => win.close()); add_result_callback(() => win.close());
...@@ -29,10 +32,17 @@ function import_csp_test( ...@@ -29,10 +32,17 @@ function import_csp_test(
promise_test(async () => { promise_test(async () => {
// Open a window that has the given CSP header. // Open a window that has the given CSP header.
const win = await openWindow(windowURL); const win = await openWindow(windowURL);
// Construct a unique name for SharedWorker.
const name = `${cspHeader}_${importType}`;
const workerProperties = { scriptURL, name };
// Check if this shared worker is newly created.
assert_false(existingWorkers.has(workerProperties));
existingWorkers.add(workerProperties);
// Ask the window to start a shared worker with the given CSP header. // Ask the window to start a shared worker with the given CSP header.
// The shared worker doesn't inherits the window's CSP header. // The shared worker doesn't inherits the window's CSP header.
// https://w3c.github.io/webappsec-csp/#initialize-global-object-csp // https://w3c.github.io/webappsec-csp/#initialize-global-object-csp
win.postMessage(scriptURL, '*'); win.postMessage(workerProperties, '*');
const msg_event = await new Promise(resolve => window.onmessage = resolve); const msg_event = await new Promise(resolve => window.onmessage = resolve);
assert_array_equals(msg_event.data, expectedImportedModules); assert_array_equals(msg_event.data, expectedImportedModules);
}, description); }, description);
......
<!DOCTYPE html> <!DOCTYPE html>
<title>SharedWorker: Referrer</title> <title>SharedWorker: Referrer</title>
<meta name="timeout" content="long" />
<script src="/resources/testharness.js"></script> <script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script> <script src="/resources/testharnessreport.js"></script>
<script> <script>
// This Set is for checking a shared worker in each test is newly created.
const existingWorkers = new Set();
async function openWindow(url) { async function openWindow(url) {
const win = window.open(url, '_blank'); const win = window.open(url, '_blank');
add_result_callback(() => win.close()); add_result_callback(() => win.close());
...@@ -77,7 +81,17 @@ function import_referrer_test(settings, description) { ...@@ -77,7 +81,17 @@ function import_referrer_test(settings, description) {
} }
const win = await openWindow(windowURL); const win = await openWindow(windowURL);
win.postMessage(scriptURL, '*'); // Construct a unique name for SharedWorker.
const name = `${settings.scriptURL}_` +
`${settings.windowReferrerPolicy ? settings.windowReferrerPolicy
: settings.workerReferrerPolicy}` +
`_${settings.fetchType}`;
const workerProperties = { scriptURL, name };
// Check if this shared worker is newly created.
assert_false(existingWorkers.has(workerProperties));
existingWorkers.add(workerProperties);
win.postMessage(workerProperties, '*');
const msgEvent = await new Promise(resolve => window.onmessage = resolve); const msgEvent = await new Promise(resolve => window.onmessage = resolve);
// Generate the expected referrer, given: // Generate the expected referrer, given:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment