2018-07-08 06:15:45 +03:00
|
|
|
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
/* eslint no-unused-vars: ["error", {args: "none"}] */
|
|
|
|
|
2018-07-30 09:36:12 +03:00
|
|
|
var EXPORTED_SYMBOLS = ["WebChannelChild"];
|
2018-07-08 06:15:45 +03:00
|
|
|
|
2019-07-05 12:04:56 +03:00
|
|
|
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
|
|
|
const { ActorChild } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/ActorChild.jsm"
|
|
|
|
);
|
2018-07-08 06:15:45 +03:00
|
|
|
|
|
|
|
function getMessageManager(event) {
|
|
|
|
let window = Cu.getGlobalForObject(event.target);
|
|
|
|
|
2018-08-03 06:49:09 +03:00
|
|
|
return window.docShell.messageManager;
|
2018-07-08 06:15:45 +03:00
|
|
|
}
|
|
|
|
|
2018-07-30 09:36:12 +03:00
|
|
|
// Preference containing the list (space separated) of origins that are
|
|
|
|
// allowed to send non-string values through a WebChannel, mainly for
|
|
|
|
// backwards compatability. See bug 1238128 for more information.
|
|
|
|
const URL_WHITELIST_PREF = "webchannel.allowObject.urlWhitelist";
|
2018-07-08 06:15:45 +03:00
|
|
|
|
2018-07-30 09:36:12 +03:00
|
|
|
// Cached list of whitelisted principals, we avoid constructing this if the
|
|
|
|
// value in `_lastWhitelistValue` hasn't changed since we constructed it last.
|
|
|
|
let _cachedWhitelist = [];
|
|
|
|
let _lastWhitelistValue = "";
|
2018-07-08 06:15:45 +03:00
|
|
|
|
2018-07-30 09:36:12 +03:00
|
|
|
class WebChannelChild extends ActorChild {
|
2018-07-08 06:15:45 +03:00
|
|
|
handleEvent(event) {
|
|
|
|
if (event.type === "WebChannelMessageToChrome") {
|
|
|
|
return this._onMessageToChrome(event);
|
|
|
|
}
|
|
|
|
return undefined;
|
2018-07-30 09:36:12 +03:00
|
|
|
}
|
2018-07-08 06:15:45 +03:00
|
|
|
|
|
|
|
receiveMessage(msg) {
|
|
|
|
if (msg.name === "WebChannelMessageToContent") {
|
|
|
|
return this._onMessageToContent(msg);
|
|
|
|
}
|
|
|
|
return undefined;
|
2018-07-30 09:36:12 +03:00
|
|
|
}
|
2018-07-08 06:15:45 +03:00
|
|
|
|
|
|
|
_getWhitelistedPrincipals() {
|
2018-07-30 09:36:12 +03:00
|
|
|
let whitelist = Services.prefs.getCharPref(URL_WHITELIST_PREF);
|
|
|
|
if (whitelist != _lastWhitelistValue) {
|
2018-07-08 06:15:45 +03:00
|
|
|
let urls = whitelist.split(/\s+/);
|
2018-07-30 09:36:12 +03:00
|
|
|
_cachedWhitelist = urls.map(origin =>
|
2019-07-05 12:04:56 +03:00
|
|
|
Services.scriptSecurityManager.createCodebasePrincipalFromOrigin(origin)
|
|
|
|
);
|
2018-07-08 06:15:45 +03:00
|
|
|
}
|
2018-07-30 09:36:12 +03:00
|
|
|
return _cachedWhitelist;
|
|
|
|
}
|
2018-07-08 06:15:45 +03:00
|
|
|
|
|
|
|
_onMessageToChrome(e) {
|
|
|
|
// If target is window then we want the document principal, otherwise fallback to target itself.
|
2019-07-05 12:04:56 +03:00
|
|
|
let principal = e.target.nodePrincipal
|
|
|
|
? e.target.nodePrincipal
|
|
|
|
: e.target.document.nodePrincipal;
|
2018-07-08 06:15:45 +03:00
|
|
|
|
|
|
|
if (e.detail) {
|
|
|
|
if (typeof e.detail != "string") {
|
|
|
|
// Check if the principal is one of the ones that's allowed to send
|
|
|
|
// non-string values for e.detail. They're whitelisted by site origin,
|
|
|
|
// so we compare on originNoSuffix in order to avoid other origin attributes
|
|
|
|
// that are not relevant here, such as containers or private browsing.
|
2019-07-05 12:04:56 +03:00
|
|
|
let objectsAllowed = this._getWhitelistedPrincipals().some(
|
|
|
|
whitelisted => principal.originNoSuffix == whitelisted.originNoSuffix
|
|
|
|
);
|
2018-07-08 06:15:45 +03:00
|
|
|
if (!objectsAllowed) {
|
2019-07-05 12:04:56 +03:00
|
|
|
Cu.reportError(
|
|
|
|
"WebChannelMessageToChrome sent with an object from a non-whitelisted principal"
|
|
|
|
);
|
2018-07-08 06:15:45 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mm = getMessageManager(e);
|
|
|
|
|
2019-07-05 12:04:56 +03:00
|
|
|
mm.sendAsyncMessage(
|
|
|
|
"WebChannelMessageToChrome",
|
|
|
|
e.detail,
|
|
|
|
{ eventTarget: e.target },
|
|
|
|
principal
|
|
|
|
);
|
2018-07-08 06:15:45 +03:00
|
|
|
} else {
|
|
|
|
Cu.reportError("WebChannel message failed. No message detail.");
|
|
|
|
}
|
2018-07-30 09:36:12 +03:00
|
|
|
}
|
2018-07-08 06:15:45 +03:00
|
|
|
|
|
|
|
_onMessageToContent(msg) {
|
|
|
|
if (msg.data) {
|
|
|
|
// msg.objects.eventTarget will be defined if sending a response to
|
|
|
|
// a WebChannelMessageToChrome event. An unsolicited send
|
|
|
|
// may not have an eventTarget defined, in this case send to the
|
|
|
|
// main content window.
|
|
|
|
let eventTarget = msg.objects.eventTarget || msg.target.content;
|
|
|
|
|
|
|
|
// Use nodePrincipal if available, otherwise fallback to document principal.
|
2019-07-05 12:04:56 +03:00
|
|
|
let targetPrincipal =
|
|
|
|
eventTarget instanceof Ci.nsIDOMWindow
|
|
|
|
? eventTarget.document.nodePrincipal
|
|
|
|
: eventTarget.nodePrincipal;
|
2018-07-08 06:15:45 +03:00
|
|
|
|
|
|
|
if (msg.principal.subsumes(targetPrincipal)) {
|
|
|
|
// If eventTarget is a window, use it as the targetWindow, otherwise
|
|
|
|
// find the window that owns the eventTarget.
|
2019-07-05 12:04:56 +03:00
|
|
|
let targetWindow =
|
|
|
|
eventTarget instanceof Ci.nsIDOMWindow
|
|
|
|
? eventTarget
|
|
|
|
: eventTarget.ownerGlobal;
|
|
|
|
|
|
|
|
eventTarget.dispatchEvent(
|
|
|
|
new targetWindow.CustomEvent("WebChannelMessageToContent", {
|
|
|
|
detail: Cu.cloneInto(
|
|
|
|
{
|
|
|
|
id: msg.data.id,
|
|
|
|
message: msg.data.message,
|
|
|
|
},
|
|
|
|
targetWindow
|
|
|
|
),
|
|
|
|
})
|
|
|
|
);
|
2018-07-08 06:15:45 +03:00
|
|
|
} else {
|
|
|
|
Cu.reportError("WebChannel message failed. Principal mismatch.");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Cu.reportError("WebChannel message failed. No message data.");
|
|
|
|
}
|
2018-07-30 09:36:12 +03:00
|
|
|
}
|
|
|
|
}
|