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"
|
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"
|
2012-01-11 00:23:08 -08:00
|
|
|
|
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
|
|
|
|
2010-10-18 15:21:50 -07:00
|
|
|
static JSObject *
|
|
|
|
GetCurrentOuter(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
2011-10-04 07:06:54 -07:00
|
|
|
obj = JS_ObjectToOuterObject(cx, obj);
|
2012-03-28 16:15:38 -07:00
|
|
|
if (!obj)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2012-03-28 16:15:38 -07:00
|
|
|
|
2011-10-04 07:06:54 -07:00
|
|
|
if (IsWrapper(obj) && !js::GetObjectClass(obj)->ext.innerObject) {
|
|
|
|
obj = UnwrapObject(obj);
|
|
|
|
NS_ASSERTION(js::GetObjectClass(obj)->ext.innerObject,
|
2010-10-18 15:21:50 -07:00
|
|
|
"weird object, expecting an outer window proxy");
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2012-07-23 06:51:18 -07:00
|
|
|
JSObject *
|
|
|
|
WrapperFactory::GetXrayWaiver(JSObject *obj)
|
|
|
|
{
|
|
|
|
// Object should come fully unwrapped but outerized.
|
|
|
|
MOZ_ASSERT(obj == UnwrapObject(obj));
|
|
|
|
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)
|
2012-07-23 06:51:18 -07:00
|
|
|
return NULL;
|
2012-10-25 08:01:09 -07:00
|
|
|
return xpc_UnmarkGrayObject(scope->mWaiverWrapperMap->Find(obj));
|
2012-07-23 06:51:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject *
|
|
|
|
WrapperFactory::CreateXrayWaiver(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
|
|
|
// 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
|
|
|
|
|
|
|
// Get a waiver for the proto.
|
2012-09-03 16:42:10 -07:00
|
|
|
JSObject *proto;
|
|
|
|
if (!js::GetObjectProto(cx, obj, &proto))
|
|
|
|
return nullptr;
|
2012-07-23 06:51:18 -07:00
|
|
|
if (proto && !(proto = WaiveXray(cx, proto)))
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2012-07-23 06:51:18 -07:00
|
|
|
|
|
|
|
// Create the waiver.
|
2012-08-21 18:42:53 -07:00
|
|
|
JSAutoCompartment ac(cx, obj);
|
|
|
|
if (!JS_WrapObject(cx, &proto))
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2012-07-23 06:51:18 -07:00
|
|
|
JSObject *waiver = Wrapper::New(cx, obj, proto,
|
|
|
|
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
|
|
|
}
|
2012-10-25 08:01:09 -07:00
|
|
|
if (!scope->mWaiverWrapperMap->Add(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 *
|
|
|
|
WrapperFactory::WaiveXray(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
2011-10-04 07:06:54 -07:00
|
|
|
obj = UnwrapObject(obj);
|
2011-01-20 14:41:30 -08:00
|
|
|
|
2011-01-13 13:03:44 -08:00
|
|
|
// We have to make sure that if we're wrapping an outer window, that
|
|
|
|
// the .wrappedJSObject also wraps the outer window.
|
|
|
|
obj = GetCurrentOuter(cx, obj);
|
|
|
|
|
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 *
|
2012-02-28 15:11:11 -08:00
|
|
|
WrapperFactory::DoubleWrap(JSContext *cx, JSObject *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 *
|
2012-02-28 15:11:11 -08:00
|
|
|
WrapperFactory::PrepareForWrapping(JSContext *cx, JSObject *scope, JSObject *obj, unsigned flags)
|
2010-10-10 15:36:38 -07:00
|
|
|
{
|
2010-10-10 15:47:22 -07:00
|
|
|
// Don't unwrap an outer window, just double wrap it if needed.
|
2011-10-04 07:06:54 -07:00
|
|
|
if (js::GetObjectClass(obj)->ext.innerObject)
|
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
|
|
|
|
|
|
|
// As soon as an object is wrapped in a security wrapper, it morphs to be
|
|
|
|
// a fat wrapper. (see also: bug XXX).
|
|
|
|
if (IS_SLIM_WRAPPER(obj) && !MorphSlimWrapper(cx, obj))
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-10-10 15:36:38 -07:00
|
|
|
|
|
|
|
// We only hand out outer objects to script.
|
2010-10-18 16:45:39 -07:00
|
|
|
obj = GetCurrentOuter(cx, obj);
|
2012-03-28 16:15:38 -07:00
|
|
|
if (!obj)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2012-03-28 16:15:38 -07:00
|
|
|
|
2011-10-04 07:06:54 -07:00
|
|
|
if (js::GetObjectClass(obj)->ext.innerObject)
|
2010-10-18 15:21:50 -07:00
|
|
|
return DoubleWrap(cx, obj, flags);
|
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...
|
2011-10-04 07:06:54 -07:00
|
|
|
if (!IS_WN_WRAPPER(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
|
|
|
|
|
|
|
XPCWrappedNative *wn = static_cast<XPCWrappedNative *>(xpc_GetJSPrivate(obj));
|
|
|
|
|
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);
|
|
|
|
|
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
|
|
|
JSObject *originalScope = scope;
|
|
|
|
nsresult rv = wn->GetScriptableInfo()->GetCallback()->
|
|
|
|
PreCreate(wn->Native(), cx, scope, &scope);
|
|
|
|
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.
|
2011-10-04 07:06:54 -07:00
|
|
|
if (js::GetObjectCompartment(originalScope) != js::GetObjectCompartment(scope))
|
2011-02-05 04:56:05 -08:00
|
|
|
return DoubleWrap(cx, obj, flags);
|
|
|
|
|
2012-05-29 14:24:03 -07:00
|
|
|
JSObject *currentScope = JS_GetGlobalForObject(cx, obj);
|
|
|
|
if (MOZ_UNLIKELY(scope != currentScope)) {
|
|
|
|
// 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).
|
|
|
|
JSObject *probe;
|
|
|
|
rv = wn->GetScriptableInfo()->GetCallback()->
|
|
|
|
PreCreate(wn->Native(), cx, currentScope, &probe);
|
|
|
|
|
|
|
|
// Check for case (2).
|
|
|
|
if (probe != currentScope) {
|
|
|
|
MOZ_ASSERT(probe == scope);
|
|
|
|
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.
|
2012-08-16 12:25:39 -07:00
|
|
|
if (!AccessCheck::isChrome(js::GetObjectCompartment(scope)) &&
|
2012-09-11 10:23:20 -07:00
|
|
|
AccessCheck::subsumesIgnoringDomain(js::GetObjectCompartment(scope),
|
|
|
|
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
|
|
|
|
2010-10-29 12:49:32 -07:00
|
|
|
// NB: Passing a holder here inhibits slim wrappers under
|
|
|
|
// WrapNativeToJSVal.
|
|
|
|
nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
|
2011-02-11 19:32:45 -08:00
|
|
|
|
|
|
|
// This public WrapNativeToJSVal API enters the compartment of 'scope'
|
|
|
|
// so we don't have to.
|
2010-10-10 15:36:38 -07:00
|
|
|
jsval v;
|
|
|
|
nsresult rv =
|
2012-07-30 07:20:58 -07:00
|
|
|
nsXPConnect::FastGetXPConnect()->WrapNativeToJSVal(cx, scope, wn->Native(), nullptr,
|
2011-10-17 07:59:28 -07:00
|
|
|
&NS_GET_IID(nsISupports), false,
|
2010-10-29 12:49:32 -07:00
|
|
|
&v, getter_AddRefs(holder));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2010-10-10 15:47:22 -07:00
|
|
|
obj = JSVAL_TO_OBJECT(v);
|
2010-10-29 12:49:32 -07:00
|
|
|
NS_ASSERTION(IS_WN_WRAPPER(obj), "bad object");
|
|
|
|
|
2012-03-16 12:47:20 -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.
|
2010-10-29 12:49:32 -07:00
|
|
|
XPCWrappedNative *newwn = static_cast<XPCWrappedNative *>(xpc_GetJSPrivate(obj));
|
2012-03-16 12:47:20 -07:00
|
|
|
XPCNativeSet *unionSet = XPCNativeSet::GetNewOrUsed(ccx, newwn->GetSet(),
|
|
|
|
wn->GetSet(), false);
|
|
|
|
if (!unionSet)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2012-03-16 12:47:20 -07:00
|
|
|
newwn->SetSet(unionSet);
|
2010-10-29 12:49:32 -07:00
|
|
|
}
|
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
|
|
|
|
DEBUG_CheckUnwrapSafety(JSObject *obj, js::Wrapper *handler,
|
|
|
|
JSCompartment *origin, JSCompartment *target)
|
|
|
|
{
|
|
|
|
typedef FilteringWrapper<CrossCompartmentSecurityWrapper, OnlyIfSubjectIsSystem> XSOW;
|
|
|
|
|
|
|
|
if (AccessCheck::isChrome(target) || xpc::IsUniversalXPConnectEnabled(target)) {
|
|
|
|
// If the caller is chrome (or effectively so), unwrap should always be allowed.
|
|
|
|
MOZ_ASSERT(handler->isSafeToUnwrap());
|
2012-11-21 13:20:05 -08:00
|
|
|
} else if (WrapperFactory::IsComponentsObject(obj) ||
|
2012-11-14 09:56:25 -08:00
|
|
|
handler == &XSOW::singleton)
|
|
|
|
{
|
|
|
|
// This is an object that is restricted regardless of origin.
|
|
|
|
MOZ_ASSERT(!handler->isSafeToUnwrap());
|
|
|
|
} 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
|
|
|
|
|
2010-07-02 13:54:53 -07:00
|
|
|
JSObject *
|
2012-09-11 17:14:24 -07:00
|
|
|
WrapperFactory::Rewrap(JSContext *cx, JSObject *existing, JSObject *obj,
|
|
|
|
JSObject *wrappedProto, JSObject *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");
|
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);
|
2010-07-02 13:54:53 -07:00
|
|
|
|
2012-07-27 03:15:46 -07:00
|
|
|
// By default we use the wrapped proto of the underlying object as the
|
|
|
|
// prototype for our wrapper, but we may select something different below.
|
|
|
|
JSObject *proxyProto = wrappedProto;
|
|
|
|
|
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
|
|
|
if (targetIsChrome) {
|
|
|
|
if (originIsChrome) {
|
2011-09-08 20:29:15 -07:00
|
|
|
wrapper = &CrossCompartmentWrapper::singleton;
|
2010-07-02 13:54:53 -07:00
|
|
|
} else {
|
2012-09-05 11:32:07 -07:00
|
|
|
if (flags & WAIVE_XRAY_WRAPPER_FLAG) {
|
2011-04-18 13:50:47 -07:00
|
|
|
// If we waived the X-ray wrapper for this object, wrap it into a
|
|
|
|
// special wrapper to transitively maintain the X-ray waiver.
|
2012-07-16 08:53:16 -07:00
|
|
|
wrapper = &WaiveXrayWrapper::singleton;
|
2010-09-02 16:02:51 -07:00
|
|
|
} else {
|
2011-04-18 13:50:47 -07:00
|
|
|
// Native objects must be wrapped into an X-ray wrapper.
|
2013-01-22 21:04:39 -08:00
|
|
|
if (xrayType == XrayForDOMObject) {
|
2012-11-14 09:56:25 -08:00
|
|
|
wrapper = &PermissiveXrayDOM::singleton;
|
2013-01-22 21:04:39 -08:00
|
|
|
} else if (xrayType == XrayForWrappedNative) {
|
2012-11-14 09:56:25 -08:00
|
|
|
wrapper = &PermissiveXrayXPCWN::singleton;
|
2011-04-18 13:50:47 -07:00
|
|
|
} else {
|
2012-06-28 14:47:55 -07:00
|
|
|
wrapper = &CrossCompartmentWrapper::singleton;
|
2011-04-18 13:50:47 -07:00
|
|
|
}
|
2010-09-02 16:02:51 -07:00
|
|
|
}
|
2010-07-02 13:54:53 -07:00
|
|
|
}
|
2012-09-11 01:05:10 -07:00
|
|
|
} else if (xpc::IsUniversalXPConnectEnabled(target)) {
|
|
|
|
wrapper = &CrossCompartmentWrapper::singleton;
|
2013-01-22 21:04:39 -08:00
|
|
|
} else if (originIsChrome) {
|
2011-10-04 07:06:54 -07:00
|
|
|
JSFunction *fun = JS_GetObjectFunction(obj);
|
|
|
|
if (fun) {
|
2011-03-02 19:57:44 -08:00
|
|
|
if (JS_IsBuiltinEvalFunction(fun) || JS_IsBuiltinFunctionConstructor(fun)) {
|
2011-01-26 18:28:49 -08:00
|
|
|
JS_ReportError(cx, "Not allowed to access chrome eval or Function from content");
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2011-01-26 18:28:49 -08:00
|
|
|
}
|
|
|
|
}
|
2011-05-10 14:41:25 -07:00
|
|
|
|
2013-01-22 21:04:39 -08:00
|
|
|
if (xrayType == XrayForWrappedNative) {
|
2012-11-21 13:20:05 -08:00
|
|
|
wrapper = &FilteringWrapper<SecurityXrayXPCWN, CrossOriginAccessiblePropertiesOnly>::singleton;
|
2013-01-22 21:04:39 -08:00
|
|
|
} else if (xrayType == XrayForDOMObject) {
|
2012-11-14 09:56:25 -08:00
|
|
|
wrapper = &FilteringWrapper<SecurityXrayDOM, CrossOriginAccessiblePropertiesOnly>::singleton;
|
2012-04-28 06:12:28 -07:00
|
|
|
} else if (IsComponentsObject(obj)) {
|
|
|
|
wrapper = &FilteringWrapper<CrossCompartmentSecurityWrapper,
|
|
|
|
ComponentsObjectPolicy>::singleton;
|
2011-05-10 14:41:25 -07:00
|
|
|
} else {
|
2012-07-27 03:15:46 -07:00
|
|
|
wrapper = &ChromeObjectWrapper::singleton;
|
2012-07-27 03:15:46 -07:00
|
|
|
|
|
|
|
// If the prototype of the chrome object being wrapped is a prototype
|
|
|
|
// for a standard class, use the one from the content compartment so
|
|
|
|
// that we can safely take advantage of things like .forEach().
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
JSProtoKey key = JSProto_Null;
|
2012-09-03 16:42:22 -07:00
|
|
|
{
|
|
|
|
JSAutoCompartment ac(cx, obj);
|
|
|
|
JSObject *unwrappedProto;
|
|
|
|
if (!js::GetObjectProto(cx, obj, &unwrappedProto))
|
|
|
|
return NULL;
|
|
|
|
if (unwrappedProto && IsCrossCompartmentWrapper(unwrappedProto))
|
|
|
|
unwrappedProto = Wrapper::wrappedObject(unwrappedProto);
|
|
|
|
if (unwrappedProto) {
|
|
|
|
JSAutoCompartment ac2(cx, unwrappedProto);
|
|
|
|
key = JS_IdentifyClassPrototype(cx, unwrappedProto);
|
|
|
|
}
|
2012-07-27 03:15:46 -07:00
|
|
|
}
|
|
|
|
if (key != JSProto_Null) {
|
|
|
|
JSObject *homeProto;
|
|
|
|
if (!JS_GetClassPrototype(cx, key, &homeProto))
|
|
|
|
return NULL;
|
|
|
|
MOZ_ASSERT(homeProto);
|
|
|
|
proxyProto = homeProto;
|
|
|
|
}
|
2011-05-10 14:41:25 -07:00
|
|
|
}
|
2013-01-22 21:04:39 -08:00
|
|
|
} else if (targetSubsumesOrigin) {
|
2012-03-23 14:59:23 -07:00
|
|
|
// For the same-origin case we use a transparent wrapper, unless one
|
|
|
|
// of the following is true:
|
2012-04-05 12:21:12 -07:00
|
|
|
// * The object is flagged as needing a SOW.
|
2012-06-05 10:07:37 -07:00
|
|
|
// * The object is a Components object.
|
2012-03-23 14:59:23 -07:00
|
|
|
// * The context compartment specifically requested Xray vision into
|
|
|
|
// same-origin compartments.
|
|
|
|
//
|
|
|
|
// The first two cases always require a security wrapper for non-chrome
|
|
|
|
// access, regardless of the origin of the object.
|
2010-07-02 13:54:53 -07:00
|
|
|
if (AccessCheck::needsSystemOnlyWrapper(obj)) {
|
2011-10-04 10:50:25 -07:00
|
|
|
wrapper = &FilteringWrapper<CrossCompartmentSecurityWrapper,
|
2010-07-02 13:54:53 -07:00
|
|
|
OnlyIfSubjectIsSystem>::singleton;
|
2012-04-28 06:12:28 -07:00
|
|
|
} else if (IsComponentsObject(obj)) {
|
|
|
|
wrapper = &FilteringWrapper<CrossCompartmentSecurityWrapper,
|
|
|
|
ComponentsObjectPolicy>::singleton;
|
2013-01-22 21:04:39 -08:00
|
|
|
} else if (!targetdata->wantXrays || xrayType == NotXray) {
|
2011-09-08 20:29:15 -07:00
|
|
|
wrapper = &CrossCompartmentWrapper::singleton;
|
2013-01-22 21:04:39 -08:00
|
|
|
} else if (xrayType == XrayForDOMObject) {
|
2012-11-14 09:56:25 -08:00
|
|
|
wrapper = &PermissiveXrayDOM::singleton;
|
2012-03-30 21:42:20 -07:00
|
|
|
} else {
|
2012-11-14 09:56:25 -08:00
|
|
|
wrapper = &PermissiveXrayXPCWN::singleton;
|
2010-10-10 15:46:07 -07:00
|
|
|
}
|
2010-07-02 13:54:53 -07:00
|
|
|
} else {
|
2010-10-10 15:48:55 -07:00
|
|
|
NS_ASSERTION(!AccessCheck::needsSystemOnlyWrapper(obj),
|
|
|
|
"bad object exposed across origins");
|
|
|
|
|
2010-07-02 13:54:53 -07:00
|
|
|
// Cross origin we want to disallow scripting and limit access to
|
2012-10-05 09:59:22 -07:00
|
|
|
// a predefined set of properties.
|
2013-01-22 21:04:39 -08:00
|
|
|
if (xrayType == NotXray) {
|
2013-01-22 21:04:38 -08:00
|
|
|
wrapper = &FilteringWrapper<CrossCompartmentSecurityWrapper, Opaque>::singleton;
|
2013-01-22 21:04:39 -08:00
|
|
|
} else if (xrayType == XrayForDOMObject) {
|
2012-11-14 09:56:25 -08:00
|
|
|
wrapper = &FilteringWrapper<SecurityXrayDOM,
|
2012-03-30 21:42:20 -07:00
|
|
|
CrossOriginAccessiblePropertiesOnly>::singleton;
|
2010-09-02 16:02:51 -07:00
|
|
|
} else {
|
2012-11-21 13:20:05 -08:00
|
|
|
wrapper = &FilteringWrapper<SecurityXrayXPCWN,
|
|
|
|
CrossOriginAccessiblePropertiesOnly>::singleton;
|
2010-09-02 16:02:51 -07:00
|
|
|
}
|
2010-06-25 15:58:09 -07:00
|
|
|
}
|
2010-09-20 14:48:01 -07:00
|
|
|
|
2012-11-14 09:56:25 -08:00
|
|
|
DEBUG_CheckUnwrapSafety(obj, wrapper, origin, target);
|
|
|
|
|
2012-09-11 17:14:24 -07:00
|
|
|
if (existing && proxyProto == wrappedProto)
|
|
|
|
return Wrapper::Renew(cx, existing, obj, wrapper);
|
|
|
|
|
2012-10-05 09:59:23 -07:00
|
|
|
return Wrapper::New(cx, obj, proxyProto, parent, wrapper);
|
2010-06-25 15:58:09 -07:00
|
|
|
}
|
|
|
|
|
2012-05-14 14:30:07 -07:00
|
|
|
JSObject *
|
|
|
|
WrapperFactory::WrapForSameCompartment(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
if (dom::GetSameCompartmentWrapperForDOMBinding(obj)) {
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(!dom::IsDOMObject(obj));
|
|
|
|
|
2012-05-14 14:30:07 -07:00
|
|
|
if (!IS_WN_WRAPPER(obj))
|
|
|
|
return obj;
|
|
|
|
|
|
|
|
// Extract the WN. It should exist.
|
|
|
|
XPCWrappedNative *wn = static_cast<XPCWrappedNative *>(xpc_GetJSPrivate(obj));
|
|
|
|
MOZ_ASSERT(wn, "Trying to wrap a dead WN!");
|
|
|
|
|
|
|
|
// The WN knows what to do.
|
2012-11-14 09:56:25 -08:00
|
|
|
JSObject *wrapper = wn->GetSameCompartmentSecurityWrapper(cx);
|
|
|
|
MOZ_ASSERT_IF(wrapper != obj, !Wrapper::wrapperHandler(wrapper)->isSafeToUnwrap());
|
|
|
|
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
|
|
|
|
WrapperFactory::WaiveXrayAndWrap(JSContext *cx, jsval *vp)
|
|
|
|
{
|
2010-10-14 13:41:52 -07:00
|
|
|
if (JSVAL_IS_PRIMITIVE(*vp))
|
|
|
|
return JS_WrapValue(cx, vp);
|
2010-10-10 15:48:29 -07:00
|
|
|
|
2011-10-04 07:06:54 -07:00
|
|
|
JSObject *obj = js::UnwrapObject(JSVAL_TO_OBJECT(*vp));
|
2011-05-23 08:20:28 -07:00
|
|
|
obj = GetCurrentOuter(cx, obj);
|
2011-12-24 00:28:55 -08:00
|
|
|
if (js::IsObjectInContextCompartment(obj, cx)) {
|
2011-05-23 08:20:28 -07:00
|
|
|
*vp = OBJECT_TO_JSVAL(obj);
|
|
|
|
return true;
|
|
|
|
}
|
2010-10-10 15:48:29 -07:00
|
|
|
|
2011-01-13 13:03:44 -08:00
|
|
|
obj = WaiveXray(cx, obj);
|
|
|
|
if (!obj)
|
|
|
|
return false;
|
2010-10-10 15:48:29 -07:00
|
|
|
|
|
|
|
*vp = OBJECT_TO_JSVAL(obj);
|
|
|
|
return JS_WrapValue(cx, vp);
|
|
|
|
}
|
|
|
|
|
2010-10-10 15:48:55 -07:00
|
|
|
JSObject *
|
|
|
|
WrapperFactory::WrapSOWObject(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
2012-09-03 16:42:17 -07:00
|
|
|
JSObject *proto;
|
|
|
|
if (!JS_GetPrototype(cx, obj, &proto))
|
|
|
|
return NULL;
|
2010-10-10 15:48:55 -07:00
|
|
|
JSObject *wrapperObj =
|
2012-09-03 16:42:17 -07:00
|
|
|
Wrapper::New(cx, obj, proto, JS_GetGlobalForObject(cx, obj),
|
2011-10-04 10:50:25 -07:00
|
|
|
&FilteringWrapper<SameCompartmentSecurityWrapper,
|
2011-10-14 10:52:48 -07:00
|
|
|
OnlyIfSubjectIsSystem>::singleton);
|
2010-10-10 15:48:55 -07:00
|
|
|
return wrapperObj;
|
|
|
|
}
|
|
|
|
|
2012-04-28 06:12:28 -07:00
|
|
|
bool
|
|
|
|
WrapperFactory::IsComponentsObject(JSObject *obj)
|
|
|
|
{
|
|
|
|
const char *name = js::GetObjectClass(obj)->name;
|
|
|
|
return name[0] == 'n' && !strcmp(name, "nsXPCComponents");
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject *
|
|
|
|
WrapperFactory::WrapComponentsObject(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
2012-09-03 16:42:17 -07:00
|
|
|
JSObject *proto;
|
|
|
|
if (!JS_GetPrototype(cx, obj, &proto))
|
|
|
|
return NULL;
|
2012-04-28 06:12:28 -07:00
|
|
|
JSObject *wrapperObj =
|
2012-09-03 16:42:17 -07:00
|
|
|
Wrapper::New(cx, obj, proto, JS_GetGlobalForObject(cx, obj),
|
2012-04-28 06:12:28 -07:00
|
|
|
&FilteringWrapper<SameCompartmentSecurityWrapper, ComponentsObjectPolicy>::singleton);
|
|
|
|
|
|
|
|
return wrapperObj;
|
|
|
|
}
|
|
|
|
|
2012-07-18 04:51:28 -07:00
|
|
|
JSObject *
|
|
|
|
WrapperFactory::WrapForSameCompartmentXray(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
|
|
|
// We should be same-compartment here.
|
|
|
|
MOZ_ASSERT(js::IsObjectInContextCompartment(obj, cx));
|
|
|
|
|
|
|
|
// Sort out what kind of Xray we can do. If we can't Xray, bail.
|
|
|
|
XrayType type = GetXrayType(obj);
|
|
|
|
if (type == NotXray)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// Select the appropriate proxy handler.
|
|
|
|
Wrapper *wrapper = NULL;
|
|
|
|
if (type == XrayForWrappedNative)
|
2012-11-14 09:56:25 -08:00
|
|
|
wrapper = &SCPermissiveXrayXPCWN::singleton;
|
2012-07-18 04:51:28 -07:00
|
|
|
else if (type == XrayForDOMObject)
|
2012-11-14 09:56:25 -08:00
|
|
|
wrapper = &SCPermissiveXrayDOM::singleton;
|
2012-07-18 04:51:28 -07:00
|
|
|
else
|
|
|
|
MOZ_NOT_REACHED("Bad Xray type");
|
|
|
|
|
|
|
|
// Make the Xray.
|
|
|
|
JSObject *parent = JS_GetGlobalForObject(cx, obj);
|
2012-10-05 09:59:23 -07:00
|
|
|
return Wrapper::New(cx, obj, NULL, parent, wrapper);
|
2012-07-18 04:51:28 -07:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
FixWaiverAfterTransplant(JSContext *cx, JSObject *oldWaiver, JSObject *newobj)
|
|
|
|
{
|
|
|
|
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 *
|
|
|
|
TransplantObject(JSContext *cx, JSObject *origobj, JSObject *target)
|
|
|
|
{
|
|
|
|
JSObject *oldWaiver = WrapperFactory::GetXrayWaiver(origobj);
|
|
|
|
JSObject *newIdentity = JS_TransplantObject(cx, origobj, target);
|
|
|
|
if (!newIdentity || !oldWaiver)
|
|
|
|
return newIdentity;
|
|
|
|
|
|
|
|
if (!FixWaiverAfterTransplant(cx, oldWaiver, newIdentity))
|
|
|
|
return NULL;
|
|
|
|
return newIdentity;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject *
|
|
|
|
TransplantObjectWithWrapper(JSContext *cx,
|
|
|
|
JSObject *origobj, JSObject *origwrapper,
|
|
|
|
JSObject *targetobj, JSObject *targetwrapper)
|
|
|
|
{
|
|
|
|
JSObject *oldWaiver = WrapperFactory::GetXrayWaiver(origobj);
|
|
|
|
JSObject *newSameCompartmentWrapper =
|
|
|
|
js_TransplantObjectWithWrapper(cx, origobj, origwrapper, targetobj,
|
|
|
|
targetwrapper);
|
|
|
|
if (!newSameCompartmentWrapper || !oldWaiver)
|
|
|
|
return newSameCompartmentWrapper;
|
|
|
|
|
|
|
|
JSObject *newIdentity = Wrapper::wrappedObject(newSameCompartmentWrapper);
|
2012-09-20 00:55:37 -07:00
|
|
|
MOZ_ASSERT(js::IsWrapper(newIdentity));
|
2012-07-23 06:51:18 -07:00
|
|
|
if (!FixWaiverAfterTransplant(cx, oldWaiver, newIdentity))
|
|
|
|
return NULL;
|
|
|
|
return newSameCompartmentWrapper;
|
|
|
|
}
|
|
|
|
|
2010-06-25 15:58:09 -07:00
|
|
|
}
|