gecko-dev/dom/media/IdpSandbox.jsm

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

291 строка
8.2 KiB
JavaScript
Исходник Обычный вид История

/* 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/. */
"use strict";
Bug 1514594: Part 3 - Change ChromeUtils.import API. *** Bug 1514594: Part 3a - Change ChromeUtils.import to return an exports object; not pollute global. r=mccr8 This changes the behavior of ChromeUtils.import() to return an exports object, rather than a module global, in all cases except when `null` is passed as a second argument, and changes the default behavior not to pollute the global scope with the module's exports. Thus, the following code written for the old model: ChromeUtils.import("resource://gre/modules/Services.jsm"); is approximately the same as the following, in the new model: var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm"); Since the two behaviors are mutually incompatible, this patch will land with a scripted rewrite to update all existing callers to use the new model rather than the old. *** Bug 1514594: Part 3b - Mass rewrite all JS code to use the new ChromeUtils.import API. rs=Gijs This was done using the followng script: https://bitbucket.org/kmaglione/m-c-rewrites/src/tip/processors/cu-import-exports.jsm *** Bug 1514594: Part 3c - Update ESLint plugin for ChromeUtils.import API changes. r=Standard8 Differential Revision: https://phabricator.services.mozilla.com/D16747 *** Bug 1514594: Part 3d - Remove/fix hundreds of duplicate imports from sync tests. r=Gijs Differential Revision: https://phabricator.services.mozilla.com/D16748 *** Bug 1514594: Part 3e - Remove no-op ChromeUtils.import() calls. r=Gijs Differential Revision: https://phabricator.services.mozilla.com/D16749 *** Bug 1514594: Part 3f.1 - Cleanup various test corner cases after mass rewrite. r=Gijs *** Bug 1514594: Part 3f.2 - Cleanup various non-test corner cases after mass rewrite. r=Gijs Differential Revision: https://phabricator.services.mozilla.com/D16750 --HG-- extra : rebase_source : 359574ee3064c90f33bf36c2ebe3159a24cc8895 extra : histedit_source : b93c8f42808b1599f9122d7842d2c0b3e656a594%2C64a3a4e3359dc889e2ab2b49461bab9e27fc10a7
2019-01-17 21:18:31 +03:00
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const { NetUtil } = ChromeUtils.import("resource://gre/modules/NetUtil.jsm");
/** This little class ensures that redirects maintain an https:// origin */
function RedirectHttpsOnly() {}
RedirectHttpsOnly.prototype = {
asyncOnChannelRedirect(oldChannel, newChannel, flags, callback) {
if (newChannel.URI.scheme !== "https") {
callback.onRedirectVerifyCallback(Cr.NS_ERROR_ABORT);
} else {
callback.onRedirectVerifyCallback(Cr.NS_OK);
}
},
getInterface(iid) {
return this.QueryInterface(iid);
},
QueryInterface: ChromeUtils.generateQI([Ci.nsIChannelEventSink]),
};
/** This class loads a resource into a single string. ResourceLoader.load() is
* the entry point. */
function ResourceLoader(res, rej) {
this.resolve = res;
this.reject = rej;
this.data = "";
}
/** Loads the identified https:// URL. */
ResourceLoader.load = function(uri, doc) {
return new Promise((resolve, reject) => {
let listener = new ResourceLoader(resolve, reject);
let ioChannel = NetUtil.newChannel({
uri,
loadingNode: doc,
securityFlags: Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL,
contentPolicyType: Ci.nsIContentPolicy.TYPE_INTERNAL_SCRIPT,
});
ioChannel.loadGroup = doc.documentLoadGroup.QueryInterface(Ci.nsILoadGroup);
ioChannel.notificationCallbacks = new RedirectHttpsOnly();
ioChannel.asyncOpen(listener);
});
};
ResourceLoader.prototype = {
onDataAvailable(request, input, offset, count) {
let stream = Cc["@mozilla.org/scriptableinputstream;1"].createInstance(
Ci.nsIScriptableInputStream
);
stream.init(input);
this.data += stream.read(count);
},
onStartRequest(request) {},
onStopRequest(request, status) {
if (Components.isSuccessCode(status)) {
var statusCode = request.QueryInterface(Ci.nsIHttpChannel).responseStatus;
if (statusCode === 200) {
this.resolve({ request, data: this.data });
} else {
this.reject(new Error("Non-200 response from server: " + statusCode));
}
} else {
this.reject(new Error("Load failed: " + status));
}
},
getInterface(iid) {
return this.QueryInterface(iid);
},
QueryInterface: ChromeUtils.generateQI([Ci.nsIStreamListener]),
};
/**
* A simple implementation of the WorkerLocation interface.
*/
function createLocationFromURI(uri) {
return {
href: uri.spec,
protocol: uri.scheme + ":",
host: uri.host + (uri.port >= 0 ? ":" + uri.port : ""),
port: uri.port,
hostname: uri.host,
pathname: uri.pathQueryRef.replace(/[#\?].*/, ""),
search: uri.pathQueryRef.replace(/^[^\?]*/, "").replace(/#.*/, ""),
hash: uri.hasRef ? "#" + uri.ref : "",
origin: uri.prePath,
toString() {
return uri.spec;
},
};
}
/**
* A javascript sandbox for running an IdP.
*
* @param domain (string) the domain of the IdP
* @param protocol (string?) the protocol of the IdP [default: 'default']
* @param win (obj) the current window
* @throws if the domain or protocol aren't valid
*/
function IdpSandbox(domain, protocol, win) {
this.source = IdpSandbox.createIdpUri(domain, protocol || "default");
this.active = null;
this.sandbox = null;
this.window = win;
}
IdpSandbox.checkDomain = function(domain) {
if (!domain || typeof domain !== "string") {
throw new Error(
"Invalid domain for identity provider: " +
"must be a non-zero length string"
);
}
};
/**
* Checks that the IdP protocol is superficially sane. In particular, we don't
* want someone adding relative paths (e.g., '../../myuri'), which could be used
* to move outside of /.well-known/ and into space that they control.
*/
IdpSandbox.checkProtocol = function(protocol) {
let message = "Invalid protocol for identity provider: ";
if (!protocol || typeof protocol !== "string") {
throw new Error(message + "must be a non-zero length string");
}
if (decodeURIComponent(protocol).match(/[\/\\]/)) {
throw new Error(message + "must not include '/' or '\\'");
}
};
/**
* Turns a domain and protocol into a URI. This does some aggressive checking
* to make sure that we aren't being fooled somehow. Throws on fooling.
*/
IdpSandbox.createIdpUri = function(domain, protocol) {
IdpSandbox.checkDomain(domain);
IdpSandbox.checkProtocol(protocol);
let message = "Invalid IdP parameters: ";
try {
let wkIdp = "https://" + domain + "/.well-known/idp-proxy/" + protocol;
let uri = Services.io.newURI(wkIdp);
if (uri.hostPort !== domain) {
throw new Error(message + "domain is invalid");
}
if (uri.pathQueryRef.indexOf("/.well-known/idp-proxy/") !== 0) {
throw new Error(message + "must produce a /.well-known/idp-proxy/ URI");
}
return uri;
} catch (e) {
if (
typeof e.result !== "undefined" &&
e.result === Cr.NS_ERROR_MALFORMED_URI
) {
throw new Error(message + "must produce a valid URI");
}
throw e;
}
};
IdpSandbox.prototype = {
isSame(domain, protocol) {
return this.source.spec === IdpSandbox.createIdpUri(domain, protocol).spec;
},
start() {
if (!this.active) {
this.active = ResourceLoader.load(
this.source,
this.window.document
).then(result => this._createSandbox(result));
}
return this.active;
},
// Provides the sandbox with some useful facilities. Initially, this is only
// a minimal set; it is far easier to add more as the need arises, than to
// take them back if we discover a mistake.
_populateSandbox(uri) {
this.sandbox.location = Cu.cloneInto(
createLocationFromURI(uri),
this.sandbox,
{ cloneFunctions: true }
);
},
_createSandbox(result) {
let principal = Services.scriptSecurityManager.getChannelResultPrincipal(
result.request
);
this.sandbox = Cu.Sandbox(principal, {
sandboxName: "IdP-" + this.source.host,
wantComponents: false,
wantExportHelpers: false,
wantGlobalProperties: [
"indexedDB",
"XMLHttpRequest",
"TextEncoder",
"TextDecoder",
"URL",
"URLSearchParams",
"atob",
"btoa",
"Blob",
"crypto",
"rtcIdentityProvider",
"fetch",
],
});
let registrar = this.sandbox.rtcIdentityProvider;
if (!Cu.isXrayWrapper(registrar)) {
throw new Error("IdP setup failed");
}
// have to use the ultimate URI, not the starting one to avoid
// that origin stealing from the one that redirected to it
this._populateSandbox(result.request.URI);
try {
Cu.evalInSandbox(
result.data,
this.sandbox,
"latest",
result.request.URI.spec,
1
);
} catch (e) {
// These can be passed straight on, because they are explicitly labelled
// as being IdP errors by the IdP and we drop line numbers as a result.
if (e.name === "IdpError" || e.name === "IdpLoginError") {
throw e;
}
this._logError(e);
throw new Error("Error in IdP, check console for details");
}
if (!registrar.hasIdp) {
throw new Error("IdP failed to call rtcIdentityProvider.register()");
}
return registrar;
},
// Capture all the details from the error and log them to the console. This
// can't rethrow anything else because that could leak information about the
// internal workings of the IdP across origins.
_logError(e) {
let winID = this.window.windowUtils.currentInnerWindowID;
let scriptError = Cc["@mozilla.org/scripterror;1"].createInstance(
Ci.nsIScriptError
);
scriptError.initWithWindowID(
e.message,
e.fileName,
null,
e.lineNumber,
e.columnNumber,
Ci.nsIScriptError.errorFlag,
"content javascript",
winID
);
Services.console.logMessage(scriptError);
},
stop() {
if (this.sandbox) {
Cu.nukeSandbox(this.sandbox);
}
this.sandbox = null;
this.active = null;
},
toString() {
return this.source.spec;
},
};
var EXPORTED_SYMBOLS = ["IdpSandbox"];