Bug 1667455 - Part 1-b: Define Services in Sandbox with wantComponents. r=kmag

Differential Revision: https://phabricator.services.mozilla.com/D150924
This commit is contained in:
Tooru Fujisawa 2022-07-11 12:41:48 +00:00
Родитель d3780ba56a
Коммит ad68a98f88
4 изменённых файлов: 31 добавлений и 7 удалений

Просмотреть файл

@ -1443,9 +1443,15 @@ nsresult xpc::CreateSandboxObject(JSContext* cx, MutableHandleValue vp,
}
bool allowComponents = principal->IsSystemPrincipal();
if (options.wantComponents && allowComponents &&
!ObjectScope(sandbox)->AttachComponentsObject(cx))
return NS_ERROR_XPC_UNEXPECTED;
if (options.wantComponents && allowComponents) {
if (!ObjectScope(sandbox)->AttachComponentsObject(cx)) {
return NS_ERROR_XPC_UNEXPECTED;
}
if (!ObjectScope(sandbox)->AttachJSServices(cx)) {
return NS_ERROR_XPC_UNEXPECTED;
}
}
if (!XPCNativeWrapper::AttachNewConstructorObject(cx, sandbox)) {
return NS_ERROR_XPC_UNEXPECTED;

Просмотреть файл

@ -19,6 +19,7 @@
#include "js/Object.h" // JS::GetCompartment
#include "js/PropertyAndElement.h" // JS_DefineProperty, JS_DefinePropertyById
#include "js/RealmIterators.h"
#include "mozJSModuleLoader.h"
#include "mozilla/dom/BindingUtils.h"
@ -170,6 +171,11 @@ bool XPCWrappedNativeScope::AttachComponentsObject(JSContext* aCx) {
return true;
}
bool XPCWrappedNativeScope::AttachJSServices(JSContext* aCx) {
RootedObject global(aCx, CurrentGlobalOrNull(aCx));
return mozJSModuleLoader::Get()->DefineJSServices(aCx, global);
}
bool XPCWrappedNativeScope::XBLScopeStateMatches(nsIPrincipal* aPrincipal) {
return mAllowContentXBLScope ==
!RemoteXULForbidsXBLScopeForPrincipal(aPrincipal);

Просмотреть файл

@ -825,6 +825,8 @@ class XPCWrappedNativeScope final
bool AttachComponentsObject(JSContext* aCx);
bool AttachJSServices(JSContext* aCx);
// Returns the JS object reflection of the Components object.
bool GetComponentsJSObject(JSContext* cx, JS::MutableHandleObject obj);

Просмотреть файл

@ -1,6 +1,16 @@
function run_test() {
var sb = Cu.Sandbox(this, {wantComponents: false});
var sb;
var rv = Cu.evalInSandbox("this.Components", sb);
Assert.equal(rv, undefined);
}
sb = Cu.Sandbox(this, {wantComponents: false});
Assert.equal(Cu.evalInSandbox("this.Components", sb), undefined);
Assert.equal(Cu.evalInSandbox("this.Services", sb), undefined);
sb = Cu.Sandbox(this, {wantComponents: true});
Assert.equal(Cu.evalInSandbox("typeof this.Components", sb), "object");
Assert.equal(Cu.evalInSandbox("typeof this.Services", sb), "object");
// wantComponents defaults to true.
sb = Cu.Sandbox(this, {});
Assert.equal(Cu.evalInSandbox("typeof this.Components", sb), "object");
Assert.equal(Cu.evalInSandbox("typeof this.Services", sb), "object");
}