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:
|
|
|
|
*
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code, released
|
|
|
|
* June 24, 2010.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* The Mozilla Foundation
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Andreas Gal <gal@mozilla.com>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
2010-07-02 13:54:53 -07:00
|
|
|
#include "CrossOriginWrapper.h"
|
|
|
|
#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"
|
2010-06-25 15:58:09 -07:00
|
|
|
|
2010-07-02 13:54:53 -07:00
|
|
|
#include "xpcprivate.h"
|
2011-05-25 08:30:50 -07:00
|
|
|
#include "dombindings.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-01-11 00:23:08 -08:00
|
|
|
|
2011-09-08 20:29:15 -07:00
|
|
|
using namespace js;
|
|
|
|
|
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.
|
2011-09-08 20:29:15 -07:00
|
|
|
Wrapper WaiveXrayWrapperWrapper(WrapperFactory::WAIVE_XRAY_WRAPPER_FLAG);
|
2010-07-02 13:54:53 -07:00
|
|
|
|
2011-01-25 15:06:45 -08:00
|
|
|
// Objects that haven't been explicitly waived, but have been exposed
|
|
|
|
// to chrome don't want a CrossOriginWrapper, since that deeply-waives
|
|
|
|
// but need the transparent behavior of a CrossOriginWrapper. The
|
|
|
|
// NoWaiverWrapper is like a CrossOriginWrapper that can also hand out
|
|
|
|
// XrayWrappers as return values.
|
|
|
|
NoWaiverWrapper NoWaiverWrapper::singleton(0);
|
|
|
|
|
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.
|
2010-10-10 15:47:22 -07:00
|
|
|
CrossOriginWrapper CrossOriginWrapper::singleton(0);
|
|
|
|
|
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)
|
|
|
|
return nsnull;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
{
|
|
|
|
// See if we already have a waiver wrapper for this object.
|
|
|
|
CompartmentPrivate *priv =
|
2012-02-29 04:18:16 -08:00
|
|
|
(CompartmentPrivate *)JS_GetCompartmentPrivate(js::GetObjectCompartment(obj));
|
2011-01-13 13:03:44 -08:00
|
|
|
JSObject *wobj = nsnull;
|
2012-02-10 18:32:13 -08:00
|
|
|
if (priv && priv->waiverWrapperMap) {
|
2011-01-13 13:03:44 -08:00
|
|
|
wobj = priv->waiverWrapperMap->Find(obj);
|
2012-02-10 18:32:13 -08:00
|
|
|
xpc_UnmarkGrayObject(wobj);
|
|
|
|
}
|
2011-01-13 13:03:44 -08:00
|
|
|
|
|
|
|
// No wrapper yet, make one.
|
|
|
|
if (!wobj) {
|
2011-10-04 07:06:54 -07:00
|
|
|
JSObject *proto = js::GetObjectProto(obj);
|
2011-01-13 13:03:44 -08:00
|
|
|
if (proto && !(proto = WaiveXray(cx, proto)))
|
|
|
|
return nsnull;
|
|
|
|
|
2011-01-18 12:51:56 -08:00
|
|
|
JSAutoEnterCompartment ac;
|
2011-07-22 12:14:02 -07:00
|
|
|
if (!ac.enter(cx, obj) || !JS_WrapObject(cx, &proto))
|
2011-01-18 12:51:56 -08:00
|
|
|
return nsnull;
|
2011-09-08 20:29:15 -07:00
|
|
|
wobj = Wrapper::New(cx, obj, proto, JS_GetGlobalForObject(cx, obj),
|
|
|
|
&WaiveXrayWrapperWrapper);
|
2011-01-13 13:03:44 -08:00
|
|
|
if (!wobj)
|
|
|
|
return nsnull;
|
|
|
|
|
|
|
|
// Add the new wrapper so we find it next time.
|
|
|
|
if (priv) {
|
|
|
|
if (!priv->waiverWrapperMap) {
|
|
|
|
priv->waiverWrapperMap = JSObject2JSObjectMap::newMap(XPC_WRAPPER_MAP_SIZE);
|
|
|
|
if (!priv->waiverWrapperMap)
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
if (!priv->waiverWrapperMap->Add(obj, wobj))
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
obj = wobj;
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
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) {
|
2011-01-20 14:41:30 -08:00
|
|
|
JSAutoEnterCompartment ac;
|
|
|
|
if (!ac.enter(cx, obj))
|
|
|
|
return nsnull;
|
|
|
|
|
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).
|
2011-10-04 07:06:54 -07:00
|
|
|
JS_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))
|
|
|
|
return nsnull;
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
return nsnull;
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
JSAutoEnterCompartment ac;
|
2011-02-11 19:32:45 -08:00
|
|
|
if (!ac.enter(cx, obj))
|
2010-10-10 15:47:22 -07:00
|
|
|
return nsnull;
|
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.
|
|
|
|
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);
|
|
|
|
|
|
|
|
// 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?"
|
|
|
|
}
|
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 =
|
|
|
|
nsXPConnect::FastGetXPConnect()->WrapNativeToJSVal(cx, scope, wn->Native(), nsnull,
|
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)
|
|
|
|
return nsnull;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-05-24 02:33:44 -07:00
|
|
|
static XPCWrappedNative *
|
|
|
|
GetWrappedNative(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
2011-10-04 07:06:54 -07:00
|
|
|
obj = JS_ObjectToInnerObject(cx, obj);
|
2011-05-24 02:33:44 -07:00
|
|
|
return IS_WN_WRAPPER(obj)
|
2011-10-04 07:06:54 -07:00
|
|
|
? static_cast<XPCWrappedNative *>(js::GetObjectPrivate(obj))
|
2011-05-24 02:33:44 -07:00
|
|
|
: nsnull;
|
|
|
|
}
|
|
|
|
|
2012-03-30 21:42:20 -07:00
|
|
|
enum XrayType {
|
|
|
|
XrayForDOMObject,
|
|
|
|
XrayForDOMProxyObject,
|
|
|
|
XrayForWrappedNative,
|
|
|
|
NotXray
|
|
|
|
};
|
|
|
|
|
|
|
|
static XrayType
|
|
|
|
GetXrayType(JSObject *obj)
|
2011-05-25 08:30:50 -07:00
|
|
|
{
|
2012-03-30 21:42:20 -07:00
|
|
|
js::Class* clasp = js::GetObjectClass(obj);
|
2012-05-02 21:35:38 -07:00
|
|
|
if (mozilla::dom::IsDOMClass(Jsvalify(clasp))) {
|
2012-03-30 21:42:20 -07:00
|
|
|
return XrayForDOMObject;
|
|
|
|
}
|
|
|
|
if (mozilla::dom::binding::instanceIsProxy(obj)) {
|
|
|
|
return XrayForDOMProxyObject;
|
2011-05-25 08:30:50 -07:00
|
|
|
}
|
2012-03-30 21:42:20 -07:00
|
|
|
if (IS_WRAPPER_CLASS(clasp) || clasp->ext.innerObject) {
|
|
|
|
NS_ASSERTION(clasp->ext.innerObject || IS_WN_WRAPPER_OBJECT(obj),
|
|
|
|
"We forgot to Morph a slim wrapper!");
|
|
|
|
return XrayForWrappedNative;
|
|
|
|
}
|
|
|
|
return NotXray;
|
2011-05-25 08:30:50 -07:00
|
|
|
}
|
|
|
|
|
2010-07-02 13:54:53 -07:00
|
|
|
JSObject *
|
2010-09-17 14:54:40 -07:00
|
|
|
WrapperFactory::Rewrap(JSContext *cx, JSObject *obj, JSObject *wrappedProto, JSObject *parent,
|
2012-02-28 15:11:11 -08:00
|
|
|
unsigned flags)
|
2010-06-25 15:58:09 -07:00
|
|
|
{
|
2011-10-04 07:06:54 -07:00
|
|
|
NS_ASSERTION(!IsWrapper(obj) ||
|
|
|
|
GetProxyHandler(obj) == &WaiveXrayWrapperWrapper ||
|
|
|
|
js::GetObjectClass(obj)->ext.innerObject,
|
2010-08-23 15:34:11 -07:00
|
|
|
"wrapped object passed to rewrap");
|
2012-02-03 16:54:57 -08:00
|
|
|
NS_ASSERTION(JS_GetClass(obj) != &XrayUtils::HolderClass, "trying to wrap a holder");
|
2010-07-02 13:54:53 -07:00
|
|
|
|
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);
|
2012-03-23 14:59:04 -07:00
|
|
|
bool usingXray = false;
|
2010-07-02 13:54:53 -07:00
|
|
|
|
2011-09-08 20:29:15 -07:00
|
|
|
Wrapper *wrapper;
|
2011-08-06 14:05:25 -07:00
|
|
|
CompartmentPrivate *targetdata =
|
2012-02-29 04:18:16 -08:00
|
|
|
static_cast<CompartmentPrivate *>(JS_GetCompartmentPrivate(target));
|
2010-07-02 13:54:53 -07:00
|
|
|
if (AccessCheck::isChrome(target)) {
|
2011-04-18 13:50:47 -07:00
|
|
|
if (AccessCheck::isChrome(origin)) {
|
2011-09-08 20:29:15 -07:00
|
|
|
wrapper = &CrossCompartmentWrapper::singleton;
|
2010-07-02 13:54:53 -07:00
|
|
|
} else {
|
2011-04-18 13:50:47 -07:00
|
|
|
bool isSystem;
|
|
|
|
{
|
|
|
|
JSAutoEnterCompartment ac;
|
|
|
|
if (!ac.enter(cx, obj))
|
2010-09-20 14:48:01 -07:00
|
|
|
return nsnull;
|
2011-04-18 13:50:47 -07:00
|
|
|
JSObject *globalObj = JS_GetGlobalForObject(cx, obj);
|
|
|
|
JS_ASSERT(globalObj);
|
|
|
|
isSystem = JS_IsSystemObject(cx, globalObj);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isSystem) {
|
2011-09-08 20:29:15 -07:00
|
|
|
wrapper = &CrossCompartmentWrapper::singleton;
|
2011-04-18 13:50:47 -07:00
|
|
|
} else if (flags & WAIVE_XRAY_WRAPPER_FLAG) {
|
|
|
|
// If we waived the X-ray wrapper for this object, wrap it into a
|
|
|
|
// special wrapper to transitively maintain the X-ray waiver.
|
|
|
|
wrapper = &CrossOriginWrapper::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.
|
2012-03-30 21:42:20 -07:00
|
|
|
XrayType type = GetXrayType(obj);
|
|
|
|
if (type == XrayForDOMObject) {
|
|
|
|
wrapper = &XrayDOM::singleton;
|
|
|
|
} else if (type == XrayForDOMProxyObject) {
|
|
|
|
wrapper = &XrayProxy::singleton;
|
|
|
|
} else if (type == XrayForWrappedNative) {
|
|
|
|
typedef XrayWrapper<CrossCompartmentWrapper> Xray;
|
|
|
|
usingXray = true;
|
|
|
|
wrapper = &Xray::singleton;
|
2011-04-18 13:50:47 -07:00
|
|
|
} else {
|
|
|
|
wrapper = &NoWaiverWrapper::singleton;
|
|
|
|
}
|
2010-09-02 16:02:51 -07:00
|
|
|
}
|
2010-07-02 13:54:53 -07:00
|
|
|
}
|
|
|
|
} else if (AccessCheck::isChrome(origin)) {
|
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");
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
}
|
2011-05-10 14:41:25 -07:00
|
|
|
|
|
|
|
XPCWrappedNative *wn;
|
2011-05-24 02:33:44 -07:00
|
|
|
if (targetdata &&
|
|
|
|
(wn = GetWrappedNative(cx, obj)) &&
|
|
|
|
wn->HasProto() && wn->GetProto()->ClassIsDOMObject()) {
|
2011-10-04 10:50:25 -07:00
|
|
|
typedef XrayWrapper<CrossCompartmentSecurityWrapper> Xray;
|
2012-03-23 14:59:04 -07:00
|
|
|
usingXray = true;
|
2012-03-23 14:59:19 -07:00
|
|
|
if (IsLocationObject(obj))
|
|
|
|
wrapper = &FilteringWrapper<Xray, LocationPolicy>::singleton;
|
|
|
|
else
|
|
|
|
wrapper = &FilteringWrapper<Xray, CrossOriginAccessiblePropertiesOnly>::singleton;
|
2012-03-30 21:42:20 -07:00
|
|
|
} else if (mozilla::dom::binding::instanceIsProxy(obj)) {
|
|
|
|
wrapper = &FilteringWrapper<XrayProxy, CrossOriginAccessiblePropertiesOnly>::singleton;
|
2012-05-02 21:35:38 -07:00
|
|
|
} else if (mozilla::dom::IsDOMClass(JS_GetClass(obj))) {
|
2012-03-30 21:42:20 -07:00
|
|
|
wrapper = &FilteringWrapper<XrayDOM, 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 {
|
2011-10-04 10:50:25 -07:00
|
|
|
wrapper = &FilteringWrapper<CrossCompartmentSecurityWrapper,
|
2011-05-10 14:41:25 -07:00
|
|
|
ExposedPropertiesOnly>::singleton;
|
|
|
|
}
|
2010-10-10 15:48:55 -07:00
|
|
|
} else if (AccessCheck::isSameOrigin(origin, target)) {
|
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.
|
|
|
|
// * The object is a location 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.
|
2012-04-05 12:21:12 -07:00
|
|
|
//
|
|
|
|
// The Location case is a bit tricky. Because the security characteristics
|
|
|
|
// depend on the current outer window, we always have a security wrapper
|
|
|
|
// around locations, same-compartment or cross-compartment. We would
|
|
|
|
// normally just use an identical security policy and just switch between
|
|
|
|
// Wrapper and CrossCompartmentWrapper to differentiate the cases (LW/XLW).
|
|
|
|
// However, there's an added wrinkle that same-origin-but-cross-compartment
|
|
|
|
// scripts expect to be able to see expandos on each others' location
|
|
|
|
// objects. So if all cross-compartment access used XLWs, then the expandos
|
|
|
|
// would live on the per-compartment XrayWrapper expando object, and would
|
|
|
|
// not be shared. So to make sure that expandos work correctly in the
|
|
|
|
// same-origin case, we need to use a transparent CrossCompartmentWrapper
|
|
|
|
// to the LW in the host compartment, rather than an XLW directly to the
|
|
|
|
// Location object. This still doesn't share expandos in the
|
|
|
|
// document.domain case, but that's probably fine. Double-wrapping sucks,
|
|
|
|
// but it's kind of unavoidable here.
|
2012-03-30 21:42:20 -07:00
|
|
|
XrayType type;
|
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;
|
2012-03-30 21:42:20 -07:00
|
|
|
} else if (!targetdata || !targetdata->wantXrays ||
|
|
|
|
(type = GetXrayType(obj)) == NotXray) {
|
2012-04-05 12:21:12 -07:00
|
|
|
// Do the double-wrapping if need be.
|
|
|
|
if (IsLocationObject(obj)) {
|
|
|
|
JSAutoEnterCompartment ac;
|
|
|
|
if (!ac.enter(cx, obj))
|
|
|
|
return nsnull;
|
|
|
|
XPCWrappedNative *wn = GetWrappedNative(cx, obj);
|
|
|
|
if (!wn)
|
|
|
|
return nsnull;
|
|
|
|
obj = wn->GetSameCompartmentSecurityWrapper(cx);
|
|
|
|
}
|
2011-09-08 20:29:15 -07:00
|
|
|
wrapper = &CrossCompartmentWrapper::singleton;
|
2012-03-30 21:42:20 -07:00
|
|
|
} else if (type == XrayForDOMObject) {
|
|
|
|
wrapper = &XrayDOM::singleton;
|
|
|
|
} else if (type == XrayForDOMProxyObject) {
|
|
|
|
wrapper = &XrayProxy::singleton;
|
|
|
|
} else {
|
|
|
|
typedef XrayWrapper<CrossCompartmentWrapper> Xray;
|
|
|
|
usingXray = true;
|
|
|
|
wrapper = &Xray::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
|
|
|
|
// a predefined set of properties. XrayWrapper adds a property
|
|
|
|
// (.wrappedJSObject) which allows bypassing the XrayWrapper, but
|
|
|
|
// we filter out access to that property.
|
2012-03-30 21:42:20 -07:00
|
|
|
XrayType type = GetXrayType(obj);
|
|
|
|
if (type == NotXray) {
|
2011-10-04 10:50:25 -07:00
|
|
|
wrapper = &FilteringWrapper<CrossCompartmentSecurityWrapper,
|
2010-09-02 16:02:51 -07:00
|
|
|
CrossOriginAccessiblePropertiesOnly>::singleton;
|
2012-03-30 21:42:20 -07:00
|
|
|
} else if (type == XrayForDOMObject) {
|
|
|
|
wrapper = &FilteringWrapper<XrayDOM,
|
|
|
|
CrossOriginAccessiblePropertiesOnly>::singleton;
|
|
|
|
} else if (type == XrayForDOMProxyObject) {
|
|
|
|
wrapper = &FilteringWrapper<XrayProxy,
|
|
|
|
CrossOriginAccessiblePropertiesOnly>::singleton;
|
2010-09-02 16:02:51 -07:00
|
|
|
} else {
|
2012-03-30 21:42:20 -07:00
|
|
|
typedef XrayWrapper<CrossCompartmentSecurityWrapper> Xray;
|
|
|
|
usingXray = true;
|
2010-09-29 10:00:52 -07:00
|
|
|
|
2012-03-30 21:42:20 -07:00
|
|
|
// Location objects can become same origin after navigation, so we might
|
|
|
|
// have to grant transparent access later on.
|
|
|
|
if (IsLocationObject(obj)) {
|
|
|
|
wrapper = &FilteringWrapper<Xray, LocationPolicy>::singleton;
|
|
|
|
} else {
|
|
|
|
wrapper = &FilteringWrapper<Xray,
|
|
|
|
CrossOriginAccessiblePropertiesOnly>::singleton;
|
2011-05-25 08:30:50 -07:00
|
|
|
}
|
2010-09-02 16:02:51 -07:00
|
|
|
}
|
2010-06-25 15:58:09 -07:00
|
|
|
}
|
2010-09-20 14:48:01 -07:00
|
|
|
|
2011-09-08 20:29:15 -07:00
|
|
|
JSObject *wrapperObj = Wrapper::New(cx, obj, wrappedProto, parent, wrapper);
|
2012-03-23 14:59:04 -07:00
|
|
|
if (!wrapperObj || !usingXray)
|
2010-09-20 14:48:01 -07:00
|
|
|
return wrapperObj;
|
2010-10-10 15:35:54 -07:00
|
|
|
|
2012-03-23 14:59:04 -07:00
|
|
|
JSObject *xrayHolder = XrayUtils::createHolder(cx, obj, parent);
|
|
|
|
if (!xrayHolder)
|
|
|
|
return nsnull;
|
2011-05-23 08:39:25 -07:00
|
|
|
js::SetProxyExtra(wrapperObj, 0, js::ObjectValue(*xrayHolder));
|
2010-09-20 14:48:01 -07:00
|
|
|
return wrapperObj;
|
2010-06-25 15:58:09 -07:00
|
|
|
}
|
|
|
|
|
2012-03-23 14:59:07 -07:00
|
|
|
typedef FilteringWrapper<XrayWrapper<SameCompartmentSecurityWrapper>, LocationPolicy> LW;
|
2010-09-29 10:00:52 -07:00
|
|
|
|
|
|
|
bool
|
|
|
|
WrapperFactory::IsLocationObject(JSObject *obj)
|
|
|
|
{
|
2011-10-04 07:06:54 -07:00
|
|
|
const char *name = js::GetObjectClass(obj)->name;
|
2010-09-29 10:00:52 -07:00
|
|
|
return name[0] == 'L' && !strcmp(name, "Location");
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject *
|
|
|
|
WrapperFactory::WrapLocationObject(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
2012-03-23 14:59:04 -07:00
|
|
|
JSObject *xrayHolder = XrayUtils::createHolder(cx, obj, js::GetObjectParent(obj));
|
2010-09-29 10:00:52 -07:00
|
|
|
if (!xrayHolder)
|
2011-01-13 13:03:44 -08:00
|
|
|
return nsnull;
|
2011-10-04 07:06:54 -07:00
|
|
|
JSObject *wrapperObj = Wrapper::New(cx, obj, js::GetObjectProto(obj), js::GetObjectParent(obj),
|
2011-09-08 20:29:15 -07:00
|
|
|
&LW::singleton);
|
2010-09-29 10:00:52 -07:00
|
|
|
if (!wrapperObj)
|
2011-01-13 13:03:44 -08:00
|
|
|
return nsnull;
|
2011-05-23 08:39:25 -07:00
|
|
|
js::SetProxyExtra(wrapperObj, 0, js::ObjectValue(*xrayHolder));
|
2010-09-29 10:00:52 -07:00
|
|
|
return wrapperObj;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
JSObject *wrapperObj =
|
2012-02-05 12:07:23 -08:00
|
|
|
Wrapper::New(cx, obj, JS_GetPrototype(obj), 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)
|
|
|
|
{
|
|
|
|
JSObject *wrapperObj =
|
|
|
|
Wrapper::New(cx, obj, JS_GetPrototype(obj), JS_GetGlobalForObject(cx, obj),
|
|
|
|
&FilteringWrapper<SameCompartmentSecurityWrapper, ComponentsObjectPolicy>::singleton);
|
|
|
|
|
|
|
|
return wrapperObj;
|
|
|
|
}
|
|
|
|
|
2010-06-25 15:58:09 -07:00
|
|
|
}
|