Skip to content

Commit

Permalink
[BroadcastChannel] Add data URL worker tests
Browse files Browse the repository at this point in the history
Test that BroadcastChannel instances in dedicated workers and
shared workers created using data URLs (which should have
unique opaque origins per the HTML spec) aren't delivered to
BroadcastChannel instances in the parent document or in other
data URL workers.

Bug: 1058759
Change-Id: I150568f5dc40f5ca7cad5343833e2379746bedf4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3610786
Reviewed-by: Ben Kelly <[email protected]>
Commit-Queue: Andrew Williams <[email protected]>
Cr-Commit-Position: refs/heads/main@{#996789}
  • Loading branch information
recvfrom authored and chromium-wpt-export-bot committed Apr 27, 2022
1 parent 55da5e1 commit bd235e3
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions webmessaging/broadcastchannel/opaque-origin.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,93 @@
document.body.appendChild(iframe1);
});
}, "BroadcastChannel messages from opaque origins should be self-contained");

const worker_src = (channel_name, worker_name) => `data:,
const handler = (reply) => {
let bc2 = new BroadcastChannel("${channel_name}");
bc2.onmessage = (e) => {
if (e.data == "from-${worker_name}") {
reply("${worker_name}-done");
} else {
reply("fail");
}
};
let bc3 = new BroadcastChannel("${channel_name}");
bc3.postMessage("from-${worker_name}");
};
// For dedicated workers:
self.addEventListener("message", () => handler(self.postMessage));
// For shared workers:
self.addEventListener("connect", (e) => handler(e.ports[0].postMessage));
`;

promise_test(t => {
return new Promise((resolve, reject) => {
const channel_name = "opaque-origin-test-3";
const bc1 = new BroadcastChannel(channel_name);
bc1.onmessage = e => { reject("Received message from an opaque origin"); };

// Same as the previous test but with data URL dedicated workers (which
// should have opaque origins per the HTML spec).
const worker_name_prefix = "data-url-dedicated-worker";
const worker_1_name = `${worker_name_prefix}-1`;
const worker_2_name = `${worker_name_prefix}-2`;

const handler = e => {
if(e.data == `${worker_1_name}-done`) {
const worker2 = new Worker(worker_src(channel_name, worker_2_name));
t.add_cleanup(() => worker2.terminate());
worker2.addEventListener("message", handler);
worker2.postMessage("go!");
} else if(e.data == `${worker_2_name}-done`) {
resolve();
} else if(e.data == "fail") {
reject("One opaque origin received a message from the other");
} else {
reject("An unexpected error occurred");
}
};

let worker1 = new Worker(worker_src(channel_name, worker_1_name));
t.add_cleanup(() => worker1.terminate());
worker1.addEventListener("message", handler);
worker1.postMessage("go!");
});
}, "BroadcastChannel messages from data URL dedicated workers should be self-contained");

promise_test(() => {
return new Promise((resolve, reject) => {
const channel_name = "opaque-origin-test-4";
const bc1 = new BroadcastChannel(channel_name);
bc1.onmessage = e => { reject("Received message from an opaque origin"); };

// Same as the previous test but with data URL shared workers (which
// should have opaque origins per the HTML spec).
const worker_name_prefix = "data-url-shared-worker";
const worker_1_name = `${worker_name_prefix}-1`;
const worker_2_name = `${worker_name_prefix}-2`;

const handler = e => {
if(e.data == `${worker_1_name}-done`) {
const worker_script = worker_src(channel_name, worker_2_name);
const worker2 = new SharedWorker(worker_script, worker_2_name);
worker2.port.addEventListener("message", handler);
worker2.port.postMessage("go!");
} else if(e.data == `${worker_2_name}-done`) {
resolve();
} else if(e.data == "fail") {
reject("One opaque origin received a message from the other");
} else {
reject("An unexpected error occurred");
}
};

const worker_script = worker_src(channel_name, worker_2_name);
const worker1 = new SharedWorker(worker_script, worker_1_name);
worker1.port.addEventListener("message", handler);
worker1.port.postMessage("go!");
});
}, "BroadcastChannel messages from data URL shared workers should be self-contained");
//-->
</script>
</body>
Expand Down

0 comments on commit bd235e3

Please sign in to comment.