2003-07-24 09:15:20 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2005-10-14 22:57:26 +04:00
|
|
|
/* vim: set ts=2 sw=2 et tw=80: */
|
2012-05-21 15:12:37 +04: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/. */
|
2003-07-24 09:15:20 +04:00
|
|
|
|
2013-08-14 10:56:21 +04:00
|
|
|
#include "nsPrincipal.h"
|
|
|
|
|
2013-01-09 01:53:32 +04:00
|
|
|
#include "mozIThirdPartyUtil.h"
|
2003-07-24 09:15:20 +04:00
|
|
|
#include "nscore.h"
|
|
|
|
#include "nsScriptSecurityManager.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsReadableUtils.h"
|
2013-07-12 00:21:45 +04:00
|
|
|
#include "pratom.h"
|
2003-07-24 09:15:20 +04:00
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsJSPrincipals.h"
|
2015-03-13 23:15:09 +03:00
|
|
|
#include "nsIEffectiveTLDService.h"
|
2003-07-24 09:15:20 +04:00
|
|
|
#include "nsIObjectInputStream.h"
|
|
|
|
#include "nsIObjectOutputStream.h"
|
2006-03-15 07:59:42 +03:00
|
|
|
#include "nsIClassInfoImpl.h"
|
2014-11-11 12:10:56 +03:00
|
|
|
#include "nsIProtocolHandler.h"
|
2012-07-27 18:03:27 +04:00
|
|
|
#include "nsError.h"
|
2010-01-23 00:38:21 +03:00
|
|
|
#include "nsIContentSecurityPolicy.h"
|
2015-03-13 23:15:09 +03:00
|
|
|
#include "nsNetCID.h"
|
2012-07-12 12:10:15 +04:00
|
|
|
#include "jswrapper.h"
|
2003-07-24 09:15:20 +04:00
|
|
|
|
2014-08-15 05:47:15 +04:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2011-06-20 07:00:16 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2012-03-13 02:53:18 +04:00
|
|
|
#include "mozilla/HashFunctions.h"
|
2009-06-16 15:00:06 +04:00
|
|
|
|
2012-08-28 06:43:57 +04:00
|
|
|
#include "nsIAppsService.h"
|
|
|
|
#include "mozIApplication.h"
|
|
|
|
|
2011-06-20 07:00:16 +04:00
|
|
|
using namespace mozilla;
|
2009-06-16 15:00:06 +04:00
|
|
|
|
2015-03-13 23:15:09 +03:00
|
|
|
static bool gIsWhitelistingTestDomains = false;
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool gCodeBasePrincipalSupport = false;
|
2009-06-16 15:00:06 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool URIIsImmutable(nsIURI* aURI)
|
2007-06-18 19:07:02 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIMutable> mutableObj(do_QueryInterface(aURI));
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isMutable;
|
2007-06-18 19:07:02 +04:00
|
|
|
return
|
|
|
|
mutableObj &&
|
|
|
|
NS_SUCCEEDED(mutableObj->GetMutable(&isMutable)) &&
|
2013-06-01 01:33:51 +04:00
|
|
|
!isMutable;
|
2007-06-18 19:07:02 +04:00
|
|
|
}
|
2003-07-24 09:15:20 +04:00
|
|
|
|
2013-04-03 04:16:25 +04:00
|
|
|
NS_IMPL_CLASSINFO(nsPrincipal, nullptr, nsIClassInfo::MAIN_THREAD_ONLY,
|
2012-06-10 02:19:26 +04:00
|
|
|
NS_PRINCIPAL_CID)
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_QUERY_INTERFACE_CI(nsPrincipal,
|
|
|
|
nsIPrincipal,
|
|
|
|
nsISerializable)
|
|
|
|
NS_IMPL_CI_INTERFACE_GETTER(nsPrincipal,
|
2012-06-10 02:19:26 +04:00
|
|
|
nsIPrincipal,
|
|
|
|
nsISerializable)
|
|
|
|
|
2015-03-13 23:15:09 +03:00
|
|
|
// Called at startup:
|
|
|
|
/* static */ void
|
|
|
|
nsPrincipal::InitializeStatics()
|
|
|
|
{
|
|
|
|
Preferences::AddBoolVarCache(
|
|
|
|
&gIsWhitelistingTestDomains,
|
|
|
|
"layout.css.unprefixing-service.include-test-domains");
|
2015-03-13 23:16:01 +03:00
|
|
|
|
|
|
|
Preferences::AddBoolVarCache(&gCodeBasePrincipalSupport,
|
|
|
|
"signed.applets.codebase_principal_support",
|
|
|
|
false);
|
2015-03-13 23:15:09 +03:00
|
|
|
}
|
|
|
|
|
2012-06-10 02:19:26 +04:00
|
|
|
nsPrincipal::nsPrincipal()
|
2012-07-20 09:44:03 +04:00
|
|
|
: mAppId(nsIScriptSecurityManager::UNKNOWN_APP_ID)
|
|
|
|
, mInMozBrowser(false)
|
|
|
|
, mCodebaseImmutable(false)
|
2012-06-10 23:25:17 +04:00
|
|
|
, mDomainImmutable(false)
|
|
|
|
, mInitialized(false)
|
2012-06-10 02:19:26 +04:00
|
|
|
{ }
|
|
|
|
|
|
|
|
nsPrincipal::~nsPrincipal()
|
|
|
|
{ }
|
|
|
|
|
2012-05-03 01:57:34 +04:00
|
|
|
nsresult
|
2012-10-22 10:29:56 +04:00
|
|
|
nsPrincipal::Init(nsIURI *aCodebase,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aAppId,
|
2012-07-20 09:44:03 +04:00
|
|
|
bool aInMozBrowser)
|
2012-06-10 02:19:26 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_STATE(!mInitialized);
|
2012-10-22 10:29:56 +04:00
|
|
|
NS_ENSURE_ARG(aCodebase);
|
2012-06-10 02:19:26 +04:00
|
|
|
|
|
|
|
mInitialized = true;
|
|
|
|
|
|
|
|
mCodebase = NS_TryToMakeImmutable(aCodebase);
|
|
|
|
mCodebaseImmutable = URIIsImmutable(mCodebase);
|
|
|
|
|
2012-07-20 09:44:03 +04:00
|
|
|
mAppId = aAppId;
|
|
|
|
mInMozBrowser = aInMozBrowser;
|
|
|
|
|
2012-10-22 10:29:56 +04:00
|
|
|
return NS_OK;
|
2012-06-10 02:19:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsPrincipal::GetScriptLocation(nsACString &aStr)
|
|
|
|
{
|
2012-10-22 10:29:56 +04:00
|
|
|
mCodebase->GetSpec(aStr);
|
2012-06-10 02:19:26 +04:00
|
|
|
}
|
|
|
|
|
2012-07-19 08:23:44 +04:00
|
|
|
/* static */ nsresult
|
2015-05-13 01:08:20 +03:00
|
|
|
nsPrincipal::GetOriginForURI(nsIURI* aURI, nsACString& aOrigin)
|
2003-07-24 09:15:20 +04:00
|
|
|
{
|
2012-07-19 08:23:44 +04:00
|
|
|
if (!aURI) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> origin = NS_GetInnermostURI(aURI);
|
2012-06-10 02:19:26 +04:00
|
|
|
if (!origin) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2007-09-28 18:31:04 +04:00
|
|
|
}
|
2003-07-24 09:15:20 +04:00
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString hostPort;
|
2012-06-10 02:19:26 +04:00
|
|
|
|
|
|
|
// chrome: URLs don't have a meaningful origin, so make
|
|
|
|
// sure we just get the full spec for them.
|
|
|
|
// XXX this should be removed in favor of the solution in
|
|
|
|
// bug 160042.
|
|
|
|
bool isChrome;
|
|
|
|
nsresult rv = origin->SchemeIs("chrome", &isChrome);
|
|
|
|
if (NS_SUCCEEDED(rv) && !isChrome) {
|
|
|
|
rv = origin->GetAsciiHost(hostPort);
|
|
|
|
// Some implementations return an empty string, treat it as no support
|
|
|
|
// for asciiHost by that implementation.
|
2012-07-19 08:23:44 +04:00
|
|
|
if (hostPort.IsEmpty()) {
|
2012-06-10 02:19:26 +04:00
|
|
|
rv = NS_ERROR_FAILURE;
|
2012-07-19 08:23:44 +04:00
|
|
|
}
|
2003-07-24 09:15:20 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t port;
|
2012-06-10 02:19:26 +04:00
|
|
|
if (NS_SUCCEEDED(rv) && !isChrome) {
|
|
|
|
rv = origin->GetPort(&port);
|
2003-07-24 09:15:20 +04:00
|
|
|
}
|
|
|
|
|
2012-06-10 02:19:26 +04:00
|
|
|
if (NS_SUCCEEDED(rv) && !isChrome) {
|
|
|
|
if (port != -1) {
|
2014-05-22 07:48:51 +04:00
|
|
|
hostPort.Append(':');
|
2012-06-10 02:19:26 +04:00
|
|
|
hostPort.AppendInt(port, 10);
|
2003-07-24 09:15:20 +04:00
|
|
|
}
|
|
|
|
|
2015-05-13 01:08:20 +03:00
|
|
|
rv = origin->GetScheme(aOrigin);
|
2012-06-10 02:19:26 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2015-05-13 01:08:20 +03:00
|
|
|
aOrigin.AppendLiteral("://");
|
|
|
|
aOrigin.Append(hostPort);
|
2012-06-10 02:19:26 +04:00
|
|
|
}
|
|
|
|
else {
|
2015-05-13 01:08:20 +03:00
|
|
|
rv = origin->GetAsciiSpec(aOrigin);
|
2012-06-10 02:19:26 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2003-07-24 09:15:20 +04:00
|
|
|
}
|
|
|
|
|
2015-05-13 01:08:20 +03:00
|
|
|
return NS_OK;
|
2003-07-24 09:15:20 +04:00
|
|
|
}
|
|
|
|
|
2012-07-19 08:23:44 +04:00
|
|
|
NS_IMETHODIMP
|
2015-05-13 01:08:20 +03:00
|
|
|
nsPrincipal::GetOrigin(nsACString& aOrigin)
|
2012-07-19 08:23:44 +04:00
|
|
|
{
|
|
|
|
return GetOriginForURI(mCodebase, aOrigin);
|
|
|
|
}
|
|
|
|
|
2003-07-24 09:15:20 +04:00
|
|
|
NS_IMETHODIMP
|
2014-02-14 06:57:36 +04:00
|
|
|
nsPrincipal::EqualsConsideringDomain(nsIPrincipal *aOther, bool *aResult)
|
2003-07-24 09:15:20 +04:00
|
|
|
{
|
2012-10-24 21:50:25 +04:00
|
|
|
*aResult = false;
|
|
|
|
|
2012-06-10 02:19:26 +04:00
|
|
|
if (!aOther) {
|
|
|
|
NS_WARNING("Need a principal to compare this to!");
|
2003-07-24 09:15:20 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-06-10 02:19:26 +04:00
|
|
|
|
2012-10-24 21:50:25 +04:00
|
|
|
if (aOther == this) {
|
|
|
|
*aResult = true;
|
2012-06-10 02:19:26 +04:00
|
|
|
return NS_OK;
|
2003-07-24 09:15:20 +04:00
|
|
|
}
|
|
|
|
|
2014-02-14 06:57:36 +04:00
|
|
|
if (!nsScriptSecurityManager::AppAttributesEqual(this, aOther)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> thisURI;
|
|
|
|
this->GetDomain(getter_AddRefs(thisURI));
|
|
|
|
bool thisSetDomain = !!thisURI;
|
|
|
|
if (!thisURI) {
|
|
|
|
this->GetURI(getter_AddRefs(thisURI));
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> otherURI;
|
|
|
|
aOther->GetDomain(getter_AddRefs(otherURI));
|
|
|
|
bool otherSetDomain = !!otherURI;
|
|
|
|
if (!otherURI) {
|
|
|
|
aOther->GetURI(getter_AddRefs(otherURI));
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResult = thisSetDomain == otherSetDomain &&
|
|
|
|
nsScriptSecurityManager::SecurityCompareURIs(thisURI, otherURI);
|
2003-07-24 09:15:20 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-02-14 06:57:36 +04:00
|
|
|
nsPrincipal::Equals(nsIPrincipal *aOther, bool *aResult)
|
2003-07-24 09:15:20 +04:00
|
|
|
{
|
2012-10-24 21:50:25 +04:00
|
|
|
*aResult = false;
|
|
|
|
|
|
|
|
if (!aOther) {
|
|
|
|
NS_WARNING("Need a principal to compare this to!");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aOther == this) {
|
2012-06-10 02:19:26 +04:00
|
|
|
*aResult = true;
|
|
|
|
return NS_OK;
|
2003-07-24 09:15:20 +04:00
|
|
|
}
|
|
|
|
|
2012-10-24 21:50:25 +04:00
|
|
|
if (!nsScriptSecurityManager::AppAttributesEqual(this, aOther)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2003-07-24 09:15:20 +04:00
|
|
|
|
2012-06-10 02:19:26 +04:00
|
|
|
nsCOMPtr<nsIURI> otherURI;
|
|
|
|
nsresult rv = aOther->GetURI(getter_AddRefs(otherURI));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
2003-07-24 09:15:20 +04:00
|
|
|
}
|
|
|
|
|
2012-06-10 02:19:26 +04:00
|
|
|
NS_ASSERTION(mCodebase,
|
|
|
|
"shouldn't be calling this on principals from preferences");
|
|
|
|
|
|
|
|
// Compare codebases.
|
|
|
|
*aResult = nsScriptSecurityManager::SecurityCompareURIs(mCodebase,
|
|
|
|
otherURI);
|
2003-07-24 09:15:20 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-06-10 02:19:26 +04:00
|
|
|
nsPrincipal::Subsumes(nsIPrincipal *aOther, bool *aResult)
|
2003-07-24 09:15:20 +04:00
|
|
|
{
|
2012-06-10 02:19:26 +04:00
|
|
|
return Equals(aOther, aResult);
|
|
|
|
}
|
2003-07-24 09:15:20 +04:00
|
|
|
|
2014-02-14 06:57:34 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrincipal::SubsumesConsideringDomain(nsIPrincipal *aOther, bool *aResult)
|
|
|
|
{
|
2014-02-14 06:57:36 +04:00
|
|
|
return EqualsConsideringDomain(aOther, aResult);
|
2014-02-14 06:57:34 +04:00
|
|
|
}
|
|
|
|
|
2003-07-24 09:15:20 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrincipal::GetURI(nsIURI** aURI)
|
|
|
|
{
|
2007-06-18 19:07:02 +04:00
|
|
|
if (mCodebaseImmutable) {
|
|
|
|
NS_ADDREF(*aURI = mCodebase);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2006-06-20 07:17:41 +04:00
|
|
|
if (!mCodebase) {
|
2012-07-30 18:20:58 +04:00
|
|
|
*aURI = nullptr;
|
2006-06-20 07:17:41 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2003-07-24 09:15:20 +04:00
|
|
|
|
2006-06-20 07:17:41 +04:00
|
|
|
return NS_EnsureSafeToReturn(mCodebase, aURI);
|
2003-07-24 09:15:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-20 22:34:33 +04:00
|
|
|
nsPrincipal::CheckMayLoad(nsIURI* aURI, bool aReport, bool aAllowIfInheritsPrincipal)
|
2003-07-24 09:15:20 +04:00
|
|
|
{
|
2012-08-20 22:34:33 +04:00
|
|
|
if (aAllowIfInheritsPrincipal) {
|
|
|
|
// If the caller specified to allow loads of URIs that inherit
|
|
|
|
// our principal, allow the load if this URI inherits its principal
|
|
|
|
if (nsPrincipal::IsPrincipalInherited(aURI)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-29 11:21:17 +03:00
|
|
|
// See if aURI is something like a Blob URI that is actually associated with
|
|
|
|
// a principal.
|
|
|
|
nsCOMPtr<nsIURIWithPrincipal> uriWithPrin = do_QueryInterface(aURI);
|
|
|
|
nsCOMPtr<nsIPrincipal> uriPrin;
|
|
|
|
if (uriWithPrin) {
|
|
|
|
uriWithPrin->GetPrincipal(getter_AddRefs(uriPrin));
|
|
|
|
}
|
|
|
|
if (uriPrin && nsIPrincipal::Subsumes(uriPrin)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-06-01 01:33:51 +04:00
|
|
|
if (nsScriptSecurityManager::SecurityCompareURIs(mCodebase, aURI)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-01-09 01:53:32 +04:00
|
|
|
|
2013-06-01 01:33:51 +04:00
|
|
|
// If strict file origin policy is in effect, local files will always fail
|
|
|
|
// SecurityCompareURIs unless they are identical. Explicitly check file origin
|
|
|
|
// policy, in that case.
|
|
|
|
if (nsScriptSecurityManager::GetStrictFileOriginPolicy() &&
|
|
|
|
NS_URIIsLocalFile(aURI) &&
|
|
|
|
NS_RelaxStrictFileOriginPolicy(aURI, mCodebase)) {
|
|
|
|
return NS_OK;
|
2005-07-22 23:05:42 +04:00
|
|
|
}
|
2012-06-10 02:19:26 +04:00
|
|
|
|
2013-06-01 01:33:51 +04:00
|
|
|
if (aReport) {
|
|
|
|
nsScriptSecurityManager::ReportError(nullptr, NS_LITERAL_STRING("CheckSameOriginError"), mCodebase, aURI);
|
|
|
|
}
|
|
|
|
return NS_ERROR_DOM_BAD_URI;
|
2005-07-22 23:05:42 +04:00
|
|
|
}
|
2003-07-24 09:15:20 +04:00
|
|
|
|
2012-06-10 02:19:26 +04:00
|
|
|
void
|
|
|
|
nsPrincipal::SetURI(nsIURI* aURI)
|
2010-01-23 00:38:21 +03:00
|
|
|
{
|
2012-06-10 02:19:26 +04:00
|
|
|
mCodebase = NS_TryToMakeImmutable(aURI);
|
|
|
|
mCodebaseImmutable = URIIsImmutable(mCodebase);
|
2010-01-23 00:38:21 +03:00
|
|
|
}
|
|
|
|
|
2003-07-24 09:15:20 +04:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsPrincipal::GetHashValue(uint32_t* aValue)
|
2003-07-24 09:15:20 +04:00
|
|
|
{
|
2012-10-22 10:29:56 +04:00
|
|
|
NS_PRECONDITION(mCodebase, "Need a codebase");
|
2003-07-24 09:15:20 +04:00
|
|
|
|
2012-10-22 10:29:56 +04:00
|
|
|
*aValue = nsScriptSecurityManager::HashPrincipalByOrigin(this);
|
2003-07-24 09:15:20 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrincipal::GetDomain(nsIURI** aDomain)
|
|
|
|
{
|
2006-06-20 07:17:41 +04:00
|
|
|
if (!mDomain) {
|
2012-07-30 18:20:58 +04:00
|
|
|
*aDomain = nullptr;
|
2006-06-20 07:17:41 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2003-07-24 09:15:20 +04:00
|
|
|
|
2007-06-18 19:07:02 +04:00
|
|
|
if (mDomainImmutable) {
|
|
|
|
NS_ADDREF(*aDomain = mDomain);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2006-06-20 07:17:41 +04:00
|
|
|
return NS_EnsureSafeToReturn(mDomain, aDomain);
|
2003-07-24 09:15:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrincipal::SetDomain(nsIURI* aDomain)
|
|
|
|
{
|
2006-06-20 07:17:41 +04:00
|
|
|
mDomain = NS_TryToMakeImmutable(aDomain);
|
2007-06-18 19:07:02 +04:00
|
|
|
mDomainImmutable = URIIsImmutable(mDomain);
|
2013-01-09 01:53:32 +04:00
|
|
|
|
2012-07-12 12:10:15 +04:00
|
|
|
// Recompute all wrappers between compartments using this principal and other
|
|
|
|
// non-chrome compartments.
|
2013-05-06 17:04:17 +04:00
|
|
|
AutoSafeJSContext cx;
|
2012-07-12 12:10:15 +04:00
|
|
|
JSPrincipals *principals = nsJSPrincipals::get(static_cast<nsIPrincipal*>(this));
|
|
|
|
bool success = js::RecomputeWrappers(cx, js::ContentCompartmentsOnly(),
|
|
|
|
js::CompartmentsWithPrincipals(principals));
|
|
|
|
NS_ENSURE_TRUE(success, NS_ERROR_FAILURE);
|
|
|
|
success = js::RecomputeWrappers(cx, js::CompartmentsWithPrincipals(principals),
|
|
|
|
js::ContentCompartmentsOnly());
|
|
|
|
NS_ENSURE_TRUE(success, NS_ERROR_FAILURE);
|
|
|
|
|
2003-07-24 09:15:20 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-07-20 09:44:03 +04:00
|
|
|
NS_IMETHODIMP
|
2013-09-11 08:18:36 +04:00
|
|
|
nsPrincipal::GetJarPrefix(nsACString& aJarPrefix)
|
2012-07-20 09:44:03 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mAppId != nsIScriptSecurityManager::UNKNOWN_APP_ID);
|
|
|
|
|
2013-09-11 08:18:36 +04:00
|
|
|
mozilla::GetJarPrefix(mAppId, mInMozBrowser, aJarPrefix);
|
2012-07-20 09:44:03 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsPrincipal::GetAppStatus(uint16_t* aAppStatus)
|
2012-07-20 09:44:03 +04:00
|
|
|
{
|
|
|
|
*aAppStatus = GetAppStatus();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsPrincipal::GetAppId(uint32_t* aAppId)
|
2012-07-20 09:44:03 +04:00
|
|
|
{
|
|
|
|
if (mAppId == nsIScriptSecurityManager::UNKNOWN_APP_ID) {
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
*aAppId = nsIScriptSecurityManager::NO_APP_ID;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aAppId = mAppId;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-07-31 19:47:20 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrincipal::GetIsInBrowserElement(bool* aIsInBrowserElement)
|
|
|
|
{
|
|
|
|
*aIsInBrowserElement = mInMozBrowser;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-26 03:28:17 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrincipal::GetUnknownAppId(bool* aUnknownAppId)
|
|
|
|
{
|
|
|
|
*aUnknownAppId = mAppId == nsIScriptSecurityManager::UNKNOWN_APP_ID;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-01-09 01:53:32 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrincipal::GetBaseDomain(nsACString& aBaseDomain)
|
|
|
|
{
|
|
|
|
// For a file URI, we return the file path.
|
2013-06-01 01:33:51 +04:00
|
|
|
if (NS_URIIsLocalFile(mCodebase)) {
|
2013-01-09 01:53:32 +04:00
|
|
|
nsCOMPtr<nsIURL> url = do_QueryInterface(mCodebase);
|
|
|
|
|
|
|
|
if (url) {
|
|
|
|
return url->GetFilePath(aBaseDomain);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-11 12:10:56 +03:00
|
|
|
bool hasNoRelativeFlag;
|
|
|
|
nsresult rv = NS_URIChainHasFlags(mCodebase,
|
|
|
|
nsIProtocolHandler::URI_NORELATIVE,
|
|
|
|
&hasNoRelativeFlag);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasNoRelativeFlag) {
|
|
|
|
return mCodebase->GetSpec(aBaseDomain);
|
|
|
|
}
|
|
|
|
|
2013-01-09 01:53:32 +04:00
|
|
|
// For everything else, we ask the TLD service via
|
|
|
|
// the ThirdPartyUtil.
|
|
|
|
nsCOMPtr<mozIThirdPartyUtil> thirdPartyUtil =
|
|
|
|
do_GetService(THIRDPARTYUTIL_CONTRACTID);
|
|
|
|
if (thirdPartyUtil) {
|
|
|
|
return thirdPartyUtil->GetBaseDomain(mCodebase, aBaseDomain);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-07-24 09:15:20 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrincipal::Read(nsIObjectInputStream* aStream)
|
|
|
|
{
|
2014-03-15 23:00:17 +04:00
|
|
|
nsCOMPtr<nsISupports> supports;
|
2007-09-18 02:18:28 +04:00
|
|
|
nsCOMPtr<nsIURI> codebase;
|
2014-03-15 23:00:17 +04:00
|
|
|
nsresult rv = NS_ReadOptionalObject(aStream, true, getter_AddRefs(supports));
|
2007-09-18 02:18:28 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
2003-07-24 09:15:20 +04:00
|
|
|
}
|
|
|
|
|
2014-03-15 23:00:17 +04:00
|
|
|
codebase = do_QueryInterface(supports);
|
|
|
|
|
2012-07-26 04:12:03 +04:00
|
|
|
nsCOMPtr<nsIURI> domain;
|
2014-03-15 23:00:17 +04:00
|
|
|
rv = NS_ReadOptionalObject(aStream, true, getter_AddRefs(supports));
|
2012-07-26 04:12:03 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-03-15 23:00:17 +04:00
|
|
|
domain = do_QueryInterface(supports);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t appId;
|
2012-07-20 09:44:03 +04:00
|
|
|
rv = aStream->Read32(&appId);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
bool inMozBrowser;
|
|
|
|
rv = aStream->ReadBoolean(&inMozBrowser);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2014-03-15 23:00:17 +04:00
|
|
|
rv = NS_ReadOptionalObject(aStream, true, getter_AddRefs(supports));
|
2014-01-24 03:34:59 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2014-03-15 23:00:17 +04:00
|
|
|
// This may be null.
|
|
|
|
nsCOMPtr<nsIContentSecurityPolicy> csp = do_QueryInterface(supports, &rv);
|
|
|
|
|
2012-10-22 10:29:56 +04:00
|
|
|
rv = Init(codebase, appId, inMozBrowser);
|
2007-09-18 02:18:28 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2014-01-24 03:34:59 +04:00
|
|
|
rv = SetCsp(csp);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2014-07-31 01:37:22 +04:00
|
|
|
// need to link in the CSP context here (link in the URI of the protected
|
|
|
|
// resource).
|
2014-01-24 03:34:59 +04:00
|
|
|
if (csp) {
|
2014-07-31 01:37:22 +04:00
|
|
|
csp->SetRequestContext(codebase, nullptr, nullptr);
|
2014-01-24 03:34:59 +04:00
|
|
|
}
|
|
|
|
|
2007-09-18 02:18:28 +04:00
|
|
|
SetDomain(domain);
|
|
|
|
|
2003-07-24 09:15:20 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrincipal::Write(nsIObjectOutputStream* aStream)
|
|
|
|
{
|
2012-10-22 10:29:56 +04:00
|
|
|
NS_ENSURE_STATE(mCodebase);
|
2007-09-18 02:18:28 +04:00
|
|
|
|
2012-10-22 10:29:56 +04:00
|
|
|
nsresult rv = NS_WriteOptionalCompoundObject(aStream, mCodebase, NS_GET_IID(nsIURI),
|
|
|
|
true);
|
2007-09-18 02:18:28 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
2003-07-24 09:15:20 +04:00
|
|
|
}
|
|
|
|
|
2007-09-18 02:18:28 +04:00
|
|
|
rv = NS_WriteOptionalCompoundObject(aStream, mDomain, NS_GET_IID(nsIURI),
|
2011-10-17 18:59:28 +04:00
|
|
|
true);
|
2003-07-24 09:15:20 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-07-20 09:44:03 +04:00
|
|
|
aStream->Write32(mAppId);
|
|
|
|
aStream->WriteBoolean(mInMozBrowser);
|
|
|
|
|
2014-01-24 03:34:59 +04:00
|
|
|
rv = NS_WriteOptionalCompoundObject(aStream, mCSP,
|
|
|
|
NS_GET_IID(nsIContentSecurityPolicy),
|
|
|
|
true);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2007-09-18 02:18:28 +04:00
|
|
|
// mCodebaseImmutable and mDomainImmutable will be recomputed based
|
|
|
|
// on the deserialized URIs in Read().
|
|
|
|
|
2003-07-24 09:15:20 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-06-10 02:19:26 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint16_t
|
2012-07-20 09:44:03 +04:00
|
|
|
nsPrincipal::GetAppStatus()
|
|
|
|
{
|
2014-07-29 19:47:52 +04:00
|
|
|
if (mAppId == nsIScriptSecurityManager::UNKNOWN_APP_ID) {
|
|
|
|
NS_WARNING("Asking for app status on a principal with an unknown app id");
|
2014-07-28 23:08:51 +04:00
|
|
|
return nsIPrincipal::APP_STATUS_NOT_INSTALLED;
|
|
|
|
}
|
2014-07-29 19:47:52 +04:00
|
|
|
return nsScriptSecurityManager::AppStatusForPrincipal(this);
|
2012-07-20 09:44:03 +04:00
|
|
|
}
|
2012-06-10 02:19:26 +04:00
|
|
|
|
2015-03-13 23:15:09 +03:00
|
|
|
// Helper-function to indicate whether the CSS Unprefixing Service
|
|
|
|
// whitelist should include dummy domains that are only intended for
|
|
|
|
// use in testing. (Controlled by a pref.)
|
|
|
|
static inline bool
|
|
|
|
IsWhitelistingTestDomains()
|
|
|
|
{
|
|
|
|
return gIsWhitelistingTestDomains;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks if the given URI's host is on our "full domain" whitelist
|
|
|
|
// (i.e. if it's an exact match against a domain that needs unprefixing)
|
|
|
|
static bool
|
|
|
|
IsOnFullDomainWhitelist(nsIURI* aURI)
|
|
|
|
{
|
|
|
|
nsAutoCString hostStr;
|
|
|
|
nsresult rv = aURI->GetHost(hostStr);
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
|
|
|
|
// NOTE: This static whitelist is expected to be short. If that changes,
|
|
|
|
// we should consider a different representation; e.g. hash-set, prefix tree.
|
|
|
|
static const nsLiteralCString sFullDomainsOnWhitelist[] = {
|
|
|
|
// 0th entry only active when testing:
|
|
|
|
NS_LITERAL_CSTRING("test1.example.org"),
|
|
|
|
NS_LITERAL_CSTRING("map.baidu.com"),
|
|
|
|
NS_LITERAL_CSTRING("3g.163.com"),
|
|
|
|
NS_LITERAL_CSTRING("3glogo.gtimg.com"), // for 3g.163.com
|
|
|
|
NS_LITERAL_CSTRING("info.3g.qq.com"), // for 3g.qq.com
|
|
|
|
NS_LITERAL_CSTRING("3gimg.qq.com"), // for 3g.qq.com
|
|
|
|
NS_LITERAL_CSTRING("img.m.baidu.com"), // for [shucheng|ks].baidu.com
|
|
|
|
NS_LITERAL_CSTRING("m.mogujie.com"),
|
|
|
|
NS_LITERAL_CSTRING("touch.qunar.com"),
|
2015-05-05 19:04:23 +03:00
|
|
|
NS_LITERAL_CSTRING("mjs.sinaimg.cn"), // for sina.cn
|
|
|
|
NS_LITERAL_CSTRING("static.qiyi.com"), // for m.iqiyi.com
|
2015-05-08 19:08:27 +03:00
|
|
|
NS_LITERAL_CSTRING("cdn.kuaidi100.com"), // for m.kuaidi100.com
|
2015-05-05 19:04:23 +03:00
|
|
|
NS_LITERAL_CSTRING("m.pc6.com"),
|
|
|
|
NS_LITERAL_CSTRING("m.haosou.com"),
|
|
|
|
NS_LITERAL_CSTRING("m.mi.com"),
|
|
|
|
NS_LITERAL_CSTRING("wappass.baidu.com"),
|
|
|
|
NS_LITERAL_CSTRING("m.video.baidu.com"),
|
|
|
|
NS_LITERAL_CSTRING("m.video.baidu.com"),
|
|
|
|
NS_LITERAL_CSTRING("imgcache.gtimg.cn"), // for m.v.qq.com
|
2015-05-07 19:04:42 +03:00
|
|
|
NS_LITERAL_CSTRING("i.yimg.jp"), // for *.yahoo.co.jp
|
|
|
|
NS_LITERAL_CSTRING("ai.yimg.jp"), // for *.yahoo.co.jp
|
|
|
|
NS_LITERAL_CSTRING("daily.c.yimg.jp"), // for sp.daily.co.jp
|
|
|
|
NS_LITERAL_CSTRING("stat100.ameba.jp"), // for ameblo.jp
|
|
|
|
NS_LITERAL_CSTRING("user.ameba.jp"), // for ameblo.jp
|
|
|
|
NS_LITERAL_CSTRING("www.goo.ne.jp"),
|
|
|
|
NS_LITERAL_CSTRING("s.tabelog.jp"),
|
|
|
|
NS_LITERAL_CSTRING("x.gnst.jp"), // for mobile.gnavi.co.jp
|
|
|
|
NS_LITERAL_CSTRING("c.x.gnst.jp"), // for mobile.gnavi.co.jp
|
|
|
|
NS_LITERAL_CSTRING("www.smbc-card.com"),
|
|
|
|
NS_LITERAL_CSTRING("static.card.jp.rakuten-static.com"), // for rakuten-card.co.jp
|
|
|
|
NS_LITERAL_CSTRING("img.mixi.net"), // for mixi.jp
|
|
|
|
NS_LITERAL_CSTRING("girlschannel.net"),
|
|
|
|
NS_LITERAL_CSTRING("www.fancl.co.jp"),
|
|
|
|
NS_LITERAL_CSTRING("s.cosme.net"),
|
|
|
|
NS_LITERAL_CSTRING("www.sapporobeer.jp"),
|
|
|
|
NS_LITERAL_CSTRING("www.mapion.co.jp"),
|
|
|
|
NS_LITERAL_CSTRING("touch.navitime.co.jp"),
|
|
|
|
NS_LITERAL_CSTRING("sp.mbga.jp"),
|
|
|
|
NS_LITERAL_CSTRING("ava-a.sp.mbga.jp"), // for sp.mbga.jp
|
|
|
|
NS_LITERAL_CSTRING("www.ntv.co.jp"),
|
|
|
|
NS_LITERAL_CSTRING("mobile.suntory.co.jp"), // for suntory.jp
|
|
|
|
NS_LITERAL_CSTRING("www.aeonsquare.net"),
|
|
|
|
NS_LITERAL_CSTRING("mw.nikkei.com"),
|
|
|
|
NS_LITERAL_CSTRING("www.nhk.or.jp"),
|
|
|
|
NS_LITERAL_CSTRING("www.tokyo-sports.co.jp"),
|
2015-03-13 23:15:09 +03:00
|
|
|
};
|
|
|
|
static const size_t sNumFullDomainsOnWhitelist =
|
|
|
|
MOZ_ARRAY_LENGTH(sFullDomainsOnWhitelist);
|
|
|
|
|
|
|
|
// Skip 0th (dummy) entry in whitelist, unless a pref is enabled.
|
|
|
|
const size_t firstWhitelistIdx = IsWhitelistingTestDomains() ? 0 : 1;
|
|
|
|
|
|
|
|
for (size_t i = firstWhitelistIdx; i < sNumFullDomainsOnWhitelist; ++i) {
|
|
|
|
if (hostStr == sFullDomainsOnWhitelist[i]) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks if the given URI's host is on our "base domain" whitelist
|
|
|
|
// (i.e. if it's a subdomain of some host that we've whitelisted as needing
|
|
|
|
// unprefixing for all its subdomains)
|
|
|
|
static bool
|
|
|
|
IsOnBaseDomainWhitelist(nsIURI* aURI)
|
|
|
|
{
|
|
|
|
static const nsLiteralCString sBaseDomainsOnWhitelist[] = {
|
|
|
|
// 0th entry only active when testing:
|
|
|
|
NS_LITERAL_CSTRING("test2.example.org"),
|
|
|
|
NS_LITERAL_CSTRING("tbcdn.cn"), // for m.taobao.com
|
|
|
|
NS_LITERAL_CSTRING("dpfile.com"), // for m.dianping.com
|
|
|
|
NS_LITERAL_CSTRING("hao123img.com"), // for hao123.com
|
|
|
|
};
|
|
|
|
static const size_t sNumBaseDomainsOnWhitelist =
|
|
|
|
MOZ_ARRAY_LENGTH(sBaseDomainsOnWhitelist);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIEffectiveTLDService> tldService =
|
|
|
|
do_GetService(NS_EFFECTIVETLDSERVICE_CONTRACTID);
|
|
|
|
|
|
|
|
if (tldService) {
|
|
|
|
// Skip 0th test-entry in whitelist, unless the testing pref is enabled.
|
|
|
|
const size_t firstWhitelistIdx = IsWhitelistingTestDomains() ? 0 : 1;
|
|
|
|
|
|
|
|
// Right now, the test base-domain "test2.example.org" is the only entry in
|
|
|
|
// its whitelist with a nonzero "depth". So we'll only bother going beyond
|
|
|
|
// 0 depth (to 1) if that entry is enabled. (No point in slowing down the
|
|
|
|
// normal codepath, for the benefit of a disabled test domain.) If we add a
|
|
|
|
// "real" base-domain with a depth of >= 1 to our whitelist, we can get rid
|
|
|
|
// of this conditional & just make this a static variable.
|
|
|
|
const uint32_t maxSubdomainDepth = IsWhitelistingTestDomains() ? 1 : 0;
|
|
|
|
|
|
|
|
for (uint32_t subdomainDepth = 0;
|
|
|
|
subdomainDepth <= maxSubdomainDepth; ++subdomainDepth) {
|
|
|
|
|
|
|
|
// Get the base domain (to depth |subdomainDepth|) from passed-in URI:
|
|
|
|
nsAutoCString baseDomainStr;
|
|
|
|
nsresult rv = tldService->GetBaseDomain(aURI, subdomainDepth,
|
|
|
|
baseDomainStr);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// aURI doesn't have |subdomainDepth| levels of subdomains. If we got
|
|
|
|
// here without a match yet, then aURI is not on our whitelist.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare the base domain against each entry in our whitelist:
|
|
|
|
for (size_t i = firstWhitelistIdx; i < sNumBaseDomainsOnWhitelist; ++i) {
|
|
|
|
if (baseDomainStr == sBaseDomainsOnWhitelist[i]) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The actual (non-cached) implementation of IsOnCSSUnprefixingWhitelist():
|
|
|
|
static bool
|
|
|
|
IsOnCSSUnprefixingWhitelistImpl(nsIURI* aURI)
|
|
|
|
{
|
|
|
|
// Check scheme, so we can drop any non-HTTP/HTTPS URIs right away
|
|
|
|
nsAutoCString schemeStr;
|
|
|
|
nsresult rv = aURI->GetScheme(schemeStr);
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
|
|
|
|
// Only proceed if scheme is "http" or "https"
|
|
|
|
if (!(StringBeginsWith(schemeStr, NS_LITERAL_CSTRING("http")) &&
|
|
|
|
(schemeStr.Length() == 4 ||
|
|
|
|
(schemeStr.Length() == 5 && schemeStr[4] == 's')))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (IsOnFullDomainWhitelist(aURI) ||
|
|
|
|
IsOnBaseDomainWhitelist(aURI));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsPrincipal::IsOnCSSUnprefixingWhitelist()
|
|
|
|
{
|
|
|
|
if (mIsOnCSSUnprefixingWhitelist.isNothing()) {
|
|
|
|
// Value not cached -- perform our lazy whitelist-check.
|
|
|
|
// (NOTE: If our URI is mutable, we just assume it's not on the whitelist,
|
|
|
|
// since our caching strategy won't work. This isn't expected to be common.)
|
|
|
|
mIsOnCSSUnprefixingWhitelist.emplace(
|
|
|
|
mCodebaseImmutable &&
|
|
|
|
IsOnCSSUnprefixingWhitelistImpl(mCodebase));
|
|
|
|
}
|
|
|
|
|
|
|
|
return *mIsOnCSSUnprefixingWhitelist;
|
|
|
|
}
|
|
|
|
|
2012-06-10 02:19:26 +04:00
|
|
|
/************************************************************************************************************************/
|
|
|
|
|
2015-05-15 02:35:18 +03:00
|
|
|
static const char EXPANDED_PRINCIPAL_SPEC[] = "[Expanded Principal]";
|
2012-06-10 02:19:26 +04:00
|
|
|
|
2013-04-03 04:16:25 +04:00
|
|
|
NS_IMPL_CLASSINFO(nsExpandedPrincipal, nullptr, nsIClassInfo::MAIN_THREAD_ONLY,
|
2012-06-10 02:19:26 +04:00
|
|
|
NS_EXPANDEDPRINCIPAL_CID)
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_QUERY_INTERFACE_CI(nsExpandedPrincipal,
|
|
|
|
nsIPrincipal,
|
|
|
|
nsIExpandedPrincipal)
|
|
|
|
NS_IMPL_CI_INTERFACE_GETTER(nsExpandedPrincipal,
|
2012-06-10 02:19:26 +04:00
|
|
|
nsIPrincipal,
|
|
|
|
nsIExpandedPrincipal)
|
|
|
|
|
2015-05-13 02:46:21 +03:00
|
|
|
struct OriginComparator
|
|
|
|
{
|
|
|
|
bool LessThan(nsIPrincipal* a, nsIPrincipal* b) const
|
|
|
|
{
|
|
|
|
nsAutoCString originA;
|
|
|
|
nsresult rv = a->GetOrigin(originA);
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
nsAutoCString originB;
|
|
|
|
rv = b->GetOrigin(originB);
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
return originA < originB;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Equals(nsIPrincipal* a, nsIPrincipal* b) const
|
|
|
|
{
|
|
|
|
nsAutoCString originA;
|
|
|
|
nsresult rv = a->GetOrigin(originA);
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
nsAutoCString originB;
|
|
|
|
rv = b->GetOrigin(originB);
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
return a == b;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-06-10 02:19:26 +04:00
|
|
|
nsExpandedPrincipal::nsExpandedPrincipal(nsTArray<nsCOMPtr <nsIPrincipal> > &aWhiteList)
|
|
|
|
{
|
2015-05-13 02:46:21 +03:00
|
|
|
// We force the principals to be sorted by origin so that nsExpandedPrincipal
|
|
|
|
// origins can have a canonical form.
|
|
|
|
OriginComparator c;
|
|
|
|
for (size_t i = 0; i < aWhiteList.Length(); ++i) {
|
|
|
|
mPrincipals.InsertElementSorted(aWhiteList[i], c);
|
|
|
|
}
|
2012-06-10 02:19:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsExpandedPrincipal::~nsExpandedPrincipal()
|
|
|
|
{ }
|
|
|
|
|
2013-01-09 01:53:32 +04:00
|
|
|
NS_IMETHODIMP
|
2012-06-10 02:19:26 +04:00
|
|
|
nsExpandedPrincipal::GetDomain(nsIURI** aDomain)
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
*aDomain = nullptr;
|
2012-06-10 02:19:26 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-01-09 01:53:32 +04:00
|
|
|
NS_IMETHODIMP
|
2012-06-10 02:19:26 +04:00
|
|
|
nsExpandedPrincipal::SetDomain(nsIURI* aDomain)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-01-09 01:53:32 +04:00
|
|
|
NS_IMETHODIMP
|
2015-05-13 01:08:20 +03:00
|
|
|
nsExpandedPrincipal::GetOrigin(nsACString& aOrigin)
|
2012-06-10 02:19:26 +04:00
|
|
|
{
|
2015-05-13 01:45:54 +03:00
|
|
|
aOrigin.AssignLiteral("[Expanded Principal [");
|
|
|
|
for (size_t i = 0; i < mPrincipals.Length(); ++i) {
|
|
|
|
if (i != 0) {
|
|
|
|
aOrigin.AppendLiteral(", ");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString subOrigin;
|
|
|
|
nsresult rv = mPrincipals.ElementAt(i)->GetOrigin(subOrigin);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
aOrigin.Append(subOrigin);
|
|
|
|
}
|
|
|
|
|
|
|
|
aOrigin.Append("]]");
|
2015-05-13 01:08:20 +03:00
|
|
|
return NS_OK;
|
2012-06-10 02:19:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef nsresult (NS_STDCALL nsIPrincipal::*nsIPrincipalMemFn)(nsIPrincipal* aOther,
|
|
|
|
bool* aResult);
|
|
|
|
#define CALL_MEMBER_FUNCTION(THIS,MEM_FN) ((THIS)->*(MEM_FN))
|
|
|
|
|
2014-02-14 06:57:36 +04:00
|
|
|
// nsExpandedPrincipal::Equals and nsExpandedPrincipal::EqualsConsideringDomain
|
2013-01-09 01:53:32 +04:00
|
|
|
// shares the same logic. The difference only that Equals requires 'this'
|
2014-02-14 06:57:36 +04:00
|
|
|
// and 'aOther' to Subsume each other while EqualsConsideringDomain requires
|
|
|
|
// bidirectional SubsumesConsideringDomain.
|
2013-01-09 01:53:32 +04:00
|
|
|
static nsresult
|
2012-06-10 02:19:26 +04:00
|
|
|
Equals(nsExpandedPrincipal* aThis, nsIPrincipalMemFn aFn, nsIPrincipal* aOther,
|
|
|
|
bool* aResult)
|
|
|
|
{
|
2014-02-14 06:57:36 +04:00
|
|
|
// If (and only if) 'aThis' and 'aOther' both Subsume/SubsumesConsideringDomain
|
2012-06-10 02:19:26 +04:00
|
|
|
// each other, then they are Equal.
|
|
|
|
*aResult = false;
|
|
|
|
// Calling the corresponding subsume function on this (aFn).
|
|
|
|
nsresult rv = CALL_MEMBER_FUNCTION(aThis, aFn)(aOther, aResult);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (!*aResult)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
// Calling the corresponding subsume function on aOther (aFn).
|
|
|
|
rv = CALL_MEMBER_FUNCTION(aOther, aFn)(aThis, aResult);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsExpandedPrincipal::Equals(nsIPrincipal* aOther, bool* aResult)
|
|
|
|
{
|
|
|
|
return ::Equals(this, &nsIPrincipal::Subsumes, aOther, aResult);
|
|
|
|
}
|
|
|
|
|
2014-02-14 06:57:34 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsExpandedPrincipal::EqualsConsideringDomain(nsIPrincipal* aOther, bool* aResult)
|
|
|
|
{
|
|
|
|
return ::Equals(this, &nsIPrincipal::SubsumesConsideringDomain, aOther, aResult);
|
|
|
|
}
|
|
|
|
|
2014-02-14 06:57:36 +04:00
|
|
|
// nsExpandedPrincipal::Subsumes and nsExpandedPrincipal::SubsumesConsideringDomain
|
2012-06-10 02:19:26 +04:00
|
|
|
// shares the same logic. The difference only that Subsumes calls are replaced
|
2014-02-14 06:57:36 +04:00
|
|
|
//with SubsumesConsideringDomain calls in the second case.
|
2013-01-09 01:53:32 +04:00
|
|
|
static nsresult
|
|
|
|
Subsumes(nsExpandedPrincipal* aThis, nsIPrincipalMemFn aFn, nsIPrincipal* aOther,
|
2012-06-10 02:19:26 +04:00
|
|
|
bool* aResult)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
2013-01-09 01:53:32 +04:00
|
|
|
nsCOMPtr<nsIExpandedPrincipal> expanded = do_QueryInterface(aOther);
|
2012-06-10 02:19:26 +04:00
|
|
|
if (expanded) {
|
2013-01-09 01:53:32 +04:00
|
|
|
// If aOther is an ExpandedPrincipal too, check if all of its
|
2012-06-10 02:19:26 +04:00
|
|
|
// principals are subsumed.
|
|
|
|
nsTArray< nsCOMPtr<nsIPrincipal> >* otherList;
|
|
|
|
expanded->GetWhiteList(&otherList);
|
|
|
|
for (uint32_t i = 0; i < otherList->Length(); ++i){
|
|
|
|
rv = CALL_MEMBER_FUNCTION(aThis, aFn)((*otherList)[i], aResult);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (!*aResult) {
|
|
|
|
// If we don't subsume at least one principal of aOther, return false.
|
2013-01-09 01:53:32 +04:00
|
|
|
return NS_OK;
|
2012-06-10 02:19:26 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// For a regular aOther, one of our principals must subsume it.
|
|
|
|
nsTArray< nsCOMPtr<nsIPrincipal> >* list;
|
|
|
|
aThis->GetWhiteList(&list);
|
|
|
|
for (uint32_t i = 0; i < list->Length(); ++i){
|
|
|
|
rv = CALL_MEMBER_FUNCTION((*list)[i], aFn)(aOther, aResult);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (*aResult) {
|
|
|
|
// If one of our principal subsumes it, return true.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef CALL_MEMBER_FUNCTION
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsExpandedPrincipal::Subsumes(nsIPrincipal* aOther, bool* aResult)
|
|
|
|
{
|
|
|
|
return ::Subsumes(this, &nsIPrincipal::Subsumes, aOther, aResult);
|
|
|
|
}
|
|
|
|
|
2014-02-14 06:57:34 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsExpandedPrincipal::SubsumesConsideringDomain(nsIPrincipal* aOther, bool* aResult)
|
|
|
|
{
|
|
|
|
return ::Subsumes(this, &nsIPrincipal::SubsumesConsideringDomain, aOther, aResult);
|
|
|
|
}
|
|
|
|
|
2012-06-10 02:19:26 +04:00
|
|
|
NS_IMETHODIMP
|
2012-08-20 22:34:33 +04:00
|
|
|
nsExpandedPrincipal::CheckMayLoad(nsIURI* uri, bool aReport, bool aAllowIfInheritsPrincipal)
|
2012-06-10 02:19:26 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
for (uint32_t i = 0; i < mPrincipals.Length(); ++i){
|
2012-08-20 22:34:33 +04:00
|
|
|
rv = mPrincipals[i]->CheckMayLoad(uri, aReport, aAllowIfInheritsPrincipal);
|
2012-06-10 02:19:26 +04:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_DOM_BAD_URI;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsExpandedPrincipal::GetHashValue(uint32_t* result)
|
2012-06-10 02:19:26 +04:00
|
|
|
{
|
2013-06-29 05:38:30 +04:00
|
|
|
MOZ_CRASH("extended principal should never be used as key in a hash map");
|
2012-06-10 02:19:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsExpandedPrincipal::GetURI(nsIURI** aURI)
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
*aURI = nullptr;
|
2012-06-10 02:19:26 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-01-09 01:53:32 +04:00
|
|
|
NS_IMETHODIMP
|
2012-06-10 02:19:26 +04:00
|
|
|
nsExpandedPrincipal::GetWhiteList(nsTArray<nsCOMPtr<nsIPrincipal> >** aWhiteList)
|
|
|
|
{
|
|
|
|
*aWhiteList = &mPrincipals;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-07-20 09:44:03 +04:00
|
|
|
NS_IMETHODIMP
|
2013-09-11 08:18:36 +04:00
|
|
|
nsExpandedPrincipal::GetJarPrefix(nsACString& aJarPrefix)
|
2012-07-20 09:44:03 +04:00
|
|
|
{
|
2013-09-11 08:18:36 +04:00
|
|
|
aJarPrefix.Truncate();
|
|
|
|
return NS_OK;
|
2012-07-20 09:44:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsExpandedPrincipal::GetAppStatus(uint16_t* aAppStatus)
|
2012-07-20 09:44:03 +04:00
|
|
|
{
|
2012-10-09 16:47:46 +04:00
|
|
|
*aAppStatus = nsIPrincipal::APP_STATUS_NOT_INSTALLED;
|
|
|
|
return NS_OK;
|
2012-07-20 09:44:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsExpandedPrincipal::GetAppId(uint32_t* aAppId)
|
2012-07-20 09:44:03 +04:00
|
|
|
{
|
2012-10-09 16:47:46 +04:00
|
|
|
*aAppId = nsIScriptSecurityManager::NO_APP_ID;
|
|
|
|
return NS_OK;
|
2012-07-20 09:44:03 +04:00
|
|
|
}
|
|
|
|
|
2012-07-31 19:47:20 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsExpandedPrincipal::GetIsInBrowserElement(bool* aIsInBrowserElement)
|
|
|
|
{
|
2012-10-09 16:47:46 +04:00
|
|
|
*aIsInBrowserElement = false;
|
|
|
|
return NS_OK;
|
2012-07-31 19:47:20 +04:00
|
|
|
}
|
|
|
|
|
2012-09-26 03:28:17 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsExpandedPrincipal::GetUnknownAppId(bool* aUnknownAppId)
|
|
|
|
{
|
|
|
|
*aUnknownAppId = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-01-09 01:53:32 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsExpandedPrincipal::GetBaseDomain(nsACString& aBaseDomain)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2015-03-13 23:15:09 +03:00
|
|
|
bool
|
|
|
|
nsExpandedPrincipal::IsOnCSSUnprefixingWhitelist()
|
|
|
|
{
|
|
|
|
// CSS Unprefixing Whitelist is a per-origin thing; doesn't really make sense
|
|
|
|
// for an expanded principal. (And probably shouldn't be needed.)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-10 02:19:26 +04:00
|
|
|
void
|
|
|
|
nsExpandedPrincipal::GetScriptLocation(nsACString& aStr)
|
|
|
|
{
|
2015-05-15 02:35:18 +03:00
|
|
|
aStr.Assign(EXPANDED_PRINCIPAL_SPEC);
|
|
|
|
aStr.AppendLiteral(" (");
|
2015-04-10 18:52:29 +03:00
|
|
|
|
|
|
|
for (size_t i = 0; i < mPrincipals.Length(); ++i) {
|
|
|
|
if (i != 0) {
|
|
|
|
aStr.AppendLiteral(", ");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString spec;
|
|
|
|
nsJSPrincipals::get(mPrincipals.ElementAt(i))->GetScriptLocation(spec);
|
|
|
|
|
|
|
|
aStr.Append(spec);
|
|
|
|
|
|
|
|
}
|
|
|
|
aStr.Append(")");
|
2012-06-10 02:19:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////
|
|
|
|
// Methods implementing nsISerializable //
|
|
|
|
//////////////////////////////////////////
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsExpandedPrincipal::Read(nsIObjectInputStream* aStream)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsExpandedPrincipal::Write(nsIObjectOutputStream* aStream)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|