Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add origin filtering for expose #605

Merged
merged 4 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/comlink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,34 @@ export const transferHandlers = new Map<
["throw", throwTransferHandler],
]);

export function expose(obj: any, ep: Endpoint = self as any) {
function isAllowedOrigin(
origins: (string | RegExp)[],
benjamind marked this conversation as resolved.
Show resolved Hide resolved
origin: string
): boolean {
for (const allowedOrigin of origins) {
if (origin === allowedOrigin || allowedOrigin === "*") {
return true;
}
if (allowedOrigin instanceof RegExp && allowedOrigin.test(origin)) {
return true;
}
}
return false;
}

export function expose(
obj: any,
ep: Endpoint = self as any,
origins: (string | RegExp)[] = ["*"]
) {
ep.addEventListener("message", function callback(ev: MessageEvent) {
if (!ev || !ev.data) {
return;
}
if (!isAllowedOrigin(origins, ev.origin)) {
console.warn(`Invalid origin '${ev.origin}' for comlink proxy`);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure exactly how to handle invalid messages, a warning seems useful, throwing an exception can be pretty hard to handle in this case but also seems more correct. Thoughts @surma ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree with a warning.

return;
}
const { id, type, path } = {
path: [] as string[],
...(ev.data as Message),
Expand Down
83 changes: 83 additions & 0 deletions tests/cross-origin.comlink.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* Copyright 2017 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as Comlink from "/base/dist/esm/comlink.mjs";

describe("Comlink origin filtering", function () {
it("rejects messages from unknown origin", async function () {
// expose on our window so comlink is listening to window postmessage
const obj = { my: "value" };
Comlink.expose(obj, self, [/^http:\/\/localhost(:[0-9]+)?\/?$/]);

let handler;
// juggle async timings to get the attack started
const attackComplete = new Promise((resolve, reject) => {
handler = (ev) => {
if (ev.data === "ready" && ev.origin === "null") {
// tell the iframe it can start the attack
ifr.contentWindow.postMessage("start", "*");
} else if (ev.data === "done") {
// confirm the attack succeeded
expect(Object.prototype.foo).to.be.undefined;
expect(obj.my).to.equal("value");
resolve();
}
};
window.addEventListener("message", handler);
});
// create a sandboxed iframe for the attack
const ifr = document.createElement("iframe");
ifr.sandbox.add("allow-scripts");
ifr.src = "/base/tests/fixtures/attack-iframe.html";
document.body.appendChild(ifr);
// wait for the iframe to load
await new Promise((resolve) => (ifr.onload = resolve));
// and wait for the attack to complete
await attackComplete;
window.removeEventListener("message", handler);
ifr.remove();
});
it("accepts messages from matching origin", async function () {
// expose on our window so comlink is listening to window postmessage
const obj = { my: "value" };
Comlink.expose(obj, self, [/^http:\/\/localhost(:[0-9]+)?\/?$/]);

let handler;
// juggle async timings to get the attack started
const attackComplete = new Promise((resolve, reject) => {
handler = (ev) => {
if (ev.data === "ready" && ev.origin === window.origin) {
// tell the iframe it can start the attack
ifr.contentWindow.postMessage("start", "*");
} else if (ev.data === "done") {
// confirm the attack succeeded
expect(Object.prototype.foo).to.equal("x");
expect(obj.my).to.equal("value");
resolve();
}
};
window.addEventListener("message", handler);
});
// create a sandboxed iframe for the attack, but with same origin
const ifr = document.createElement("iframe");
ifr.sandbox.add("allow-scripts", "allow-same-origin");
ifr.src = "/base/tests/fixtures/attack-iframe.html";
document.body.appendChild(ifr);
// wait for the iframe to load
await new Promise((resolve) => (ifr.onload = resolve));
// and wait for the attack to complete
await attackComplete;
window.removeEventListener("message", handler);
ifr.remove();
});
});
21 changes: 21 additions & 0 deletions tests/fixtures/attack-iframe.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<html>
<body>
<script>
window.addEventListener("message", (ev) => {
if (ev.data === "start") {
// send back a message to modify the prototype
parent.postMessage(
{
type: "SET",
value: { type: "RAW", value: "x" },
path: ["__proto__", "foo"],
},
"*"
);
parent.postMessage("done", "*");
}
});
parent.postMessage("ready", "*");
</script>
</body>
</html>