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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce Site Isolation #1784

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 20 additions & 2 deletions src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "content-scripts-register-polyfill";
import { sendRealRequestToCustomServer, setupBackgroundRequestProxy } from "./maze-utils/background-request-proxy";
import { setupTabUpdates } from "./maze-utils/tab-updates";
import { generateUserID } from "./maze-utils/setup";
import { isFirefoxOrSafari } from "./maze-utils/";

// Make the config public for debugging purposes

Expand All @@ -31,6 +32,20 @@ utils.wait(() => Config.config !== null).then(function() {
setupBackgroundRequestProxy();
setupTabUpdates(Config);

function isUnsafe(sender) {
// Only trust messages from extension pages.
// https://chromium.googlesource.com/chromium/src/+/master/docs/security/compromised-renderers.md#Messaging
if (sender.origin) {
if (sender.origin === location.origin) return false;
console.warn(`Unsafe message from: ${sender.origin} via MessageSender.origin`);
} else if (sender.url && isFirefoxOrSafari()) {
const senderOrigin = new URL(sender.url).origin;
if (senderOrigin === location.origin) return false;
console.warn(`Unsafe message from: ${senderOrigin} via MessageSender.url`);
}
return true;
}

chrome.runtime.onMessage.addListener(function (request, sender, callback) {
switch(request.message) {
case "openConfig":
Expand All @@ -48,12 +63,15 @@ chrome.runtime.onMessage.addListener(function (request, sender, callback) {
//this allows the callback to be called later
return true;
case "registerContentScript":
registerFirefoxContentScript(request);
if (isUnsafe(sender)) return false;
utils.setupExtraSiteContentScripts();
return false;
case "unregisterContentScript":
if (isUnsafe(sender)) return false;
unregisterFirefoxContentScript(request.id)
return false;
case "tabs": {
if (isUnsafe(sender)) return false;
chrome.tabs.query({
active: true,
currentWindow: true
Expand Down Expand Up @@ -205,4 +223,4 @@ async function asyncRequestToServer(type: string, address: string, data = {}) {
const serverAddress = Config.config.testingServer ? CompileConfig.testingServerAddress : Config.config.serverAddress;

return await (sendRealRequestToCustomServer(type, serverAddress + address, data));
}
}
2 changes: 1 addition & 1 deletion src/popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class MessageHandler {
let allowPopup = window === window.top;
window.addEventListener("message", async (e): Promise<void> => {
if (e.source !== window.parent) return;
if (e.origin.endsWith('.youtube.com')) {
if (e.origin.startsWith('https://') && e.origin.endsWith('.youtube.com')) {
allowPopup = true;
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export default class Utils {
if (this.backgroundScriptContainer) {
this.backgroundScriptContainer.registerFirefoxContentScript(registration);
} else {
chrome.runtime.sendMessage(registration);
chrome.runtime.sendMessage({message: "registerContentScript"});
}
}

Expand Down