1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 /* eslint no-unused-vars: ["error", {args: "none"}] */
8 var EXPORTED_SYMBOLS = ["WebChannelChild"];
10 const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
11 const {ActorChild} = ChromeUtils.import("resource://gre/modules/ActorChild.jsm");
13 function getMessageManager(event) {
14 let window = Cu.getGlobalForObject(event.target);
16 return window.docShell.messageManager;
19 // Preference containing the list (space separated) of origins that are
20 // allowed to send non-string values through a WebChannel, mainly for
21 // backwards compatability. See bug 1238128 for more information.
22 const URL_WHITELIST_PREF = "webchannel.allowObject.urlWhitelist";
24 // Cached list of whitelisted principals, we avoid constructing this if the
25 // value in `_lastWhitelistValue` hasn't changed since we constructed it last.
26 let _cachedWhitelist = [];
27 let _lastWhitelistValue = "";
29 class WebChannelChild extends ActorChild {
31 if (event.type === "WebChannelMessageToChrome") {
32 return this._onMessageToChrome(event);
38 if (msg.name === "WebChannelMessageToContent") {
39 return this._onMessageToContent(msg);
44 _getWhitelistedPrincipals() {
45 let whitelist = Services.prefs.getCharPref(URL_WHITELIST_PREF);
46 if (whitelist != _lastWhitelistValue) {
47 let urls = whitelist.split(/\s+/);
48 _cachedWhitelist = urls.map(origin =>
49 Services.scriptSecurityManager.createCodebasePrincipalFromOrigin(origin));
51 return _cachedWhitelist;
54 _onMessageToChrome(e) {
55 // If target is window then we want the document principal, otherwise fallback to target itself.
56 let principal = e.target.nodePrincipal ? e.target.nodePrincipal : e.target.document.nodePrincipal;
59 if (typeof e.detail != "string") {
60 // Check if the principal is one of the ones that's allowed to send
61 // non-string values for e.detail. They're whitelisted by site origin,
62 // so we compare on originNoSuffix in order to avoid other origin attributes
63 // that are not relevant here, such as containers or private browsing.
64 let objectsAllowed = this._getWhitelistedPrincipals().some(whitelisted =>
65 principal.originNoSuffix == whitelisted.originNoSuffix);
66 if (!objectsAllowed) {
67 Cu.reportError("WebChannelMessageToChrome sent with an object from a non-whitelisted principal");
72 let mm = getMessageManager(e);
74 mm.sendAsyncMessage("WebChannelMessageToChrome", e.detail, { eventTarget: e.target }, principal);
76 Cu.reportError("WebChannel message failed. No message detail.");
80 _onMessageToContent(msg) {
82 // msg.objects.eventTarget will be defined if sending a response to
83 // a WebChannelMessageToChrome event. An unsolicited send
84 // may not have an eventTarget defined, in this case send to the
85 // main content window.
86 let eventTarget = msg.objects.eventTarget || msg.target.content;
88 // Use nodePrincipal if available, otherwise fallback to document principal.
89 let targetPrincipal = eventTarget instanceof Ci.nsIDOMWindow ? eventTarget.document.nodePrincipal : eventTarget.nodePrincipal;
91 if (msg.principal.subsumes(targetPrincipal)) {
92 // If eventTarget is a window, use it as the targetWindow, otherwise
93 // find the window that owns the eventTarget.
94 let targetWindow = eventTarget instanceof Ci.nsIDOMWindow ? eventTarget : eventTarget.ownerGlobal;
96 eventTarget.dispatchEvent(new targetWindow.CustomEvent("WebChannelMessageToContent", {
97 detail: Cu.cloneInto({
99 message: msg.data.message,
103 Cu.reportError("WebChannel message failed. Principal mismatch.");
106 Cu.reportError("WebChannel message failed. No message data.");