2006-04-02 20:58:26 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2015-02-24 20:54:40 +00:00
|
|
|
/* vim: set sw=2 sts=2 ts=2 et tw=80: */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2006-04-02 20:58:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the principal that has no rights and can't be accessed by
|
|
|
|
* anything other than itself and chrome; null principals are not
|
|
|
|
* same-origin with anything but themselves.
|
|
|
|
*/
|
|
|
|
|
2013-12-09 02:52:54 +00:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2011-10-11 05:50:08 +00:00
|
|
|
|
2006-04-02 20:58:26 +00:00
|
|
|
#include "nsNullPrincipal.h"
|
2008-08-27 22:11:02 +00:00
|
|
|
#include "nsNullPrincipalURI.h"
|
2006-04-02 20:58:26 +00:00
|
|
|
#include "nsMemory.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsIClassInfoImpl.h"
|
2014-07-29 15:47:52 +00:00
|
|
|
#include "nsIObjectInputStream.h"
|
|
|
|
#include "nsIObjectOutputStream.h"
|
2006-05-11 16:06:35 +00:00
|
|
|
#include "nsNetCID.h"
|
2012-07-27 14:03:27 +00:00
|
|
|
#include "nsError.h"
|
2012-07-20 05:44:03 +00:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
2014-03-29 19:10:27 +00:00
|
|
|
#include "nsPrincipal.h"
|
2008-02-27 03:45:29 +00:00
|
|
|
#include "nsScriptSecurityManager.h"
|
2013-07-11 20:21:45 +00:00
|
|
|
#include "pratom.h"
|
2006-05-11 16:06:35 +00:00
|
|
|
|
2011-10-11 05:50:08 +00:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2013-04-03 00:16:25 +00:00
|
|
|
NS_IMPL_CLASSINFO(nsNullPrincipal, nullptr, nsIClassInfo::MAIN_THREAD_ONLY,
|
2010-06-22 16:59:57 +00:00
|
|
|
NS_NULLPRINCIPAL_CID)
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_QUERY_INTERFACE_CI(nsNullPrincipal,
|
|
|
|
nsIPrincipal,
|
|
|
|
nsISerializable)
|
|
|
|
NS_IMPL_CI_INTERFACE_GETTER(nsNullPrincipal,
|
2006-04-02 20:58:26 +00:00
|
|
|
nsIPrincipal,
|
|
|
|
nsISerializable)
|
|
|
|
|
2014-07-29 15:47:52 +00:00
|
|
|
/* static */ already_AddRefed<nsNullPrincipal>
|
|
|
|
nsNullPrincipal::CreateWithInheritedAttributes(nsIPrincipal* aInheritFrom)
|
|
|
|
{
|
|
|
|
nsRefPtr<nsNullPrincipal> nullPrin = new nsNullPrincipal();
|
|
|
|
nsresult rv = nullPrin->Init(aInheritFrom->GetAppId(),
|
|
|
|
aInheritFrom->GetIsInBrowserElement());
|
|
|
|
return NS_SUCCEEDED(rv) ? nullPrin.forget() : nullptr;
|
|
|
|
}
|
|
|
|
|
2015-03-31 17:11:00 +00:00
|
|
|
/* static */ already_AddRefed<nsNullPrincipal>
|
|
|
|
nsNullPrincipal::Create(uint32_t aAppId, bool aInMozBrowser)
|
|
|
|
{
|
|
|
|
nsRefPtr<nsNullPrincipal> nullPrin = new nsNullPrincipal();
|
|
|
|
nsresult rv = nullPrin->Init(aAppId, aInMozBrowser);
|
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
|
|
|
|
|
|
|
return nullPrin.forget();
|
|
|
|
}
|
2007-09-28 14:31:04 +00:00
|
|
|
|
2006-04-02 20:58:26 +00:00
|
|
|
nsresult
|
2014-07-29 15:47:52 +00:00
|
|
|
nsNullPrincipal::Init(uint32_t aAppId, bool aInMozBrowser)
|
2006-04-02 20:58:26 +00:00
|
|
|
{
|
2014-07-29 15:47:52 +00:00
|
|
|
MOZ_ASSERT(aAppId != nsIScriptSecurityManager::UNKNOWN_APP_ID);
|
|
|
|
mAppId = aAppId;
|
|
|
|
mInMozBrowser = aInMozBrowser;
|
|
|
|
|
2015-03-31 17:11:00 +00:00
|
|
|
mURI = nsNullPrincipalURI::Create();
|
|
|
|
NS_ENSURE_TRUE(mURI, NS_ERROR_NOT_AVAILABLE);
|
2015-02-24 20:54:40 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsNullPrincipal::GetScriptLocation(nsACString &aStr)
|
|
|
|
{
|
|
|
|
mURI->GetSpec(aStr);
|
|
|
|
}
|
|
|
|
|
2006-04-02 20:58:26 +00:00
|
|
|
/**
|
|
|
|
* nsIPrincipal implementation
|
|
|
|
*/
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsNullPrincipal::Equals(nsIPrincipal *aOther, bool *aResult)
|
2006-04-02 20:58:26 +00:00
|
|
|
{
|
|
|
|
// Just equal to ourselves. Note that nsPrincipal::Equals will return false
|
|
|
|
// for us since we have a unique domain/origin/etc.
|
|
|
|
*aResult = (aOther == this);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-02-14 02:57:34 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNullPrincipal::EqualsConsideringDomain(nsIPrincipal *aOther, bool *aResult)
|
|
|
|
{
|
|
|
|
return Equals(aOther, aResult);
|
|
|
|
}
|
|
|
|
|
2006-04-02 20:58:26 +00:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsNullPrincipal::GetHashValue(uint32_t *aResult)
|
2006-04-02 20:58:26 +00:00
|
|
|
{
|
|
|
|
*aResult = (NS_PTR_TO_INT32(this) >> 2);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNullPrincipal::GetURI(nsIURI** aURI)
|
|
|
|
{
|
2006-06-20 03:17:41 +00:00
|
|
|
return NS_EnsureSafeToReturn(mURI, aURI);
|
2006-04-02 20:58:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNullPrincipal::GetDomain(nsIURI** aDomain)
|
|
|
|
{
|
2006-06-20 03:17:41 +00:00
|
|
|
return NS_EnsureSafeToReturn(mURI, aDomain);
|
2006-04-02 20:58:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNullPrincipal::SetDomain(nsIURI* aDomain)
|
|
|
|
{
|
|
|
|
// I think the right thing to do here is to just throw... Silently failing
|
|
|
|
// seems counterproductive.
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2015-05-12 22:08:20 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNullPrincipal::GetOrigin(nsACString& aOrigin)
|
2006-04-02 20:58:26 +00:00
|
|
|
{
|
2015-05-12 22:08:20 +00:00
|
|
|
return mURI->GetSpec(aOrigin);
|
2006-04-02 20:58:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsNullPrincipal::Subsumes(nsIPrincipal *aOther, bool *aResult)
|
2006-04-02 20:58:26 +00:00
|
|
|
{
|
|
|
|
// We don't subsume anything except ourselves. Note that nsPrincipal::Equals
|
|
|
|
// will return false for us, since we're not about:blank and not Equals to
|
|
|
|
// reasonable nsPrincipals.
|
|
|
|
*aResult = (aOther == this);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-02-14 02:57:34 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNullPrincipal::SubsumesConsideringDomain(nsIPrincipal *aOther, bool *aResult)
|
|
|
|
{
|
|
|
|
return Subsumes(aOther, aResult);
|
|
|
|
}
|
|
|
|
|
2008-02-27 03:45:29 +00:00
|
|
|
NS_IMETHODIMP
|
2012-08-20 18:34:33 +00:00
|
|
|
nsNullPrincipal::CheckMayLoad(nsIURI* aURI, bool aReport, bool aAllowIfInheritsPrincipal)
|
|
|
|
{
|
|
|
|
if (aAllowIfInheritsPrincipal) {
|
|
|
|
if (nsPrincipal::IsPrincipalInherited(aURI)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-09-30 08:09:36 +00:00
|
|
|
}
|
2012-08-20 18:34:33 +00:00
|
|
|
|
2014-09-30 08:09:36 +00:00
|
|
|
// Also allow the load if we are the principal of the URI being checked.
|
|
|
|
nsCOMPtr<nsIURIWithPrincipal> uriPrinc = do_QueryInterface(aURI);
|
|
|
|
if (uriPrinc) {
|
|
|
|
nsCOMPtr<nsIPrincipal> principal;
|
|
|
|
uriPrinc->GetPrincipal(getter_AddRefs(principal));
|
2012-08-20 18:34:33 +00:00
|
|
|
|
2014-09-30 08:09:36 +00:00
|
|
|
if (principal == this) {
|
|
|
|
return NS_OK;
|
2012-08-20 18:34:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-27 03:45:29 +00:00
|
|
|
if (aReport) {
|
|
|
|
nsScriptSecurityManager::ReportError(
|
2012-07-30 14:20:58 +00:00
|
|
|
nullptr, NS_LITERAL_STRING("CheckSameOriginError"), mURI, aURI);
|
2008-02-27 03:45:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_DOM_BAD_URI;
|
|
|
|
}
|
|
|
|
|
2012-07-20 05:44:03 +00:00
|
|
|
NS_IMETHODIMP
|
2013-09-11 04:18:36 +00:00
|
|
|
nsNullPrincipal::GetJarPrefix(nsACString& aJarPrefix)
|
2012-07-20 05:44:03 +00:00
|
|
|
{
|
2013-09-11 04:18:36 +00:00
|
|
|
aJarPrefix.Truncate();
|
|
|
|
return NS_OK;
|
2012-07-20 05:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsNullPrincipal::GetAppStatus(uint16_t* aAppStatus)
|
2012-07-20 05:44:03 +00:00
|
|
|
{
|
2014-07-29 15:47:52 +00:00
|
|
|
*aAppStatus = nsScriptSecurityManager::AppStatusForPrincipal(this);
|
2012-07-20 05:44:03 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsNullPrincipal::GetAppId(uint32_t* aAppId)
|
2012-07-20 05:44:03 +00:00
|
|
|
{
|
2014-07-29 15:47:52 +00:00
|
|
|
*aAppId = mAppId;
|
2012-07-20 05:44:03 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-07-31 15:47:20 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNullPrincipal::GetIsInBrowserElement(bool* aIsInBrowserElement)
|
|
|
|
{
|
2014-07-29 15:47:52 +00:00
|
|
|
*aIsInBrowserElement = mInMozBrowser;
|
2012-07-31 15:47:20 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-25 23:28:17 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNullPrincipal::GetUnknownAppId(bool* aUnknownAppId)
|
|
|
|
{
|
|
|
|
*aUnknownAppId = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-10-23 16:31:19 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNullPrincipal::GetIsNullPrincipal(bool* aIsNullPrincipal)
|
|
|
|
{
|
|
|
|
*aIsNullPrincipal = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-01-08 21:53:32 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNullPrincipal::GetBaseDomain(nsACString& aBaseDomain)
|
|
|
|
{
|
|
|
|
// For a null principal, we use our unique uuid as the base domain.
|
|
|
|
return mURI->GetPath(aBaseDomain);
|
|
|
|
}
|
|
|
|
|
2006-04-02 20:58:26 +00:00
|
|
|
/**
|
|
|
|
* nsISerializable implementation
|
|
|
|
*/
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNullPrincipal::Read(nsIObjectInputStream* aStream)
|
|
|
|
{
|
2014-07-29 15:47:52 +00:00
|
|
|
// Note - nsNullPrincipal use NS_GENERIC_FACTORY_CONSTRUCTOR_INIT, which means
|
|
|
|
// that the Init() method has already been invoked by the time we deserialize.
|
|
|
|
// This is in contrast to nsPrincipal, which uses NS_GENERIC_FACTORY_CONSTRUCTOR,
|
|
|
|
// in which case ::Read needs to invoke Init().
|
|
|
|
nsresult rv = aStream->Read32(&mAppId);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aStream->ReadBoolean(&mInMozBrowser);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2006-04-02 20:58:26 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNullPrincipal::Write(nsIObjectOutputStream* aStream)
|
|
|
|
{
|
2014-07-29 15:47:52 +00:00
|
|
|
aStream->Write32(mAppId);
|
|
|
|
aStream->WriteBoolean(mInMozBrowser);
|
2006-04-02 20:58:26 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|