-
Notifications
You must be signed in to change notification settings - Fork 397
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
surma
merged 4 commits into
GoogleChromeLabs:main
from
benjamind:delarre/origin-filtering
Jan 24, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 failed, the prototype was not updated | ||
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, the prototype was updated | ||
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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.