2014-04-03 04:58:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* vim: set ts=8 sts=4 et sw=4 tw=99: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-21 04:12:37 -07:00
|
|
|
* 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
|
|
|
|
2010-07-02 13:54:53 -07:00
|
|
|
#include "AccessCheck.h"
|
2010-06-25 15:58:09 -07:00
|
|
|
|
|
|
|
#include "nsJSPrincipals.h"
|
2013-11-05 00:47:59 -08:00
|
|
|
#include "nsGlobalWindow.h"
|
2010-06-25 15:58:09 -07:00
|
|
|
|
2010-07-02 13:54:53 -07:00
|
|
|
#include "XPCWrapper.h"
|
2010-09-17 14:54:40 -07:00
|
|
|
#include "XrayWrapper.h"
|
2010-06-25 15:58:09 -07:00
|
|
|
|
2011-04-13 09:27:37 -07:00
|
|
|
#include "jsfriendapi.h"
|
2012-09-12 13:29:30 -07:00
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
2013-12-09 07:34:03 -08:00
|
|
|
#include "mozilla/dom/WindowBinding.h"
|
2013-11-05 00:47:59 -08:00
|
|
|
#include "nsIDOMWindowCollection.h"
|
|
|
|
#include "nsJSUtils.h"
|
2010-10-28 08:15:53 -07:00
|
|
|
|
2011-10-10 22:50:08 -07:00
|
|
|
using namespace mozilla;
|
2013-09-01 21:51:02 -07:00
|
|
|
using namespace JS;
|
2011-09-08 20:29:15 -07:00
|
|
|
using namespace js;
|
|
|
|
|
2010-06-25 15:58:09 -07:00
|
|
|
namespace xpc {
|
|
|
|
|
2011-08-06 14:05:25 -07:00
|
|
|
nsIPrincipal *
|
2010-06-25 15:58:09 -07:00
|
|
|
GetCompartmentPrincipal(JSCompartment *compartment)
|
|
|
|
{
|
2012-03-09 01:48:50 -08:00
|
|
|
return nsJSPrincipals::get(JS_GetCompartmentPrincipals(compartment));
|
2010-06-25 15:58:09 -07:00
|
|
|
}
|
|
|
|
|
2013-05-06 16:53:10 -07:00
|
|
|
nsIPrincipal *
|
|
|
|
GetObjectPrincipal(JSObject *obj)
|
|
|
|
{
|
|
|
|
return GetCompartmentPrincipal(js::GetObjectCompartment(obj));
|
|
|
|
}
|
|
|
|
|
2012-07-12 01:10:15 -07:00
|
|
|
// Does the principal of compartment a subsume the principal of compartment b?
|
2010-06-25 15:58:09 -07:00
|
|
|
bool
|
2012-07-12 01:10:15 -07:00
|
|
|
AccessCheck::subsumes(JSCompartment *a, JSCompartment *b)
|
2010-07-02 13:54:53 -07:00
|
|
|
{
|
2010-10-13 11:37:25 -07:00
|
|
|
nsIPrincipal *aprin = GetCompartmentPrincipal(a);
|
|
|
|
nsIPrincipal *bprin = GetCompartmentPrincipal(b);
|
2014-02-13 18:57:34 -08:00
|
|
|
return aprin->Subsumes(bprin);
|
2010-07-02 13:54:53 -07:00
|
|
|
}
|
|
|
|
|
2012-12-07 14:49:11 -08:00
|
|
|
bool
|
|
|
|
AccessCheck::subsumes(JSObject *a, JSObject *b)
|
|
|
|
{
|
|
|
|
return subsumes(js::GetObjectCompartment(a), js::GetObjectCompartment(b));
|
|
|
|
}
|
|
|
|
|
2014-02-13 18:57:34 -08:00
|
|
|
// Same as above, but considering document.domain.
|
|
|
|
bool
|
|
|
|
AccessCheck::subsumesConsideringDomain(JSCompartment *a, JSCompartment *b)
|
|
|
|
{
|
|
|
|
nsIPrincipal *aprin = GetCompartmentPrincipal(a);
|
|
|
|
nsIPrincipal *bprin = GetCompartmentPrincipal(b);
|
|
|
|
return aprin->SubsumesConsideringDomain(bprin);
|
2012-09-11 10:23:20 -07:00
|
|
|
}
|
|
|
|
|
2012-09-11 01:05:10 -07:00
|
|
|
// Does the compartment of the wrapper subsumes the compartment of the wrappee?
|
|
|
|
bool
|
|
|
|
AccessCheck::wrapperSubsumes(JSObject *wrapper)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(js::IsWrapper(wrapper));
|
2013-04-11 11:50:18 -07:00
|
|
|
JSObject *wrapped = js::UncheckedUnwrap(wrapper);
|
2012-09-11 01:05:10 -07:00
|
|
|
return AccessCheck::subsumes(js::GetObjectCompartment(wrapper),
|
|
|
|
js::GetObjectCompartment(wrapped));
|
|
|
|
}
|
|
|
|
|
2010-07-02 13:54:53 -07:00
|
|
|
bool
|
|
|
|
AccessCheck::isChrome(JSCompartment *compartment)
|
2010-06-25 15:58:09 -07:00
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
bool privileged;
|
2010-07-02 13:54:53 -07:00
|
|
|
nsIPrincipal *principal = GetCompartmentPrincipal(compartment);
|
2014-05-06 23:17:43 -07:00
|
|
|
return NS_SUCCEEDED(nsXPConnect::SecurityManager()->IsSystemPrincipal(principal, &privileged)) && privileged;
|
2010-07-02 13:54:53 -07:00
|
|
|
}
|
|
|
|
|
2012-09-11 01:05:10 -07:00
|
|
|
bool
|
|
|
|
AccessCheck::isChrome(JSObject *obj)
|
|
|
|
{
|
|
|
|
return isChrome(js::GetObjectCompartment(obj));
|
|
|
|
}
|
|
|
|
|
2010-09-24 18:00:58 -07:00
|
|
|
nsIPrincipal *
|
|
|
|
AccessCheck::getPrincipal(JSCompartment *compartment)
|
|
|
|
{
|
|
|
|
return GetCompartmentPrincipal(compartment);
|
|
|
|
}
|
|
|
|
|
2010-10-28 08:15:53 -07:00
|
|
|
#define NAME(ch, str, cases) \
|
|
|
|
case ch: if (!strcmp(name, str)) switch (propChars[0]) { cases }; break;
|
2010-07-02 13:54:53 -07:00
|
|
|
#define PROP(ch, actions) case ch: { actions }; break;
|
2010-12-03 00:24:17 -08:00
|
|
|
#define RW(str) if (JS_FlatStringEqualsAscii(prop, str)) return true;
|
|
|
|
#define R(str) if (!set && JS_FlatStringEqualsAscii(prop, str)) return true;
|
|
|
|
#define W(str) if (set && JS_FlatStringEqualsAscii(prop, str)) return true;
|
2010-07-02 13:54:53 -07:00
|
|
|
|
|
|
|
// Hardcoded policy for cross origin property access. This was culled from the
|
|
|
|
// preferences file (all.js). We don't want users to overwrite highly sensitive
|
|
|
|
// security policies.
|
|
|
|
static bool
|
2010-12-03 00:24:17 -08:00
|
|
|
IsPermitted(const char *name, JSFlatString *prop, bool set)
|
2010-07-02 13:54:53 -07:00
|
|
|
{
|
2010-10-28 08:15:53 -07:00
|
|
|
size_t propLength;
|
2012-01-15 00:13:07 -08:00
|
|
|
const jschar *propChars =
|
|
|
|
JS_GetInternedStringCharsAndLength(JS_FORGET_STRING_FLATNESS(prop), &propLength);
|
2010-10-28 08:15:53 -07:00
|
|
|
if (!propLength)
|
|
|
|
return false;
|
2011-10-14 10:52:48 -07:00
|
|
|
switch (name[0]) {
|
2010-09-17 14:54:40 -07:00
|
|
|
NAME('L', "Location",
|
2013-02-10 15:05:17 -08:00
|
|
|
PROP('h', W("href"))
|
2010-09-17 14:54:40 -07:00
|
|
|
PROP('r', R("replace")))
|
2013-12-09 07:34:03 -08:00
|
|
|
case 'W':
|
|
|
|
if (!strcmp(name, "Window"))
|
|
|
|
return dom::WindowBinding::IsPermitted(prop, propChars[0], set);
|
|
|
|
break;
|
2010-07-02 13:54:53 -07:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef NAME
|
|
|
|
#undef RW
|
|
|
|
#undef R
|
|
|
|
#undef W
|
|
|
|
|
2013-09-20 10:32:32 -07:00
|
|
|
static bool
|
|
|
|
IsFrameId(JSContext *cx, JSObject *objArg, jsid idArg)
|
|
|
|
{
|
|
|
|
RootedObject obj(cx, objArg);
|
|
|
|
RootedId id(cx, idArg);
|
|
|
|
|
|
|
|
obj = JS_ObjectToInnerObject(cx, obj);
|
|
|
|
MOZ_ASSERT(!js::IsWrapper(obj));
|
2013-11-05 00:47:59 -08:00
|
|
|
nsGlobalWindow* win = WindowOrNull(obj);
|
|
|
|
if (!win) {
|
2013-09-20 10:32:32 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMWindowCollection> col;
|
2013-11-05 00:47:59 -08:00
|
|
|
win->GetFrames(getter_AddRefs(col));
|
2013-09-20 10:32:32 -07:00
|
|
|
if (!col) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-05 00:47:59 -08:00
|
|
|
nsCOMPtr<nsIDOMWindow> domwin;
|
2013-09-20 10:32:32 -07:00
|
|
|
if (JSID_IS_INT(id)) {
|
|
|
|
col->Item(JSID_TO_INT(id), getter_AddRefs(domwin));
|
|
|
|
} else if (JSID_IS_STRING(id)) {
|
2013-11-05 00:47:59 -08:00
|
|
|
col->NamedItem(nsDependentJSString(id), getter_AddRefs(domwin));
|
2013-09-20 10:32:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return domwin != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
IsWindow(const char *name)
|
|
|
|
{
|
|
|
|
return name[0] == 'W' && !strcmp(name, "Window");
|
|
|
|
}
|
|
|
|
|
2010-07-02 13:54:53 -07:00
|
|
|
bool
|
2013-04-23 09:50:17 -07:00
|
|
|
AccessCheck::isCrossOriginAccessPermitted(JSContext *cx, JSObject *wrapperArg, jsid idArg,
|
2011-09-08 20:29:15 -07:00
|
|
|
Wrapper::Action act)
|
2010-07-02 13:54:53 -07:00
|
|
|
{
|
2011-09-08 20:29:15 -07:00
|
|
|
if (act == Wrapper::CALL)
|
2013-05-22 21:27:16 -07:00
|
|
|
return false;
|
2010-09-17 14:54:40 -07:00
|
|
|
|
2013-04-23 09:50:17 -07:00
|
|
|
RootedId id(cx, idArg);
|
|
|
|
RootedObject wrapper(cx, wrapperArg);
|
|
|
|
RootedObject obj(cx, Wrapper::wrappedObject(wrapper));
|
2010-07-02 13:54:53 -07:00
|
|
|
|
2014-02-13 10:54:08 -08:00
|
|
|
// For XOWs, we generally want to deny enumerate-like operations, but fail
|
|
|
|
// silently (see CrossOriginAccessiblePropertiesOnly::deny).
|
|
|
|
if (act == Wrapper::ENUMERATE)
|
2013-05-22 21:27:15 -07:00
|
|
|
return false;
|
|
|
|
|
2010-09-17 14:54:40 -07:00
|
|
|
const char *name;
|
2013-09-11 05:49:05 -07:00
|
|
|
const js::Class *clasp = js::GetObjectClass(obj);
|
2014-02-21 15:55:31 -08:00
|
|
|
MOZ_ASSERT(!XrayUtils::IsXPCWNHolderClass(Jsvalify(clasp)), "shouldn't have a holder here");
|
2010-09-17 14:54:40 -07:00
|
|
|
if (clasp->ext.innerObject)
|
|
|
|
name = "Window";
|
|
|
|
else
|
|
|
|
name = clasp->name;
|
2010-07-02 13:54:53 -07:00
|
|
|
|
2012-01-15 00:13:07 -08:00
|
|
|
if (JSID_IS_STRING(id)) {
|
2011-09-08 20:29:15 -07:00
|
|
|
if (IsPermitted(name, JSID_TO_FLAT_STRING(id), act == Wrapper::SET))
|
2010-07-02 13:54:53 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-20 10:32:32 -07:00
|
|
|
if (act != Wrapper::GET)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check for frame IDs. If we're resolving named frames, make sure to only
|
|
|
|
// resolve ones that don't shadow native properties. See bug 860494.
|
|
|
|
if (IsWindow(name)) {
|
|
|
|
if (JSID_IS_STRING(id) && !XrayUtils::IsXrayResolving(cx, wrapper, id)) {
|
|
|
|
bool wouldShadow = false;
|
|
|
|
if (!XrayUtils::HasNativeProperty(cx, wrapper, id, &wouldShadow) ||
|
|
|
|
wouldShadow)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return IsFrameId(cx, obj, id);
|
|
|
|
}
|
2013-04-23 09:50:17 -07:00
|
|
|
return false;
|
2010-07-02 13:54:53 -07:00
|
|
|
}
|
|
|
|
|
2011-01-29 18:47:17 -08:00
|
|
|
enum Access { READ = (1<<0), WRITE = (1<<1), NO_ACCESS = 0 };
|
|
|
|
|
2012-12-07 14:49:11 -08:00
|
|
|
static void
|
|
|
|
EnterAndThrow(JSContext *cx, JSObject *wrapper, const char *msg)
|
|
|
|
{
|
|
|
|
JSAutoCompartment ac(cx, wrapper);
|
|
|
|
JS_ReportError(cx, msg);
|
|
|
|
}
|
|
|
|
|
2010-06-25 15:58:09 -07:00
|
|
|
bool
|
2013-04-17 08:38:44 -07:00
|
|
|
ExposedPropertiesOnly::check(JSContext *cx, JSObject *wrapperArg, jsid idArg, Wrapper::Action act)
|
2010-06-25 15:58:09 -07:00
|
|
|
{
|
2013-04-17 08:38:44 -07:00
|
|
|
RootedObject wrapper(cx, wrapperArg);
|
|
|
|
RootedId id(cx, idArg);
|
|
|
|
RootedObject wrappedObject(cx, Wrapper::wrappedObject(wrapper));
|
2011-01-29 18:47:17 -08:00
|
|
|
|
2012-11-02 17:47:49 -07:00
|
|
|
if (act == Wrapper::CALL)
|
2011-01-29 18:47:17 -08:00
|
|
|
return true;
|
2010-07-02 13:54:53 -07:00
|
|
|
|
2013-04-17 08:38:44 -07:00
|
|
|
RootedId exposedPropsId(cx, GetRTIdByIndex(cx, XPCJSRuntime::IDX_EXPOSEDPROPS));
|
2010-07-02 13:54:53 -07:00
|
|
|
|
2012-06-18 06:47:09 -07:00
|
|
|
// We need to enter the wrappee's compartment to look at __exposedProps__,
|
2012-09-11 01:05:10 -07:00
|
|
|
// but we want to be in the wrapper's compartment if we call Deny().
|
2012-06-18 06:47:09 -07:00
|
|
|
//
|
|
|
|
// Unfortunately, |cx| can be in either compartment when we call ::check. :-(
|
2012-08-21 18:42:53 -07:00
|
|
|
JSAutoCompartment ac(cx, wrappedObject);
|
2012-06-18 06:47:09 -07:00
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
bool found = false;
|
2012-06-18 06:47:09 -07:00
|
|
|
if (!JS_HasPropertyById(cx, wrappedObject, exposedPropsId, &found))
|
2010-07-02 13:54:53 -07:00
|
|
|
return false;
|
2011-01-29 18:47:17 -08:00
|
|
|
|
|
|
|
// Always permit access to "length" and indexed properties of arrays.
|
2012-09-05 14:05:32 -07:00
|
|
|
if ((JS_IsArrayObject(cx, wrappedObject) ||
|
2012-11-14 09:56:26 -08:00
|
|
|
JS_IsTypedArrayObject(wrappedObject)) &&
|
2011-01-29 18:47:17 -08:00
|
|
|
((JSID_IS_INT(id) && JSID_TO_INT(id) >= 0) ||
|
2012-01-15 00:13:07 -08:00
|
|
|
(JSID_IS_STRING(id) && JS_FlatStringEqualsAscii(JSID_TO_FLAT_STRING(id), "length")))) {
|
2010-07-02 13:54:53 -07:00
|
|
|
return true; // Allow
|
|
|
|
}
|
|
|
|
|
2011-01-29 18:47:17 -08:00
|
|
|
// If no __exposedProps__ existed, deny access.
|
|
|
|
if (!found) {
|
2012-11-02 17:47:49 -07:00
|
|
|
return false;
|
2011-01-29 18:47:17 -08:00
|
|
|
}
|
|
|
|
|
2013-12-16 18:27:43 -08:00
|
|
|
if (id == JSID_VOID)
|
2010-06-25 15:58:09 -07:00
|
|
|
return true;
|
2010-07-02 13:54:53 -07:00
|
|
|
|
2013-04-17 08:38:44 -07:00
|
|
|
RootedValue exposedProps(cx);
|
2013-08-02 05:15:39 -07:00
|
|
|
if (!JS_LookupPropertyById(cx, wrappedObject, exposedPropsId, &exposedProps))
|
2010-07-01 15:45:08 -07:00
|
|
|
return false;
|
2010-07-02 13:54:53 -07:00
|
|
|
|
2012-11-02 17:47:49 -07:00
|
|
|
if (exposedProps.isNullOrUndefined())
|
|
|
|
return false;
|
2010-07-01 15:45:08 -07:00
|
|
|
|
2012-05-11 08:46:26 -07:00
|
|
|
if (!exposedProps.isObject()) {
|
2012-12-07 14:49:11 -08:00
|
|
|
EnterAndThrow(cx, wrapper, "__exposedProps__ must be undefined, null, or an Object");
|
2010-07-02 13:54:53 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-04-26 10:50:18 -07:00
|
|
|
RootedObject hallpass(cx, &exposedProps.toObject());
|
2010-07-02 13:54:53 -07:00
|
|
|
|
2013-04-11 11:50:18 -07:00
|
|
|
if (!AccessCheck::subsumes(js::UncheckedUnwrap(hallpass), wrappedObject)) {
|
2012-12-07 14:49:11 -08:00
|
|
|
EnterAndThrow(cx, wrapper, "Invalid __exposedProps__");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-07-02 13:54:53 -07:00
|
|
|
Access access = NO_ACCESS;
|
|
|
|
|
2013-04-26 10:50:18 -07:00
|
|
|
Rooted<JSPropertyDescriptor> desc(cx);
|
2014-04-25 14:11:01 -07:00
|
|
|
if (!JS_GetPropertyDescriptorById(cx, hallpass, id, &desc)) {
|
2010-07-02 13:54:53 -07:00
|
|
|
return false; // Error
|
2010-07-01 15:45:08 -07:00
|
|
|
}
|
2013-04-26 10:50:18 -07:00
|
|
|
if (!desc.object() || !desc.isEnumerable())
|
2012-11-02 17:47:49 -07:00
|
|
|
return false;
|
2010-07-02 13:54:53 -07:00
|
|
|
|
2013-04-26 10:50:18 -07:00
|
|
|
if (!desc.value().isString()) {
|
2012-12-07 14:49:11 -08:00
|
|
|
EnterAndThrow(cx, wrapper, "property must be a string");
|
2010-07-02 13:54:53 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-04-26 10:50:18 -07:00
|
|
|
JSString *str = desc.value().toString();
|
2010-12-03 00:24:17 -08:00
|
|
|
size_t length;
|
|
|
|
const jschar *chars = JS_GetStringCharsAndLength(cx, str, &length);
|
|
|
|
if (!chars)
|
|
|
|
return false;
|
|
|
|
|
2010-07-02 13:54:53 -07:00
|
|
|
for (size_t i = 0; i < length; ++i) {
|
|
|
|
switch (chars[i]) {
|
|
|
|
case 'r':
|
|
|
|
if (access & READ) {
|
2012-12-07 14:49:11 -08:00
|
|
|
EnterAndThrow(cx, wrapper, "duplicate 'readable' property flag");
|
2010-07-02 13:54:53 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
access = Access(access | READ);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'w':
|
|
|
|
if (access & WRITE) {
|
2012-12-07 14:49:11 -08:00
|
|
|
EnterAndThrow(cx, wrapper, "duplicate 'writable' property flag");
|
2010-07-02 13:54:53 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
access = Access(access | WRITE);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2012-12-07 14:49:11 -08:00
|
|
|
EnterAndThrow(cx, wrapper, "properties can only be readable or read and writable");
|
2010-07-02 13:54:53 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (access == NO_ACCESS) {
|
2012-12-07 14:49:11 -08:00
|
|
|
EnterAndThrow(cx, wrapper, "specified properties must have a permission bit set");
|
2010-07-02 13:54:53 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-09-08 20:29:15 -07:00
|
|
|
if ((act == Wrapper::SET && !(access & WRITE)) ||
|
|
|
|
(act != Wrapper::SET && !(access & READ))) {
|
2012-11-02 17:47:49 -07:00
|
|
|
return false;
|
2010-07-02 13:54:53 -07:00
|
|
|
}
|
|
|
|
|
2012-11-02 17:47:49 -07:00
|
|
|
return true;
|
2010-06-25 15:58:09 -07:00
|
|
|
}
|
|
|
|
|
2012-12-20 22:33:26 -08:00
|
|
|
bool
|
|
|
|
ExposedPropertiesOnly::allowNativeCall(JSContext *cx, JS::IsAcceptableThis test,
|
|
|
|
JS::NativeImpl impl)
|
|
|
|
{
|
2014-03-23 07:02:12 -07:00
|
|
|
return js::IsTypedArrayThisCheck(test);
|
2012-12-20 22:33:26 -08:00
|
|
|
}
|
|
|
|
|
2010-06-25 15:58:09 -07:00
|
|
|
}
|