Skip to content

Commit

Permalink
Isolate 4.3.0 test from attack iframe one
Browse files Browse the repository at this point in the history
  • Loading branch information
Nemikolh committed Mar 7, 2023
1 parent 191bb15 commit 0d49cdc
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 29 deletions.
6 changes: 5 additions & 1 deletion src/comlink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,11 @@ function isMessagePort(endpoint: Endpoint): endpoint is MessagePort {
}

function isEndpoint(endpoint: object): endpoint is Endpoint {
return "postMessage" in endpoint && "addEventListener" in endpoint && "removeEventListener" in endpoint;
return (
"postMessage" in endpoint &&
"addEventListener" in endpoint &&
"removeEventListener" in endpoint
);
}

function markV430Ports(...args: any[]) {
Expand Down
57 changes: 34 additions & 23 deletions tests/fixtures/v430comlink.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,45 @@
// comlink main
import * as Comlink from "/base/dist/esm/comlink.mjs";

const parentEndpoint = Comlink.windowEndpoint(self.parent);

class Test {
async acceptProxy(aProxy) {
return (await aProxy()) + 1;
window.addEventListener("message", function l(ev) {
if (
ev.data &&
typeof ev.data === "object" &&
ev.data.constructor.name === "MessagePort"
) {
window.removeEventListener("message", l);
register(ev.data);
}
});

callme(port) {
Comlink.wrap(port).maybe();
}
function register(parentEndpoint) {
class Test {
async acceptProxy(aProxy) {
return (await aProxy()) + 1;
}

iwannabeaport = undefined;
callme(port) {
Comlink.wrap(port).maybe();
}

wrapWannaBeAndCall() {
Comlink.wrap(this.iwannabeaport).rise();
}
iwannabeaport = undefined;

pong(ping) {
return ping;
wrapWannaBeAndCall() {
Comlink.wrap(this.iwannabeaport).rise();
}

pong(ping) {
return ping;
}
}
}

Comlink.expose(new Test(), parentEndpoint);
Comlink.transferHandlers.set("pingpong", {
canHandle: (obj) => obj === "ping",
serialize: (obj) => {
return ["pong", []];
},
deserialize: (obj) => "ping",
});
Comlink.expose(new Test(), parentEndpoint);
Comlink.transferHandlers.set("pingpong", {
canHandle: (obj) => obj === "ping",
serialize: (obj) => {
return ["pong", []];
},
deserialize: (obj) => "ping",
});
}
</script>
17 changes: 12 additions & 5 deletions tests/v430comlink.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe("Comlink across versions (4.3.0 to latest main)", function () {
let oldTruncateThreshold;
let notcalled;
let error;
let iframePort;

function unhandledrejectionCallback(ev) {
notcalled = false;
Expand All @@ -40,7 +41,13 @@ describe("Comlink across versions (4.3.0 to latest main)", function () {
this.ifr.sandbox.add("allow-scripts", "allow-same-origin");
this.ifr.src = "/base/tests/fixtures/v430comlink.html";
document.body.appendChild(this.ifr);
return new Promise((resolve) => (this.ifr.onload = resolve));
return new Promise((resolve) => (this.ifr.onload = resolve)).then(() => {
const iframeChannel = new MessageChannel();
iframePort = iframeChannel.port1;
this.ifr.contentWindow.postMessage(iframeChannel.port2, "*", [
iframeChannel.port2,
]);
});
});

afterEach(function () {
Expand All @@ -53,7 +60,7 @@ describe("Comlink across versions (4.3.0 to latest main)", function () {
});

it("can send a proxy and call a function", async function () {
const iframe = Comlink.windowEndpoint(this.ifr.contentWindow);
const iframe = iframePort;
const latest = Comlink.wrap(iframe);

expect(await latest.acceptProxy(Comlink.proxy(() => 3))).to.equal(4);
Expand All @@ -62,7 +69,7 @@ describe("Comlink across versions (4.3.0 to latest main)", function () {
});

it("can send port that get wrapped and transfer by argument", async function () {
const iframe = Comlink.windowEndpoint(this.ifr.contentWindow);
const iframe = iframePort;
const latest = Comlink.wrap(iframe);
let maybecalled = false;
class Test {
Expand All @@ -82,7 +89,7 @@ describe("Comlink across versions (4.3.0 to latest main)", function () {

it("can send port that get wrapped and transfer by a setter", async function () {
// Verify that it also works with a setter
const iframe = Comlink.windowEndpoint(this.ifr.contentWindow);
const iframe = iframePort;
const latest = Comlink.wrap(iframe);
const channel2 = new MessageChannel();
let risecalled = false;
Expand All @@ -104,7 +111,7 @@ describe("Comlink across versions (4.3.0 to latest main)", function () {
});

it("works with custom handlers", async function () {
const iframe = Comlink.windowEndpoint(this.ifr.contentWindow);
const iframe = iframePort;
const proxy = Comlink.wrap(iframe);

Comlink.transferHandlers.set("pingpong", {
Expand Down

0 comments on commit 0d49cdc

Please sign in to comment.