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-27 03:15:46 -07:00
|
|
|
#ifndef __AccessCheck_h__
|
|
|
|
#define __AccessCheck_h__
|
|
|
|
|
2010-06-25 15:58:09 -07:00
|
|
|
#include "jsapi.h"
|
|
|
|
#include "jswrapper.h"
|
2012-03-23 14:59:07 -07:00
|
|
|
#include "WrapperFactory.h"
|
2010-06-25 15:58:09 -07:00
|
|
|
|
2010-09-24 18:00:58 -07:00
|
|
|
class nsIPrincipal;
|
|
|
|
|
2010-06-25 15:58:09 -07:00
|
|
|
namespace xpc {
|
|
|
|
|
|
|
|
class AccessCheck {
|
|
|
|
public:
|
2012-07-12 01:10:15 -07:00
|
|
|
static bool subsumes(JSCompartment *a, JSCompartment *b);
|
2012-09-11 01:05:10 -07:00
|
|
|
static bool wrapperSubsumes(JSObject *wrapper);
|
2012-09-11 10:23:20 -07:00
|
|
|
static bool subsumesIgnoringDomain(JSCompartment *a, JSCompartment *b);
|
2010-07-02 13:54:53 -07:00
|
|
|
static bool isChrome(JSCompartment *compartment);
|
2012-09-11 01:05:10 -07:00
|
|
|
static bool isChrome(JSObject *obj);
|
2012-06-18 06:47:09 -07:00
|
|
|
static bool callerIsChrome();
|
2010-09-24 18:00:58 -07:00
|
|
|
static nsIPrincipal *getPrincipal(JSCompartment *compartment);
|
2010-09-17 14:54:40 -07:00
|
|
|
static bool isCrossOriginAccessPermitted(JSContext *cx, JSObject *obj, jsid id,
|
2011-09-08 20:29:15 -07:00
|
|
|
js::Wrapper::Action act);
|
2010-07-02 13:54:53 -07:00
|
|
|
static bool isSystemOnlyAccessPermitted(JSContext *cx);
|
2010-10-10 15:36:41 -07:00
|
|
|
static bool isLocationObjectSameOrigin(JSContext *cx, JSObject *wrapper);
|
2010-07-02 13:54:53 -07:00
|
|
|
|
|
|
|
static bool needsSystemOnlyWrapper(JSObject *obj);
|
2010-06-25 15:58:09 -07:00
|
|
|
|
2010-10-10 15:39:08 -07:00
|
|
|
static bool isScriptAccessOnly(JSContext *cx, JSObject *wrapper);
|
|
|
|
|
2010-06-25 15:58:09 -07:00
|
|
|
static void deny(JSContext *cx, jsid id);
|
2010-07-02 13:54:53 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Policy {
|
2011-09-08 20:29:15 -07:00
|
|
|
typedef js::Wrapper::Permission Permission;
|
2010-07-02 13:54:53 -07:00
|
|
|
|
2011-09-08 20:29:15 -07: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 13:54:53 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// This policy permits access to all properties.
|
|
|
|
struct Permissive : public Policy {
|
2011-09-08 20:29:15 -07:00
|
|
|
static bool check(JSContext *cx, JSObject *wrapper, jsid id, js::Wrapper::Action act,
|
2010-09-17 14:54:40 -07:00
|
|
|
Permission &perm) {
|
2010-07-02 13:54:53 -07: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-08 20:29:15 -07:00
|
|
|
static bool check(JSContext *cx, JSObject *wrapper, jsid id, js::Wrapper::Action act,
|
2010-09-17 14:54:40 -07:00
|
|
|
Permission &perm) {
|
2011-01-29 18:48:30 -08:00
|
|
|
if (AccessCheck::isSystemOnlyAccessPermitted(cx)) {
|
2010-07-02 13:54:53 -07:00
|
|
|
perm = PermitObjectAccess;
|
2011-01-29 18:48:30 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
perm = DenyAccess;
|
2012-08-21 18:42:53 -07:00
|
|
|
JSAutoCompartment ac(cx, wrapper);
|
2011-01-29 18:48:30 -08:00
|
|
|
AccessCheck::deny(cx, id);
|
|
|
|
return false;
|
2010-07-02 13:54:53 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// This policy only permits access to properties that are safe to be used
|
|
|
|
// across origins.
|
|
|
|
struct CrossOriginAccessiblePropertiesOnly : public Policy {
|
2011-09-08 20:29:15 -07:00
|
|
|
static bool check(JSContext *cx, JSObject *wrapper, jsid id, js::Wrapper::Action act,
|
2010-09-17 14:54:40 -07:00
|
|
|
Permission &perm) {
|
2012-03-23 14:59:07 -07:00
|
|
|
// Location objects should always use LocationPolicy.
|
|
|
|
MOZ_ASSERT(!WrapperFactory::IsLocationObject(js::UnwrapObject(wrapper)));
|
|
|
|
|
2011-01-29 18:48:30 -08:00
|
|
|
if (AccessCheck::isCrossOriginAccessPermitted(cx, wrapper, id, act)) {
|
2010-07-02 13:54:53 -07:00
|
|
|
perm = PermitPropertyAccess;
|
2011-01-29 18:48:30 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
perm = DenyAccess;
|
2012-08-21 18:42:53 -07:00
|
|
|
JSAutoCompartment ac(cx, wrapper);
|
2011-01-29 18:48:30 -08:00
|
|
|
AccessCheck::deny(cx, id);
|
|
|
|
return false;
|
2010-07-02 13:54:53 -07:00
|
|
|
}
|
|
|
|
};
|
2010-06-25 15:58:09 -07:00
|
|
|
|
2012-03-23 14:59:07 -07: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-08 20:29:15 -07:00
|
|
|
static bool check(JSContext *cx, JSObject *wrapper, jsid id, js::Wrapper::Action act,
|
2010-09-29 10:00:52 -07:00
|
|
|
Permission &perm) {
|
2012-03-23 14:59:07 -07:00
|
|
|
// We should only be dealing with Location objects here.
|
|
|
|
MOZ_ASSERT(WrapperFactory::IsLocationObject(js::UnwrapObject(wrapper)));
|
|
|
|
|
2012-03-23 14:59:27 -07:00
|
|
|
// Default to deny.
|
|
|
|
perm = DenyAccess;
|
|
|
|
|
|
|
|
// Location object security is complicated enough. Don't allow punctures.
|
2012-05-04 05:22:55 -07:00
|
|
|
if (act != js::Wrapper::PUNCTURE &&
|
|
|
|
(AccessCheck::isCrossOriginAccessPermitted(cx, wrapper, id, act) ||
|
|
|
|
AccessCheck::isLocationObjectSameOrigin(cx, wrapper))) {
|
2010-09-29 10:00:52 -07:00
|
|
|
perm = PermitPropertyAccess;
|
2011-01-29 18:48:30 -08:00
|
|
|
return true;
|
2010-09-29 10:00:52 -07:00
|
|
|
}
|
2012-05-04 05:22:55 -07:00
|
|
|
|
2012-08-21 18:42:53 -07:00
|
|
|
JSAutoCompartment ac(cx, wrapper);
|
2011-01-29 18:48:30 -08:00
|
|
|
AccessCheck::deny(cx, id);
|
|
|
|
return false;
|
2010-09-29 10:00:52 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-07-02 13:54:53 -07:00
|
|
|
// This policy only permits access to properties if they appear in the
|
|
|
|
// objects exposed properties list.
|
|
|
|
struct ExposedPropertiesOnly : public Policy {
|
2011-09-08 20:29:15 -07:00
|
|
|
static bool check(JSContext *cx, JSObject *wrapper, jsid id, js::Wrapper::Action act,
|
2010-09-17 14:54:40 -07:00
|
|
|
Permission &perm);
|
2010-06-25 15:58:09 -07:00
|
|
|
};
|
|
|
|
|
2012-04-28 06:12:28 -07: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 15:58:09 -07:00
|
|
|
}
|
2012-07-27 03:15:46 -07:00
|
|
|
|
|
|
|
#endif /* __AccessCheck_h__ */
|