2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
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/. */
|
2011-10-10 22:50:08 -07:00
|
|
|
|
|
|
|
#include "mozilla/Util.h"
|
|
|
|
|
2010-04-11 06:55:24 -07:00
|
|
|
#include "xpcprivate.h"
|
2012-05-18 01:29:40 -07:00
|
|
|
#include "XPCWrapper.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsScriptSecurityManager.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIScriptObjectPrincipal.h"
|
|
|
|
#include "nsIScriptContext.h"
|
|
|
|
#include "nsIURL.h"
|
|
|
|
#include "nsINestedURI.h"
|
|
|
|
#include "nspr.h"
|
|
|
|
#include "nsJSPrincipals.h"
|
|
|
|
#include "nsSystemPrincipal.h"
|
|
|
|
#include "nsPrincipal.h"
|
|
|
|
#include "nsNullPrincipal.h"
|
|
|
|
#include "nsXPIDLString.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "nsCRTGlue.h"
|
|
|
|
#include "nsIJSContextStack.h"
|
2012-07-27 07:03:27 -07:00
|
|
|
#include "nsError.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsDOMCID.h"
|
|
|
|
#include "jsdbgapi.h"
|
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
#include "nsIXPCSecurityManager.h"
|
|
|
|
#include "nsTextFormatter.h"
|
|
|
|
#include "nsIStringBundle.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsIProperties.h"
|
|
|
|
#include "nsDirectoryServiceDefs.h"
|
|
|
|
#include "nsIFile.h"
|
2007-09-06 00:02:57 -07:00
|
|
|
#include "nsIFileURL.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIZipReader.h"
|
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
#include "nsIScriptGlobalObject.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
#include "nsIDocShell.h"
|
|
|
|
#include "nsIDocShellTreeItem.h"
|
|
|
|
#include "nsIPrompt.h"
|
|
|
|
#include "nsIWindowWatcher.h"
|
|
|
|
#include "nsIConsoleService.h"
|
|
|
|
#include "nsISecurityCheckedComponent.h"
|
|
|
|
#include "nsIJSRuntimeService.h"
|
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsDOMJSUtils.h"
|
|
|
|
#include "nsAboutProtocolUtils.h"
|
|
|
|
#include "nsIClassInfo.h"
|
|
|
|
#include "nsIURIFixup.h"
|
|
|
|
#include "nsCDefaultURIFixup.h"
|
2008-04-12 14:26:19 -07:00
|
|
|
#include "nsIChromeRegistry.h"
|
2010-03-08 00:24:50 -08:00
|
|
|
#include "nsIContentSecurityPolicy.h"
|
2010-08-04 19:15:55 -07:00
|
|
|
#include "nsIAsyncVerifyRedirectCallback.h"
|
2011-06-19 20:00:16 -07:00
|
|
|
#include "mozilla/Preferences.h"
|
2012-05-02 21:35:38 -07:00
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
2012-04-11 17:17:44 -07:00
|
|
|
#include "mozilla/StandardInteger.h"
|
2012-06-19 10:20:34 -07:00
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2012-07-26 12:33:45 -07:00
|
|
|
#include "mozilla/StaticPtr.h"
|
2011-06-19 20:00:16 -07:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2012-03-30 21:42:20 -07:00
|
|
|
using namespace mozilla::dom;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
static NS_DEFINE_CID(kZipReaderCID, NS_ZIPREADER_CID);
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
nsIIOService *nsScriptSecurityManager::sIOService = nullptr;
|
|
|
|
nsIXPConnect *nsScriptSecurityManager::sXPConnect = nullptr;
|
|
|
|
nsIThreadJSContextStack *nsScriptSecurityManager::sJSContextStack = nullptr;
|
|
|
|
nsIStringBundle *nsScriptSecurityManager::sStrBundle = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSRuntime *nsScriptSecurityManager::sRuntime = 0;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool nsScriptSecurityManager::sStrictFileOriginPolicy = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
///////////////////////////
|
|
|
|
// Convenience Functions //
|
|
|
|
///////////////////////////
|
|
|
|
// Result of this function should not be freed.
|
|
|
|
static inline const PRUnichar *
|
2010-07-14 23:19:36 -07:00
|
|
|
IDToString(JSContext *cx, jsid id)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
if (JSID_IS_STRING(id))
|
2010-12-03 00:24:17 -08:00
|
|
|
return JS_GetInternedStringChars(JSID_TO_STRING(id));
|
2010-07-14 23:19:36 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
JSAutoRequest ar(cx);
|
2010-07-14 23:19:36 -07:00
|
|
|
jsval idval;
|
|
|
|
if (!JS_IdToValue(cx, id, &idval))
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSString *str = JS_ValueToString(cx, idval);
|
|
|
|
if(!str)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-12-03 00:24:17 -08:00
|
|
|
return JS_GetStringCharsZ(cx, str);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-05-20 18:49:42 -07:00
|
|
|
class nsAutoInPrincipalDomainOriginSetter {
|
|
|
|
public:
|
|
|
|
nsAutoInPrincipalDomainOriginSetter() {
|
|
|
|
++sInPrincipalDomainOrigin;
|
|
|
|
}
|
|
|
|
~nsAutoInPrincipalDomainOriginSetter() {
|
|
|
|
--sInPrincipalDomainOrigin;
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
static uint32_t sInPrincipalDomainOrigin;
|
2009-05-20 18:49:42 -07:00
|
|
|
};
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t nsAutoInPrincipalDomainOriginSetter::sInPrincipalDomainOrigin;
|
2009-05-20 18:49:42 -07:00
|
|
|
|
2008-07-28 23:37:58 -07:00
|
|
|
static
|
|
|
|
nsresult
|
2009-07-26 18:27:33 -07:00
|
|
|
GetOriginFromURI(nsIURI* aURI, nsACString& aOrigin)
|
2008-07-28 23:37:58 -07:00
|
|
|
{
|
2009-05-21 12:46:05 -07:00
|
|
|
if (nsAutoInPrincipalDomainOriginSetter::sInPrincipalDomainOrigin > 1) {
|
|
|
|
// Allow a single recursive call to GetPrincipalDomainOrigin, since that
|
|
|
|
// might be happening on a different principal from the first call. But
|
|
|
|
// after that, cut off the recursion; it just indicates that something
|
|
|
|
// we're doing in this method causes us to reenter a security check here.
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2009-05-20 18:49:42 -07:00
|
|
|
nsAutoInPrincipalDomainOriginSetter autoSetter;
|
2008-07-28 23:37:58 -07:00
|
|
|
|
2009-07-26 18:27:33 -07:00
|
|
|
nsCOMPtr<nsIURI> uri = NS_GetInnermostURI(aURI);
|
2008-07-28 23:37:58 -07:00
|
|
|
NS_ENSURE_TRUE(uri, NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
nsCAutoString hostPort;
|
|
|
|
|
|
|
|
nsresult rv = uri->GetHostPort(hostPort);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsCAutoString scheme;
|
|
|
|
rv = uri->GetScheme(scheme);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
aOrigin = scheme + NS_LITERAL_CSTRING("://") + hostPort;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Some URIs (e.g., nsSimpleURI) don't support host. Just
|
|
|
|
// get the full spec.
|
|
|
|
rv = uri->GetSpec(aOrigin);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-07-26 18:27:33 -07:00
|
|
|
static
|
|
|
|
nsresult
|
|
|
|
GetPrincipalDomainOrigin(nsIPrincipal* aPrincipal,
|
|
|
|
nsACString& aOrigin)
|
|
|
|
{
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
aPrincipal->GetDomain(getter_AddRefs(uri));
|
|
|
|
if (!uri) {
|
|
|
|
aPrincipal->GetURI(getter_AddRefs(uri));
|
|
|
|
}
|
|
|
|
NS_ENSURE_TRUE(uri, NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
return GetOriginFromURI(uri, aOrigin);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static nsIScriptContext *
|
|
|
|
GetScriptContext(JSContext *cx)
|
|
|
|
{
|
|
|
|
return GetScriptContextFromJSContext(cx);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void SetPendingException(JSContext *cx, const char *aMsg)
|
|
|
|
{
|
|
|
|
JSAutoRequest ar(cx);
|
2008-03-20 01:19:15 -07:00
|
|
|
JS_ReportError(cx, "%s", aMsg);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void SetPendingException(JSContext *cx, const PRUnichar *aMsg)
|
|
|
|
{
|
|
|
|
JSAutoRequest ar(cx);
|
2008-03-20 01:19:15 -07:00
|
|
|
JS_ReportError(cx, "%hs", aMsg);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// DomainPolicy members
|
|
|
|
#ifdef DEBUG_CAPS_DomainPolicyLifeCycle
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t DomainPolicy::sObjects=0;
|
2007-03-22 10:30:00 -07:00
|
|
|
void DomainPolicy::_printPopulationInfo()
|
|
|
|
{
|
|
|
|
printf("CAPS.DomainPolicy: Gen. %d, %d DomainPolicy objects.\n",
|
|
|
|
sGeneration, sObjects);
|
|
|
|
}
|
|
|
|
#endif
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t DomainPolicy::sGeneration = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Helper class to get stuff from the ClassInfo and not waste extra time with
|
|
|
|
// virtual method calls for things it has already gotten
|
|
|
|
class ClassInfoData
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ClassInfoData(nsIClassInfo *aClassInfo, const char *aName)
|
|
|
|
: mClassInfo(aClassInfo),
|
2007-07-08 00:08:04 -07:00
|
|
|
mName(const_cast<char *>(aName)),
|
2011-10-17 07:59:28 -07:00
|
|
|
mDidGetFlags(false),
|
|
|
|
mMustFreeName(false)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~ClassInfoData()
|
|
|
|
{
|
|
|
|
if (mMustFreeName)
|
|
|
|
nsMemory::Free(mName);
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t GetFlags()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (!mDidGetFlags) {
|
|
|
|
if (mClassInfo) {
|
|
|
|
nsresult rv = mClassInfo->GetFlags(&mFlags);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mFlags = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mFlags = 0;
|
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
mDidGetFlags = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return mFlags;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool IsDOMClass()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-11-12 13:47:11 -08:00
|
|
|
return !!(GetFlags() & nsIClassInfo::DOM_OBJECT);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* GetName()
|
|
|
|
{
|
|
|
|
if (!mName) {
|
|
|
|
if (mClassInfo) {
|
|
|
|
mClassInfo->GetClassDescription(&mName);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mName) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mMustFreeName = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2007-07-08 00:08:04 -07:00
|
|
|
mName = const_cast<char *>("UnnamedClass");
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mName;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsIClassInfo *mClassInfo; // WEAK
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mFlags;
|
2007-03-22 10:30:00 -07:00
|
|
|
char *mName;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mDidGetFlags;
|
|
|
|
bool mMustFreeName;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
2008-10-08 15:05:25 -07:00
|
|
|
|
|
|
|
class AutoCxPusher {
|
|
|
|
public:
|
|
|
|
AutoCxPusher(nsIJSContextStack *aStack, JSContext *cx)
|
|
|
|
: mStack(aStack), mContext(cx)
|
|
|
|
{
|
|
|
|
if (NS_FAILED(mStack->Push(mContext))) {
|
2012-07-30 07:20:58 -07:00
|
|
|
mStack = nullptr;
|
2008-10-08 15:05:25 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~AutoCxPusher()
|
|
|
|
{
|
|
|
|
if (mStack) {
|
2012-07-30 07:20:58 -07:00
|
|
|
mStack->Pop(nullptr);
|
2008-10-08 15:05:25 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIJSContextStack> mStack;
|
|
|
|
JSContext *mContext;
|
|
|
|
};
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
JSContext *
|
|
|
|
nsScriptSecurityManager::GetCurrentJSContext()
|
|
|
|
{
|
|
|
|
// Get JSContext from stack.
|
|
|
|
JSContext *cx;
|
2008-10-14 07:16:25 -07:00
|
|
|
if (NS_FAILED(sJSContextStack->Peek(&cx)))
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
return cx;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSContext *
|
|
|
|
nsScriptSecurityManager::GetSafeJSContext()
|
|
|
|
{
|
|
|
|
// Get JSContext from stack.
|
2012-05-05 02:00:05 -07:00
|
|
|
return sJSContextStack->GetSafeJSContext();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-02-26 19:45:29 -08:00
|
|
|
/* static */
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsScriptSecurityManager::SecurityCompareURIs(nsIURI* aSourceURI,
|
|
|
|
nsIURI* aTargetURI)
|
|
|
|
{
|
2008-08-27 18:15:32 -07:00
|
|
|
return NS_SecurityCompareURIs(aSourceURI, aTargetURI, sStrictFileOriginPolicy);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-10-08 06:16:27 -07:00
|
|
|
// SecurityHashURI is consistent with SecurityCompareURIs because NS_SecurityHashURI
|
|
|
|
// is consistent with NS_SecurityCompareURIs. See nsNetUtil.h.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t
|
2008-10-08 06:16:27 -07:00
|
|
|
nsScriptSecurityManager::SecurityHashURI(nsIURI* aURI)
|
|
|
|
{
|
|
|
|
return NS_SecurityHashURI(aURI);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScriptSecurityManager::GetChannelPrincipal(nsIChannel* aChannel,
|
|
|
|
nsIPrincipal** aPrincipal)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aChannel, "Must have channel!");
|
|
|
|
nsCOMPtr<nsISupports> owner;
|
|
|
|
aChannel->GetOwner(getter_AddRefs(owner));
|
|
|
|
if (owner) {
|
|
|
|
CallQueryInterface(owner, aPrincipal);
|
|
|
|
if (*aPrincipal) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OK, get the principal from the URI. Make sure this does the same thing
|
|
|
|
// as nsDocument::Reset and nsXULDocument::StartDocumentLoad.
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
2007-10-23 14:56:41 -07:00
|
|
|
nsresult rv = NS_GetFinalChannelURI(aChannel, getter_AddRefs(uri));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t appId = UNKNOWN_APP_ID;
|
2012-07-20 00:07:49 -07:00
|
|
|
bool isInBrowserElement = false;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDocShell> docShell;
|
|
|
|
NS_QueryNotificationCallbacks(aChannel, docShell);
|
|
|
|
|
|
|
|
if (docShell) {
|
|
|
|
docShell->GetAppId(&appId);
|
|
|
|
docShell->GetIsInBrowserElement(&isInBrowserElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetCodebasePrincipalInternal(uri, appId, isInBrowserElement,
|
|
|
|
aPrincipal);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-06-18 08:12:09 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScriptSecurityManager::IsSystemPrincipal(nsIPrincipal* aPrincipal,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool* aIsSystem)
|
2007-06-18 08:12:09 -07:00
|
|
|
{
|
|
|
|
*aIsSystem = (aPrincipal == mSystemPrincipal);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-01-04 15:59:12 -08:00
|
|
|
NS_IMETHODIMP_(nsIPrincipal *)
|
|
|
|
nsScriptSecurityManager::GetCxSubjectPrincipal(JSContext *cx)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(cx == GetCurrentJSContext(),
|
|
|
|
"Uh, cx is not the current JS context!");
|
|
|
|
|
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
|
|
|
nsIPrincipal *principal = GetSubjectPrincipal(cx, &rv);
|
|
|
|
if (NS_FAILED(rv))
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2008-01-04 15:59:12 -08:00
|
|
|
|
|
|
|
return principal;
|
|
|
|
}
|
|
|
|
|
2008-10-22 13:15:22 -07:00
|
|
|
NS_IMETHODIMP_(nsIPrincipal *)
|
|
|
|
nsScriptSecurityManager::GetCxSubjectPrincipalAndFrame(JSContext *cx, JSStackFrame **fp)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(cx == GetCurrentJSContext(),
|
|
|
|
"Uh, cx is not the current JS context!");
|
|
|
|
|
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
|
|
|
nsIPrincipal *principal = GetPrincipalAndFrame(cx, fp, &rv);
|
|
|
|
if (NS_FAILED(rv))
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2008-10-22 13:15:22 -07:00
|
|
|
|
|
|
|
return principal;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
////////////////////
|
|
|
|
// Policy Storage //
|
|
|
|
////////////////////
|
|
|
|
|
|
|
|
// Table of security levels
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool
|
2007-03-22 10:30:00 -07:00
|
|
|
DeleteCapability(nsHashKey *aKey, void *aData, void* closure)
|
|
|
|
{
|
|
|
|
NS_Free(aData);
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//-- Per-Domain Policy - applies to one or more protocols or hosts
|
|
|
|
struct DomainEntry
|
|
|
|
{
|
|
|
|
DomainEntry(const char* aOrigin,
|
|
|
|
DomainPolicy* aDomainPolicy) : mOrigin(aOrigin),
|
|
|
|
mDomainPolicy(aDomainPolicy),
|
2012-07-30 07:20:58 -07:00
|
|
|
mNext(nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
mDomainPolicy->Hold();
|
|
|
|
}
|
|
|
|
|
|
|
|
~DomainEntry()
|
|
|
|
{
|
|
|
|
mDomainPolicy->Drop();
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool Matches(const char *anOrigin)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
int len = strlen(anOrigin);
|
|
|
|
int thisLen = mOrigin.Length();
|
|
|
|
if (len < thisLen)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (mOrigin.RFindChar(':', thisLen-1, 1) != -1)
|
|
|
|
//-- Policy applies to all URLs of this scheme, compare scheme only
|
|
|
|
return mOrigin.EqualsIgnoreCase(anOrigin, thisLen);
|
|
|
|
|
|
|
|
//-- Policy applies to a particular host; compare domains
|
|
|
|
if (!mOrigin.Equals(anOrigin + (len - thisLen)))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (len == thisLen)
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
char charBefore = anOrigin[len-thisLen-1];
|
|
|
|
return (charBefore == '.' || charBefore == ':' || charBefore == '/');
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCString mOrigin;
|
|
|
|
DomainPolicy* mDomainPolicy;
|
|
|
|
DomainEntry* mNext;
|
|
|
|
#if defined(DEBUG) || defined(DEBUG_CAPS_HACKER)
|
|
|
|
nsCString mPolicyName_DEBUG;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool
|
2007-03-22 10:30:00 -07:00
|
|
|
DeleteDomainEntry(nsHashKey *aKey, void *aData, void* closure)
|
|
|
|
{
|
|
|
|
DomainEntry *entry = (DomainEntry*) aData;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
DomainEntry *next = entry->mNext;
|
|
|
|
delete entry;
|
|
|
|
entry = next;
|
|
|
|
} while (entry);
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////
|
|
|
|
// nsScriptSecurityManager //
|
|
|
|
/////////////////////////////
|
|
|
|
|
|
|
|
////////////////////////////////////
|
|
|
|
// Methods implementing ISupports //
|
|
|
|
////////////////////////////////////
|
2010-06-08 16:43:54 -07:00
|
|
|
NS_IMPL_ISUPPORTS4(nsScriptSecurityManager,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIScriptSecurityManager,
|
|
|
|
nsIXPCSecurityManager,
|
|
|
|
nsIChannelEventSink,
|
|
|
|
nsIObserver)
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////
|
|
|
|
// Methods implementing nsIScriptSecurityManager //
|
|
|
|
///////////////////////////////////////////////////
|
|
|
|
|
|
|
|
///////////////// Security Checks /////////////////
|
2012-03-09 01:48:50 -08:00
|
|
|
|
|
|
|
/* static */ JSPrincipals *
|
|
|
|
nsScriptSecurityManager::ObjectPrincipalFinder(JSObject *aObj)
|
|
|
|
{
|
|
|
|
return nsJSPrincipals::get(doGetObjectPrincipal(aObj));
|
|
|
|
}
|
|
|
|
|
2010-03-08 00:24:50 -08:00
|
|
|
JSBool
|
|
|
|
nsScriptSecurityManager::ContentSecurityPolicyPermitsJSAction(JSContext *cx)
|
|
|
|
{
|
|
|
|
// Get the security manager
|
|
|
|
nsScriptSecurityManager *ssm =
|
|
|
|
nsScriptSecurityManager::GetScriptSecurityManager();
|
|
|
|
|
|
|
|
NS_ASSERTION(ssm, "Failed to get security manager service");
|
|
|
|
if (!ssm)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsIPrincipal* subjectPrincipal = ssm->GetSubjectPrincipal(cx, &rv);
|
|
|
|
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "CSP: Failed to get nsIPrincipal from js context");
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return JS_FALSE; // Not just absence of principal, but failure.
|
|
|
|
|
2010-03-18 17:27:39 -07:00
|
|
|
if (!subjectPrincipal) {
|
|
|
|
// See bug 553448 for discussion of this case.
|
2012-03-09 01:48:50 -08:00
|
|
|
NS_ASSERTION(!JS_GetSecurityCallbacks(js::GetRuntime(cx))->findObjectPrincipals,
|
2010-03-18 17:27:39 -07:00
|
|
|
"CSP: Should have been able to find subject principal. "
|
|
|
|
"Reluctantly granting access.");
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2010-03-08 00:24:50 -08:00
|
|
|
|
|
|
|
nsCOMPtr<nsIContentSecurityPolicy> csp;
|
|
|
|
rv = subjectPrincipal->GetCsp(getter_AddRefs(csp));
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "CSP: Failed to get CSP from principal.");
|
|
|
|
|
|
|
|
// don't do anything unless there's a CSP
|
|
|
|
if (!csp)
|
|
|
|
return JS_TRUE;
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool evalOK = true;
|
2010-03-08 00:24:50 -08:00
|
|
|
rv = csp->GetAllowsEval(&evalOK);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
{
|
|
|
|
NS_WARNING("CSP: failed to get allowsEval");
|
|
|
|
return JS_TRUE; // fail open to not break sites.
|
|
|
|
}
|
|
|
|
|
2011-01-31 10:09:44 -08:00
|
|
|
if (!evalOK) {
|
|
|
|
// get the script filename, script sample, and line number
|
|
|
|
// to log with the violation
|
|
|
|
nsAutoString fileName;
|
2012-04-16 12:30:00 -07:00
|
|
|
unsigned lineNum = 0;
|
2011-01-31 10:09:44 -08:00
|
|
|
NS_NAMED_LITERAL_STRING(scriptSample, "call to eval() or related function blocked by CSP");
|
|
|
|
|
2012-04-16 12:30:00 -07:00
|
|
|
JSScript *script;
|
|
|
|
if (JS_DescribeScriptedCaller(cx, &script, &lineNum)) {
|
|
|
|
if (const char *file = JS_GetScriptFilename(cx, script)) {
|
|
|
|
CopyUTF8toUTF16(nsDependentCString(file), fileName);
|
2011-01-31 10:09:44 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
csp->LogViolationDetails(nsIContentSecurityPolicy::VIOLATION_TYPE_EVAL,
|
|
|
|
fileName,
|
|
|
|
scriptSample,
|
|
|
|
lineNum);
|
|
|
|
}
|
|
|
|
|
2010-03-08 00:24:50 -08:00
|
|
|
return evalOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-06 15:21:43 -07:00
|
|
|
JSBool
|
2012-05-19 15:03:45 -07:00
|
|
|
nsScriptSecurityManager::CheckObjectAccess(JSContext *cx, JSHandleObject obj,
|
|
|
|
JSHandleId id, JSAccessMode mode,
|
2007-03-22 10:30:00 -07:00
|
|
|
jsval *vp)
|
|
|
|
{
|
|
|
|
// Get the security manager
|
|
|
|
nsScriptSecurityManager *ssm =
|
|
|
|
nsScriptSecurityManager::GetScriptSecurityManager();
|
|
|
|
|
|
|
|
NS_ASSERTION(ssm, "Failed to get security manager service");
|
|
|
|
if (!ssm)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
// Get the object being accessed. We protect these cases:
|
|
|
|
// 1. The Function.prototype.caller property's value, which might lead
|
|
|
|
// an attacker up a call-stack to a function or another object from
|
|
|
|
// a different trust domain.
|
|
|
|
// 2. A user-defined getter or setter function accessible on another
|
|
|
|
// trust domain's window or document object.
|
|
|
|
// *vp can be a primitive, in that case, we use obj as the target
|
|
|
|
// object.
|
|
|
|
JSObject* target = JSVAL_IS_PRIMITIVE(*vp) ? obj : JSVAL_TO_OBJECT(*vp);
|
|
|
|
|
|
|
|
// Do the same-origin check -- this sets a JS exception if the check fails.
|
|
|
|
// Pass the parent object's class name, as we have no class-info for it.
|
2007-08-30 17:52:58 -07:00
|
|
|
nsresult rv =
|
2011-10-04 07:06:54 -07:00
|
|
|
ssm->CheckPropertyAccess(cx, target, js::GetObjectClass(obj)->name, id,
|
2007-08-30 17:52:58 -07:00
|
|
|
(mode & JSACC_WRITE) ?
|
2012-08-22 08:56:38 -07:00
|
|
|
(int32_t)nsIXPCSecurityManager::ACCESS_SET_PROPERTY :
|
|
|
|
(int32_t)nsIXPCSecurityManager::ACCESS_GET_PROPERTY);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return JS_FALSE; // Security check failed (XXX was an error reported?)
|
|
|
|
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScriptSecurityManager::CheckPropertyAccess(JSContext* cx,
|
|
|
|
JSObject* aJSObject,
|
|
|
|
const char* aClassName,
|
2010-07-14 23:19:36 -07:00
|
|
|
jsid aProperty,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aAction)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
return CheckPropertyAccessImpl(aAction, nullptr, cx, aJSObject,
|
|
|
|
nullptr, nullptr,
|
|
|
|
aClassName, aProperty, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScriptSecurityManager::CheckSameOrigin(JSContext* cx,
|
|
|
|
nsIURI* aTargetURI)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// Get a context if necessary
|
|
|
|
if (!cx)
|
|
|
|
{
|
|
|
|
cx = GetCurrentJSContext();
|
|
|
|
if (!cx)
|
|
|
|
return NS_OK; // No JS context, so allow access
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a principal from the context
|
|
|
|
nsIPrincipal* sourcePrincipal = GetSubjectPrincipal(cx, &rv);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
if (!sourcePrincipal)
|
|
|
|
{
|
|
|
|
NS_WARNING("CheckSameOrigin called on script w/o principals; should this happen?");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sourcePrincipal == mSystemPrincipal)
|
|
|
|
{
|
|
|
|
// This is a system (chrome) script, so allow access
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the original URI from the source principal.
|
|
|
|
// This has the effect of ignoring any change to document.domain
|
|
|
|
// which must be done to avoid DNS spoofing (bug 154930)
|
|
|
|
nsCOMPtr<nsIURI> sourceURI;
|
|
|
|
sourcePrincipal->GetDomain(getter_AddRefs(sourceURI));
|
|
|
|
if (!sourceURI) {
|
|
|
|
sourcePrincipal->GetURI(getter_AddRefs(sourceURI));
|
|
|
|
NS_ENSURE_TRUE(sourceURI, NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare origins
|
|
|
|
if (!SecurityCompareURIs(sourceURI, aTargetURI))
|
|
|
|
{
|
|
|
|
ReportError(cx, NS_LITERAL_STRING("CheckSameOriginError"), sourceURI, aTargetURI);
|
2008-03-17 07:10:48 -07:00
|
|
|
return NS_ERROR_DOM_BAD_URI;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScriptSecurityManager::CheckSameOriginURI(nsIURI* aSourceURI,
|
2007-10-26 18:46:09 -07:00
|
|
|
nsIURI* aTargetURI,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool reportError)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (!SecurityCompareURIs(aSourceURI, aTargetURI))
|
|
|
|
{
|
2007-10-26 18:46:09 -07:00
|
|
|
if (reportError) {
|
2012-07-30 07:20:58 -07:00
|
|
|
ReportError(nullptr, NS_LITERAL_STRING("CheckSameOriginError"),
|
2007-03-22 10:30:00 -07:00
|
|
|
aSourceURI, aTargetURI);
|
2007-10-26 18:46:09 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_DOM_BAD_URI;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsScriptSecurityManager::CheckPropertyAccessImpl(uint32_t aAction,
|
2008-01-15 07:50:57 -08:00
|
|
|
nsAXPCNativeCallContext* aCallContext,
|
2007-03-22 10:30:00 -07:00
|
|
|
JSContext* cx, JSObject* aJSObject,
|
2012-07-17 22:28:47 -07:00
|
|
|
nsISupports* aObj,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIClassInfo* aClassInfo,
|
2010-07-14 23:19:36 -07:00
|
|
|
const char* aClassName, jsid aProperty,
|
2007-03-22 10:30:00 -07:00
|
|
|
void** aCachedClassPolicy)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsIPrincipal* subjectPrincipal = GetSubjectPrincipal(cx, &rv);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
if (!subjectPrincipal || subjectPrincipal == mSystemPrincipal)
|
|
|
|
// We have native code or the system principal: just allow access
|
|
|
|
return NS_OK;
|
|
|
|
|
2008-07-28 23:03:19 -07:00
|
|
|
nsCOMPtr<nsIPrincipal> objectPrincipal;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Hold the class info data here so we don't have to go back to virtual
|
|
|
|
// methods all the time
|
|
|
|
ClassInfoData classInfoData(aClassInfo, aClassName);
|
|
|
|
#ifdef DEBUG_CAPS_CheckPropertyAccessImpl
|
|
|
|
nsCAutoString propertyName;
|
2010-07-14 23:19:36 -07:00
|
|
|
propertyName.AssignWithConversion((PRUnichar*)IDToString(cx, aProperty));
|
2007-03-22 10:30:00 -07:00
|
|
|
printf("### CanAccess(%s.%s, %i) ", classInfoData.GetName(),
|
|
|
|
propertyName.get(), aAction);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//-- Look up the security policy for this class and subject domain
|
|
|
|
SecurityLevel securityLevel;
|
|
|
|
rv = LookupPolicy(subjectPrincipal, classInfoData, aProperty, aAction,
|
|
|
|
(ClassPolicy**)aCachedClassPolicy, &securityLevel);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
if (securityLevel.level == SCRIPT_SECURITY_UNDEFINED_ACCESS)
|
|
|
|
{
|
|
|
|
// No policy found for this property so use the default of last resort.
|
|
|
|
// If we were called from somewhere other than XPConnect
|
|
|
|
// (no XPC call context), assume this is a DOM class. Otherwise,
|
|
|
|
// ask the ClassInfo.
|
|
|
|
if (!aCallContext || classInfoData.IsDOMClass())
|
|
|
|
securityLevel.level = SCRIPT_SECURITY_SAME_ORIGIN_ACCESS;
|
|
|
|
else
|
|
|
|
securityLevel.level = SCRIPT_SECURITY_NO_ACCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SECURITY_ACCESS_LEVEL_FLAG(securityLevel))
|
|
|
|
// This flag means securityLevel is allAccess, noAccess, or sameOrigin
|
|
|
|
{
|
|
|
|
switch (securityLevel.level)
|
|
|
|
{
|
|
|
|
case SCRIPT_SECURITY_NO_ACCESS:
|
|
|
|
#ifdef DEBUG_CAPS_CheckPropertyAccessImpl
|
|
|
|
printf("noAccess ");
|
|
|
|
#endif
|
|
|
|
rv = NS_ERROR_DOM_PROP_ACCESS_DENIED;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SCRIPT_SECURITY_ALL_ACCESS:
|
|
|
|
#ifdef DEBUG_CAPS_CheckPropertyAccessImpl
|
|
|
|
printf("allAccess ");
|
|
|
|
#endif
|
|
|
|
rv = NS_OK;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SCRIPT_SECURITY_SAME_ORIGIN_ACCESS:
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_CAPS_CheckPropertyAccessImpl
|
|
|
|
printf("sameOrigin ");
|
|
|
|
#endif
|
2008-01-16 16:32:26 -08:00
|
|
|
nsCOMPtr<nsIPrincipal> principalHolder;
|
2007-03-22 10:30:00 -07:00
|
|
|
if(aJSObject)
|
|
|
|
{
|
2008-01-29 12:51:01 -08:00
|
|
|
objectPrincipal = doGetObjectPrincipal(aJSObject);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!objectPrincipal)
|
2008-01-04 17:32:23 -08:00
|
|
|
rv = NS_ERROR_DOM_SECURITY_ERR;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-04-18 10:35:55 -07:00
|
|
|
NS_ERROR("CheckPropertyAccessImpl called without a target object or URL");
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2008-01-04 17:32:23 -08:00
|
|
|
if(NS_SUCCEEDED(rv))
|
|
|
|
rv = CheckSameOriginDOMProp(subjectPrincipal, objectPrincipal,
|
2010-02-02 02:29:15 -08:00
|
|
|
aAction);
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
#ifdef DEBUG_CAPS_CheckPropertyAccessImpl
|
|
|
|
printf("ERROR ");
|
|
|
|
#endif
|
|
|
|
NS_ERROR("Bad Security Level Value");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else // if SECURITY_ACCESS_LEVEL_FLAG is false, securityLevel is a capability
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_CAPS_CheckPropertyAccessImpl
|
|
|
|
printf("Cap:%s ", securityLevel.capability);
|
|
|
|
#endif
|
2011-09-28 23:19:26 -07:00
|
|
|
bool capabilityEnabled = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = IsCapabilityEnabled(securityLevel.capability, &capabilityEnabled);
|
|
|
|
if (NS_FAILED(rv) || !capabilityEnabled)
|
|
|
|
rv = NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
else
|
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_CAPS_CheckPropertyAccessImpl
|
|
|
|
printf(" GRANTED.\n");
|
|
|
|
#endif
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--See if the object advertises a non-default level of access
|
|
|
|
// using nsISecurityCheckedComponent
|
|
|
|
nsCOMPtr<nsISecurityCheckedComponent> checkedComponent =
|
|
|
|
do_QueryInterface(aObj);
|
|
|
|
|
|
|
|
nsXPIDLCString objectSecurityLevel;
|
|
|
|
if (checkedComponent)
|
|
|
|
{
|
2007-08-30 17:52:58 -07:00
|
|
|
nsCOMPtr<nsIXPConnectWrappedNative> wrapper;
|
|
|
|
nsCOMPtr<nsIInterfaceInfo> interfaceInfo;
|
2012-07-30 07:20:58 -07:00
|
|
|
const nsIID* objIID = nullptr;
|
2007-08-30 17:52:58 -07:00
|
|
|
rv = aCallContext->GetCalleeWrapper(getter_AddRefs(wrapper));
|
2010-03-22 15:50:04 -07:00
|
|
|
if (NS_SUCCEEDED(rv) && wrapper)
|
2007-08-30 17:52:58 -07:00
|
|
|
rv = wrapper->FindInterfaceWithMember(aProperty, getter_AddRefs(interfaceInfo));
|
2010-03-22 15:50:04 -07:00
|
|
|
if (NS_SUCCEEDED(rv) && interfaceInfo)
|
2007-08-30 17:52:58 -07:00
|
|
|
rv = interfaceInfo->GetIIDShared(&objIID);
|
2010-03-22 15:50:04 -07:00
|
|
|
if (NS_SUCCEEDED(rv) && objIID)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
switch (aAction)
|
|
|
|
{
|
|
|
|
case nsIXPCSecurityManager::ACCESS_GET_PROPERTY:
|
|
|
|
checkedComponent->CanGetProperty(objIID,
|
2010-07-14 23:19:36 -07:00
|
|
|
IDToString(cx, aProperty),
|
2007-03-22 10:30:00 -07:00
|
|
|
getter_Copies(objectSecurityLevel));
|
|
|
|
break;
|
|
|
|
case nsIXPCSecurityManager::ACCESS_SET_PROPERTY:
|
|
|
|
checkedComponent->CanSetProperty(objIID,
|
2010-07-14 23:19:36 -07:00
|
|
|
IDToString(cx, aProperty),
|
2007-03-22 10:30:00 -07:00
|
|
|
getter_Copies(objectSecurityLevel));
|
|
|
|
break;
|
|
|
|
case nsIXPCSecurityManager::ACCESS_CALL_METHOD:
|
|
|
|
checkedComponent->CanCallMethod(objIID,
|
2010-07-14 23:19:36 -07:00
|
|
|
IDToString(cx, aProperty),
|
2007-03-22 10:30:00 -07:00
|
|
|
getter_Copies(objectSecurityLevel));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-08-06 20:26:33 -07:00
|
|
|
rv = CheckXPCPermissions(cx, aObj, aJSObject, subjectPrincipal,
|
2009-05-13 15:01:01 -07:00
|
|
|
objectSecurityLevel);
|
2007-03-22 10:30:00 -07:00
|
|
|
#ifdef DEBUG_CAPS_CheckPropertyAccessImpl
|
|
|
|
if(NS_SUCCEEDED(rv))
|
|
|
|
printf("CheckXPCPerms GRANTED.\n");
|
|
|
|
else
|
|
|
|
printf("CheckXPCPerms DENIED.\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) //-- Security tests failed, access is denied, report error
|
|
|
|
{
|
|
|
|
nsAutoString stringName;
|
|
|
|
switch(aAction)
|
|
|
|
{
|
|
|
|
case nsIXPCSecurityManager::ACCESS_GET_PROPERTY:
|
2008-07-28 23:03:19 -07:00
|
|
|
stringName.AssignLiteral("GetPropertyDeniedOrigins");
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
case nsIXPCSecurityManager::ACCESS_SET_PROPERTY:
|
2008-07-28 23:03:19 -07:00
|
|
|
stringName.AssignLiteral("SetPropertyDeniedOrigins");
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
case nsIXPCSecurityManager::ACCESS_CALL_METHOD:
|
2008-07-28 23:03:19 -07:00
|
|
|
stringName.AssignLiteral("CallMethodDeniedOrigins");
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-12-22 16:55:42 -08:00
|
|
|
// Null out objectPrincipal for now, so we don't leak information about
|
|
|
|
// it. Whenever we can report different error strings to content and
|
|
|
|
// the UI we can take this out again.
|
2012-07-30 07:20:58 -07:00
|
|
|
objectPrincipal = nullptr;
|
2010-12-22 16:55:42 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ConvertUTF8toUTF16 className(classInfoData.GetName());
|
2008-07-28 23:03:19 -07:00
|
|
|
nsCAutoString subjectOrigin;
|
2009-07-26 18:27:33 -07:00
|
|
|
nsCAutoString subjectDomain;
|
2009-05-20 18:49:42 -07:00
|
|
|
if (!nsAutoInPrincipalDomainOriginSetter::sInPrincipalDomainOrigin) {
|
2009-07-26 18:27:33 -07:00
|
|
|
nsCOMPtr<nsIURI> uri, domain;
|
|
|
|
subjectPrincipal->GetURI(getter_AddRefs(uri));
|
2012-08-20 11:22:34 -07:00
|
|
|
if (uri) { // Object principal might be expanded
|
|
|
|
GetOriginFromURI(uri, subjectOrigin);
|
|
|
|
}
|
2009-07-26 18:27:33 -07:00
|
|
|
subjectPrincipal->GetDomain(getter_AddRefs(domain));
|
|
|
|
if (domain) {
|
|
|
|
GetOriginFromURI(domain, subjectDomain);
|
|
|
|
}
|
2009-05-20 18:49:42 -07:00
|
|
|
} else {
|
|
|
|
subjectOrigin.AssignLiteral("the security manager");
|
|
|
|
}
|
2008-07-28 23:03:19 -07:00
|
|
|
NS_ConvertUTF8toUTF16 subjectOriginUnicode(subjectOrigin);
|
2009-07-26 18:27:33 -07:00
|
|
|
NS_ConvertUTF8toUTF16 subjectDomainUnicode(subjectDomain);
|
2008-07-28 23:03:19 -07:00
|
|
|
|
|
|
|
nsCAutoString objectOrigin;
|
2009-07-26 18:27:33 -07:00
|
|
|
nsCAutoString objectDomain;
|
2009-05-20 18:49:42 -07:00
|
|
|
if (!nsAutoInPrincipalDomainOriginSetter::sInPrincipalDomainOrigin &&
|
|
|
|
objectPrincipal) {
|
2009-07-26 18:27:33 -07:00
|
|
|
nsCOMPtr<nsIURI> uri, domain;
|
|
|
|
objectPrincipal->GetURI(getter_AddRefs(uri));
|
|
|
|
if (uri) { // Object principal might be system
|
|
|
|
GetOriginFromURI(uri, objectOrigin);
|
|
|
|
}
|
|
|
|
objectPrincipal->GetDomain(getter_AddRefs(domain));
|
|
|
|
if (domain) {
|
|
|
|
GetOriginFromURI(domain, objectDomain);
|
|
|
|
}
|
2008-07-28 23:03:19 -07:00
|
|
|
}
|
|
|
|
NS_ConvertUTF8toUTF16 objectOriginUnicode(objectOrigin);
|
2009-07-26 18:27:33 -07:00
|
|
|
NS_ConvertUTF8toUTF16 objectDomainUnicode(objectDomain);
|
|
|
|
|
2008-07-28 23:03:19 -07:00
|
|
|
nsXPIDLString errorMsg;
|
2007-03-22 10:30:00 -07:00
|
|
|
const PRUnichar *formatStrings[] =
|
|
|
|
{
|
2008-07-28 23:03:19 -07:00
|
|
|
subjectOriginUnicode.get(),
|
2007-03-22 10:30:00 -07:00
|
|
|
className.get(),
|
2010-07-14 23:19:36 -07:00
|
|
|
IDToString(cx, aProperty),
|
2009-07-26 18:27:33 -07:00
|
|
|
objectOriginUnicode.get(),
|
|
|
|
subjectDomainUnicode.get(),
|
|
|
|
objectDomainUnicode.get()
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t length = ArrayLength(formatStrings);
|
2008-07-28 23:03:19 -07:00
|
|
|
|
2009-07-26 18:27:33 -07:00
|
|
|
// XXXbz Our localization system is stupid and can't handle not showing
|
|
|
|
// some strings that get passed in. Which means that we have to get
|
|
|
|
// our length precisely right: it has to be exactly the number of
|
|
|
|
// strings our format string wants. This means we'll have to move
|
|
|
|
// strings in the array as needed, sadly...
|
2009-05-20 18:49:42 -07:00
|
|
|
if (nsAutoInPrincipalDomainOriginSetter::sInPrincipalDomainOrigin ||
|
|
|
|
!objectPrincipal) {
|
2008-07-28 23:03:19 -07:00
|
|
|
stringName.AppendLiteral("OnlySubject");
|
2009-07-26 18:27:33 -07:00
|
|
|
length -= 3;
|
|
|
|
} else {
|
|
|
|
// default to a length that doesn't include the domains, then
|
|
|
|
// increase it as needed.
|
|
|
|
length -= 2;
|
|
|
|
if (!subjectDomainUnicode.IsEmpty()) {
|
|
|
|
stringName.AppendLiteral("SubjectDomain");
|
|
|
|
length += 1;
|
|
|
|
}
|
|
|
|
if (!objectDomainUnicode.IsEmpty()) {
|
|
|
|
stringName.AppendLiteral("ObjectDomain");
|
|
|
|
length += 1;
|
2011-10-10 22:50:08 -07:00
|
|
|
if (length != ArrayLength(formatStrings)) {
|
2009-07-26 18:27:33 -07:00
|
|
|
// We have an object domain but not a subject domain.
|
|
|
|
// Scoot our string over one slot. See the XXX comment
|
|
|
|
// above for why we need to do this.
|
|
|
|
formatStrings[length-1] = formatStrings[length];
|
|
|
|
}
|
|
|
|
}
|
2008-07-28 23:03:19 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// We need to keep our existing failure rv and not override it
|
|
|
|
// with a likely success code from the following string bundle
|
|
|
|
// call in order to throw the correct security exception later.
|
|
|
|
nsresult rv2 = sStrBundle->FormatStringFromName(stringName.get(),
|
|
|
|
formatStrings,
|
2008-07-28 23:03:19 -07:00
|
|
|
length,
|
2007-03-22 10:30:00 -07:00
|
|
|
getter_Copies(errorMsg));
|
2008-07-28 23:03:19 -07:00
|
|
|
if (NS_FAILED(rv2)) {
|
|
|
|
// Might just be missing the string... Do our best
|
|
|
|
errorMsg = stringName;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
SetPendingException(cx, errorMsg.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2008-03-18 14:14:49 -07:00
|
|
|
/* static */
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult
|
2008-03-18 14:14:49 -07:00
|
|
|
nsScriptSecurityManager::CheckSameOriginPrincipal(nsIPrincipal* aSubject,
|
2010-02-02 02:29:15 -08:00
|
|
|
nsIPrincipal* aObject)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
** Get origin of subject and object and compare.
|
|
|
|
*/
|
|
|
|
if (aSubject == aObject)
|
|
|
|
return NS_OK;
|
|
|
|
|
2010-02-02 02:29:15 -08:00
|
|
|
// Default to false, and change if that turns out wrong.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool subjectSetDomain = false;
|
|
|
|
bool objectSetDomain = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> subjectURI;
|
|
|
|
nsCOMPtr<nsIURI> objectURI;
|
|
|
|
|
2010-02-02 02:29:15 -08:00
|
|
|
aSubject->GetDomain(getter_AddRefs(subjectURI));
|
|
|
|
if (!subjectURI) {
|
2007-03-22 10:30:00 -07:00
|
|
|
aSubject->GetURI(getter_AddRefs(subjectURI));
|
2010-02-02 02:29:15 -08:00
|
|
|
} else {
|
2011-10-17 07:59:28 -07:00
|
|
|
subjectSetDomain = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-02-02 02:29:15 -08:00
|
|
|
aObject->GetDomain(getter_AddRefs(objectURI));
|
|
|
|
if (!objectURI) {
|
|
|
|
aObject->GetURI(getter_AddRefs(objectURI));
|
|
|
|
} else {
|
2011-10-17 07:59:28 -07:00
|
|
|
objectSetDomain = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (SecurityCompareURIs(subjectURI, objectURI))
|
|
|
|
{ // If either the subject or the object has changed its principal by
|
|
|
|
// explicitly setting document.domain then the other must also have
|
|
|
|
// done so in order to be considered the same origin. This prevents
|
|
|
|
// DNS spoofing based on document.domain (154930)
|
|
|
|
|
|
|
|
// If both or neither explicitly set their domain, allow the access
|
|
|
|
if (subjectSetDomain == objectSetDomain)
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Access tests failed, so now report error.
|
|
|
|
*/
|
|
|
|
return NS_ERROR_DOM_PROP_ACCESS_DENIED;
|
|
|
|
}
|
|
|
|
|
2008-10-08 06:16:27 -07:00
|
|
|
// It's important that
|
|
|
|
//
|
2010-02-02 02:29:15 -08:00
|
|
|
// CheckSameOriginPrincipal(A, B) == NS_OK
|
2008-10-08 06:16:27 -07:00
|
|
|
//
|
|
|
|
// imply
|
|
|
|
//
|
|
|
|
// HashPrincipalByOrigin(A) == HashPrincipalByOrigin(B)
|
|
|
|
//
|
|
|
|
// if principals A and B could ever be used as keys in a hashtable.
|
|
|
|
// Violation of this invariant leads to spurious failures of hashtable
|
|
|
|
// lookups. See bug 454850.
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
/*static*/ uint32_t
|
2008-10-08 06:16:27 -07:00
|
|
|
nsScriptSecurityManager::HashPrincipalByOrigin(nsIPrincipal* aPrincipal)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
aPrincipal->GetDomain(getter_AddRefs(uri));
|
|
|
|
if (!uri)
|
|
|
|
aPrincipal->GetURI(getter_AddRefs(uri));
|
|
|
|
return SecurityHashURI(uri);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsScriptSecurityManager::CheckSameOriginDOMProp(nsIPrincipal* aSubject,
|
|
|
|
nsIPrincipal* aObject,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aAction)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-03-18 14:14:49 -07:00
|
|
|
nsresult rv;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool subsumes;
|
2010-02-02 02:29:15 -08:00
|
|
|
rv = aSubject->Subsumes(aObject, &subsumes);
|
|
|
|
if (NS_SUCCEEDED(rv) && !subsumes) {
|
|
|
|
rv = NS_ERROR_DOM_PROP_ACCESS_DENIED;
|
2008-03-18 14:14:49 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Content can't ever touch chrome (we check for UniversalXPConnect later)
|
|
|
|
*/
|
|
|
|
if (aObject == mSystemPrincipal)
|
|
|
|
return NS_ERROR_DOM_PROP_ACCESS_DENIED;
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Access tests failed, so now report error.
|
|
|
|
*/
|
|
|
|
return NS_ERROR_DOM_PROP_ACCESS_DENIED;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsScriptSecurityManager::LookupPolicy(nsIPrincipal* aPrincipal,
|
|
|
|
ClassInfoData& aClassData,
|
2010-07-14 23:19:36 -07:00
|
|
|
jsid aProperty,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aAction,
|
2007-03-22 10:30:00 -07:00
|
|
|
ClassPolicy** aCachedClassPolicy,
|
|
|
|
SecurityLevel* result)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
result->level = SCRIPT_SECURITY_UNDEFINED_ACCESS;
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
DomainPolicy* dpolicy = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
//-- Initialize policies if necessary
|
|
|
|
if (mPolicyPrefsChanged)
|
|
|
|
{
|
2011-06-19 20:00:16 -07:00
|
|
|
if (!mPrefInitialized) {
|
2009-01-07 20:42:15 -08:00
|
|
|
rv = InitPrefs();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = InitPolicies();
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
aPrincipal->GetSecurityPolicy((void**)&dpolicy);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dpolicy && mOriginToPolicyMap)
|
|
|
|
{
|
|
|
|
//-- Look up the relevant domain policy, if any
|
|
|
|
#ifdef DEBUG_CAPS_LookupPolicy
|
|
|
|
printf("DomainLookup ");
|
|
|
|
#endif
|
2012-08-20 11:22:34 -07:00
|
|
|
if (nsCOMPtr<nsIExpandedPrincipal> exp = do_QueryInterface(aPrincipal))
|
|
|
|
{
|
|
|
|
// For expanded principals domain origin is not defined so let's just
|
|
|
|
// use the default policy
|
|
|
|
dpolicy = mDefaultPolicy;
|
|
|
|
}
|
|
|
|
else
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-20 11:22:34 -07:00
|
|
|
nsCAutoString origin;
|
|
|
|
rv = GetPrincipalDomainOrigin(aPrincipal, origin);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
char *start = origin.BeginWriting();
|
|
|
|
const char *nextToLastDot = nullptr;
|
|
|
|
const char *lastDot = nullptr;
|
|
|
|
const char *colon = nullptr;
|
|
|
|
char *p = start;
|
|
|
|
|
|
|
|
//-- search domain (stop at the end of the string or at the 3rd slash)
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t slashes=0; *p; p++)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-20 11:22:34 -07:00
|
|
|
if (*p == '/' && ++slashes == 3)
|
|
|
|
{
|
|
|
|
*p = '\0'; // truncate at 3rd slash
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (*p == '.')
|
|
|
|
{
|
|
|
|
nextToLastDot = lastDot;
|
|
|
|
lastDot = p;
|
|
|
|
}
|
|
|
|
else if (!colon && *p == ':')
|
|
|
|
colon = p;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-20 11:22:34 -07:00
|
|
|
nsCStringKey key(nextToLastDot ? nextToLastDot+1 : start);
|
|
|
|
DomainEntry *de = (DomainEntry*) mOriginToPolicyMap->Get(&key);
|
|
|
|
if (!de)
|
|
|
|
{
|
|
|
|
nsCAutoString scheme(start, colon-start+1);
|
|
|
|
nsCStringKey schemeKey(scheme);
|
|
|
|
de = (DomainEntry*) mOriginToPolicyMap->Get(&schemeKey);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-20 11:22:34 -07:00
|
|
|
while (de)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-20 11:22:34 -07:00
|
|
|
if (de->Matches(start))
|
|
|
|
{
|
|
|
|
dpolicy = de->mDomainPolicy;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
de = de->mNext;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-20 11:22:34 -07:00
|
|
|
if (!dpolicy)
|
|
|
|
dpolicy = mDefaultPolicy;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
aPrincipal->SetSecurityPolicy((void*)dpolicy);
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
ClassPolicy* cpolicy = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if ((dpolicy == mDefaultPolicy) && aCachedClassPolicy)
|
|
|
|
{
|
|
|
|
// No per-domain policy for this principal (the more common case)
|
|
|
|
// so look for a cached class policy from the object wrapper
|
|
|
|
cpolicy = *aCachedClassPolicy;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cpolicy)
|
|
|
|
{ //-- No cached policy for this class, need to look it up
|
|
|
|
#ifdef DEBUG_CAPS_LookupPolicy
|
|
|
|
printf("ClassLookup ");
|
|
|
|
#endif
|
|
|
|
|
2007-07-08 00:08:04 -07:00
|
|
|
cpolicy = static_cast<ClassPolicy*>
|
|
|
|
(PL_DHashTableOperate(dpolicy,
|
2007-03-22 10:30:00 -07:00
|
|
|
aClassData.GetName(),
|
|
|
|
PL_DHASH_LOOKUP));
|
|
|
|
|
|
|
|
if (PL_DHASH_ENTRY_IS_FREE(cpolicy))
|
|
|
|
cpolicy = NO_POLICY_FOR_CLASS;
|
|
|
|
|
|
|
|
if ((dpolicy == mDefaultPolicy) && aCachedClassPolicy)
|
|
|
|
*aCachedClassPolicy = cpolicy;
|
|
|
|
}
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
NS_ASSERTION(JSID_IS_INT(aProperty) || JSID_IS_OBJECT(aProperty) ||
|
|
|
|
JSID_IS_STRING(aProperty), "Property must be a valid id");
|
|
|
|
|
|
|
|
// Only atomized strings are stored in the policies' hash tables.
|
|
|
|
if (!JSID_IS_STRING(aProperty))
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
JSString *propertyKey = JSID_TO_STRING(aProperty);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// We look for a PropertyPolicy in the following places:
|
|
|
|
// 1) The ClassPolicy for our class we got from our DomainPolicy
|
|
|
|
// 2) The mWildcardPolicy of our DomainPolicy
|
|
|
|
// 3) The ClassPolicy for our class we got from mDefaultPolicy
|
|
|
|
// 4) The mWildcardPolicy of our mDefaultPolicy
|
2012-07-30 07:20:58 -07:00
|
|
|
PropertyPolicy* ppolicy = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (cpolicy != NO_POLICY_FOR_CLASS)
|
|
|
|
{
|
2007-07-08 00:08:04 -07:00
|
|
|
ppolicy = static_cast<PropertyPolicy*>
|
|
|
|
(PL_DHashTableOperate(cpolicy->mPolicy,
|
2010-07-14 23:19:36 -07:00
|
|
|
propertyKey,
|
2007-03-22 10:30:00 -07:00
|
|
|
PL_DHASH_LOOKUP));
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is no class policy for this property, and we have a wildcard
|
|
|
|
// policy, try that.
|
|
|
|
if (dpolicy->mWildcardPolicy &&
|
|
|
|
(!ppolicy || PL_DHASH_ENTRY_IS_FREE(ppolicy)))
|
|
|
|
{
|
|
|
|
ppolicy =
|
2007-07-08 00:08:04 -07:00
|
|
|
static_cast<PropertyPolicy*>
|
|
|
|
(PL_DHashTableOperate(dpolicy->mWildcardPolicy->mPolicy,
|
2010-07-14 23:19:36 -07:00
|
|
|
propertyKey,
|
2007-03-22 10:30:00 -07:00
|
|
|
PL_DHASH_LOOKUP));
|
|
|
|
}
|
|
|
|
|
|
|
|
// If dpolicy is not the defauly policy and there's no class or wildcard
|
|
|
|
// policy for this property, check the default policy for this class and
|
|
|
|
// the default wildcard policy
|
|
|
|
if (dpolicy != mDefaultPolicy &&
|
|
|
|
(!ppolicy || PL_DHASH_ENTRY_IS_FREE(ppolicy)))
|
|
|
|
{
|
2007-07-08 00:08:04 -07:00
|
|
|
cpolicy = static_cast<ClassPolicy*>
|
|
|
|
(PL_DHashTableOperate(mDefaultPolicy,
|
2007-03-22 10:30:00 -07:00
|
|
|
aClassData.GetName(),
|
|
|
|
PL_DHASH_LOOKUP));
|
|
|
|
|
|
|
|
if (PL_DHASH_ENTRY_IS_BUSY(cpolicy))
|
|
|
|
{
|
|
|
|
ppolicy =
|
2007-07-08 00:08:04 -07:00
|
|
|
static_cast<PropertyPolicy*>
|
|
|
|
(PL_DHashTableOperate(cpolicy->mPolicy,
|
2010-07-14 23:19:36 -07:00
|
|
|
propertyKey,
|
2007-03-22 10:30:00 -07:00
|
|
|
PL_DHASH_LOOKUP));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((!ppolicy || PL_DHASH_ENTRY_IS_FREE(ppolicy)) &&
|
|
|
|
mDefaultPolicy->mWildcardPolicy)
|
|
|
|
{
|
|
|
|
ppolicy =
|
2007-07-08 00:08:04 -07:00
|
|
|
static_cast<PropertyPolicy*>
|
|
|
|
(PL_DHashTableOperate(mDefaultPolicy->mWildcardPolicy->mPolicy,
|
2010-07-14 23:19:36 -07:00
|
|
|
propertyKey,
|
2007-03-22 10:30:00 -07:00
|
|
|
PL_DHASH_LOOKUP));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ppolicy || PL_DHASH_ENTRY_IS_FREE(ppolicy))
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
// Get the correct security level from the property policy
|
|
|
|
if (aAction == nsIXPCSecurityManager::ACCESS_SET_PROPERTY)
|
|
|
|
*result = ppolicy->mSet;
|
|
|
|
else
|
|
|
|
*result = ppolicy->mGet;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScriptSecurityManager::CheckLoadURIFromScript(JSContext *cx, nsIURI *aURI)
|
|
|
|
{
|
|
|
|
// Get principal of currently executing script.
|
|
|
|
nsresult rv;
|
|
|
|
nsIPrincipal* principal = GetSubjectPrincipal(cx, &rv);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
// Native code can load all URIs.
|
|
|
|
if (!principal)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
rv = CheckLoadURIWithPrincipal(principal, aURI,
|
|
|
|
nsIScriptSecurityManager::STANDARD);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
// OK to load
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if we're attempting to load a file: URI. If so, let a
|
2011-12-30 09:35:52 -08:00
|
|
|
// UniversalXPConnect capability trump the above check.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isFile = false;
|
|
|
|
bool isRes = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(aURI->SchemeIs("file", &isFile)) ||
|
|
|
|
NS_FAILED(aURI->SchemeIs("resource", &isRes)))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
if (isFile || isRes)
|
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
bool enabled;
|
2011-12-30 09:35:52 -08:00
|
|
|
if (NS_FAILED(IsCapabilityEnabled("UniversalXPConnect", &enabled)))
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
if (enabled)
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Report error.
|
|
|
|
nsCAutoString spec;
|
|
|
|
if (NS_FAILED(aURI->GetAsciiSpec(spec)))
|
|
|
|
return NS_ERROR_FAILURE;
|
2009-05-19 21:57:37 -07:00
|
|
|
nsCAutoString msg("Access to '");
|
|
|
|
msg.Append(spec);
|
|
|
|
msg.AppendLiteral("' from script denied");
|
|
|
|
SetPendingException(cx, msg.get());
|
2008-03-17 07:10:48 -07:00
|
|
|
return NS_ERROR_DOM_BAD_URI;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method to handle cases where a flag passed to
|
|
|
|
* CheckLoadURIWithPrincipal means denying loading if the given URI has certain
|
|
|
|
* nsIProtocolHandler flags set.
|
|
|
|
* @return if success, access is allowed. Otherwise, deny access
|
|
|
|
*/
|
|
|
|
static nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
DenyAccessIfURIHasFlags(nsIURI* aURI, uint32_t aURIFlags)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aURI, "Must have URI!");
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool uriHasFlags;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv =
|
|
|
|
NS_URIChainHasFlags(aURI, aURIFlags, &uriHasFlags);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (uriHasFlags) {
|
|
|
|
return NS_ERROR_DOM_BAD_URI;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScriptSecurityManager::CheckLoadURIWithPrincipal(nsIPrincipal* aPrincipal,
|
|
|
|
nsIURI *aTargetURI,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aFlags)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aPrincipal, "CheckLoadURIWithPrincipal must have a principal");
|
|
|
|
// If someone passes a flag that we don't understand, we should
|
|
|
|
// fail, because they may need a security check that we don't
|
|
|
|
// provide.
|
|
|
|
NS_ENSURE_FALSE(aFlags & ~(nsIScriptSecurityManager::LOAD_IS_AUTOMATIC_DOCUMENT_REPLACEMENT |
|
|
|
|
nsIScriptSecurityManager::ALLOW_CHROME |
|
|
|
|
nsIScriptSecurityManager::DISALLOW_SCRIPT |
|
|
|
|
nsIScriptSecurityManager::DISALLOW_INHERIT_PRINCIPAL),
|
|
|
|
NS_ERROR_UNEXPECTED);
|
|
|
|
NS_ENSURE_ARG_POINTER(aPrincipal);
|
2009-01-01 15:45:23 -08:00
|
|
|
NS_ENSURE_ARG_POINTER(aTargetURI);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-03-14 11:55:47 -07:00
|
|
|
// If DISALLOW_INHERIT_PRINCIPAL is set, we prevent loading of URIs which
|
|
|
|
// would do such inheriting. That would be URIs that do not have their own
|
|
|
|
// security context. We do this even for the system principal.
|
|
|
|
if (aFlags & nsIScriptSecurityManager::DISALLOW_INHERIT_PRINCIPAL) {
|
|
|
|
nsresult rv =
|
|
|
|
DenyAccessIfURIHasFlags(aTargetURI,
|
|
|
|
nsIProtocolHandler::URI_INHERITS_SECURITY_CONTEXT);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aPrincipal == mSystemPrincipal) {
|
|
|
|
// Allow access
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-03-14 11:55:47 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIURI> sourceURI;
|
|
|
|
aPrincipal->GetURI(getter_AddRefs(sourceURI));
|
2007-08-06 19:09:16 -07:00
|
|
|
if (!sourceURI) {
|
2012-06-10 16:44:50 -07:00
|
|
|
nsCOMPtr<nsIExpandedPrincipal> expanded = do_QueryInterface(aPrincipal);
|
|
|
|
if (expanded) {
|
|
|
|
nsTArray< nsCOMPtr<nsIPrincipal> > *whiteList;
|
|
|
|
expanded->GetWhiteList(&whiteList);
|
|
|
|
for (uint32_t i = 0; i < whiteList->Length(); ++i) {
|
|
|
|
nsresult rv = CheckLoadURIWithPrincipal((*whiteList)[i],
|
|
|
|
aTargetURI,
|
|
|
|
aFlags);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
// Allow access if it succeeded with one of the white listed principals
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
NS_ERROR("Non-system principals or expanded principal passed to CheckLoadURIWithPrincipal "
|
2007-08-06 19:09:16 -07:00
|
|
|
"must have a URI!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Automatic loads are not allowed from certain protocols.
|
|
|
|
if (aFlags & nsIScriptSecurityManager::LOAD_IS_AUTOMATIC_DOCUMENT_REPLACEMENT) {
|
|
|
|
nsresult rv =
|
|
|
|
DenyAccessIfURIHasFlags(sourceURI,
|
|
|
|
nsIProtocolHandler::URI_FORBIDS_AUTOMATIC_DOCUMENT_REPLACEMENT);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If either URI is a nested URI, get the base URI
|
|
|
|
nsCOMPtr<nsIURI> sourceBaseURI = NS_GetInnermostURI(sourceURI);
|
|
|
|
nsCOMPtr<nsIURI> targetBaseURI = NS_GetInnermostURI(aTargetURI);
|
|
|
|
|
|
|
|
//-- get the target scheme
|
|
|
|
nsCAutoString targetScheme;
|
|
|
|
nsresult rv = targetBaseURI->GetScheme(targetScheme);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
//-- Some callers do not allow loading javascript:
|
|
|
|
if ((aFlags & nsIScriptSecurityManager::DISALLOW_SCRIPT) &&
|
|
|
|
targetScheme.EqualsLiteral("javascript"))
|
|
|
|
{
|
|
|
|
return NS_ERROR_DOM_BAD_URI;
|
|
|
|
}
|
|
|
|
|
2010-03-02 23:51:09 -08:00
|
|
|
NS_NAMED_LITERAL_STRING(errorTag, "CheckLoadURIError");
|
|
|
|
|
|
|
|
// Check for uris that are only loadable by principals that subsume them
|
2011-09-28 23:19:26 -07:00
|
|
|
bool hasFlags;
|
2010-03-02 23:51:09 -08:00
|
|
|
rv = NS_URIChainHasFlags(targetBaseURI,
|
|
|
|
nsIProtocolHandler::URI_LOADABLE_BY_SUBSUMERS,
|
|
|
|
&hasFlags);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (hasFlags) {
|
2012-08-20 11:34:33 -07:00
|
|
|
return aPrincipal->CheckMayLoad(targetBaseURI, true, false);
|
2010-03-02 23:51:09 -08:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
//-- get the source scheme
|
|
|
|
nsCAutoString sourceScheme;
|
|
|
|
rv = sourceBaseURI->GetScheme(sourceScheme);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2008-03-27 20:46:15 -07:00
|
|
|
if (sourceScheme.LowerCaseEqualsLiteral(NS_NULLPRINCIPAL_SCHEME)) {
|
|
|
|
// A null principal can target its own URI.
|
|
|
|
if (sourceURI == aTargetURI) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (targetScheme.Equals(sourceScheme,
|
|
|
|
nsCaseInsensitiveCStringComparator()))
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// every scheme can access another URI from the same scheme,
|
|
|
|
// as long as they don't represent null principals.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the schemes don't match, the policy is specified by the protocol
|
|
|
|
// flags on the target URI. Note that the order of policy checks here is
|
|
|
|
// very important! We start from most restrictive and work our way down.
|
|
|
|
// Note that since we're working with the innermost URI, we can just use
|
|
|
|
// the methods that work on chains of nested URIs and they will only look
|
|
|
|
// at the flags for our one URI.
|
|
|
|
|
|
|
|
// Check for system target URI
|
|
|
|
rv = DenyAccessIfURIHasFlags(targetBaseURI,
|
|
|
|
nsIProtocolHandler::URI_DANGEROUS_TO_LOAD);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// Deny access, since the origin principal is not system
|
2012-07-30 07:20:58 -07:00
|
|
|
ReportError(nullptr, errorTag, sourceURI, aTargetURI);
|
2007-03-22 10:30:00 -07:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for chrome target URI
|
|
|
|
rv = NS_URIChainHasFlags(targetBaseURI,
|
|
|
|
nsIProtocolHandler::URI_IS_UI_RESOURCE,
|
|
|
|
&hasFlags);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (hasFlags) {
|
|
|
|
if (aFlags & nsIScriptSecurityManager::ALLOW_CHROME) {
|
2008-04-12 14:26:19 -07:00
|
|
|
if (!targetScheme.EqualsLiteral("chrome")) {
|
|
|
|
// for now don't change behavior for resource: or moz-icon:
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// allow load only if chrome package is whitelisted
|
|
|
|
nsCOMPtr<nsIXULChromeRegistry> reg(do_GetService(
|
|
|
|
NS_CHROMEREGISTRY_CONTRACTID));
|
|
|
|
if (reg) {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool accessAllowed = false;
|
2008-04-12 14:26:19 -07:00
|
|
|
reg->AllowContentToAccess(targetBaseURI, &accessAllowed);
|
|
|
|
if (accessAllowed) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// resource: and chrome: are equivalent, securitywise
|
|
|
|
// That's bogus!! Fix this. But watch out for
|
|
|
|
// the view-source stylesheet?
|
2011-09-28 23:19:26 -07:00
|
|
|
bool sourceIsChrome;
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = NS_URIChainHasFlags(sourceBaseURI,
|
|
|
|
nsIProtocolHandler::URI_IS_UI_RESOURCE,
|
|
|
|
&sourceIsChrome);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (sourceIsChrome) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-07-30 07:20:58 -07:00
|
|
|
ReportError(nullptr, errorTag, sourceURI, aTargetURI);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_DOM_BAD_URI;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for target URI pointing to a file
|
|
|
|
rv = NS_URIChainHasFlags(targetBaseURI,
|
|
|
|
nsIProtocolHandler::URI_IS_LOCAL_FILE,
|
|
|
|
&hasFlags);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (hasFlags) {
|
|
|
|
// resource: and chrome: are equivalent, securitywise
|
|
|
|
// That's bogus!! Fix this. But watch out for
|
|
|
|
// the view-source stylesheet?
|
2011-09-28 23:19:26 -07:00
|
|
|
bool sourceIsChrome;
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = NS_URIChainHasFlags(sourceURI,
|
|
|
|
nsIProtocolHandler::URI_IS_UI_RESOURCE,
|
|
|
|
&sourceIsChrome);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (sourceIsChrome) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now check capability policies
|
|
|
|
static const char loadURIPrefGroup[] = "checkloaduri";
|
2012-07-30 07:20:58 -07:00
|
|
|
ClassInfoData nameData(nullptr, loadURIPrefGroup);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
SecurityLevel secLevel;
|
|
|
|
rv = LookupPolicy(aPrincipal, nameData, sEnabledID,
|
|
|
|
nsIXPCSecurityManager::ACCESS_GET_PROPERTY,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr, &secLevel);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_SUCCEEDED(rv) && secLevel.level == SCRIPT_SECURITY_ALL_ACCESS)
|
|
|
|
{
|
|
|
|
// OK for this site!
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
ReportError(nullptr, errorTag, sourceURI, aTargetURI);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_DOM_BAD_URI;
|
|
|
|
}
|
|
|
|
|
|
|
|
// OK, everyone is allowed to load this, since unflagged handlers are
|
|
|
|
// deprecated but treated as URI_LOADABLE_BY_ANYONE. But check whether we
|
|
|
|
// need to warn. At some point we'll want to make this warning into an
|
|
|
|
// error and treat unflagged handlers as URI_DANGEROUS_TO_LOAD.
|
|
|
|
rv = NS_URIChainHasFlags(targetBaseURI,
|
|
|
|
nsIProtocolHandler::URI_LOADABLE_BY_ANYONE,
|
|
|
|
&hasFlags);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (!hasFlags) {
|
|
|
|
nsXPIDLString message;
|
|
|
|
NS_ConvertASCIItoUTF16 ucsTargetScheme(targetScheme);
|
|
|
|
const PRUnichar* formatStrings[] = { ucsTargetScheme.get() };
|
|
|
|
rv = sStrBundle->
|
|
|
|
FormatStringFromName(NS_LITERAL_STRING("ProtocolFlagError").get(),
|
|
|
|
formatStrings,
|
2011-10-10 22:50:08 -07:00
|
|
|
ArrayLength(formatStrings),
|
2007-03-22 10:30:00 -07:00
|
|
|
getter_Copies(message));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsCOMPtr<nsIConsoleService> console(
|
|
|
|
do_GetService("@mozilla.org/consoleservice;1"));
|
|
|
|
NS_ENSURE_TRUE(console, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
console->LogStringMessage(message.get());
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "%s\n", NS_ConvertUTF16toUTF8(message).get());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsScriptSecurityManager::ReportError(JSContext* cx, const nsAString& messageTag,
|
|
|
|
nsIURI* aSource, nsIURI* aTarget)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
NS_ENSURE_TRUE(aSource && aTarget, NS_ERROR_NULL_POINTER);
|
|
|
|
|
|
|
|
// Get the source URL spec
|
|
|
|
nsCAutoString sourceSpec;
|
|
|
|
rv = aSource->GetAsciiSpec(sourceSpec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Get the target URL spec
|
|
|
|
nsCAutoString targetSpec;
|
|
|
|
rv = aTarget->GetAsciiSpec(targetSpec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Localize the error message
|
|
|
|
nsXPIDLString message;
|
|
|
|
NS_ConvertASCIItoUTF16 ucsSourceSpec(sourceSpec);
|
|
|
|
NS_ConvertASCIItoUTF16 ucsTargetSpec(targetSpec);
|
|
|
|
const PRUnichar *formatStrings[] = { ucsSourceSpec.get(), ucsTargetSpec.get() };
|
|
|
|
rv = sStrBundle->FormatStringFromName(PromiseFlatString(messageTag).get(),
|
|
|
|
formatStrings,
|
2011-10-10 22:50:08 -07:00
|
|
|
ArrayLength(formatStrings),
|
2007-03-22 10:30:00 -07:00
|
|
|
getter_Copies(message));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// If a JS context was passed in, set a JS exception.
|
|
|
|
// Otherwise, print the error message directly to the JS console
|
|
|
|
// and to standard output
|
|
|
|
if (cx)
|
|
|
|
{
|
|
|
|
SetPendingException(cx, message.get());
|
|
|
|
}
|
|
|
|
else // Print directly to the console
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIConsoleService> console(
|
|
|
|
do_GetService("@mozilla.org/consoleservice;1"));
|
|
|
|
NS_ENSURE_TRUE(console, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
console->LogStringMessage(message.get());
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "%s\n", NS_LossyConvertUTF16toASCII(message).get());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScriptSecurityManager::CheckLoadURIStrWithPrincipal(nsIPrincipal* aPrincipal,
|
|
|
|
const nsACString& aTargetURIStr,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aFlags)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIURI> target;
|
|
|
|
rv = NS_NewURI(getter_AddRefs(target), aTargetURIStr,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr, nullptr, sIOService);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = CheckLoadURIWithPrincipal(aPrincipal, target, aFlags);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Now start testing fixup -- since aTargetURIStr is a string, not
|
|
|
|
// an nsIURI, we may well end up fixing it up before loading.
|
|
|
|
// Note: This needs to stay in sync with the nsIURIFixup api.
|
|
|
|
nsCOMPtr<nsIURIFixup> fixup = do_GetService(NS_URIFIXUP_CONTRACTID);
|
|
|
|
if (!fixup) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t flags[] = {
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIURIFixup::FIXUP_FLAG_NONE,
|
|
|
|
nsIURIFixup::FIXUP_FLAG_ALLOW_KEYWORD_LOOKUP,
|
|
|
|
nsIURIFixup::FIXUP_FLAGS_MAKE_ALTERNATE_URI,
|
|
|
|
nsIURIFixup::FIXUP_FLAG_ALLOW_KEYWORD_LOOKUP |
|
|
|
|
nsIURIFixup::FIXUP_FLAGS_MAKE_ALTERNATE_URI
|
|
|
|
};
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < ArrayLength(flags); ++i) {
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = fixup->CreateFixupURI(aTargetURIStr, flags[i],
|
|
|
|
getter_AddRefs(target));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = CheckLoadURIWithPrincipal(aPrincipal, target, aFlags);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScriptSecurityManager::CheckFunctionAccess(JSContext *aCx, void *aFunObj,
|
|
|
|
void *aTargetObj)
|
|
|
|
{
|
|
|
|
// This check is called for event handlers
|
|
|
|
nsresult rv;
|
|
|
|
nsIPrincipal* subject =
|
2012-07-30 07:20:58 -07:00
|
|
|
GetFunctionObjectPrincipal(aCx, (JSObject *)aFunObj, nullptr, &rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// If subject is null, get a principal from the function object's scope.
|
|
|
|
if (NS_SUCCEEDED(rv) && !subject)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
2011-08-18 10:34:20 -07:00
|
|
|
JS_ASSERT(JS_ObjectIsFunction(aCx, (JSObject *)aFunObj));
|
2011-10-12 08:32:42 -07:00
|
|
|
JSFunction *fun = JS_GetObjectFunction((JSObject *)aFunObj);
|
2007-03-22 10:30:00 -07:00
|
|
|
JSScript *script = JS_GetFunctionScript(aCx, fun);
|
|
|
|
|
|
|
|
NS_ASSERTION(!script, "Null principal for non-native function!");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-01-29 12:51:01 -08:00
|
|
|
subject = doGetObjectPrincipal((JSObject*)aFunObj);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!subject)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
if (subject == mSystemPrincipal)
|
|
|
|
// This is the system principal: just allow access
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
// Check if the principal the function was compiled under is
|
|
|
|
// allowed to execute scripts.
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool result;
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = CanExecuteScripts(aCx, subject, &result);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Get origin of subject and object and compare.
|
|
|
|
*/
|
|
|
|
JSObject* obj = (JSObject*)aTargetObj;
|
2008-01-29 12:51:01 -08:00
|
|
|
nsIPrincipal* object = doGetObjectPrincipal(obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!object)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool subsumes;
|
2008-03-18 14:14:49 -07:00
|
|
|
rv = subject->Subsumes(object, &subsumes);
|
|
|
|
if (NS_SUCCEEDED(rv) && !subsumes) {
|
|
|
|
rv = NS_ERROR_DOM_PROP_ACCESS_DENIED;
|
|
|
|
}
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScriptSecurityManager::CanExecuteScripts(JSContext* cx,
|
|
|
|
nsIPrincipal *aPrincipal,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool *result)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
*result = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (aPrincipal == mSystemPrincipal)
|
|
|
|
{
|
|
|
|
// Even if JavaScript is disabled, we must still execute system scripts
|
2011-10-17 07:59:28 -07:00
|
|
|
*result = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-- See if the current window allows JS execution
|
|
|
|
nsIScriptContext *scriptContext = GetScriptContext(cx);
|
|
|
|
if (!scriptContext) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
if (!scriptContext->GetScriptsEnabled()) {
|
|
|
|
// No scripting on this context, folks
|
2011-10-17 07:59:28 -07:00
|
|
|
*result = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIScriptGlobalObject *sgo = scriptContext->GetGlobalObject();
|
|
|
|
|
|
|
|
if (!sgo) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// window can be null here if we're running with a non-DOM window
|
|
|
|
// as the script global (i.e. a XUL prototype document).
|
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(sgo);
|
|
|
|
nsCOMPtr<nsIDocShell> docshell;
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (window) {
|
|
|
|
docshell = window->GetDocShell();
|
|
|
|
}
|
|
|
|
|
2010-06-04 14:03:50 -07:00
|
|
|
if (docshell) {
|
|
|
|
rv = docshell->GetCanExecuteScripts(result);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
if (!*result) return NS_OK;
|
2010-06-28 12:29:30 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// OK, the docshell doesn't have script execution explicitly disabled.
|
|
|
|
// Check whether our URI is an "about:" URI that allows scripts. If it is,
|
|
|
|
// we need to allow JS to run. In this case, don't apply the JS enabled
|
|
|
|
// pref or policies. On failures, just press on and don't do this special
|
|
|
|
// case.
|
|
|
|
nsCOMPtr<nsIURI> principalURI;
|
|
|
|
aPrincipal->GetURI(getter_AddRefs(principalURI));
|
2007-08-06 19:09:16 -07:00
|
|
|
if (!principalURI) {
|
|
|
|
// Broken principal of some sort. Disallow.
|
2011-10-17 07:59:28 -07:00
|
|
|
*result = false;
|
2007-08-06 19:09:16 -07:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isAbout;
|
2007-08-06 19:09:16 -07:00
|
|
|
rv = principalURI->SchemeIs("about", &isAbout);
|
|
|
|
if (NS_SUCCEEDED(rv) && isAbout) {
|
|
|
|
nsCOMPtr<nsIAboutModule> module;
|
|
|
|
rv = NS_GetAboutModule(principalURI, getter_AddRefs(module));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t flags;
|
2007-08-06 19:09:16 -07:00
|
|
|
rv = module->GetURIFlags(principalURI, &flags);
|
|
|
|
if (NS_SUCCEEDED(rv) &&
|
|
|
|
(flags & nsIAboutModule::ALLOW_SCRIPT)) {
|
2011-10-17 07:59:28 -07:00
|
|
|
*result = true;
|
2007-08-06 19:09:16 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*result = mIsJavaScriptEnabled;
|
|
|
|
if (!*result)
|
|
|
|
return NS_OK; // Do not run scripts
|
|
|
|
|
|
|
|
//-- Check for a per-site policy
|
|
|
|
static const char jsPrefGroupName[] = "javascript";
|
2012-07-30 07:20:58 -07:00
|
|
|
ClassInfoData nameData(nullptr, jsPrefGroupName);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
SecurityLevel secLevel;
|
|
|
|
rv = LookupPolicy(aPrincipal, nameData, sEnabledID,
|
|
|
|
nsIXPCSecurityManager::ACCESS_GET_PROPERTY,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr, &secLevel);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv) || secLevel.level == SCRIPT_SECURITY_NO_ACCESS)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
*result = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-- Nobody vetoed, so allow the JS to run.
|
2011-10-17 07:59:28 -07:00
|
|
|
*result = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////// Principals ///////////////////////
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScriptSecurityManager::GetSubjectPrincipal(nsIPrincipal **aSubjectPrincipal)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
*aSubjectPrincipal = doGetSubjectPrincipal(&rv);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
NS_IF_ADDREF(*aSubjectPrincipal);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIPrincipal*
|
|
|
|
nsScriptSecurityManager::doGetSubjectPrincipal(nsresult* rv)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(rv, "Null out param");
|
|
|
|
JSContext *cx = GetCurrentJSContext();
|
|
|
|
if (!cx)
|
|
|
|
{
|
|
|
|
*rv = NS_OK;
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return GetSubjectPrincipal(cx, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScriptSecurityManager::GetSystemPrincipal(nsIPrincipal **result)
|
|
|
|
{
|
|
|
|
NS_ADDREF(*result = mSystemPrincipal);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsScriptSecurityManager::SubjectPrincipalIsSystem(bool* aIsSystem)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aIsSystem);
|
2011-10-17 07:59:28 -07:00
|
|
|
*aIsSystem = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!mSystemPrincipal)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> subject;
|
|
|
|
nsresult rv = GetSubjectPrincipal(getter_AddRefs(subject));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
if(!subject)
|
|
|
|
{
|
|
|
|
// No subject principal means no JS is running;
|
|
|
|
// this is the equivalent of system principal code
|
2011-10-17 07:59:28 -07:00
|
|
|
*aIsSystem = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mSystemPrincipal->Equals(subject, aIsSystem);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScriptSecurityManager::GetCertificatePrincipal(const nsACString& aCertFingerprint,
|
|
|
|
const nsACString& aSubjectName,
|
|
|
|
const nsACString& aPrettyName,
|
|
|
|
nsISupports* aCertificate,
|
|
|
|
nsIURI* aURI,
|
|
|
|
nsIPrincipal **result)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
*result = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_ENSURE_ARG(!aCertFingerprint.IsEmpty() &&
|
|
|
|
!aSubjectName.IsEmpty() &&
|
|
|
|
aCertificate);
|
|
|
|
|
|
|
|
return DoGetCertificatePrincipal(aCertFingerprint, aSubjectName,
|
2011-10-17 07:59:28 -07:00
|
|
|
aPrettyName, aCertificate, aURI, true,
|
2007-03-22 10:30:00 -07:00
|
|
|
result);
|
|
|
|
}
|
2007-12-12 15:02:25 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult
|
|
|
|
nsScriptSecurityManager::DoGetCertificatePrincipal(const nsACString& aCertFingerprint,
|
|
|
|
const nsACString& aSubjectName,
|
|
|
|
const nsACString& aPrettyName,
|
|
|
|
nsISupports* aCertificate,
|
|
|
|
nsIURI* aURI,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aModifyTable,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIPrincipal **result)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(!aCertFingerprint.IsEmpty());
|
|
|
|
|
|
|
|
// Create a certificate principal out of the certificate ID
|
|
|
|
// and URI given to us. We will use this principal to test
|
|
|
|
// equality when doing our hashtable lookups below.
|
|
|
|
nsRefPtr<nsPrincipal> certificate = new nsPrincipal();
|
|
|
|
if (!certificate)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
nsresult rv = certificate->Init(aCertFingerprint, aSubjectName,
|
2012-07-19 22:44:03 -07:00
|
|
|
aPrettyName, aCertificate, aURI,
|
|
|
|
UNKNOWN_APP_ID, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Check to see if we already have this principal.
|
|
|
|
nsCOMPtr<nsIPrincipal> fromTable;
|
|
|
|
mPrincipals.Get(certificate, getter_AddRefs(fromTable));
|
|
|
|
if (fromTable) {
|
|
|
|
// Bingo. We found the certificate in the table, which means
|
|
|
|
// that it has escalated privileges.
|
|
|
|
|
|
|
|
if (aModifyTable) {
|
|
|
|
// Make sure this principal has names, so if we ever go to save it
|
|
|
|
// we'll save them. If we get a name mismatch here we'll throw,
|
|
|
|
// but that's desirable.
|
2007-07-08 00:08:04 -07:00
|
|
|
rv = static_cast<nsPrincipal*>
|
|
|
|
(static_cast<nsIPrincipal*>(fromTable))
|
2007-03-22 10:30:00 -07:00
|
|
|
->EnsureCertData(aSubjectName, aPrettyName, aCertificate);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// We have a subject name mismatch for the same cert id.
|
|
|
|
// Hand back the |certificate| object we created and don't give
|
|
|
|
// it any rights from the table.
|
|
|
|
NS_ADDREF(*result = certificate);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aURI) {
|
|
|
|
// We were asked to just get the base certificate, so output
|
|
|
|
// what we have in the table.
|
2007-07-08 00:08:04 -07:00
|
|
|
certificate = static_cast<nsPrincipal*>
|
|
|
|
(static_cast<nsIPrincipal*>
|
|
|
|
(fromTable));
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
|
|
|
// We found a certificate and now need to install a codebase
|
|
|
|
// on it. We don't want to modify the principal in the hash
|
|
|
|
// table, so create a new principal and clone the pertinent
|
|
|
|
// things.
|
|
|
|
nsXPIDLCString prefName;
|
|
|
|
nsXPIDLCString id;
|
|
|
|
nsXPIDLCString subjectName;
|
|
|
|
nsXPIDLCString granted;
|
|
|
|
nsXPIDLCString denied;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isTrusted;
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = fromTable->GetPreferences(getter_Copies(prefName),
|
|
|
|
getter_Copies(id),
|
|
|
|
getter_Copies(subjectName),
|
|
|
|
getter_Copies(granted),
|
|
|
|
getter_Copies(denied),
|
|
|
|
&isTrusted);
|
|
|
|
// XXXbz assert something about subjectName and aSubjectName here?
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
NS_ASSERTION(!isTrusted, "Shouldn't have isTrusted true here");
|
|
|
|
|
|
|
|
certificate = new nsPrincipal();
|
|
|
|
if (!certificate)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
rv = certificate->InitFromPersistent(prefName, id,
|
|
|
|
subjectName, aPrettyName,
|
|
|
|
granted, denied,
|
|
|
|
aCertificate,
|
2012-07-19 22:44:03 -07:00
|
|
|
true, false,
|
|
|
|
UNKNOWN_APP_ID, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
certificate->SetURI(aURI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ADDREF(*result = certificate);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsScriptSecurityManager::CreateCodebasePrincipal(nsIURI* aURI, uint32_t aAppId,
|
2012-07-19 15:32:08 -07:00
|
|
|
bool aInMozBrowser,
|
|
|
|
nsIPrincipal **result)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// I _think_ it's safe to not create null principals here based on aURI.
|
|
|
|
// At least all the callers would do the right thing in those cases, as far
|
|
|
|
// as I can tell. --bz
|
2010-03-02 23:51:09 -08:00
|
|
|
|
|
|
|
nsCOMPtr<nsIURIWithPrincipal> uriPrinc = do_QueryInterface(aURI);
|
|
|
|
if (uriPrinc) {
|
|
|
|
nsCOMPtr<nsIPrincipal> principal;
|
|
|
|
uriPrinc->GetPrincipal(getter_AddRefs(principal));
|
|
|
|
if (!principal || principal == mSystemPrincipal) {
|
|
|
|
return CallCreateInstance(NS_NULLPRINCIPAL_CONTRACTID, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
principal.forget(result);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsRefPtr<nsPrincipal> codebase = new nsPrincipal();
|
|
|
|
if (!codebase)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
nsresult rv = codebase->Init(EmptyCString(), EmptyCString(),
|
2012-07-30 07:20:58 -07:00
|
|
|
EmptyCString(), nullptr, aURI, aAppId,
|
2012-07-19 15:32:08 -07:00
|
|
|
aInMozBrowser);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
NS_ADDREF(*result = codebase);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-07-21 00:29:40 -07:00
|
|
|
nsScriptSecurityManager::GetSimpleCodebasePrincipal(nsIURI* aURI,
|
2012-07-19 20:28:08 -07:00
|
|
|
nsIPrincipal** aPrincipal)
|
|
|
|
{
|
2012-07-21 00:29:40 -07:00
|
|
|
return GetCodebasePrincipalInternal(aURI,
|
|
|
|
nsIScriptSecurityManager::UNKNOWN_APP_ID,
|
|
|
|
false, aPrincipal);
|
2012-07-19 20:28:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScriptSecurityManager::GetNoAppCodebasePrincipal(nsIURI* aURI,
|
|
|
|
nsIPrincipal** aPrincipal)
|
|
|
|
{
|
|
|
|
return GetCodebasePrincipalInternal(aURI, nsIScriptSecurityManager::NO_APP_ID,
|
2012-07-21 00:29:40 -07:00
|
|
|
false, aPrincipal);
|
2012-07-19 20:28:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScriptSecurityManager::GetAppCodebasePrincipal(nsIURI* aURI,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aAppId,
|
2012-07-19 20:28:08 -07:00
|
|
|
bool aInMozBrowser,
|
|
|
|
nsIPrincipal** aPrincipal)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(aAppId != nsIScriptSecurityManager::UNKNOWN_APP_ID,
|
|
|
|
NS_ERROR_INVALID_ARG);
|
|
|
|
|
|
|
|
return GetCodebasePrincipalInternal(aURI, aAppId, aInMozBrowser, aPrincipal);
|
|
|
|
}
|
|
|
|
|
2012-07-20 00:06:24 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScriptSecurityManager::GetDocShellCodebasePrincipal(nsIURI* aURI,
|
|
|
|
nsIDocShell* aDocShell,
|
|
|
|
nsIPrincipal** aPrincipal)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aDocShell);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t appId;
|
2012-07-20 00:06:24 -07:00
|
|
|
bool isInBrowserElement;
|
|
|
|
aDocShell->GetAppId(&appId);
|
|
|
|
aDocShell->GetIsInBrowserElement(&isInBrowserElement);
|
|
|
|
|
|
|
|
return GetCodebasePrincipalInternal(aURI, appId, isInBrowserElement,
|
|
|
|
aPrincipal);
|
|
|
|
}
|
|
|
|
|
2012-07-19 20:28:08 -07:00
|
|
|
nsresult
|
|
|
|
nsScriptSecurityManager::GetCodebasePrincipalInternal(nsIURI *aURI,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aAppId,
|
2012-07-21 00:29:40 -07:00
|
|
|
bool aInMozBrowser,
|
|
|
|
nsIPrincipal **result)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-08-06 19:09:16 -07:00
|
|
|
NS_ENSURE_ARG(aURI);
|
2012-07-19 20:28:08 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool inheritsPrincipal;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv =
|
|
|
|
NS_URIChainHasFlags(aURI,
|
|
|
|
nsIProtocolHandler::URI_INHERITS_SECURITY_CONTEXT,
|
|
|
|
&inheritsPrincipal);
|
|
|
|
if (NS_FAILED(rv) || inheritsPrincipal) {
|
|
|
|
return CallCreateInstance(NS_NULLPRINCIPAL_CONTRACTID, result);
|
|
|
|
}
|
2012-07-19 15:32:08 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIPrincipal> principal;
|
2012-07-19 20:28:08 -07:00
|
|
|
rv = CreateCodebasePrincipal(aURI, aAppId, aInMozBrowser,
|
|
|
|
getter_AddRefs(principal));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (mPrincipals.Count() > 0)
|
|
|
|
{
|
|
|
|
//-- Check to see if we already have this principal.
|
|
|
|
nsCOMPtr<nsIPrincipal> fromTable;
|
|
|
|
mPrincipals.Get(principal, getter_AddRefs(fromTable));
|
|
|
|
if (fromTable) {
|
|
|
|
// We found an existing codebase principal. But it might have a
|
|
|
|
// generic codebase for this origin on it. Install our particular
|
|
|
|
// codebase.
|
|
|
|
// XXXbz this is kinda similar to the code in
|
|
|
|
// GetCertificatePrincipal, but just ever so slightly different.
|
|
|
|
// Oh, well.
|
|
|
|
nsXPIDLCString prefName;
|
|
|
|
nsXPIDLCString id;
|
|
|
|
nsXPIDLCString subjectName;
|
|
|
|
nsXPIDLCString granted;
|
|
|
|
nsXPIDLCString denied;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isTrusted;
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = fromTable->GetPreferences(getter_Copies(prefName),
|
|
|
|
getter_Copies(id),
|
|
|
|
getter_Copies(subjectName),
|
|
|
|
getter_Copies(granted),
|
|
|
|
getter_Copies(denied),
|
|
|
|
&isTrusted);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsRefPtr<nsPrincipal> codebase = new nsPrincipal();
|
|
|
|
if (!codebase)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
rv = codebase->InitFromPersistent(prefName, id,
|
|
|
|
subjectName, EmptyCString(),
|
|
|
|
granted, denied,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr, false,
|
2012-07-19 22:44:03 -07:00
|
|
|
isTrusted,
|
2012-07-19 20:28:08 -07:00
|
|
|
aAppId,
|
|
|
|
aInMozBrowser);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
codebase->SetURI(aURI);
|
|
|
|
principal = codebase;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IF_ADDREF(*result = principal);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScriptSecurityManager::GetPrincipalFromContext(JSContext *cx,
|
|
|
|
nsIPrincipal **result)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
*result = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-05-18 05:28:37 -07:00
|
|
|
nsIScriptContextPrincipal* scp =
|
|
|
|
GetScriptContextPrincipalFromJSContext(cx);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-05-18 05:28:37 -07:00
|
|
|
if (!scp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2010-05-18 05:28:37 -07:00
|
|
|
nsIScriptObjectPrincipal* globalData = scp->GetObjectPrincipal();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (globalData)
|
|
|
|
NS_IF_ADDREF(*result = globalData->GetPrincipal());
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
nsIPrincipal*
|
2012-02-13 05:10:04 -08:00
|
|
|
nsScriptSecurityManager::GetScriptPrincipal(JSScript *script,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult* rv)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(rv, "Null out param");
|
|
|
|
*rv = NS_OK;
|
|
|
|
if (!script)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-02-13 05:10:04 -08:00
|
|
|
JSPrincipals *jsp = JS_GetScriptPrincipals(script);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!jsp) {
|
|
|
|
*rv = NS_ERROR_FAILURE;
|
2008-09-04 15:52:20 -07:00
|
|
|
NS_ERROR("Script compiled without principals!");
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-03-09 01:48:50 -08:00
|
|
|
return nsJSPrincipals::get(jsp);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
nsIPrincipal*
|
|
|
|
nsScriptSecurityManager::GetFunctionObjectPrincipal(JSContext *cx,
|
|
|
|
JSObject *obj,
|
|
|
|
JSStackFrame *fp,
|
|
|
|
nsresult *rv)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(rv, "Null out param");
|
2011-01-04 08:40:54 -08:00
|
|
|
|
|
|
|
*rv = NS_OK;
|
|
|
|
|
2009-06-12 14:38:05 -07:00
|
|
|
if (!JS_ObjectIsFunction(cx, obj))
|
|
|
|
{
|
|
|
|
// Protect against pseudo-functions (like SJOWs).
|
|
|
|
nsIPrincipal *result = doGetObjectPrincipal(obj);
|
|
|
|
if (!result)
|
|
|
|
*rv = NS_ERROR_FAILURE;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-10-12 08:32:42 -07:00
|
|
|
JSFunction *fun = JS_GetObjectFunction(obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
JSScript *script = JS_GetFunctionScript(cx, fun);
|
|
|
|
|
|
|
|
if (!script)
|
|
|
|
{
|
|
|
|
// A native function: skip it in order to find its scripted caller.
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
JSScript *frameScript = fp ? JS_GetFrameScript(cx, fp) : nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (frameScript && frameScript != script)
|
|
|
|
{
|
|
|
|
// There is a frame script, and it's different from the
|
|
|
|
// function script. In this case we're dealing with either
|
|
|
|
// an eval or a Script object, and in these cases the
|
|
|
|
// principal we want is in the frame's script, not in the
|
|
|
|
// function's script. The function's script is where the
|
|
|
|
// eval-calling code came from, not where the eval or new
|
|
|
|
// Script object came from, and we want the principal of
|
|
|
|
// the eval function object or new Script object.
|
|
|
|
|
2008-03-28 15:27:36 -07:00
|
|
|
script = frameScript;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-10-21 14:31:01 -07:00
|
|
|
else if (!js::IsOriginalScriptFunction(fun))
|
2008-03-28 15:27:36 -07:00
|
|
|
{
|
|
|
|
// Here, obj is a cloned function object. In this case, the
|
|
|
|
// clone's prototype may have been precompiled from brutally
|
|
|
|
// shared chrome, or else it is a lambda or nested function.
|
|
|
|
// The general case here is a function compiled against a
|
|
|
|
// different scope than the one it is parented by at runtime,
|
|
|
|
// hence the creation of a clone to carry the correct scope
|
|
|
|
// chain linkage.
|
|
|
|
//
|
|
|
|
// Since principals follow scope, we must get the object
|
|
|
|
// principal from the clone's scope chain. There are no
|
|
|
|
// reliable principals compiled into the function itself.
|
|
|
|
|
|
|
|
nsIPrincipal *result = doGetObjectPrincipal(obj);
|
|
|
|
if (!result)
|
|
|
|
*rv = NS_ERROR_FAILURE;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-02-13 05:10:04 -08:00
|
|
|
return GetScriptPrincipal(script, rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIPrincipal*
|
|
|
|
nsScriptSecurityManager::GetFramePrincipal(JSContext *cx,
|
|
|
|
JSStackFrame *fp,
|
|
|
|
nsresult *rv)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(rv, "Null out param");
|
|
|
|
JSObject *obj = JS_GetFrameFunctionObject(cx, fp);
|
|
|
|
if (!obj)
|
|
|
|
{
|
|
|
|
// Must be in a top-level script. Get principal from the script.
|
|
|
|
JSScript *script = JS_GetFrameScript(cx, fp);
|
2012-02-13 05:10:04 -08:00
|
|
|
return GetScriptPrincipal(script, rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIPrincipal* result = GetFunctionObjectPrincipal(cx, obj, fp, rv);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (NS_SUCCEEDED(*rv) && !result)
|
|
|
|
{
|
2011-10-12 08:32:42 -07:00
|
|
|
JSFunction *fun = JS_GetObjectFunction(obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
JSScript *script = JS_GetFunctionScript(cx, fun);
|
|
|
|
|
|
|
|
NS_ASSERTION(!script, "Null principal for non-native function!");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIPrincipal*
|
|
|
|
nsScriptSecurityManager::GetPrincipalAndFrame(JSContext *cx,
|
|
|
|
JSStackFrame **frameResult,
|
|
|
|
nsresult* rv)
|
|
|
|
{
|
2009-08-21 18:20:20 -07:00
|
|
|
NS_PRECONDITION(rv, "Null out param");
|
2007-03-22 10:30:00 -07:00
|
|
|
//-- If there's no principal on the stack, look at the global object
|
|
|
|
// and return the innermost frame for annotations.
|
|
|
|
*rv = NS_OK;
|
2009-08-21 18:20:20 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (cx)
|
|
|
|
{
|
2009-08-21 18:20:20 -07:00
|
|
|
// Get principals from innermost JavaScript frame.
|
2012-07-30 07:20:58 -07:00
|
|
|
JSStackFrame *fp = nullptr; // tell JS_FrameIterator to start at innermost
|
2007-03-22 10:30:00 -07:00
|
|
|
for (fp = JS_FrameIterator(cx, &fp); fp; fp = JS_FrameIterator(cx, &fp))
|
|
|
|
{
|
|
|
|
nsIPrincipal* result = GetFramePrincipal(cx, fp, rv);
|
|
|
|
if (result)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(*rv), "Weird return");
|
|
|
|
*frameResult = fp;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-18 05:28:37 -07:00
|
|
|
nsIScriptContextPrincipal* scp =
|
|
|
|
GetScriptContextPrincipalFromJSContext(cx);
|
|
|
|
if (scp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-05-18 05:28:37 -07:00
|
|
|
nsIScriptObjectPrincipal* globalData = scp->GetObjectPrincipal();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!globalData)
|
|
|
|
{
|
|
|
|
*rv = NS_ERROR_FAILURE;
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Note that we're not in a loop or anything, and nothing comes
|
|
|
|
// after this point in the function, so we can just return here.
|
|
|
|
nsIPrincipal* result = globalData->GetPrincipal();
|
|
|
|
if (result)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
JSStackFrame *inner = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
*frameResult = JS_FrameIterator(cx, &inner);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIPrincipal*
|
|
|
|
nsScriptSecurityManager::GetSubjectPrincipal(JSContext *cx,
|
|
|
|
nsresult* rv)
|
|
|
|
{
|
2012-06-28 14:47:55 -07:00
|
|
|
*rv = NS_OK;
|
|
|
|
JSCompartment *compartment = js::GetContextCompartment(cx);
|
|
|
|
|
|
|
|
// The context should always be in a compartment, either one it has entered
|
|
|
|
// or the one associated with its global.
|
|
|
|
MOZ_ASSERT(!!compartment);
|
|
|
|
|
|
|
|
JSPrincipals *principals = JS_GetCompartmentPrincipals(compartment);
|
|
|
|
return nsJSPrincipals::get(principals);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScriptSecurityManager::GetObjectPrincipal(JSContext *aCx, JSObject *aObj,
|
|
|
|
nsIPrincipal **result)
|
|
|
|
{
|
2008-01-29 12:51:01 -08:00
|
|
|
*result = doGetObjectPrincipal(aObj);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!*result)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
NS_ADDREF(*result);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
nsIPrincipal*
|
2012-06-28 14:47:55 -07:00
|
|
|
nsScriptSecurityManager::doGetObjectPrincipal(JSObject *aObj)
|
|
|
|
{
|
|
|
|
JSCompartment *compartment = js::GetObjectCompartment(aObj);
|
|
|
|
JSPrincipals *principals = JS_GetCompartmentPrincipals(compartment);
|
|
|
|
nsIPrincipal *principal = nsJSPrincipals::get(principals);
|
|
|
|
|
|
|
|
// We leave the old code in for a little while to make sure that pulling
|
|
|
|
// object principals directly off the compartment always gives an equivalent
|
|
|
|
// result (from a security perspective).
|
2007-12-12 15:02:25 -08:00
|
|
|
#ifdef DEBUG
|
2012-06-28 14:47:55 -07:00
|
|
|
nsIPrincipal *old = old_doGetObjectPrincipal(aObj);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(CheckSameOriginPrincipal(principal, old)));
|
2007-12-12 15:02:25 -08:00
|
|
|
#endif
|
2012-06-28 14:47:55 -07:00
|
|
|
|
|
|
|
return principal;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
// static
|
|
|
|
nsIPrincipal*
|
|
|
|
nsScriptSecurityManager::old_doGetObjectPrincipal(JSObject *aObj,
|
|
|
|
bool aAllowShortCircuit)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-01-29 13:11:24 -08:00
|
|
|
NS_ASSERTION(aObj, "Bad call to doGetObjectPrincipal()!");
|
2012-07-30 07:20:58 -07:00
|
|
|
nsIPrincipal* result = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
JSObject* origObj = aObj;
|
2011-10-04 07:06:54 -07:00
|
|
|
js::Class *jsClass = js::GetObjectClass(aObj);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-12-12 15:02:25 -08:00
|
|
|
// A common case seen in this code is that we enter this function
|
|
|
|
// with aObj being a Function object, whose parent is a Call
|
|
|
|
// object. Neither of those have object principals, so we can skip
|
|
|
|
// those objects here before we enter the below loop. That way we
|
|
|
|
// avoid wasting time checking properties of their classes etc in
|
|
|
|
// the loop.
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-02 17:23:26 -07:00
|
|
|
if (jsClass == &js::FunctionClass) {
|
2011-10-04 07:06:54 -07:00
|
|
|
aObj = js::GetObjectParent(aObj);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-12-12 15:02:25 -08:00
|
|
|
if (!aObj)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-12-12 15:02:25 -08:00
|
|
|
|
2011-10-04 07:06:54 -07:00
|
|
|
jsClass = js::GetObjectClass(aObj);
|
2007-12-12 15:02:25 -08:00
|
|
|
|
2011-09-02 17:23:26 -07:00
|
|
|
if (jsClass == &js::CallClass) {
|
2011-10-12 22:29:43 -07:00
|
|
|
aObj = js::GetObjectParentMaybeScope(aObj);
|
2007-12-12 15:02:25 -08:00
|
|
|
|
|
|
|
if (!aObj)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-12-12 15:02:25 -08:00
|
|
|
|
2011-10-04 07:06:54 -07:00
|
|
|
jsClass = js::GetObjectClass(aObj);
|
2007-12-12 15:02:25 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
// Note: jsClass is set before this loop, and also at the
|
|
|
|
// *end* of this loop.
|
2009-05-12 13:20:42 -07:00
|
|
|
|
2010-08-28 01:04:25 -07:00
|
|
|
if (IS_WRAPPER_CLASS(jsClass)) {
|
2009-05-12 13:20:42 -07:00
|
|
|
result = sXPConnect->GetPrincipal(aObj,
|
2012-06-28 14:47:55 -07:00
|
|
|
aAllowShortCircuit);
|
2009-05-12 13:20:42 -07:00
|
|
|
if (result) {
|
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-03-30 21:42:20 -07:00
|
|
|
} else {
|
|
|
|
nsISupports *priv;
|
|
|
|
if (!(~jsClass->flags & (JSCLASS_HAS_PRIVATE |
|
|
|
|
JSCLASS_PRIVATE_IS_NSISUPPORTS))) {
|
|
|
|
priv = (nsISupports *) js::GetObjectPrivate(aObj);
|
2012-05-22 06:46:20 -07:00
|
|
|
} else if (!UnwrapDOMObjectToISupports(aObj, priv)) {
|
2012-07-30 07:20:58 -07:00
|
|
|
priv = nullptr;
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-12-12 15:02:25 -08:00
|
|
|
if (aAllowShortCircuit) {
|
|
|
|
nsCOMPtr<nsIXPConnectWrappedNative> xpcWrapper =
|
|
|
|
do_QueryInterface(priv);
|
|
|
|
|
2007-12-21 11:06:29 -08:00
|
|
|
NS_ASSERTION(!xpcWrapper ||
|
|
|
|
!strcmp(jsClass->name, "XPCNativeWrapper"),
|
2007-12-12 15:02:25 -08:00
|
|
|
"Uh, an nsIXPConnectWrappedNative with the "
|
|
|
|
"wrong JSClass or getObjectOps hooks!");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIScriptObjectPrincipal> objPrin =
|
|
|
|
do_QueryInterface(priv);
|
|
|
|
|
|
|
|
if (objPrin) {
|
|
|
|
result = objPrin->GetPrincipal();
|
|
|
|
|
|
|
|
if (result) {
|
|
|
|
break;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-12 22:29:43 -07:00
|
|
|
aObj = js::GetObjectParentMaybeScope(aObj);
|
2007-12-12 15:02:25 -08:00
|
|
|
|
|
|
|
if (!aObj)
|
|
|
|
break;
|
|
|
|
|
2011-10-04 07:06:54 -07:00
|
|
|
jsClass = js::GetObjectClass(aObj);
|
2007-12-12 15:02:25 -08:00
|
|
|
} while (1);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-09-29 10:00:52 -07:00
|
|
|
if (aAllowShortCircuit) {
|
2012-06-28 14:47:55 -07:00
|
|
|
nsIPrincipal *principal = old_doGetObjectPrincipal(origObj, false);
|
2010-09-29 10:00:52 -07:00
|
|
|
|
2012-02-18 08:55:28 -08:00
|
|
|
// Because of inner window reuse, we can have objects with one principal
|
|
|
|
// living in a scope with a different (but same-origin) principal. So
|
|
|
|
// just check same-origin here.
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(CheckSameOriginPrincipal(result, principal)),
|
2010-09-29 10:00:52 -07:00
|
|
|
"Principal mismatch. Not good");
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return result;
|
|
|
|
}
|
2012-06-28 14:47:55 -07:00
|
|
|
#endif /* DEBUG */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
///////////////// Capabilities API /////////////////////
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScriptSecurityManager::IsCapabilityEnabled(const char *capability,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool *result)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2012-07-30 07:20:58 -07:00
|
|
|
JSStackFrame *fp = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSContext *cx = GetCurrentJSContext();
|
2012-07-30 07:20:58 -07:00
|
|
|
fp = cx ? JS_FrameIterator(cx, &fp) : nullptr;
|
2010-12-28 11:21:30 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!fp)
|
|
|
|
{
|
2012-06-28 14:47:55 -07:00
|
|
|
// No script code on stack. Allow access if and only if the subject
|
|
|
|
// principal is system.
|
|
|
|
nsresult ignored;
|
|
|
|
nsIPrincipal *subjectPrin = doGetSubjectPrincipal(&ignored);
|
|
|
|
*result = (!subjectPrin || subjectPrin == mSystemPrincipal);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-12-28 11:21:30 -08:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
*result = false;
|
2012-07-30 07:20:58 -07:00
|
|
|
nsIPrincipal* previousPrincipal = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
do
|
|
|
|
{
|
|
|
|
nsIPrincipal* principal = GetFramePrincipal(cx, fp, &rv);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
if (!principal)
|
|
|
|
continue;
|
|
|
|
// If caller has a different principal, stop looking up the stack.
|
|
|
|
if(previousPrincipal)
|
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isEqual = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
if(NS_FAILED(previousPrincipal->Equals(principal, &isEqual)) || !isEqual)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
previousPrincipal = principal;
|
|
|
|
|
|
|
|
// First check if the principal is even able to enable the
|
|
|
|
// given capability. If not, don't look any further.
|
2012-08-22 08:56:38 -07:00
|
|
|
int16_t canEnable;
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = principal->CanEnableCapability(capability, &canEnable);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
if (canEnable != nsIPrincipal::ENABLE_GRANTED &&
|
|
|
|
canEnable != nsIPrincipal::ENABLE_WITH_USER_PERMISSION)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
// Now see if the capability is enabled.
|
|
|
|
void *annotation = JS_GetFrameAnnotation(cx, fp);
|
|
|
|
rv = principal->IsCapabilityEnabled(capability, annotation, result);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
if (*result)
|
|
|
|
return NS_OK;
|
2011-05-11 09:11:40 -07:00
|
|
|
|
|
|
|
// Capabilities do not extend to calls into C/C++ and then back into
|
|
|
|
// the JS engine via JS_EvaluateScript or similar APIs.
|
|
|
|
if (JS_IsGlobalFrame(cx, fp))
|
|
|
|
break;
|
2012-07-30 07:20:58 -07:00
|
|
|
} while ((fp = JS_FrameIterator(cx, &fp)) != nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!previousPrincipal)
|
|
|
|
{
|
|
|
|
// No principals on the stack, all native code. Allow
|
|
|
|
// execution if the subject principal is the system principal.
|
|
|
|
|
|
|
|
return SubjectPrincipalIsSystem(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsScriptSecurityManager::FormatCapabilityString(nsAString& aCapability)
|
|
|
|
{
|
|
|
|
nsAutoString newcaps;
|
|
|
|
nsAutoString rawcap;
|
|
|
|
NS_NAMED_LITERAL_STRING(capdesc, "capdesc.");
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t pos;
|
|
|
|
int32_t index = kNotFound;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
NS_ASSERTION(kNotFound == -1, "Basic constant changed, algorithm broken!");
|
|
|
|
|
|
|
|
do {
|
|
|
|
pos = index+1;
|
|
|
|
index = aCapability.FindChar(' ', pos);
|
|
|
|
rawcap = Substring(aCapability, pos,
|
|
|
|
(index == kNotFound) ? index : index - pos);
|
|
|
|
|
|
|
|
nsXPIDLString capstr;
|
|
|
|
rv = sStrBundle->GetStringFromName(
|
|
|
|
nsPromiseFlatString(capdesc+rawcap).get(),
|
|
|
|
getter_Copies(capstr));
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
newcaps += capstr;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nsXPIDLString extensionCap;
|
|
|
|
const PRUnichar* formatArgs[] = { rawcap.get() };
|
|
|
|
rv = sStrBundle->FormatStringFromName(
|
|
|
|
NS_LITERAL_STRING("ExtensionCapability").get(),
|
|
|
|
formatArgs,
|
2011-10-10 22:50:08 -07:00
|
|
|
ArrayLength(formatArgs),
|
2007-03-22 10:30:00 -07:00
|
|
|
getter_Copies(extensionCap));
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
newcaps += extensionCap;
|
|
|
|
else
|
|
|
|
newcaps += rawcap;
|
|
|
|
}
|
|
|
|
|
|
|
|
newcaps += NS_LITERAL_STRING("\n");
|
|
|
|
} while (index != kNotFound);
|
|
|
|
|
|
|
|
aCapability = newcaps;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScriptSecurityManager::RequestCapability(nsIPrincipal* aPrincipal,
|
2012-08-22 08:56:38 -07:00
|
|
|
const char *capability, int16_t* canEnable)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (NS_FAILED(aPrincipal->CanEnableCapability(capability, canEnable)))
|
|
|
|
return NS_ERROR_FAILURE;
|
2012-05-02 14:57:34 -07:00
|
|
|
// The confirm dialog is no longer supported. All of this stuff is going away
|
|
|
|
// real soon now anyhow.
|
2007-03-22 10:30:00 -07:00
|
|
|
if (*canEnable == nsIPrincipal::ENABLE_WITH_USER_PERMISSION)
|
2012-05-02 14:57:34 -07:00
|
|
|
*canEnable = nsIPrincipal::ENABLE_DENIED;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScriptSecurityManager::EnableCapability(const char *capability)
|
|
|
|
{
|
|
|
|
JSContext *cx = GetCurrentJSContext();
|
|
|
|
JSStackFrame *fp;
|
|
|
|
|
|
|
|
//-- Error checks for capability string length (200)
|
|
|
|
if(PL_strlen(capability)>200)
|
|
|
|
{
|
|
|
|
static const char msg[] = "Capability name too long";
|
|
|
|
SetPendingException(cx, msg);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-- Check capability string for valid characters
|
|
|
|
//
|
|
|
|
// Logically we might have wanted this in nsPrincipal, but performance
|
|
|
|
// worries dictate it can't go in IsCapabilityEnabled() and we may have
|
|
|
|
// to show the capability on a dialog before we call the principal's
|
|
|
|
// EnableCapability().
|
|
|
|
//
|
|
|
|
// We don't need to validate the capability string on the other APIs
|
|
|
|
// available to web content. Without the ability to enable junk then
|
|
|
|
// isPrivilegeEnabled, disablePrivilege, and revertPrivilege all do
|
|
|
|
// the right thing (effectively nothing) when passed unallowed chars.
|
|
|
|
for (const char *ch = capability; *ch; ++ch)
|
|
|
|
{
|
|
|
|
if (!NS_IS_ALPHA(*ch) && *ch != ' ' && !NS_IS_DIGIT(*ch)
|
|
|
|
&& *ch != '_' && *ch != '-' && *ch != '.')
|
|
|
|
{
|
|
|
|
static const char msg[] = "Invalid character in capability name";
|
|
|
|
SetPendingException(cx, msg);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsIPrincipal* principal = GetPrincipalAndFrame(cx, &fp, &rv);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
if (!principal)
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
void *annotation = JS_GetFrameAnnotation(cx, fp);
|
2011-09-28 23:19:26 -07:00
|
|
|
bool enabled;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(principal->IsCapabilityEnabled(capability, annotation,
|
|
|
|
&enabled)))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
if (enabled)
|
|
|
|
return NS_OK;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int16_t canEnable;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(RequestCapability(principal, capability, &canEnable)))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
if (canEnable != nsIPrincipal::ENABLE_GRANTED)
|
|
|
|
{
|
2008-03-18 17:27:56 -07:00
|
|
|
nsCAutoString val;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool hasCert;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv;
|
|
|
|
principal->GetHasCertificate(&hasCert);
|
|
|
|
if (hasCert)
|
|
|
|
rv = principal->GetPrettyName(val);
|
|
|
|
else
|
2008-03-18 17:27:56 -07:00
|
|
|
rv = GetPrincipalDomainOrigin(principal, val);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
NS_ConvertUTF8toUTF16 location(val);
|
|
|
|
NS_ConvertUTF8toUTF16 cap(capability);
|
|
|
|
const PRUnichar *formatStrings[] = { location.get(), cap.get() };
|
|
|
|
|
|
|
|
nsXPIDLString message;
|
|
|
|
rv = sStrBundle->FormatStringFromName(NS_LITERAL_STRING("EnableCapabilityDenied").get(),
|
|
|
|
formatStrings,
|
2011-10-10 22:50:08 -07:00
|
|
|
ArrayLength(formatStrings),
|
2007-03-22 10:30:00 -07:00
|
|
|
getter_Copies(message));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
SetPendingException(cx, message.get());
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE; // XXX better error code?
|
|
|
|
}
|
|
|
|
if (NS_FAILED(principal->EnableCapability(capability, &annotation)))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
JS_SetFrameAnnotation(cx, fp, annotation);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////
|
|
|
|
// Methods implementing nsIXPCSecurityManager //
|
|
|
|
////////////////////////////////////////////////
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScriptSecurityManager::CanCreateWrapper(JSContext *cx,
|
|
|
|
const nsIID &aIID,
|
|
|
|
nsISupports *aObj,
|
|
|
|
nsIClassInfo *aClassInfo,
|
|
|
|
void **aPolicy)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_CAPS_CanCreateWrapper
|
|
|
|
char* iidStr = aIID.ToString();
|
|
|
|
printf("### CanCreateWrapper(%s) ", iidStr);
|
2009-07-29 10:54:44 -07:00
|
|
|
NS_Free(iidStr);
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
// XXX Special case for nsIXPCException ?
|
2012-07-30 07:20:58 -07:00
|
|
|
ClassInfoData objClassInfo = ClassInfoData(aClassInfo, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (objClassInfo.IsDOMClass())
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_CAPS_CanCreateWrapper
|
|
|
|
printf("DOM class - GRANTED.\n");
|
|
|
|
#endif
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--See if the object advertises a non-default level of access
|
|
|
|
// using nsISecurityCheckedComponent
|
|
|
|
nsCOMPtr<nsISecurityCheckedComponent> checkedComponent =
|
|
|
|
do_QueryInterface(aObj);
|
|
|
|
|
|
|
|
nsXPIDLCString objectSecurityLevel;
|
|
|
|
if (checkedComponent)
|
|
|
|
checkedComponent->CanCreateWrapper((nsIID *)&aIID, getter_Copies(objectSecurityLevel));
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
nsresult rv = CheckXPCPermissions(cx, aObj, nullptr, nullptr, objectSecurityLevel);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
{
|
|
|
|
//-- Access denied, report an error
|
2008-07-28 23:03:19 -07:00
|
|
|
NS_ConvertUTF8toUTF16 strName("CreateWrapperDenied");
|
|
|
|
nsCAutoString origin;
|
|
|
|
nsresult rv2;
|
|
|
|
nsIPrincipal* subjectPrincipal = doGetSubjectPrincipal(&rv2);
|
|
|
|
if (NS_SUCCEEDED(rv2) && subjectPrincipal) {
|
|
|
|
GetPrincipalDomainOrigin(subjectPrincipal, origin);
|
|
|
|
}
|
|
|
|
NS_ConvertUTF8toUTF16 originUnicode(origin);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ConvertUTF8toUTF16 className(objClassInfo.GetName());
|
2008-07-28 23:03:19 -07:00
|
|
|
const PRUnichar* formatStrings[] = {
|
|
|
|
className.get(),
|
|
|
|
originUnicode.get()
|
|
|
|
};
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t length = ArrayLength(formatStrings);
|
2008-07-28 23:03:19 -07:00
|
|
|
if (originUnicode.IsEmpty()) {
|
|
|
|
--length;
|
|
|
|
} else {
|
|
|
|
strName.AppendLiteral("ForOrigin");
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
nsXPIDLString errorMsg;
|
|
|
|
// We need to keep our existing failure rv and not override it
|
|
|
|
// with a likely success code from the following string bundle
|
|
|
|
// call in order to throw the correct security exception later.
|
2008-07-28 23:03:19 -07:00
|
|
|
rv2 = sStrBundle->FormatStringFromName(strName.get(),
|
|
|
|
formatStrings,
|
|
|
|
length,
|
|
|
|
getter_Copies(errorMsg));
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv2, rv2);
|
|
|
|
|
|
|
|
SetPendingException(cx, errorMsg.get());
|
|
|
|
|
|
|
|
#ifdef DEBUG_CAPS_CanCreateWrapper
|
|
|
|
printf("DENIED.\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("GRANTED.\n");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScriptSecurityManager::CanCreateInstance(JSContext *cx,
|
|
|
|
const nsCID &aCID)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_CAPS_CanCreateInstance
|
|
|
|
char* cidStr = aCID.ToString();
|
|
|
|
printf("### CanCreateInstance(%s) ", cidStr);
|
2009-07-29 10:54:44 -07:00
|
|
|
NS_Free(cidStr);
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
nsresult rv = CheckXPCPermissions(nullptr, nullptr, nullptr, nullptr, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
{
|
|
|
|
//-- Access denied, report an error
|
|
|
|
nsCAutoString errorMsg("Permission denied to create instance of class. CID=");
|
2008-01-11 20:30:42 -08:00
|
|
|
char cidStr[NSID_LENGTH];
|
|
|
|
aCID.ToProvidedString(cidStr);
|
2007-03-22 10:30:00 -07:00
|
|
|
errorMsg.Append(cidStr);
|
|
|
|
SetPendingException(cx, errorMsg.get());
|
|
|
|
|
|
|
|
#ifdef DEBUG_CAPS_CanCreateInstance
|
|
|
|
printf("DENIED\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("GRANTED\n");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScriptSecurityManager::CanGetService(JSContext *cx,
|
|
|
|
const nsCID &aCID)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_CAPS_CanGetService
|
|
|
|
char* cidStr = aCID.ToString();
|
|
|
|
printf("### CanGetService(%s) ", cidStr);
|
2009-07-29 10:54:44 -07:00
|
|
|
NS_Free(cidStr);
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
nsresult rv = CheckXPCPermissions(nullptr, nullptr, nullptr, nullptr, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
{
|
|
|
|
//-- Access denied, report an error
|
|
|
|
nsCAutoString errorMsg("Permission denied to get service. CID=");
|
2008-01-11 20:30:42 -08:00
|
|
|
char cidStr[NSID_LENGTH];
|
|
|
|
aCID.ToProvidedString(cidStr);
|
2007-03-22 10:30:00 -07:00
|
|
|
errorMsg.Append(cidStr);
|
|
|
|
SetPendingException(cx, errorMsg.get());
|
|
|
|
|
|
|
|
#ifdef DEBUG_CAPS_CanGetService
|
|
|
|
printf("DENIED\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("GRANTED\n");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsScriptSecurityManager::CanAccess(uint32_t aAction,
|
2008-01-15 07:50:57 -08:00
|
|
|
nsAXPCNativeCallContext* aCallContext,
|
2007-03-22 10:30:00 -07:00
|
|
|
JSContext* cx,
|
|
|
|
JSObject* aJSObject,
|
|
|
|
nsISupports* aObj,
|
|
|
|
nsIClassInfo* aClassInfo,
|
2010-07-14 23:19:36 -07:00
|
|
|
jsid aPropertyName,
|
2007-03-22 10:30:00 -07:00
|
|
|
void** aPolicy)
|
|
|
|
{
|
|
|
|
return CheckPropertyAccessImpl(aAction, aCallContext, cx,
|
2012-07-17 22:28:47 -07:00
|
|
|
aJSObject, aObj, aClassInfo,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr, aPropertyName, aPolicy);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2009-08-06 20:26:33 -07:00
|
|
|
nsScriptSecurityManager::CheckXPCPermissions(JSContext* cx,
|
|
|
|
nsISupports* aObj, JSObject* aJSObject,
|
2009-05-13 15:01:01 -07:00
|
|
|
nsIPrincipal* aSubjectPrincipal,
|
2007-03-22 10:30:00 -07:00
|
|
|
const char* aObjectSecurityLevel)
|
|
|
|
{
|
|
|
|
//-- Check for the all-powerful UniversalXPConnect privilege
|
2011-09-28 23:19:26 -07:00
|
|
|
bool ok = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_SUCCEEDED(IsCapabilityEnabled("UniversalXPConnect", &ok)) && ok)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
//-- If the object implements nsISecurityCheckedComponent, it has a non-default policy.
|
|
|
|
if (aObjectSecurityLevel)
|
|
|
|
{
|
|
|
|
if (PL_strcasecmp(aObjectSecurityLevel, "allAccess") == 0)
|
|
|
|
return NS_OK;
|
2009-08-06 20:26:33 -07:00
|
|
|
if (cx && PL_strcasecmp(aObjectSecurityLevel, "sameOrigin") == 0)
|
2009-05-13 15:01:01 -07:00
|
|
|
{
|
2009-08-06 20:26:33 -07:00
|
|
|
nsresult rv;
|
|
|
|
if (!aJSObject)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIXPConnectWrappedJS> xpcwrappedjs =
|
|
|
|
do_QueryInterface(aObj);
|
|
|
|
if (xpcwrappedjs)
|
|
|
|
{
|
|
|
|
rv = xpcwrappedjs->GetJSObject(&aJSObject);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
}
|
2009-05-13 15:01:01 -07:00
|
|
|
|
2009-08-06 20:26:33 -07:00
|
|
|
if (!aSubjectPrincipal)
|
2009-05-13 15:01:01 -07:00
|
|
|
{
|
2009-08-06 20:26:33 -07:00
|
|
|
// No subject principal passed in. Compute it.
|
|
|
|
aSubjectPrincipal = GetSubjectPrincipal(cx, &rv);
|
2009-05-13 15:01:01 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2009-08-06 20:26:33 -07:00
|
|
|
}
|
|
|
|
if (aSubjectPrincipal && aJSObject)
|
|
|
|
{
|
|
|
|
nsIPrincipal* objectPrincipal = doGetObjectPrincipal(aJSObject);
|
|
|
|
|
|
|
|
// Only do anything if we have both a subject and object
|
|
|
|
// principal.
|
|
|
|
if (objectPrincipal)
|
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
bool subsumes;
|
2009-08-06 20:26:33 -07:00
|
|
|
rv = aSubjectPrincipal->Subsumes(objectPrincipal, &subsumes);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (subsumes)
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2009-05-13 15:01:01 -07:00
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
else if (PL_strcasecmp(aObjectSecurityLevel, "noAccess") != 0)
|
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
bool canAccess = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_SUCCEEDED(IsCapabilityEnabled(aObjectSecurityLevel, &canAccess)) &&
|
|
|
|
canAccess)
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-- Access tests failed
|
|
|
|
return NS_ERROR_DOM_XPCONNECT_ACCESS_DENIED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////
|
|
|
|
// Method implementing nsIChannelEventSink //
|
|
|
|
/////////////////////////////////////////////
|
|
|
|
NS_IMETHODIMP
|
2010-08-04 19:15:55 -07:00
|
|
|
nsScriptSecurityManager::AsyncOnChannelRedirect(nsIChannel* oldChannel,
|
|
|
|
nsIChannel* newChannel,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t redirFlags,
|
2010-08-04 19:15:55 -07:00
|
|
|
nsIAsyncVerifyRedirectCallback *cb)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIPrincipal> oldPrincipal;
|
|
|
|
GetChannelPrincipal(oldChannel, getter_AddRefs(oldPrincipal));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> newURI;
|
|
|
|
newChannel->GetURI(getter_AddRefs(newURI));
|
2008-11-25 17:50:04 -08:00
|
|
|
nsCOMPtr<nsIURI> newOriginalURI;
|
|
|
|
newChannel->GetOriginalURI(getter_AddRefs(newOriginalURI));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-11-25 17:50:04 -08:00
|
|
|
NS_ENSURE_STATE(oldPrincipal && newURI && newOriginalURI);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
const uint32_t flags =
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIScriptSecurityManager::LOAD_IS_AUTOMATIC_DOCUMENT_REPLACEMENT |
|
|
|
|
nsIScriptSecurityManager::DISALLOW_SCRIPT;
|
2008-11-25 17:50:04 -08:00
|
|
|
nsresult rv = CheckLoadURIWithPrincipal(oldPrincipal, newURI, flags);
|
|
|
|
if (NS_SUCCEEDED(rv) && newOriginalURI != newURI) {
|
|
|
|
rv = CheckLoadURIWithPrincipal(oldPrincipal, newOriginalURI, flags);
|
|
|
|
}
|
2010-08-04 19:15:55 -07:00
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
cb->OnRedirectVerifyCallback(NS_OK);
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////
|
|
|
|
// Method implementing nsIObserver //
|
|
|
|
/////////////////////////////////////
|
2011-06-19 20:00:16 -07:00
|
|
|
const char sJSEnabledPrefName[] = "javascript.enabled";
|
|
|
|
const char sFileOriginPolicyPrefName[] =
|
|
|
|
"security.fileuri.strict_origin_policy";
|
2007-03-22 10:30:00 -07:00
|
|
|
static const char sPrincipalPrefix[] = "capability.principal";
|
|
|
|
static const char sPolicyPrefix[] = "capability.policy.";
|
|
|
|
|
2011-06-19 20:00:16 -07:00
|
|
|
static const char* kObservedPrefs[] = {
|
|
|
|
sJSEnabledPrefName,
|
|
|
|
sFileOriginPolicyPrefName,
|
|
|
|
sPolicyPrefix,
|
|
|
|
sPrincipalPrefix,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr
|
2011-06-19 20:00:16 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScriptSecurityManager::Observe(nsISupports* aObject, const char* aTopic,
|
|
|
|
const PRUnichar* aMessage)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
NS_ConvertUTF16toUTF8 messageStr(aMessage);
|
|
|
|
const char *message = messageStr.get();
|
|
|
|
|
|
|
|
static const char jsPrefix[] = "javascript.";
|
2007-09-06 00:02:57 -07:00
|
|
|
static const char securityPrefix[] = "security.";
|
|
|
|
if ((PL_strncmp(message, jsPrefix, sizeof(jsPrefix)-1) == 0) ||
|
|
|
|
(PL_strncmp(message, securityPrefix, sizeof(securityPrefix)-1) == 0) )
|
|
|
|
{
|
|
|
|
ScriptSecurityPrefChanged();
|
|
|
|
}
|
|
|
|
else if (PL_strncmp(message, sPolicyPrefix, sizeof(sPolicyPrefix)-1) == 0)
|
|
|
|
{
|
|
|
|
// This will force re-initialization of the pref table
|
2011-10-17 07:59:28 -07:00
|
|
|
mPolicyPrefsChanged = true;
|
2007-09-06 00:02:57 -07:00
|
|
|
}
|
|
|
|
else if ((PL_strncmp(message, sPrincipalPrefix, sizeof(sPrincipalPrefix)-1) == 0) &&
|
|
|
|
!mIsWritingPrefs)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
static const char id[] = "id";
|
|
|
|
char* lastDot = PL_strrchr(message, '.');
|
|
|
|
//-- This check makes sure the string copy below doesn't overwrite its bounds
|
|
|
|
if(PL_strlen(lastDot) >= sizeof(id))
|
|
|
|
{
|
|
|
|
PL_strcpy(lastDot + 1, id);
|
|
|
|
const char** idPrefArray = (const char**)&message;
|
2010-06-08 16:43:54 -07:00
|
|
|
rv = InitPrincipals(1, idPrefArray);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////
|
|
|
|
// Constructor, Destructor, Initialization //
|
|
|
|
/////////////////////////////////////////////
|
|
|
|
nsScriptSecurityManager::nsScriptSecurityManager(void)
|
2012-07-30 07:20:58 -07:00
|
|
|
: mOriginToPolicyMap(nullptr),
|
|
|
|
mDefaultPolicy(nullptr),
|
|
|
|
mCapabilities(nullptr),
|
2011-10-17 07:59:28 -07:00
|
|
|
mPrefInitialized(false),
|
|
|
|
mIsJavaScriptEnabled(false),
|
|
|
|
mIsWritingPrefs(false),
|
|
|
|
mPolicyPrefsChanged(true)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-04-11 17:17:44 -07:00
|
|
|
MOZ_STATIC_ASSERT(sizeof(intptr_t) == sizeof(void*),
|
|
|
|
"intptr_t and void* have different lengths on this platform. "
|
|
|
|
"This may cause a security failure with the SecurityLevel union.");
|
2007-03-22 10:30:00 -07:00
|
|
|
mPrincipals.Init(31);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsScriptSecurityManager::Init()
|
|
|
|
{
|
2010-04-11 06:55:24 -07:00
|
|
|
nsXPConnect* xpconnect = nsXPConnect::GetXPConnect();
|
|
|
|
if (!xpconnect)
|
|
|
|
return NS_ERROR_FAILURE;
|
2008-10-14 07:16:25 -07:00
|
|
|
|
2010-04-11 06:55:24 -07:00
|
|
|
NS_ADDREF(sXPConnect = xpconnect);
|
|
|
|
NS_ADDREF(sJSContextStack = xpconnect);
|
2008-10-14 07:16:25 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
JSContext* cx = GetSafeJSContext();
|
|
|
|
if (!cx) return NS_ERROR_FAILURE; // this can happen of xpt loading fails
|
|
|
|
|
|
|
|
::JS_BeginRequest(cx);
|
2010-07-14 23:19:36 -07:00
|
|
|
if (sEnabledID == JSID_VOID)
|
2011-05-17 12:15:12 -07:00
|
|
|
sEnabledID = INTERNED_STRING_TO_JSID(cx, ::JS_InternString(cx, "enabled"));
|
2007-03-22 10:30:00 -07:00
|
|
|
::JS_EndRequest(cx);
|
|
|
|
|
2009-01-07 20:42:15 -08:00
|
|
|
InitPrefs();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-04-11 06:55:24 -07:00
|
|
|
nsresult rv = CallGetService(NS_IOSERVICE_CONTRACTID, &sIOService);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-05-14 02:24:41 -07:00
|
|
|
nsCOMPtr<nsIStringBundleService> bundleService =
|
|
|
|
mozilla::services::GetStringBundleService();
|
|
|
|
if (!bundleService)
|
|
|
|
return NS_ERROR_FAILURE;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
rv = bundleService->CreateBundle("chrome://global/locale/security/caps.properties", &sStrBundle);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Create our system principal singleton
|
|
|
|
nsRefPtr<nsSystemPrincipal> system = new nsSystemPrincipal();
|
|
|
|
NS_ENSURE_TRUE(system, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
mSystemPrincipal = system;
|
|
|
|
|
|
|
|
//-- Register security check callback in the JS engine
|
|
|
|
// Currently this is used to control access to function.caller
|
|
|
|
nsCOMPtr<nsIJSRuntimeService> runtimeService =
|
2010-04-11 06:55:24 -07:00
|
|
|
do_QueryInterface(sXPConnect, &rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = runtimeService->GetRuntime(&sRuntime);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-03-09 01:48:50 -08:00
|
|
|
static const JSSecurityCallbacks securityCallbacks = {
|
2010-03-08 00:24:50 -08:00
|
|
|
CheckObjectAccess,
|
2012-03-09 01:48:50 -08:00
|
|
|
ObjectPrincipalFinder,
|
2012-06-28 14:47:55 -07:00
|
|
|
ContentSecurityPolicyPermitsJSAction
|
2008-09-05 16:26:04 -07:00
|
|
|
};
|
|
|
|
|
2012-03-09 01:48:50 -08:00
|
|
|
MOZ_ASSERT(!JS_GetSecurityCallbacks(sRuntime));
|
|
|
|
JS_SetSecurityCallbacks(sRuntime, &securityCallbacks);
|
|
|
|
JS_InitDestroyPrincipalsCallback(sRuntime, nsJSPrincipals::Destroy);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-03-09 01:48:50 -08:00
|
|
|
JS_SetTrustedPrincipals(sRuntime, system);
|
2011-06-30 09:26:56 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-07-26 12:33:45 -07:00
|
|
|
static StaticRefPtr<nsScriptSecurityManager> gScriptSecMan;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
jsid nsScriptSecurityManager::sEnabledID = JSID_VOID;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsScriptSecurityManager::~nsScriptSecurityManager(void)
|
|
|
|
{
|
2011-06-19 20:00:16 -07:00
|
|
|
Preferences::RemoveObservers(this, kObservedPrefs);
|
2007-03-22 10:30:00 -07:00
|
|
|
delete mOriginToPolicyMap;
|
|
|
|
if(mDefaultPolicy)
|
|
|
|
mDefaultPolicy->Drop();
|
|
|
|
delete mCapabilities;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsScriptSecurityManager::Shutdown()
|
|
|
|
{
|
|
|
|
if (sRuntime) {
|
2012-03-09 01:48:50 -08:00
|
|
|
JS_SetSecurityCallbacks(sRuntime, NULL);
|
2011-06-30 09:26:56 -07:00
|
|
|
JS_SetTrustedPrincipals(sRuntime, NULL);
|
2012-07-30 07:20:58 -07:00
|
|
|
sRuntime = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-07-14 23:19:36 -07:00
|
|
|
sEnabledID = JSID_VOID;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_IF_RELEASE(sIOService);
|
|
|
|
NS_IF_RELEASE(sXPConnect);
|
2008-10-14 07:16:25 -07:00
|
|
|
NS_IF_RELEASE(sJSContextStack);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IF_RELEASE(sStrBundle);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsScriptSecurityManager *
|
|
|
|
nsScriptSecurityManager::GetScriptSecurityManager()
|
|
|
|
{
|
|
|
|
if (!gScriptSecMan)
|
|
|
|
{
|
2012-06-19 10:20:34 -07:00
|
|
|
nsRefPtr<nsScriptSecurityManager> ssManager = new nsScriptSecurityManager();
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv;
|
|
|
|
rv = ssManager->Init();
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to initialize nsScriptSecurityManager");
|
|
|
|
if (NS_FAILED(rv)) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
rv = sXPConnect->SetDefaultSecurityManager(ssManager,
|
|
|
|
nsIXPCSecurityManager::HOOK_ALL);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to install xpconnect security manager!");
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-06-19 10:20:34 -07:00
|
|
|
ClearOnShutdown(&gScriptSecMan);
|
2007-03-22 10:30:00 -07:00
|
|
|
gScriptSecMan = ssManager;
|
|
|
|
}
|
|
|
|
return gScriptSecMan;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Currently this nsGenericFactory constructor is used only from FastLoad
|
|
|
|
// (XPCOM object deserialization) code, when "creating" the system principal
|
|
|
|
// singleton.
|
|
|
|
nsSystemPrincipal *
|
|
|
|
nsScriptSecurityManager::SystemPrincipalSingletonConstructor()
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
nsIPrincipal *sysprin = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (gScriptSecMan)
|
|
|
|
NS_ADDREF(sysprin = gScriptSecMan->mSystemPrincipal);
|
2007-07-08 00:08:04 -07:00
|
|
|
return static_cast<nsSystemPrincipal*>(sysprin);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsScriptSecurityManager::InitPolicies()
|
|
|
|
{
|
|
|
|
// Clear any policies cached on XPConnect wrappers
|
|
|
|
NS_ENSURE_STATE(sXPConnect);
|
|
|
|
nsresult rv = sXPConnect->ClearAllWrappedNativeSecurityPolicies();
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
//-- Clear mOriginToPolicyMap: delete mapped DomainEntry items,
|
|
|
|
//-- whose dtor decrements refcount of stored DomainPolicy object
|
|
|
|
delete mOriginToPolicyMap;
|
|
|
|
|
|
|
|
//-- Marks all the survivor DomainPolicy objects (those cached
|
|
|
|
//-- by nsPrincipal objects) as invalid: they will be released
|
|
|
|
//-- on first nsPrincipal::GetSecurityPolicy() attempt.
|
|
|
|
DomainPolicy::InvalidateAll();
|
|
|
|
|
|
|
|
//-- Release old default policy
|
|
|
|
if(mDefaultPolicy) {
|
|
|
|
mDefaultPolicy->Drop();
|
2012-07-30 07:20:58 -07:00
|
|
|
mDefaultPolicy = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//-- Initialize a new mOriginToPolicyMap
|
|
|
|
mOriginToPolicyMap =
|
2012-07-30 07:20:58 -07:00
|
|
|
new nsObjectHashtable(nullptr, nullptr, DeleteDomainEntry, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!mOriginToPolicyMap)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
//-- Create, refcount and initialize a new default policy
|
|
|
|
mDefaultPolicy = new DomainPolicy();
|
|
|
|
if (!mDefaultPolicy)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
mDefaultPolicy->Hold();
|
|
|
|
if (!mDefaultPolicy->Init())
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
//-- Initialize the table of security levels
|
|
|
|
if (!mCapabilities)
|
|
|
|
{
|
|
|
|
mCapabilities =
|
2012-07-30 07:20:58 -07:00
|
|
|
new nsObjectHashtable(nullptr, nullptr, DeleteCapability, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!mCapabilities)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a JS context - we need it to create internalized strings later.
|
|
|
|
JSContext* cx = GetSafeJSContext();
|
|
|
|
NS_ASSERTION(cx, "failed to get JS context");
|
2008-10-14 07:16:25 -07:00
|
|
|
AutoCxPusher autoPusher(sJSContextStack, cx);
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = InitDomainPolicy(cx, "default", mDefaultPolicy);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-06-19 20:00:16 -07:00
|
|
|
nsAdoptingCString policyNames =
|
|
|
|
Preferences::GetCString("capability.policy.policynames");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-06-19 20:00:16 -07:00
|
|
|
nsAdoptingCString defaultPolicyNames =
|
|
|
|
Preferences::GetCString("capability.policy.default_policynames");
|
2007-03-22 10:30:00 -07:00
|
|
|
policyNames += NS_LITERAL_CSTRING(" ") + defaultPolicyNames;
|
|
|
|
|
|
|
|
//-- Initialize domain policies
|
|
|
|
char* policyCurrent = policyNames.BeginWriting();
|
2011-09-28 23:19:26 -07:00
|
|
|
bool morePolicies = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
while (morePolicies)
|
|
|
|
{
|
|
|
|
while(*policyCurrent == ' ' || *policyCurrent == ',')
|
|
|
|
policyCurrent++;
|
|
|
|
if (*policyCurrent == '\0')
|
|
|
|
break;
|
|
|
|
char* nameBegin = policyCurrent;
|
|
|
|
|
|
|
|
while(*policyCurrent != '\0' && *policyCurrent != ' ' && *policyCurrent != ',')
|
|
|
|
policyCurrent++;
|
|
|
|
|
|
|
|
morePolicies = (*policyCurrent != '\0');
|
|
|
|
*policyCurrent = '\0';
|
|
|
|
policyCurrent++;
|
|
|
|
|
|
|
|
nsCAutoString sitesPrefName(
|
|
|
|
NS_LITERAL_CSTRING(sPolicyPrefix) +
|
2008-10-08 15:05:25 -07:00
|
|
|
nsDependentCString(nameBegin) +
|
|
|
|
NS_LITERAL_CSTRING(".sites"));
|
2011-06-19 20:00:16 -07:00
|
|
|
nsAdoptingCString domainList =
|
|
|
|
Preferences::GetCString(sitesPrefName.get());
|
|
|
|
if (!domainList) {
|
2007-03-22 10:30:00 -07:00
|
|
|
continue;
|
2011-06-19 20:00:16 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
DomainPolicy* domainPolicy = new DomainPolicy();
|
|
|
|
if (!domainPolicy)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
if (!domainPolicy->Init())
|
|
|
|
{
|
|
|
|
delete domainPolicy;
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
domainPolicy->Hold();
|
|
|
|
//-- Parse list of sites and create an entry in mOriginToPolicyMap for each
|
|
|
|
char* domainStart = domainList.BeginWriting();
|
|
|
|
char* domainCurrent = domainStart;
|
2012-07-30 07:20:58 -07:00
|
|
|
char* lastDot = nullptr;
|
|
|
|
char* nextToLastDot = nullptr;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool moreDomains = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
while (moreDomains)
|
|
|
|
{
|
|
|
|
if (*domainCurrent == ' ' || *domainCurrent == '\0')
|
|
|
|
{
|
|
|
|
moreDomains = (*domainCurrent != '\0');
|
|
|
|
*domainCurrent = '\0';
|
|
|
|
nsCStringKey key(nextToLastDot ? nextToLastDot+1 : domainStart);
|
|
|
|
DomainEntry *newEntry = new DomainEntry(domainStart, domainPolicy);
|
|
|
|
if (!newEntry)
|
|
|
|
{
|
|
|
|
domainPolicy->Drop();
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
newEntry->mPolicyName_DEBUG = nameBegin;
|
|
|
|
#endif
|
|
|
|
DomainEntry *existingEntry = (DomainEntry *)
|
|
|
|
mOriginToPolicyMap->Get(&key);
|
|
|
|
if (!existingEntry)
|
|
|
|
mOriginToPolicyMap->Put(&key, newEntry);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (existingEntry->Matches(domainStart))
|
|
|
|
{
|
|
|
|
newEntry->mNext = existingEntry;
|
|
|
|
mOriginToPolicyMap->Put(&key, newEntry);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (existingEntry->mNext)
|
|
|
|
{
|
|
|
|
if (existingEntry->mNext->Matches(domainStart))
|
|
|
|
{
|
|
|
|
newEntry->mNext = existingEntry->mNext;
|
|
|
|
existingEntry->mNext = newEntry;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
existingEntry = existingEntry->mNext;
|
|
|
|
}
|
|
|
|
if (!existingEntry->mNext)
|
|
|
|
existingEntry->mNext = newEntry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
domainStart = domainCurrent + 1;
|
2012-07-30 07:20:58 -07:00
|
|
|
lastDot = nextToLastDot = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else if (*domainCurrent == '.')
|
|
|
|
{
|
|
|
|
nextToLastDot = lastDot;
|
|
|
|
lastDot = domainCurrent;
|
|
|
|
}
|
|
|
|
domainCurrent++;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = InitDomainPolicy(cx, nameBegin, domainPolicy);
|
|
|
|
domainPolicy->Drop();
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset the "dirty" flag
|
2011-10-17 07:59:28 -07:00
|
|
|
mPolicyPrefsChanged = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#ifdef DEBUG_CAPS_HACKER
|
|
|
|
PrintPolicyDB();
|
|
|
|
#endif
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsScriptSecurityManager::InitDomainPolicy(JSContext* cx,
|
|
|
|
const char* aPolicyName,
|
|
|
|
DomainPolicy* aDomainPolicy)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCAutoString policyPrefix(NS_LITERAL_CSTRING(sPolicyPrefix) +
|
|
|
|
nsDependentCString(aPolicyName) +
|
|
|
|
NS_LITERAL_CSTRING("."));
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t prefixLength = policyPrefix.Length() - 1; // subtract the '.'
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t prefCount;
|
2007-03-22 10:30:00 -07:00
|
|
|
char** prefNames;
|
2011-06-19 20:00:16 -07:00
|
|
|
nsIPrefBranch* branch = Preferences::GetRootBranch();
|
|
|
|
NS_ASSERTION(branch, "failed to get the root pref branch");
|
|
|
|
rv = branch->GetChildList(policyPrefix.get(), &prefCount, &prefNames);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
if (prefCount == 0)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
//-- Populate the policy
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t currentPref = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
for (; currentPref < prefCount; currentPref++)
|
|
|
|
{
|
|
|
|
// Get the class name
|
|
|
|
const char* start = prefNames[currentPref] + prefixLength + 1;
|
|
|
|
char* end = PL_strchr(start, '.');
|
|
|
|
if (!end) // malformed pref, bail on this one
|
|
|
|
continue;
|
|
|
|
static const char sitesStr[] = "sites";
|
|
|
|
|
|
|
|
// We dealt with "sites" in InitPolicies(), so no need to do
|
|
|
|
// that again...
|
|
|
|
if (PL_strncmp(start, sitesStr, sizeof(sitesStr)-1) == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Get the pref value
|
2011-06-19 20:00:16 -07:00
|
|
|
nsAdoptingCString prefValue =
|
|
|
|
Preferences::GetCString(prefNames[currentPref]);
|
|
|
|
if (!prefValue) {
|
2007-03-22 10:30:00 -07:00
|
|
|
continue;
|
2011-06-19 20:00:16 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
SecurityLevel secLevel;
|
|
|
|
if (PL_strcasecmp(prefValue, "noAccess") == 0)
|
|
|
|
secLevel.level = SCRIPT_SECURITY_NO_ACCESS;
|
|
|
|
else if (PL_strcasecmp(prefValue, "allAccess") == 0)
|
|
|
|
secLevel.level = SCRIPT_SECURITY_ALL_ACCESS;
|
|
|
|
else if (PL_strcasecmp(prefValue, "sameOrigin") == 0)
|
|
|
|
secLevel.level = SCRIPT_SECURITY_SAME_ORIGIN_ACCESS;
|
|
|
|
else
|
|
|
|
{ //-- pref value is the name of a capability
|
|
|
|
nsCStringKey secLevelKey(prefValue);
|
|
|
|
secLevel.capability =
|
2007-07-08 00:08:04 -07:00
|
|
|
reinterpret_cast<char*>(mCapabilities->Get(&secLevelKey));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!secLevel.capability)
|
|
|
|
{
|
|
|
|
secLevel.capability = NS_strdup(prefValue);
|
|
|
|
if (!secLevel.capability)
|
|
|
|
break;
|
|
|
|
mCapabilities->Put(&secLevelKey,
|
|
|
|
secLevel.capability);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*end = '\0';
|
|
|
|
// Find or store this class in the classes table
|
|
|
|
ClassPolicy* cpolicy =
|
2007-07-08 00:08:04 -07:00
|
|
|
static_cast<ClassPolicy*>
|
|
|
|
(PL_DHashTableOperate(aDomainPolicy, start,
|
2007-03-22 10:30:00 -07:00
|
|
|
PL_DHASH_ADD));
|
|
|
|
if (!cpolicy)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// If this is the wildcard class (class '*'), save it in mWildcardPolicy
|
|
|
|
// (we leave it stored in the hashtable too to take care of the cleanup)
|
|
|
|
if ((*start == '*') && (end == start + 1)) {
|
|
|
|
aDomainPolicy->mWildcardPolicy = cpolicy;
|
|
|
|
|
|
|
|
// Make sure that cpolicy knows about aDomainPolicy so it can reset
|
|
|
|
// the mWildcardPolicy pointer as needed if it gets moved in the
|
|
|
|
// hashtable.
|
|
|
|
cpolicy->mDomainWeAreWildcardFor = aDomainPolicy;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the property name
|
|
|
|
start = end + 1;
|
|
|
|
end = PL_strchr(start, '.');
|
|
|
|
if (end)
|
|
|
|
*end = '\0';
|
|
|
|
|
|
|
|
JSAutoRequest ar(cx);
|
|
|
|
|
|
|
|
JSString* propertyKey = ::JS_InternString(cx, start);
|
|
|
|
if (!propertyKey)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
// Store this property in the class policy
|
|
|
|
PropertyPolicy* ppolicy =
|
2007-07-08 00:08:04 -07:00
|
|
|
static_cast<PropertyPolicy*>
|
2010-07-14 23:19:36 -07:00
|
|
|
(PL_DHashTableOperate(cpolicy->mPolicy, propertyKey,
|
2007-03-22 10:30:00 -07:00
|
|
|
PL_DHASH_ADD));
|
|
|
|
if (!ppolicy)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (end) // The pref specifies an access mode
|
|
|
|
{
|
|
|
|
start = end + 1;
|
|
|
|
if (PL_strcasecmp(start, "set") == 0)
|
|
|
|
ppolicy->mSet = secLevel;
|
|
|
|
else
|
|
|
|
ppolicy->mGet = secLevel;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (ppolicy->mGet.level == SCRIPT_SECURITY_UNDEFINED_ACCESS)
|
|
|
|
ppolicy->mGet = secLevel;
|
|
|
|
if (ppolicy->mSet.level == SCRIPT_SECURITY_UNDEFINED_ACCESS)
|
|
|
|
ppolicy->mSet = secLevel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(prefCount, prefNames);
|
|
|
|
if (currentPref < prefCount) // Loop exited early because of out-of-memory error
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// XXXbz We should really just get a prefbranch to handle this...
|
|
|
|
nsresult
|
|
|
|
nsScriptSecurityManager::GetPrincipalPrefNames(const char* prefBase,
|
|
|
|
nsCString& grantedPref,
|
|
|
|
nsCString& deniedPref,
|
|
|
|
nsCString& subjectNamePref)
|
|
|
|
{
|
|
|
|
char* lastDot = PL_strrchr(prefBase, '.');
|
|
|
|
if (!lastDot) return NS_ERROR_FAILURE;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t prefLen = lastDot - prefBase + 1;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
grantedPref.Assign(prefBase, prefLen);
|
|
|
|
deniedPref.Assign(prefBase, prefLen);
|
|
|
|
subjectNamePref.Assign(prefBase, prefLen);
|
|
|
|
|
|
|
|
#define GRANTED "granted"
|
|
|
|
#define DENIED "denied"
|
|
|
|
#define SUBJECTNAME "subjectName"
|
|
|
|
|
|
|
|
grantedPref.AppendLiteral(GRANTED);
|
|
|
|
if (grantedPref.Length() != prefLen + sizeof(GRANTED) - 1) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
deniedPref.AppendLiteral(DENIED);
|
|
|
|
if (deniedPref.Length() != prefLen + sizeof(DENIED) - 1) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
subjectNamePref.AppendLiteral(SUBJECTNAME);
|
|
|
|
if (subjectNamePref.Length() != prefLen + sizeof(SUBJECTNAME) - 1) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef SUBJECTNAME
|
|
|
|
#undef DENIED
|
|
|
|
#undef GRANTED
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsScriptSecurityManager::InitPrincipals(uint32_t aPrefCount, const char** aPrefNames)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
/* This is the principal preference syntax:
|
|
|
|
* capability.principal.[codebase|codebaseTrusted|certificate].<name>.[id|granted|denied]
|
|
|
|
* For example:
|
|
|
|
* user_pref("capability.principal.certificate.p1.id","12:34:AB:CD");
|
|
|
|
* user_pref("capability.principal.certificate.p1.granted","Capability1 Capability2");
|
|
|
|
* user_pref("capability.principal.certificate.p1.denied","Capability3");
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* codebaseTrusted means a codebase principal that can enable capabilities even if
|
|
|
|
* codebase principals are disabled. Don't use trustedCodebase except with unspoofable
|
|
|
|
* URLs such as HTTPS URLs.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static const char idSuffix[] = ".id";
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t c = 0; c < aPrefCount; c++)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t prefNameLen = PL_strlen(aPrefNames[c]) -
|
2011-10-10 22:50:08 -07:00
|
|
|
(ArrayLength(idSuffix) - 1);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (PL_strcasecmp(aPrefNames[c] + prefNameLen, idSuffix) != 0)
|
|
|
|
continue;
|
|
|
|
|
2011-06-19 20:00:16 -07:00
|
|
|
nsAdoptingCString id = Preferences::GetCString(aPrefNames[c]);
|
|
|
|
if (!id) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2011-06-19 20:00:16 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCAutoString grantedPrefName;
|
|
|
|
nsCAutoString deniedPrefName;
|
|
|
|
nsCAutoString subjectNamePrefName;
|
|
|
|
nsresult rv = GetPrincipalPrefNames(aPrefNames[c],
|
|
|
|
grantedPrefName,
|
|
|
|
deniedPrefName,
|
|
|
|
subjectNamePrefName);
|
|
|
|
if (rv == NS_ERROR_OUT_OF_MEMORY)
|
|
|
|
return rv;
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
continue;
|
|
|
|
|
2011-06-19 20:00:16 -07:00
|
|
|
nsAdoptingCString grantedList =
|
|
|
|
Preferences::GetCString(grantedPrefName.get());
|
|
|
|
nsAdoptingCString deniedList =
|
|
|
|
Preferences::GetCString(deniedPrefName.get());
|
|
|
|
nsAdoptingCString subjectName =
|
|
|
|
Preferences::GetCString(subjectNamePrefName.get());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
//-- Delete prefs if their value is the empty string
|
|
|
|
if (id.IsEmpty() || (grantedList.IsEmpty() && deniedList.IsEmpty()))
|
|
|
|
{
|
2011-06-19 20:00:16 -07:00
|
|
|
Preferences::ClearUser(aPrefNames[c]);
|
|
|
|
Preferences::ClearUser(grantedPrefName.get());
|
|
|
|
Preferences::ClearUser(deniedPrefName.get());
|
|
|
|
Preferences::ClearUser(subjectNamePrefName.get());
|
2007-03-22 10:30:00 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-- Create a principal based on the prefs
|
|
|
|
static const char certificateName[] = "capability.principal.certificate";
|
|
|
|
static const char codebaseName[] = "capability.principal.codebase";
|
|
|
|
static const char codebaseTrustedName[] = "capability.principal.codebaseTrusted";
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isCert = false;
|
|
|
|
bool isTrusted = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (PL_strncmp(aPrefNames[c], certificateName,
|
|
|
|
sizeof(certificateName) - 1) == 0)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
isCert = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else if (PL_strncmp(aPrefNames[c], codebaseName,
|
|
|
|
sizeof(codebaseName) - 1) == 0)
|
|
|
|
{
|
|
|
|
isTrusted = (PL_strncmp(aPrefNames[c], codebaseTrustedName,
|
|
|
|
sizeof(codebaseTrustedName) - 1) == 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NS_ERROR("Not a codebase or a certificate?!");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<nsPrincipal> newPrincipal = new nsPrincipal();
|
|
|
|
if (!newPrincipal)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
rv = newPrincipal->InitFromPersistent(aPrefNames[c], id, subjectName,
|
|
|
|
EmptyCString(),
|
2012-07-30 07:20:58 -07:00
|
|
|
grantedList, deniedList, nullptr,
|
2012-07-19 22:44:03 -07:00
|
|
|
isCert, isTrusted, UNKNOWN_APP_ID,
|
|
|
|
false);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
mPrincipals.Put(newPrincipal, newPrincipal);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
2007-09-06 00:02:57 -07:00
|
|
|
nsScriptSecurityManager::ScriptSecurityPrefChanged()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// JavaScript defaults to enabled in failure cases.
|
2011-10-17 07:59:28 -07:00
|
|
|
mIsJavaScriptEnabled = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
sStrictFileOriginPolicy = true;
|
2009-01-07 20:42:15 -08:00
|
|
|
|
|
|
|
nsresult rv;
|
2011-06-19 20:00:16 -07:00
|
|
|
if (!mPrefInitialized) {
|
2009-01-07 20:42:15 -08:00
|
|
|
rv = InitPrefs();
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-06-19 20:00:16 -07:00
|
|
|
mIsJavaScriptEnabled =
|
|
|
|
Preferences::GetBool(sJSEnabledPrefName, mIsJavaScriptEnabled);
|
2009-01-07 20:42:15 -08:00
|
|
|
|
2011-06-19 20:00:16 -07:00
|
|
|
sStrictFileOriginPolicy =
|
2011-09-28 23:19:26 -07:00
|
|
|
Preferences::GetBool(sFileOriginPolicyPrefName, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsScriptSecurityManager::InitPrefs()
|
|
|
|
{
|
|
|
|
nsresult rv;
|
2011-06-19 20:00:16 -07:00
|
|
|
nsIPrefBranch* branch = Preferences::GetRootBranch();
|
|
|
|
NS_ENSURE_TRUE(branch, NS_ERROR_FAILURE);
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
mPrefInitialized = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Set the initial value of the "javascript.enabled" prefs
|
2007-09-06 00:02:57 -07:00
|
|
|
ScriptSecurityPrefChanged();
|
2011-06-19 20:00:16 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// set observer callbacks in case the value of the prefs change
|
2011-06-19 20:00:16 -07:00
|
|
|
Preferences::AddStrongObservers(this, kObservedPrefs);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t prefCount;
|
2007-03-22 10:30:00 -07:00
|
|
|
char** prefNames;
|
|
|
|
//-- Initialize the principals database from prefs
|
2011-06-19 20:00:16 -07:00
|
|
|
rv = branch->GetChildList(sPrincipalPrefix, &prefCount, &prefNames);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_SUCCEEDED(rv) && prefCount > 0)
|
|
|
|
{
|
2010-06-08 16:43:54 -07:00
|
|
|
rv = InitPrincipals(prefCount, (const char**)prefNames);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(prefCount, prefNames);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-07-19 22:44:03 -07:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
void
|
2012-08-22 08:56:38 -07:00
|
|
|
GetExtendedOrigin(nsIURI* aURI, uint32_t aAppId, bool aInMozBrowser,
|
2012-07-19 22:44:03 -07:00
|
|
|
nsACString& aExtendedOrigin)
|
2012-07-18 21:25:19 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aURI);
|
2012-07-19 22:44:03 -07:00
|
|
|
MOZ_ASSERT(aAppId != nsIScriptSecurityManager::UNKNOWN_APP_ID);
|
2012-07-18 21:25:19 -07:00
|
|
|
|
2012-07-19 22:44:03 -07:00
|
|
|
if (aAppId == nsIScriptSecurityManager::UNKNOWN_APP_ID) {
|
|
|
|
aAppId = nsIScriptSecurityManager::NO_APP_ID;
|
2012-07-18 21:25:19 -07:00
|
|
|
}
|
|
|
|
|
2012-07-19 22:44:03 -07:00
|
|
|
nsCAutoString origin;
|
|
|
|
nsPrincipal::GetOriginForURI(aURI, getter_Copies(origin));
|
|
|
|
|
2012-07-18 21:25:19 -07:00
|
|
|
// Fallback.
|
|
|
|
if (aAppId == nsIScriptSecurityManager::NO_APP_ID && !aInMozBrowser) {
|
|
|
|
aExtendedOrigin.Assign(origin);
|
2012-07-19 22:44:03 -07:00
|
|
|
return;
|
2012-07-18 21:25:19 -07:00
|
|
|
}
|
|
|
|
|
2012-07-21 17:37:02 -07:00
|
|
|
// aExtendedOrigin = appId + "+" + origin + "+" + { 't', 'f' }
|
|
|
|
aExtendedOrigin.Truncate();
|
2012-07-18 21:25:19 -07:00
|
|
|
aExtendedOrigin.AppendInt(aAppId);
|
2012-07-21 17:37:02 -07:00
|
|
|
aExtendedOrigin.Append(NS_LITERAL_CSTRING("+") + origin + NS_LITERAL_CSTRING("+"));
|
|
|
|
aExtendedOrigin.Append(aInMozBrowser ? 't' : 'f');
|
2012-07-18 21:25:19 -07:00
|
|
|
|
2012-07-19 22:44:03 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScriptSecurityManager::GetExtendedOrigin(nsIURI* aURI,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aAppId,
|
2012-07-19 22:44:03 -07:00
|
|
|
bool aInMozBrowser,
|
|
|
|
nsACString& aExtendedOrigin)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aAppId != nsIScriptSecurityManager::UNKNOWN_APP_ID);
|
|
|
|
|
|
|
|
mozilla::GetExtendedOrigin(aURI, aAppId, aInMozBrowser, aExtendedOrigin);
|
2012-07-18 21:25:19 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// The following code prints the contents of the policy DB to the console.
|
|
|
|
#ifdef DEBUG_CAPS_HACKER
|
|
|
|
|
|
|
|
//typedef PLDHashOperator
|
2008-10-10 08:04:34 -07:00
|
|
|
//(* PLDHashEnumerator)(PLDHashTable *table, PLDHashEntryHdr *hdr,
|
2012-08-22 08:56:38 -07:00
|
|
|
// uint32_t number, void *arg);
|
2008-10-10 08:04:34 -07:00
|
|
|
static PLDHashOperator
|
2007-03-22 10:30:00 -07:00
|
|
|
PrintPropertyPolicy(PLDHashTable *table, PLDHashEntryHdr *entry,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t number, void *arg)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
PropertyPolicy* pp = (PropertyPolicy*)entry;
|
|
|
|
nsCAutoString prop(" ");
|
|
|
|
JSContext* cx = (JSContext*)arg;
|
2012-08-22 08:56:38 -07:00
|
|
|
prop.AppendInt((uint32_t)pp->key);
|
2007-03-22 10:30:00 -07:00
|
|
|
prop += ' ';
|
2012-07-06 02:43:28 -07:00
|
|
|
LossyAppendUTF16toASCII((PRUnichar*)JS_GetStringChars(pp->key), prop);
|
2007-03-22 10:30:00 -07:00
|
|
|
prop += ": Get=";
|
|
|
|
if (SECURITY_ACCESS_LEVEL_FLAG(pp->mGet))
|
|
|
|
prop.AppendInt(pp->mGet.level);
|
|
|
|
else
|
|
|
|
prop += pp->mGet.capability;
|
|
|
|
|
|
|
|
prop += " Set=";
|
|
|
|
if (SECURITY_ACCESS_LEVEL_FLAG(pp->mSet))
|
|
|
|
prop.AppendInt(pp->mSet.level);
|
|
|
|
else
|
|
|
|
prop += pp->mSet.capability;
|
|
|
|
|
|
|
|
printf("%s.\n", prop.get());
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
2008-10-10 08:04:34 -07:00
|
|
|
static PLDHashOperator
|
2007-03-22 10:30:00 -07:00
|
|
|
PrintClassPolicy(PLDHashTable *table, PLDHashEntryHdr *entry,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t number, void *arg)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
ClassPolicy* cp = (ClassPolicy*)entry;
|
|
|
|
printf(" %s\n", cp->key);
|
|
|
|
|
|
|
|
PL_DHashTableEnumerate(cp->mPolicy, PrintPropertyPolicy, arg);
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
// typedef bool
|
2008-10-10 08:04:34 -07:00
|
|
|
// (* nsHashtableEnumFunc)(nsHashKey *aKey, void *aData, void* aClosure);
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool
|
2007-03-22 10:30:00 -07:00
|
|
|
PrintDomainPolicy(nsHashKey *aKey, void *aData, void* aClosure)
|
|
|
|
{
|
|
|
|
DomainEntry* de = (DomainEntry*)aData;
|
|
|
|
printf("----------------------------\n");
|
|
|
|
printf("Domain: %s Policy Name: %s.\n", de->mOrigin.get(),
|
|
|
|
de->mPolicyName_DEBUG.get());
|
|
|
|
PL_DHashTableEnumerate(de->mDomainPolicy, PrintClassPolicy, aClosure);
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool
|
2007-03-22 10:30:00 -07:00
|
|
|
PrintCapability(nsHashKey *aKey, void *aData, void* aClosure)
|
|
|
|
{
|
|
|
|
char* cap = (char*)aData;
|
|
|
|
printf(" %s.\n", cap);
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsScriptSecurityManager::PrintPolicyDB()
|
|
|
|
{
|
|
|
|
printf("############## Security Policies ###############\n");
|
|
|
|
if(mOriginToPolicyMap)
|
|
|
|
{
|
|
|
|
JSContext* cx = GetCurrentJSContext();
|
|
|
|
if (!cx)
|
|
|
|
cx = GetSafeJSContext();
|
|
|
|
printf("----------------------------\n");
|
|
|
|
printf("Domain: Default.\n");
|
|
|
|
PL_DHashTableEnumerate(mDefaultPolicy, PrintClassPolicy, (void*)cx);
|
|
|
|
mOriginToPolicyMap->Enumerate(PrintDomainPolicy, (void*)cx);
|
|
|
|
}
|
|
|
|
printf("############ End Security Policies #############\n\n");
|
|
|
|
printf("############## Capabilities ###############\n");
|
|
|
|
mCapabilities->Enumerate(PrintCapability);
|
|
|
|
printf("############## End Capabilities ###############\n");
|
|
|
|
}
|
|
|
|
#endif
|