2010-06-25 22:58:09 +00: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 11:12:37 +00: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 22:58:09 +00:00
|
|
|
|
|
|
|
#include "jsapi.h"
|
|
|
|
#include "jswrapper.h"
|
2012-03-23 21:59:07 +00:00
|
|
|
#include "WrapperFactory.h"
|
2010-06-25 22:58:09 +00:00
|
|
|
|
2010-09-25 01:00:58 +00:00
|
|
|
class nsIPrincipal;
|
|
|
|
|
2010-06-25 22:58:09 +00:00
|
|
|
namespace xpc {
|
|
|
|
|
|
|
|
class AccessCheck {
|
|
|
|
public:
|
2010-07-02 20:54:53 +00:00
|
|
|
static bool isSameOrigin(JSCompartment *a, JSCompartment *b);
|
|
|
|
static bool isChrome(JSCompartment *compartment);
|
2012-06-18 13:47:09 +00:00
|
|
|
static bool callerIsChrome();
|
2010-09-25 01:00:58 +00:00
|
|
|
static nsIPrincipal *getPrincipal(JSCompartment *compartment);
|
2010-09-17 21:54:40 +00:00
|
|
|
static bool isCrossOriginAccessPermitted(JSContext *cx, JSObject *obj, jsid id,
|
2011-09-09 03:29:15 +00:00
|
|
|
js::Wrapper::Action act);
|
2010-07-02 20:54:53 +00:00
|
|
|
static bool isSystemOnlyAccessPermitted(JSContext *cx);
|
2010-10-10 22:36:41 +00:00
|
|
|
static bool isLocationObjectSameOrigin(JSContext *cx, JSObject *wrapper);
|
2010-10-16 22:26:14 +00:00
|
|
|
static bool documentDomainMakesSameOrigin(JSContext *cx, JSObject *obj);
|
2010-07-02 20:54:53 +00:00
|
|
|
|
|
|
|
static bool needsSystemOnlyWrapper(JSObject *obj);
|
2010-06-25 22:58:09 +00:00
|
|
|
|
2010-10-10 22:39:08 +00:00
|
|
|
static bool isScriptAccessOnly(JSContext *cx, JSObject *wrapper);
|
|
|
|
|
2010-06-25 22:58:09 +00:00
|
|
|
static void deny(JSContext *cx, jsid id);
|
2010-07-02 20:54:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Policy {
|
2011-09-09 03:29:15 +00:00
|
|
|
typedef js::Wrapper::Permission Permission;
|
2010-07-02 20:54:53 +00:00
|
|
|
|
2011-09-09 03:29:15 +00:00
|
|
|
static const Permission PermitObjectAccess = js::Wrapper::PermitObjectAccess;
|
|
|
|
static const Permission PermitPropertyAccess = js::Wrapper::PermitPropertyAccess;
|
|
|
|
static const Permission DenyAccess = js::Wrapper::DenyAccess;
|
2010-07-02 20:54:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// This policy permits access to all properties.
|
|
|
|
struct Permissive : public Policy {
|
2011-09-09 03:29:15 +00:00
|
|
|
static bool check(JSContext *cx, JSObject *wrapper, jsid id, js::Wrapper::Action act,
|
2010-09-17 21:54:40 +00:00
|
|
|
Permission &perm) {
|
2010-07-02 20:54:53 +00:00
|
|
|
perm = PermitObjectAccess;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// This policy only permits access to the object if the subject can touch
|
|
|
|
// system objects.
|
|
|
|
struct OnlyIfSubjectIsSystem : public Policy {
|
2011-09-09 03:29:15 +00:00
|
|
|
static bool check(JSContext *cx, JSObject *wrapper, jsid id, js::Wrapper::Action act,
|
2010-09-17 21:54:40 +00:00
|
|
|
Permission &perm) {
|
2011-01-30 02:48:30 +00:00
|
|
|
if (AccessCheck::isSystemOnlyAccessPermitted(cx)) {
|
2010-07-02 20:54:53 +00:00
|
|
|
perm = PermitObjectAccess;
|
2011-01-30 02:48:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
perm = DenyAccess;
|
|
|
|
JSAutoEnterCompartment ac;
|
|
|
|
if (!ac.enter(cx, wrapper))
|
|
|
|
return false;
|
|
|
|
AccessCheck::deny(cx, id);
|
|
|
|
return false;
|
2010-07-02 20:54:53 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// This policy only permits access to properties that are safe to be used
|
|
|
|
// across origins.
|
|
|
|
struct CrossOriginAccessiblePropertiesOnly : public Policy {
|
2011-09-09 03:29:15 +00:00
|
|
|
static bool check(JSContext *cx, JSObject *wrapper, jsid id, js::Wrapper::Action act,
|
2010-09-17 21:54:40 +00:00
|
|
|
Permission &perm) {
|
2012-03-23 21:59:07 +00:00
|
|
|
// Location objects should always use LocationPolicy.
|
|
|
|
MOZ_ASSERT(!WrapperFactory::IsLocationObject(js::UnwrapObject(wrapper)));
|
|
|
|
|
2011-01-30 02:48:30 +00:00
|
|
|
if (AccessCheck::isCrossOriginAccessPermitted(cx, wrapper, id, act)) {
|
2010-07-02 20:54:53 +00:00
|
|
|
perm = PermitPropertyAccess;
|
2011-01-30 02:48:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
perm = DenyAccess;
|
|
|
|
JSAutoEnterCompartment ac;
|
|
|
|
if (!ac.enter(cx, wrapper))
|
|
|
|
return false;
|
|
|
|
AccessCheck::deny(cx, id);
|
|
|
|
return false;
|
2010-07-02 20:54:53 +00:00
|
|
|
}
|
|
|
|
};
|
2010-06-25 22:58:09 +00:00
|
|
|
|
2012-03-23 21:59:07 +00:00
|
|
|
// We need a special security policy for Location objects.
|
|
|
|
//
|
|
|
|
// Location objects are special because their effective principal is that of
|
|
|
|
// the outer window, not the inner window. So while the security characteristics
|
|
|
|
// of most objects can be inferred from their compartments, those of the Location
|
|
|
|
// object cannot. This has two implications:
|
|
|
|
//
|
|
|
|
// 1 - Same-compartment access of Location objects is not necessarily allowed.
|
|
|
|
// This means that objects must see a security wrapper around Location objects
|
|
|
|
// in their own compartment.
|
|
|
|
// 2 - Cross-origin access of Location objects is not necessarily forbidden.
|
|
|
|
// Since the security decision depends on the current state of the outer window,
|
|
|
|
// we can't make it at wrap time. Instead, we need to make it at the time of
|
|
|
|
// access.
|
|
|
|
//
|
|
|
|
// So for any Location object access, be it same-compartment or cross-compartment,
|
|
|
|
// we need to do a dynamic security check to determine whether the outer window is
|
|
|
|
// same-origin with the caller.
|
|
|
|
//
|
|
|
|
// So this policy first checks whether the access is something that any code,
|
|
|
|
// same-origin or not, is allowed to make. If it isn't, it _also_ checks the
|
|
|
|
// state of the outer window to determine whether we happen to be same-origin
|
|
|
|
// at the moment.
|
|
|
|
struct LocationPolicy : public Policy {
|
2011-09-09 03:29:15 +00:00
|
|
|
static bool check(JSContext *cx, JSObject *wrapper, jsid id, js::Wrapper::Action act,
|
2010-09-29 17:00:52 +00:00
|
|
|
Permission &perm) {
|
2012-03-23 21:59:07 +00:00
|
|
|
// We should only be dealing with Location objects here.
|
|
|
|
MOZ_ASSERT(WrapperFactory::IsLocationObject(js::UnwrapObject(wrapper)));
|
|
|
|
|
2012-03-23 21:59:27 +00:00
|
|
|
// Default to deny.
|
|
|
|
perm = DenyAccess;
|
|
|
|
|
|
|
|
// Location object security is complicated enough. Don't allow punctures.
|
2012-05-04 12:22:55 +00:00
|
|
|
if (act != js::Wrapper::PUNCTURE &&
|
|
|
|
(AccessCheck::isCrossOriginAccessPermitted(cx, wrapper, id, act) ||
|
|
|
|
AccessCheck::isLocationObjectSameOrigin(cx, wrapper))) {
|
2010-09-29 17:00:52 +00:00
|
|
|
perm = PermitPropertyAccess;
|
2011-01-30 02:48:30 +00:00
|
|
|
return true;
|
2010-09-29 17:00:52 +00:00
|
|
|
}
|
2012-05-04 12:22:55 +00:00
|
|
|
|
2011-01-30 02:48:30 +00:00
|
|
|
JSAutoEnterCompartment ac;
|
|
|
|
if (!ac.enter(cx, wrapper))
|
|
|
|
return false;
|
|
|
|
AccessCheck::deny(cx, id);
|
|
|
|
return false;
|
2010-09-29 17:00:52 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-07-02 20:54:53 +00:00
|
|
|
// This policy only permits access to properties if they appear in the
|
|
|
|
// objects exposed properties list.
|
|
|
|
struct ExposedPropertiesOnly : public Policy {
|
2011-09-09 03:29:15 +00:00
|
|
|
static bool check(JSContext *cx, JSObject *wrapper, jsid id, js::Wrapper::Action act,
|
2010-09-17 21:54:40 +00:00
|
|
|
Permission &perm);
|
2010-06-25 22:58:09 +00:00
|
|
|
};
|
|
|
|
|
2012-04-28 13:12:28 +00:00
|
|
|
// Components specific policy
|
|
|
|
struct ComponentsObjectPolicy : public Policy {
|
|
|
|
static bool check(JSContext *cx, JSObject *wrapper, jsid id, js::Wrapper::Action act,
|
|
|
|
Permission &perm);
|
|
|
|
};
|
|
|
|
|
2010-06-25 22:58:09 +00:00
|
|
|
}
|