2010-06-25 15:58:09 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* vim: set ts=4 sw=4 et tw=99 ft=cpp:
|
|
|
|
*
|
2012-05-21 04:12:37 -07:00
|
|
|
* 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/. */
|
2010-06-25 15:58:09 -07:00
|
|
|
|
2012-07-16 10:22:51 -07:00
|
|
|
#include "WaiveXrayWrapper.h"
|
2010-07-02 13:54:53 -07:00
|
|
|
#include "FilteringWrapper.h"
|
|
|
|
#include "XrayWrapper.h"
|
2010-06-25 15:58:09 -07:00
|
|
|
#include "AccessCheck.h"
|
2010-10-10 15:36:38 -07:00
|
|
|
#include "XPCWrapper.h"
|
2012-07-27 03:15:46 -07:00
|
|
|
#include "ChromeObjectWrapper.h"
|
2013-05-13 09:04:23 -07:00
|
|
|
#include "WrapperFactory.h"
|
2010-06-25 15:58:09 -07:00
|
|
|
|
2010-07-02 13:54:53 -07:00
|
|
|
#include "xpcprivate.h"
|
2011-10-14 10:52:47 -07:00
|
|
|
#include "XPCMaps.h"
|
2012-05-02 21:35:38 -07:00
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
2012-01-15 00:13:09 -08:00
|
|
|
#include "jsfriendapi.h"
|
2012-05-29 14:24:03 -07:00
|
|
|
#include "mozilla/Likely.h"
|
2013-02-08 06:24:20 -08:00
|
|
|
#include "nsContentUtils.h"
|
2012-01-11 00:23:08 -08:00
|
|
|
|
2013-09-01 21:51:02 -07:00
|
|
|
using namespace JS;
|
2011-09-08 20:29:15 -07:00
|
|
|
using namespace js;
|
2012-09-12 13:29:30 -07:00
|
|
|
using namespace mozilla;
|
2011-09-08 20:29:15 -07:00
|
|
|
|
2010-06-25 15:58:09 -07:00
|
|
|
namespace xpc {
|
|
|
|
|
2010-07-02 13:54:53 -07:00
|
|
|
// When chrome pulls a naked property across the membrane using
|
|
|
|
// .wrappedJSObject, we want it to cross the membrane into the
|
|
|
|
// chrome compartment without automatically being wrapped into an
|
|
|
|
// X-ray wrapper. We achieve this by wrapping it into a special
|
|
|
|
// transparent wrapper in the origin (non-chrome) compartment. When
|
|
|
|
// an object with that special wrapper applied crosses into chrome,
|
|
|
|
// we know to not apply an X-ray wrapper.
|
2012-10-29 08:52:53 -07:00
|
|
|
Wrapper XrayWaiver(WrapperFactory::WAIVE_XRAY_WRAPPER_FLAG);
|
2010-07-02 13:54:53 -07:00
|
|
|
|
|
|
|
// When objects for which we waived the X-ray wrapper cross into
|
|
|
|
// chrome, we wrap them into a special cross-compartment wrapper
|
|
|
|
// that transitively extends the waiver to all properties we get
|
|
|
|
// off it.
|
2012-07-16 08:53:16 -07:00
|
|
|
WaiveXrayWrapper WaiveXrayWrapper::singleton(0);
|
2010-10-10 15:47:22 -07:00
|
|
|
|
2013-03-21 08:20:44 -07:00
|
|
|
bool
|
|
|
|
WrapperFactory::IsCOW(JSObject *obj)
|
|
|
|
{
|
|
|
|
return IsWrapper(obj) &&
|
|
|
|
Wrapper::wrapperHandler(obj) == &ChromeObjectWrapper::singleton;
|
|
|
|
}
|
|
|
|
|
2012-07-23 06:51:18 -07:00
|
|
|
JSObject *
|
2013-11-19 10:12:10 -08:00
|
|
|
WrapperFactory::GetXrayWaiver(HandleObject obj)
|
2012-07-23 06:51:18 -07:00
|
|
|
{
|
|
|
|
// Object should come fully unwrapped but outerized.
|
2013-04-11 11:50:18 -07:00
|
|
|
MOZ_ASSERT(obj == UncheckedUnwrap(obj));
|
2012-07-23 06:51:18 -07:00
|
|
|
MOZ_ASSERT(!js::GetObjectClass(obj)->ext.outerObject);
|
2012-10-25 08:01:09 -07:00
|
|
|
XPCWrappedNativeScope *scope = GetObjectScope(obj);
|
2012-10-25 08:01:09 -07:00
|
|
|
MOZ_ASSERT(scope);
|
2012-07-23 06:51:18 -07:00
|
|
|
|
2012-10-25 08:01:09 -07:00
|
|
|
if (!scope->mWaiverWrapperMap)
|
2013-10-10 13:35:53 -07:00
|
|
|
return nullptr;
|
2013-09-08 20:28:48 -07:00
|
|
|
|
|
|
|
JSObject* xrayWaiver = scope->mWaiverWrapperMap->Find(obj);
|
|
|
|
if (xrayWaiver)
|
|
|
|
JS::ExposeObjectToActiveJS(xrayWaiver);
|
|
|
|
|
|
|
|
return xrayWaiver;
|
2012-07-23 06:51:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject *
|
2013-04-12 02:50:43 -07:00
|
|
|
WrapperFactory::CreateXrayWaiver(JSContext *cx, HandleObject obj)
|
2012-07-23 06:51:18 -07:00
|
|
|
{
|
|
|
|
// The caller is required to have already done a lookup.
|
|
|
|
// NB: This implictly performs the assertions of GetXrayWaiver.
|
|
|
|
MOZ_ASSERT(!GetXrayWaiver(obj));
|
2012-10-25 08:01:09 -07:00
|
|
|
XPCWrappedNativeScope *scope = GetObjectScope(obj);
|
2012-07-23 06:51:18 -07:00
|
|
|
|
2012-08-21 18:42:53 -07:00
|
|
|
JSAutoCompartment ac(cx, obj);
|
2013-12-13 12:01:30 -08:00
|
|
|
JSObject *waiver = Wrapper::New(cx, obj,
|
2012-07-23 06:51:18 -07:00
|
|
|
JS_GetGlobalForObject(cx, obj),
|
|
|
|
&XrayWaiver);
|
|
|
|
if (!waiver)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2012-07-23 06:51:18 -07:00
|
|
|
|
|
|
|
// Add the new waiver to the map. It's important that we only ever have
|
|
|
|
// one waiver for the lifetime of the target object.
|
2012-10-25 08:01:09 -07:00
|
|
|
if (!scope->mWaiverWrapperMap) {
|
|
|
|
scope->mWaiverWrapperMap =
|
|
|
|
JSObject2JSObjectMap::newMap(XPC_WRAPPER_MAP_SIZE);
|
|
|
|
MOZ_ASSERT(scope->mWaiverWrapperMap);
|
2012-07-23 06:51:18 -07:00
|
|
|
}
|
2013-06-05 16:40:02 -07:00
|
|
|
if (!scope->mWaiverWrapperMap->Add(cx, obj, waiver))
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2012-07-23 06:51:18 -07:00
|
|
|
return waiver;
|
|
|
|
}
|
|
|
|
|
2011-01-13 13:03:44 -08:00
|
|
|
JSObject *
|
2013-04-12 02:50:43 -07:00
|
|
|
WrapperFactory::WaiveXray(JSContext *cx, JSObject *objArg)
|
2011-01-13 13:03:44 -08:00
|
|
|
{
|
2013-04-12 02:50:43 -07:00
|
|
|
RootedObject obj(cx, objArg);
|
2013-04-11 11:50:18 -07:00
|
|
|
obj = UncheckedUnwrap(obj);
|
2013-02-04 07:13:13 -08:00
|
|
|
MOZ_ASSERT(!js::IsInnerObject(obj));
|
2011-01-13 13:03:44 -08:00
|
|
|
|
2012-07-23 06:51:18 -07:00
|
|
|
JSObject *waiver = GetXrayWaiver(obj);
|
|
|
|
if (waiver)
|
|
|
|
return waiver;
|
|
|
|
return CreateXrayWaiver(cx, obj);
|
2011-01-13 13:03:44 -08:00
|
|
|
}
|
|
|
|
|
2011-05-23 08:20:28 -07:00
|
|
|
// DoubleWrap is called from PrepareForWrapping to maintain the state that
|
|
|
|
// we're supposed to waive Xray wrappers for the given on. On entrance, it
|
|
|
|
// expects |cx->compartment != obj->compartment()|. The returned object will
|
|
|
|
// be in the same compartment as |obj|.
|
2011-01-13 13:03:44 -08:00
|
|
|
JSObject *
|
2013-04-12 02:50:43 -07:00
|
|
|
WrapperFactory::DoubleWrap(JSContext *cx, HandleObject obj, unsigned flags)
|
2011-01-13 13:03:44 -08:00
|
|
|
{
|
|
|
|
if (flags & WrapperFactory::WAIVE_XRAY_WRAPPER_FLAG) {
|
2012-08-21 18:42:53 -07:00
|
|
|
JSAutoCompartment ac(cx, obj);
|
2011-01-13 13:03:44 -08:00
|
|
|
return WaiveXray(cx, obj);
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2010-10-10 15:36:38 -07:00
|
|
|
JSObject *
|
2013-04-15 10:32:55 -07:00
|
|
|
WrapperFactory::PrepareForWrapping(JSContext *cx, HandleObject scope,
|
|
|
|
HandleObject objArg, unsigned flags)
|
2010-10-10 15:36:38 -07:00
|
|
|
{
|
2013-04-12 02:50:43 -07:00
|
|
|
RootedObject obj(cx, objArg);
|
2013-02-04 07:13:13 -08:00
|
|
|
// Outerize any raw inner objects at the entry point here, so that we don't
|
|
|
|
// have to worry about them for the rest of the wrapping code.
|
|
|
|
if (js::IsInnerObject(obj)) {
|
|
|
|
JSAutoCompartment ac(cx, obj);
|
|
|
|
obj = JS_ObjectToOuterObject(cx, obj);
|
|
|
|
NS_ENSURE_TRUE(obj, nullptr);
|
|
|
|
// The outerization hook wraps, which means that we can end up with a
|
|
|
|
// CCW here if |obj| was a navigated-away-from inner. Strip any CCWs.
|
2013-04-11 11:50:18 -07:00
|
|
|
obj = js::UncheckedUnwrap(obj);
|
2013-02-04 07:13:13 -08:00
|
|
|
MOZ_ASSERT(js::IsOuterObject(obj));
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we've got an outer window, there's nothing special that needs to be
|
|
|
|
// done here, and we can move on to the next phase of wrapping. We handle
|
|
|
|
// this case first to allow us to assert against wrappers below.
|
|
|
|
if (js::IsOuterObject(obj))
|
2010-10-10 15:47:22 -07:00
|
|
|
return DoubleWrap(cx, obj, flags);
|
|
|
|
|
2010-10-10 15:36:38 -07:00
|
|
|
// Here are the rules for wrapping:
|
|
|
|
// We should never get a proxy here (the JS engine unwraps those for us).
|
2012-09-20 00:55:37 -07:00
|
|
|
MOZ_ASSERT(!IsWrapper(obj));
|
2010-10-10 15:36:38 -07:00
|
|
|
|
2013-05-15 21:13:12 -07:00
|
|
|
// If the object being wrapped is a prototype for a standard class and the
|
|
|
|
// wrapper does not subsumes the wrappee, use the one from the content
|
|
|
|
// compartment. This is generally safer all-around, and in the COW case this
|
|
|
|
// lets us safely take advantage of things like .forEach() via the
|
|
|
|
// ChromeObjectWrapper machinery.
|
|
|
|
//
|
|
|
|
// If the prototype chain of chrome object |obj| looks like this:
|
|
|
|
//
|
|
|
|
// obj => foo => bar => chromeWin.StandardClass.prototype
|
|
|
|
//
|
|
|
|
// The prototype chain of COW(obj) looks lke this:
|
|
|
|
//
|
|
|
|
// COW(obj) => COW(foo) => COW(bar) => contentWin.StandardClass.prototype
|
|
|
|
//
|
|
|
|
// NB: We now remap all non-subsuming access of standard prototypes.
|
2013-05-21 06:53:35 -07:00
|
|
|
//
|
|
|
|
// NB: We need to ignore domain here so that the security relationship we
|
|
|
|
// compute here can't change over time. See the comment above the other
|
|
|
|
// subsumesIgnoringDomain call below.
|
|
|
|
bool subsumes = AccessCheck::subsumesIgnoringDomain(js::GetContextCompartment(cx),
|
|
|
|
js::GetObjectCompartment(obj));
|
2013-05-15 21:13:12 -07:00
|
|
|
XrayType xrayType = GetXrayType(obj);
|
|
|
|
if (!subsumes && xrayType == NotXray) {
|
|
|
|
JSProtoKey key = JSProto_Null;
|
|
|
|
{
|
|
|
|
JSAutoCompartment ac(cx, obj);
|
|
|
|
key = JS_IdentifyClassPrototype(cx, obj);
|
|
|
|
}
|
|
|
|
if (key != JSProto_Null) {
|
|
|
|
RootedObject homeProto(cx);
|
|
|
|
if (!JS_GetClassPrototype(cx, key, homeProto.address()))
|
|
|
|
return nullptr;
|
|
|
|
MOZ_ASSERT(homeProto);
|
|
|
|
// No need to double-wrap here. We should never have waivers to
|
|
|
|
// COWs.
|
|
|
|
return homeProto;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-10 15:36:38 -07:00
|
|
|
// Now, our object is ready to be wrapped, but several objects (notably
|
|
|
|
// nsJSIIDs) have a wrapper per scope. If we are about to wrap one of
|
|
|
|
// those objects in a security wrapper, then we need to hand back the
|
2010-10-10 15:47:16 -07:00
|
|
|
// wrapper for the new scope instead. Also, global objects don't move
|
|
|
|
// between scopes so for those we also want to return the wrapper. So...
|
2013-04-19 12:58:30 -07:00
|
|
|
if (!IS_WN_REFLECTOR(obj) || !js::GetObjectParent(obj))
|
2010-10-10 15:47:22 -07:00
|
|
|
return DoubleWrap(cx, obj, flags);
|
2010-10-10 15:36:38 -07:00
|
|
|
|
2013-06-23 00:15:55 -07:00
|
|
|
XPCWrappedNative *wn = XPCWrappedNative::Get(obj);
|
2010-10-10 15:36:38 -07:00
|
|
|
|
2012-08-21 18:42:53 -07:00
|
|
|
JSAutoCompartment ac(cx, obj);
|
2011-02-11 19:32:45 -08:00
|
|
|
XPCCallContext ccx(JS_CALLER, cx, obj);
|
2013-04-15 10:32:55 -07:00
|
|
|
RootedObject wrapScope(cx, scope);
|
2011-02-11 19:32:45 -08:00
|
|
|
|
2011-02-05 04:56:05 -08:00
|
|
|
{
|
|
|
|
if (NATIVE_HAS_FLAG(&ccx, WantPreCreate)) {
|
|
|
|
// We have a precreate hook. This object might enforce that we only
|
|
|
|
// ever create JS object for it.
|
2012-05-29 14:24:03 -07:00
|
|
|
|
|
|
|
// Note: this penalizes objects that only have one wrapper, but are
|
|
|
|
// being accessed across compartments. We would really prefer to
|
|
|
|
// replace the above code with a test that says "do you only have one
|
|
|
|
// wrapper?"
|
2011-02-05 04:56:05 -08:00
|
|
|
nsresult rv = wn->GetScriptableInfo()->GetCallback()->
|
2013-04-15 10:32:55 -07:00
|
|
|
PreCreate(wn->Native(), cx, scope, wrapScope.address());
|
2011-02-05 04:56:05 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, DoubleWrap(cx, obj, flags));
|
|
|
|
|
|
|
|
// If the handed back scope differs from the passed-in scope and is in
|
|
|
|
// a separate compartment, then this object is explicitly requesting
|
|
|
|
// that we don't create a second JS object for it: create a security
|
|
|
|
// wrapper.
|
2013-04-15 10:32:55 -07:00
|
|
|
if (js::GetObjectCompartment(scope) != js::GetObjectCompartment(wrapScope))
|
2011-02-05 04:56:05 -08:00
|
|
|
return DoubleWrap(cx, obj, flags);
|
|
|
|
|
2013-04-12 02:50:43 -07:00
|
|
|
RootedObject currentScope(cx, JS_GetGlobalForObject(cx, obj));
|
2013-04-15 10:32:55 -07:00
|
|
|
if (MOZ_UNLIKELY(wrapScope != currentScope)) {
|
2012-05-29 14:24:03 -07:00
|
|
|
// The wrapper claims it wants to be in the new scope, but
|
|
|
|
// currently has a reflection that lives in the old scope. This
|
|
|
|
// can mean one of two things, both of which are rare:
|
|
|
|
//
|
|
|
|
// 1 - The object has a PreCreate hook (we checked for it above),
|
|
|
|
// but is deciding to request one-wrapper-per-scope (rather than
|
|
|
|
// one-wrapper-per-native) for some reason. Usually, a PreCreate
|
|
|
|
// hook indicates one-wrapper-per-native. In this case we want to
|
|
|
|
// make a new wrapper in the new scope.
|
|
|
|
//
|
|
|
|
// 2 - We're midway through wrapper reparenting. The document has
|
|
|
|
// moved to a new scope, but |wn| hasn't been moved yet, and
|
|
|
|
// we ended up calling JS_WrapObject() on its JS object. In this
|
|
|
|
// case, we want to return the existing wrapper.
|
|
|
|
//
|
|
|
|
// So we do a trick: call PreCreate _again_, but say that we're
|
|
|
|
// wrapping for the old scope, rather than the new one. If (1) is
|
|
|
|
// the case, then PreCreate will return the scope we pass to it
|
|
|
|
// (the old scope). If (2) is the case, PreCreate will return the
|
|
|
|
// scope of the document (the new scope).
|
2013-04-12 02:50:43 -07:00
|
|
|
RootedObject probe(cx);
|
2012-05-29 14:24:03 -07:00
|
|
|
rv = wn->GetScriptableInfo()->GetCallback()->
|
2013-04-12 02:50:43 -07:00
|
|
|
PreCreate(wn->Native(), cx, currentScope, probe.address());
|
2012-05-29 14:24:03 -07:00
|
|
|
|
|
|
|
// Check for case (2).
|
|
|
|
if (probe != currentScope) {
|
2013-04-15 10:32:55 -07:00
|
|
|
MOZ_ASSERT(probe == wrapScope);
|
2012-05-29 14:24:03 -07:00
|
|
|
return DoubleWrap(cx, obj, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ok, must be case (1). Fall through and create a new wrapper.
|
|
|
|
}
|
2012-08-16 12:25:39 -07:00
|
|
|
|
|
|
|
// Nasty hack for late-breaking bug 781476. This will confuse identity checks,
|
|
|
|
// but it's probably better than any of our alternatives.
|
2012-09-11 10:23:20 -07:00
|
|
|
//
|
|
|
|
// Note: We have to ignore domain here. The JS engine assumes that, given a
|
|
|
|
// compartment c, if c->wrap(x) returns a cross-compartment wrapper at time t0,
|
|
|
|
// it will also return a cross-compartment wrapper for any time t1 > t0 unless
|
|
|
|
// an explicit transplant is performed. In particular, wrapper recomputation
|
|
|
|
// assumes that recomputing a wrapper will always result in a wrapper.
|
|
|
|
//
|
|
|
|
// This doesn't actually pose a security issue, because we'll still compute
|
|
|
|
// the correct (opaque) wrapper for the object below given the security
|
|
|
|
// characteristics of the two compartments.
|
2013-04-15 10:32:55 -07:00
|
|
|
if (!AccessCheck::isChrome(js::GetObjectCompartment(wrapScope)) &&
|
|
|
|
AccessCheck::subsumesIgnoringDomain(js::GetObjectCompartment(wrapScope),
|
2012-09-11 10:23:20 -07:00
|
|
|
js::GetObjectCompartment(obj)))
|
2012-08-16 12:25:39 -07:00
|
|
|
{
|
|
|
|
return DoubleWrap(cx, obj, flags);
|
|
|
|
}
|
2011-02-05 04:56:05 -08:00
|
|
|
}
|
2010-10-10 15:36:38 -07:00
|
|
|
}
|
2010-10-10 15:47:22 -07:00
|
|
|
|
2013-04-15 10:32:55 -07:00
|
|
|
// This public WrapNativeToJSVal API enters the compartment of 'wrapScope'
|
2011-02-11 19:32:45 -08:00
|
|
|
// so we don't have to.
|
2013-04-12 02:50:43 -07:00
|
|
|
RootedValue v(cx);
|
2010-10-10 15:36:38 -07:00
|
|
|
nsresult rv =
|
2013-05-17 10:28:08 -07:00
|
|
|
nsXPConnect::XPConnect()->WrapNativeToJSVal(cx, wrapScope, wn->Native(), nullptr,
|
|
|
|
&NS_GET_IID(nsISupports), false,
|
2014-01-06 11:54:43 -08:00
|
|
|
v.address());
|
2013-06-04 10:30:11 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
|
|
|
|
|
|
|
obj = JSVAL_TO_OBJECT(v);
|
2013-08-14 00:00:34 -07:00
|
|
|
MOZ_ASSERT(IS_WN_REFLECTOR(obj), "bad object");
|
2013-06-04 10:30:11 -07:00
|
|
|
|
|
|
|
// Because the underlying native didn't have a PreCreate hook, we had
|
|
|
|
// to a new (or possibly pre-existing) XPCWN in our compartment.
|
|
|
|
// This could be a problem for chrome code that passes XPCOM objects
|
|
|
|
// across compartments, because the effects of QI would disappear across
|
|
|
|
// compartments.
|
|
|
|
//
|
|
|
|
// So whenever we pull an XPCWN across compartments in this manner, we
|
|
|
|
// give the destination object the union of the two native sets. We try
|
|
|
|
// to do this cleverly in the common case to avoid too much overhead.
|
2013-06-23 00:15:55 -07:00
|
|
|
XPCWrappedNative *newwn = XPCWrappedNative::Get(obj);
|
2013-06-04 21:11:18 -07:00
|
|
|
XPCNativeSet *unionSet = XPCNativeSet::GetNewOrUsed(newwn->GetSet(),
|
2013-06-04 10:30:11 -07:00
|
|
|
wn->GetSet(), false);
|
|
|
|
if (!unionSet)
|
|
|
|
return nullptr;
|
|
|
|
newwn->SetSet(unionSet);
|
2010-10-10 15:47:22 -07:00
|
|
|
|
|
|
|
return DoubleWrap(cx, obj, flags);
|
2010-10-10 15:36:38 -07:00
|
|
|
}
|
|
|
|
|
2012-11-14 09:56:25 -08:00
|
|
|
#ifdef DEBUG
|
|
|
|
static void
|
2013-05-09 02:39:21 -07:00
|
|
|
DEBUG_CheckUnwrapSafety(HandleObject obj, js::Wrapper *handler,
|
2012-11-14 09:56:25 -08:00
|
|
|
JSCompartment *origin, JSCompartment *target)
|
|
|
|
{
|
|
|
|
if (AccessCheck::isChrome(target) || xpc::IsUniversalXPConnectEnabled(target)) {
|
|
|
|
// If the caller is chrome (or effectively so), unwrap should always be allowed.
|
|
|
|
MOZ_ASSERT(handler->isSafeToUnwrap());
|
2013-10-04 04:29:35 -07:00
|
|
|
} else if (AccessCheck::needsSystemOnlyWrapper(obj)) {
|
|
|
|
// The rules for SOWs are complicated enough. Just skip double-checking them here.
|
2013-04-03 11:41:23 -07:00
|
|
|
} else if (handler == &FilteringWrapper<CrossCompartmentSecurityWrapper, GentlyOpaque>::singleton) {
|
|
|
|
// We explicitly use a SecurityWrapper to protect privileged callers from
|
|
|
|
// less-privileged objects that they should never see. Skip the check in
|
|
|
|
// this case.
|
2012-11-14 09:56:25 -08:00
|
|
|
} else {
|
|
|
|
// Otherwise, it should depend on whether the target subsumes the origin.
|
|
|
|
MOZ_ASSERT(handler->isSafeToUnwrap() == AccessCheck::subsumes(target, origin));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define DEBUG_CheckUnwrapSafety(obj, handler, origin, target) {}
|
|
|
|
#endif
|
|
|
|
|
2013-01-22 21:04:39 -08:00
|
|
|
static Wrapper *
|
|
|
|
SelectWrapper(bool securityWrapper, bool wantXrays, XrayType xrayType,
|
|
|
|
bool waiveXrays)
|
|
|
|
{
|
|
|
|
// Waived Xray uses a modified CCW that has transparent behavior but
|
|
|
|
// transitively waives Xrays on arguments.
|
|
|
|
if (waiveXrays) {
|
|
|
|
MOZ_ASSERT(!securityWrapper);
|
|
|
|
return &WaiveXrayWrapper::singleton;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we don't want or can't use Xrays, select a wrapper that's either
|
|
|
|
// entirely transparent or entirely opaque.
|
|
|
|
if (!wantXrays || xrayType == NotXray) {
|
|
|
|
if (!securityWrapper)
|
|
|
|
return &CrossCompartmentWrapper::singleton;
|
|
|
|
return &FilteringWrapper<CrossCompartmentSecurityWrapper, Opaque>::singleton;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ok, we're using Xray. If this isn't a security wrapper, use the permissive
|
|
|
|
// version and skip the filter.
|
|
|
|
if (!securityWrapper) {
|
|
|
|
if (xrayType == XrayForWrappedNative)
|
|
|
|
return &PermissiveXrayXPCWN::singleton;
|
|
|
|
return &PermissiveXrayDOM::singleton;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is a security wrapper. Use the security versions and filter.
|
|
|
|
if (xrayType == XrayForWrappedNative)
|
|
|
|
return &FilteringWrapper<SecurityXrayXPCWN,
|
|
|
|
CrossOriginAccessiblePropertiesOnly>::singleton;
|
|
|
|
return &FilteringWrapper<SecurityXrayDOM,
|
|
|
|
CrossOriginAccessiblePropertiesOnly>::singleton;
|
|
|
|
}
|
|
|
|
|
2010-07-02 13:54:53 -07:00
|
|
|
JSObject *
|
2013-04-15 10:32:55 -07:00
|
|
|
WrapperFactory::Rewrap(JSContext *cx, HandleObject existing, HandleObject obj,
|
|
|
|
HandleObject wrappedProto, HandleObject parent,
|
2012-02-28 15:11:11 -08:00
|
|
|
unsigned flags)
|
2010-06-25 15:58:09 -07:00
|
|
|
{
|
2013-01-22 21:04:38 -08:00
|
|
|
MOZ_ASSERT(!IsWrapper(obj) ||
|
|
|
|
GetProxyHandler(obj) == &XrayWaiver ||
|
|
|
|
js::GetObjectClass(obj)->ext.innerObject,
|
|
|
|
"wrapped object passed to rewrap");
|
|
|
|
MOZ_ASSERT(JS_GetClass(obj) != &XrayUtils::HolderClass, "trying to wrap a holder");
|
2013-02-04 07:13:13 -08:00
|
|
|
MOZ_ASSERT(!js::IsInnerObject(obj));
|
2013-02-26 11:04:13 -08:00
|
|
|
// We sometimes end up here after nsContentUtils has been shut down but before
|
|
|
|
// XPConnect has been shut down, so check the context stack the roundabout way.
|
|
|
|
MOZ_ASSERT(XPCJSRuntime::Get()->GetJSContextStack()->Peek() == cx);
|
2010-07-02 13:54:53 -07:00
|
|
|
|
2013-01-22 21:04:39 -08:00
|
|
|
// Compute the information we need to select the right wrapper.
|
2011-10-04 07:06:54 -07:00
|
|
|
JSCompartment *origin = js::GetObjectCompartment(obj);
|
2012-01-15 00:13:09 -08:00
|
|
|
JSCompartment *target = js::GetContextCompartment(cx);
|
2013-01-22 21:04:39 -08:00
|
|
|
bool originIsChrome = AccessCheck::isChrome(origin);
|
|
|
|
bool targetIsChrome = AccessCheck::isChrome(target);
|
|
|
|
bool originSubsumesTarget = AccessCheck::subsumes(origin, target);
|
|
|
|
bool targetSubsumesOrigin = AccessCheck::subsumes(target, origin);
|
|
|
|
bool sameOrigin = targetSubsumesOrigin && originSubsumesTarget;
|
|
|
|
XrayType xrayType = GetXrayType(obj);
|
2013-04-03 11:41:23 -07:00
|
|
|
bool waiveXrayFlag = flags & WAIVE_XRAY_WRAPPER_FLAG;
|
2010-07-02 13:54:53 -07:00
|
|
|
|
2011-09-08 20:29:15 -07:00
|
|
|
Wrapper *wrapper;
|
2013-01-22 21:04:39 -08:00
|
|
|
CompartmentPrivate *targetdata = EnsureCompartmentPrivate(target);
|
2013-01-22 21:04:39 -08:00
|
|
|
|
|
|
|
//
|
|
|
|
// First, handle the special cases.
|
|
|
|
//
|
|
|
|
|
|
|
|
// If UniversalXPConnect is enabled, this is just some dumb mochitest. Use
|
|
|
|
// a vanilla CCW.
|
|
|
|
if (xpc::IsUniversalXPConnectEnabled(target)) {
|
|
|
|
wrapper = &CrossCompartmentWrapper::singleton;
|
|
|
|
|
|
|
|
// If this is a chrome object being exposed to content without Xrays, use
|
|
|
|
// a COW.
|
|
|
|
} else if (originIsChrome && !targetIsChrome && xrayType == NotXray) {
|
|
|
|
wrapper = &ChromeObjectWrapper::singleton;
|
|
|
|
|
2014-01-14 18:49:30 -08:00
|
|
|
// If content is accessing NAC, we need a special filter, even if the
|
|
|
|
// object is same origin. Note that we allow access to NAC for remote-XUL
|
|
|
|
// whitelisted domains, since they don't have XBL scopes.
|
2013-03-08 18:49:36 -08:00
|
|
|
} else if (AccessCheck::needsSystemOnlyWrapper(obj) &&
|
2013-05-06 19:38:22 -07:00
|
|
|
xpc::AllowXBLScope(target) &&
|
2013-03-08 18:49:36 -08:00
|
|
|
!(targetIsChrome || (targetSubsumesOrigin && nsContentUtils::IsCallerXBL())))
|
|
|
|
{
|
2013-05-06 19:38:23 -07:00
|
|
|
wrapper = &FilteringWrapper<CrossCompartmentSecurityWrapper, Opaque>::singleton;
|
2013-01-22 21:04:39 -08:00
|
|
|
}
|
|
|
|
|
2013-04-03 11:41:23 -07:00
|
|
|
// Normally, a non-xrayable non-waived content object that finds itself in
|
|
|
|
// a privileged scope is wrapped with a CrossCompartmentWrapper, even though
|
|
|
|
// the lack of a waiver _really_ should give it an opaque wrapper. This is
|
|
|
|
// a bit too entrenched to change for content-chrome, but we can at least fix
|
|
|
|
// it for XBL scopes.
|
|
|
|
//
|
|
|
|
// See bug 843829.
|
|
|
|
else if (targetSubsumesOrigin && !originSubsumesTarget &&
|
|
|
|
!waiveXrayFlag && xrayType == NotXray &&
|
|
|
|
IsXBLScope(target))
|
|
|
|
{
|
|
|
|
wrapper = &FilteringWrapper<CrossCompartmentSecurityWrapper, GentlyOpaque>::singleton;
|
|
|
|
}
|
|
|
|
|
2013-01-22 21:04:39 -08:00
|
|
|
//
|
|
|
|
// Now, handle the regular cases.
|
|
|
|
//
|
2013-01-22 21:04:39 -08:00
|
|
|
// These are wrappers we can compute using a rule-based approach. In order
|
|
|
|
// to do so, we need to compute some parameters.
|
|
|
|
//
|
|
|
|
else {
|
2013-01-22 21:04:39 -08:00
|
|
|
|
2013-01-22 21:04:39 -08:00
|
|
|
// The wrapper is a security wrapper (protecting the wrappee) if and
|
|
|
|
// only if the target does not subsume the origin.
|
|
|
|
bool securityWrapper = !targetSubsumesOrigin;
|
|
|
|
|
|
|
|
// Xrays are warranted if either the target or the origin don't trust
|
|
|
|
// each other. This is generally the case, unless the two are same-origin
|
|
|
|
// and the caller has not requested same-origin Xrays.
|
2012-03-23 14:59:23 -07:00
|
|
|
//
|
2013-01-22 21:04:39 -08:00
|
|
|
// Xrays are a bidirectional protection, since it affords clarity to the
|
|
|
|
// caller and privacy to the callee.
|
|
|
|
bool wantXrays = !(sameOrigin && !targetdata->wantXrays);
|
|
|
|
|
|
|
|
// If Xrays are warranted, the caller may waive them for non-security
|
|
|
|
// wrappers.
|
2013-04-03 11:41:23 -07:00
|
|
|
bool waiveXrays = wantXrays && !securityWrapper && waiveXrayFlag;
|
2013-01-22 21:04:39 -08:00
|
|
|
|
|
|
|
wrapper = SelectWrapper(securityWrapper, wantXrays, xrayType, waiveXrays);
|
2010-06-25 15:58:09 -07:00
|
|
|
}
|
2010-09-20 14:48:01 -07:00
|
|
|
|
2013-01-22 21:04:39 -08:00
|
|
|
if (wrapper == &ChromeObjectWrapper::singleton) {
|
|
|
|
// This shouldn't happen, but do a quick check to make some dumb addon
|
|
|
|
// doesn't expose chrome eval or Function().
|
|
|
|
JSFunction *fun = JS_GetObjectFunction(obj);
|
|
|
|
if (fun) {
|
|
|
|
if (JS_IsBuiltinEvalFunction(fun) || JS_IsBuiltinFunctionConstructor(fun)) {
|
|
|
|
JS_ReportError(cx, "Not allowed to access chrome eval or Function from content");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-14 09:56:25 -08:00
|
|
|
DEBUG_CheckUnwrapSafety(obj, wrapper, origin, target);
|
|
|
|
|
2013-12-13 12:01:30 -08:00
|
|
|
if (existing)
|
2012-09-11 17:14:24 -07:00
|
|
|
return Wrapper::Renew(cx, existing, obj, wrapper);
|
|
|
|
|
2013-12-13 12:01:30 -08:00
|
|
|
return Wrapper::New(cx, obj, parent, wrapper);
|
2010-06-25 15:58:09 -07:00
|
|
|
}
|
|
|
|
|
2012-05-14 14:30:07 -07:00
|
|
|
JSObject *
|
2013-04-15 10:32:55 -07:00
|
|
|
WrapperFactory::WrapForSameCompartment(JSContext *cx, HandleObject objArg)
|
2012-05-14 14:30:07 -07:00
|
|
|
{
|
2013-04-15 10:32:55 -07:00
|
|
|
RootedObject obj(cx, objArg);
|
2013-02-04 07:13:14 -08:00
|
|
|
MOZ_ASSERT(js::IsObjectInContextCompartment(obj, cx));
|
|
|
|
|
2012-05-14 14:30:07 -07:00
|
|
|
// NB: The contract of WrapForSameCompartment says that |obj| may or may not
|
2012-09-12 13:29:30 -07:00
|
|
|
// be a security wrapper. These checks implicitly handle the security
|
|
|
|
// wrapper case.
|
|
|
|
|
2013-02-04 07:13:14 -08:00
|
|
|
// Outerize if necessary. This, in combination with the check in
|
|
|
|
// PrepareForUnwrapping, means that calling JS_Wrap* always outerizes.
|
|
|
|
obj = JS_ObjectToOuterObject(cx, obj);
|
|
|
|
NS_ENSURE_TRUE(obj, nullptr);
|
|
|
|
|
2013-04-15 10:32:55 -07:00
|
|
|
if (dom::GetSameCompartmentWrapperForDOMBinding(*obj.address())) {
|
2012-09-12 13:29:30 -07:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(!dom::IsDOMObject(obj));
|
|
|
|
|
2013-04-19 12:58:30 -07:00
|
|
|
if (!IS_WN_REFLECTOR(obj))
|
2012-05-14 14:30:07 -07:00
|
|
|
return obj;
|
|
|
|
|
|
|
|
// Extract the WN. It should exist.
|
2013-06-23 00:15:55 -07:00
|
|
|
XPCWrappedNative *wn = XPCWrappedNative::Get(obj);
|
2012-05-14 14:30:07 -07:00
|
|
|
MOZ_ASSERT(wn, "Trying to wrap a dead WN!");
|
|
|
|
|
|
|
|
// The WN knows what to do.
|
2013-04-12 02:50:43 -07:00
|
|
|
RootedObject wrapper(cx, wn->GetSameCompartmentSecurityWrapper(cx));
|
2012-11-14 09:56:25 -08:00
|
|
|
return wrapper;
|
2012-05-14 14:30:07 -07:00
|
|
|
}
|
|
|
|
|
2011-05-23 08:20:28 -07:00
|
|
|
// Call WaiveXrayAndWrap when you have a JS object that you don't want to be
|
|
|
|
// wrapped in an Xray wrapper. cx->compartment is the compartment that will be
|
|
|
|
// using the returned object. If the object to be wrapped is already in the
|
|
|
|
// correct compartment, then this returns the unwrapped object.
|
2010-10-10 15:48:29 -07:00
|
|
|
bool
|
2013-10-26 09:19:05 -07:00
|
|
|
WrapperFactory::WaiveXrayAndWrap(JSContext *cx, MutableHandleValue vp)
|
2010-10-10 15:48:29 -07:00
|
|
|
{
|
2013-10-26 09:19:05 -07:00
|
|
|
if (vp.isPrimitive())
|
2010-10-14 13:41:52 -07:00
|
|
|
return JS_WrapValue(cx, vp);
|
2010-10-10 15:48:29 -07:00
|
|
|
|
2013-12-13 12:01:30 -08:00
|
|
|
RootedObject obj(cx, &vp.toObject());
|
|
|
|
if (!WaiveXrayAndWrap(cx, &obj))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
vp.setObject(*obj);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WrapperFactory::WaiveXrayAndWrap(JSContext *cx, MutableHandleObject argObj)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(argObj);
|
|
|
|
RootedObject obj(cx, js::UncheckedUnwrap(argObj));
|
2013-02-04 07:13:13 -08:00
|
|
|
MOZ_ASSERT(!js::IsInnerObject(obj));
|
2011-12-24 00:28:55 -08:00
|
|
|
if (js::IsObjectInContextCompartment(obj, cx)) {
|
2013-12-13 12:01:30 -08:00
|
|
|
argObj.set(obj);
|
2011-05-23 08:20:28 -07:00
|
|
|
return true;
|
|
|
|
}
|
2010-10-10 15:48:29 -07:00
|
|
|
|
2013-05-15 21:13:12 -07:00
|
|
|
// Even though waivers have no effect on access by scopes that don't subsume
|
|
|
|
// the underlying object, good defense-in-depth dictates that we should avoid
|
|
|
|
// handing out waivers to callers that can't use them. The transitive waiving
|
|
|
|
// machinery unconditionally calls WaiveXrayAndWrap on return values from
|
|
|
|
// waived functions, even though the return value might be not be same-origin
|
|
|
|
// with the function. So if we find ourselves trying to create a waiver for
|
|
|
|
// |cx|, we should check whether the caller has any business with waivers
|
|
|
|
// to things in |obj|'s compartment.
|
|
|
|
JSCompartment *target = js::GetContextCompartment(cx);
|
|
|
|
JSCompartment *origin = js::GetObjectCompartment(obj);
|
|
|
|
obj = AccessCheck::subsumes(target, origin) ? WaiveXray(cx, obj) : obj;
|
2011-01-13 13:03:44 -08:00
|
|
|
if (!obj)
|
|
|
|
return false;
|
2010-10-10 15:48:29 -07:00
|
|
|
|
2013-12-13 12:01:30 -08:00
|
|
|
if (!JS_WrapObject(cx, &obj))
|
|
|
|
return false;
|
|
|
|
argObj.set(obj);
|
|
|
|
return true;
|
2010-10-10 15:48:29 -07:00
|
|
|
}
|
|
|
|
|
2010-10-10 15:48:55 -07:00
|
|
|
JSObject *
|
2013-04-12 02:50:43 -07:00
|
|
|
WrapperFactory::WrapSOWObject(JSContext *cx, JSObject *objArg)
|
2010-10-10 15:48:55 -07:00
|
|
|
{
|
2013-04-12 02:50:43 -07:00
|
|
|
RootedObject obj(cx, objArg);
|
2013-05-06 19:38:22 -07:00
|
|
|
|
|
|
|
// If we're not allowing XBL scopes, that means we're running as a remote
|
|
|
|
// XUL domain, in which we can't have SOWs. We should never be called in
|
|
|
|
// that case.
|
|
|
|
MOZ_ASSERT(xpc::AllowXBLScope(js::GetContextCompartment(cx)));
|
2010-10-10 15:48:55 -07:00
|
|
|
JSObject *wrapperObj =
|
2013-12-13 12:01:30 -08:00
|
|
|
Wrapper::New(cx, obj, JS_GetGlobalForObject(cx, obj),
|
2011-10-04 10:50:25 -07:00
|
|
|
&FilteringWrapper<SameCompartmentSecurityWrapper,
|
2013-05-06 19:38:23 -07:00
|
|
|
Opaque>::singleton);
|
2010-10-10 15:48:55 -07:00
|
|
|
return wrapperObj;
|
|
|
|
}
|
|
|
|
|
2012-08-27 06:06:34 -07:00
|
|
|
bool
|
|
|
|
WrapperFactory::XrayWrapperNotShadowing(JSObject *wrapper, jsid id)
|
|
|
|
{
|
|
|
|
ResolvingId *rid = ResolvingId::getResolvingIdFromWrapper(wrapper);
|
|
|
|
return rid->isXrayShadowing(id);
|
|
|
|
}
|
|
|
|
|
2012-07-23 06:51:18 -07:00
|
|
|
/*
|
|
|
|
* Calls to JS_TransplantObject* should go through these helpers here so that
|
|
|
|
* waivers get fixed up properly.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static bool
|
2013-04-12 02:50:43 -07:00
|
|
|
FixWaiverAfterTransplant(JSContext *cx, HandleObject oldWaiver, HandleObject newobj)
|
2012-07-23 06:51:18 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(Wrapper::wrapperHandler(oldWaiver) == &XrayWaiver);
|
|
|
|
MOZ_ASSERT(!js::IsCrossCompartmentWrapper(newobj));
|
|
|
|
|
|
|
|
// Create a waiver in the new compartment. We know there's not one already
|
|
|
|
// because we _just_ transplanted, which means that |newobj| was either
|
|
|
|
// created from scratch, or was previously cross-compartment wrapper (which
|
|
|
|
// should have no waiver). CreateXrayWaiver asserts this.
|
|
|
|
JSObject *newWaiver = WrapperFactory::CreateXrayWaiver(cx, newobj);
|
|
|
|
if (!newWaiver)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Update all the cross-compartment references to oldWaiver to point to
|
|
|
|
// newWaiver.
|
|
|
|
if (!js::RemapAllWrappersForObject(cx, oldWaiver, newWaiver))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// There should be no same-compartment references to oldWaiver, and we
|
|
|
|
// just remapped all cross-compartment references. It's dead, so we can
|
|
|
|
// remove it from the map.
|
2012-10-25 08:01:09 -07:00
|
|
|
XPCWrappedNativeScope *scope = GetObjectScope(oldWaiver);
|
2012-07-23 06:51:18 -07:00
|
|
|
JSObject *key = Wrapper::wrappedObject(oldWaiver);
|
2012-10-25 08:01:09 -07:00
|
|
|
MOZ_ASSERT(scope->mWaiverWrapperMap->Find(key));
|
|
|
|
scope->mWaiverWrapperMap->Remove(key);
|
2012-07-23 06:51:18 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject *
|
2013-06-13 17:09:55 -07:00
|
|
|
TransplantObject(JSContext *cx, JS::HandleObject origobj, JS::HandleObject target)
|
2012-07-23 06:51:18 -07:00
|
|
|
{
|
2013-04-12 02:50:43 -07:00
|
|
|
RootedObject oldWaiver(cx, WrapperFactory::GetXrayWaiver(origobj));
|
|
|
|
RootedObject newIdentity(cx, JS_TransplantObject(cx, origobj, target));
|
2012-07-23 06:51:18 -07:00
|
|
|
if (!newIdentity || !oldWaiver)
|
|
|
|
return newIdentity;
|
|
|
|
|
|
|
|
if (!FixWaiverAfterTransplant(cx, oldWaiver, newIdentity))
|
2013-10-10 13:35:53 -07:00
|
|
|
return nullptr;
|
2012-07-23 06:51:18 -07:00
|
|
|
return newIdentity;
|
|
|
|
}
|
|
|
|
|
2013-04-04 02:27:42 -07:00
|
|
|
nsIGlobalObject *
|
|
|
|
GetNativeForGlobal(JSObject *obj)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(JS_IsGlobalObject(obj));
|
2013-11-22 06:58:32 -08:00
|
|
|
if (!MaybeGetObjectScope(obj))
|
2013-04-04 02:27:42 -07:00
|
|
|
return nullptr;
|
|
|
|
|
2013-12-09 07:34:03 -08:00
|
|
|
// Every global needs to hold a native as its private or be a
|
|
|
|
// WebIDL object with an nsISupports DOM object.
|
|
|
|
MOZ_ASSERT((GetObjectClass(obj)->flags & (JSCLASS_PRIVATE_IS_NSISUPPORTS |
|
|
|
|
JSCLASS_HAS_PRIVATE)) ||
|
|
|
|
dom::UnwrapDOMObjectToISupports(obj));
|
|
|
|
|
|
|
|
nsISupports *native = dom::UnwrapDOMObjectToISupports(obj);
|
|
|
|
if (!native) {
|
|
|
|
native = static_cast<nsISupports *>(js::GetObjectPrivate(obj));
|
|
|
|
MOZ_ASSERT(native);
|
|
|
|
|
|
|
|
// In some cases (like for windows) it is a wrapped native,
|
|
|
|
// in other cases (sandboxes, backstage passes) it's just
|
|
|
|
// a direct pointer to the native. If it's a wrapped native
|
|
|
|
// let's unwrap it first.
|
|
|
|
if (nsCOMPtr<nsIXPConnectWrappedNative> wn = do_QueryInterface(native)) {
|
|
|
|
native = wn->Native();
|
|
|
|
}
|
2013-04-04 02:27:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(native);
|
|
|
|
MOZ_ASSERT(global, "Native held by global needs to implement nsIGlobalObject!");
|
|
|
|
|
|
|
|
return global;
|
|
|
|
}
|
|
|
|
|
2010-06-25 15:58:09 -07:00
|
|
|
}
|