2006-04-02 20:58:26 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
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 "nsIUUIDGenerator.h"
|
|
|
|
#include "nsID.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsIClassInfoImpl.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"
|
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)
|
2006-04-02 20:58:26 +00:00
|
|
|
NS_IMPL_QUERY_INTERFACE2_CI(nsNullPrincipal,
|
|
|
|
nsIPrincipal,
|
|
|
|
nsISerializable)
|
|
|
|
NS_IMPL_CI_INTERFACE_GETTER2(nsNullPrincipal,
|
|
|
|
nsIPrincipal,
|
|
|
|
nsISerializable)
|
|
|
|
|
2013-11-27 15:37:10 +00:00
|
|
|
NS_IMETHODIMP_(nsrefcnt)
|
2006-04-02 20:58:26 +00:00
|
|
|
nsNullPrincipal::AddRef()
|
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
NS_PRECONDITION(int32_t(refcount) >= 0, "illegal refcnt");
|
2013-11-27 15:37:10 +00:00
|
|
|
nsrefcnt count = ++refcount;
|
2006-04-02 20:58:26 +00:00
|
|
|
NS_LOG_ADDREF(this, count, "nsNullPrincipal", sizeof(*this));
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(nsrefcnt)
|
|
|
|
nsNullPrincipal::Release()
|
|
|
|
{
|
2012-03-09 09:48:50 +00:00
|
|
|
NS_PRECONDITION(0 != refcount, "dup release");
|
2013-11-27 15:37:10 +00:00
|
|
|
nsrefcnt count = --refcount;
|
2006-04-02 20:58:26 +00:00
|
|
|
NS_LOG_RELEASE(this, count, "nsNullPrincipal");
|
|
|
|
if (count == 0) {
|
2010-07-05 09:42:18 +00:00
|
|
|
delete this;
|
2006-04-02 20:58:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsNullPrincipal::nsNullPrincipal()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsNullPrincipal::~nsNullPrincipal()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-09-28 14:31:04 +00:00
|
|
|
#define NS_NULLPRINCIPAL_PREFIX NS_NULLPRINCIPAL_SCHEME ":"
|
|
|
|
|
2006-04-02 20:58:26 +00:00
|
|
|
nsresult
|
|
|
|
nsNullPrincipal::Init()
|
|
|
|
{
|
|
|
|
// FIXME: bug 327161 -- make sure the uuid generator is reseeding-resistant.
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIUUIDGenerator> uuidgen =
|
|
|
|
do_GetService("@mozilla.org/uuid-generator;1", &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsID id;
|
|
|
|
rv = uuidgen->GenerateUUIDInPlace(&id);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2008-01-12 04:30:42 +00:00
|
|
|
char chars[NSID_LENGTH];
|
|
|
|
id.ToProvidedString(chars);
|
2006-04-02 20:58:26 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t suffixLen = NSID_LENGTH - 1;
|
|
|
|
uint32_t prefixLen = ArrayLength(NS_NULLPRINCIPAL_PREFIX) - 1;
|
2007-09-28 14:31:04 +00:00
|
|
|
|
|
|
|
// Use an nsCString so we only do the allocation once here and then share
|
|
|
|
// with nsJSPrincipals
|
|
|
|
nsCString str;
|
|
|
|
str.SetCapacity(prefixLen + suffixLen);
|
2006-04-02 20:58:26 +00:00
|
|
|
|
2007-09-28 14:31:04 +00:00
|
|
|
str.Append(NS_NULLPRINCIPAL_PREFIX);
|
2006-04-02 20:58:26 +00:00
|
|
|
str.Append(chars);
|
2008-08-27 22:11:02 +00:00
|
|
|
|
2006-04-02 20:58:26 +00:00
|
|
|
if (str.Length() != prefixLen + suffixLen) {
|
2007-09-28 14:31:04 +00:00
|
|
|
NS_WARNING("Out of memory allocating null-principal URI");
|
2006-04-02 20:58:26 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2008-08-27 22:11:02 +00:00
|
|
|
mURI = new nsNullPrincipalURI(str);
|
|
|
|
NS_ENSURE_TRUE(mURI, NS_ERROR_OUT_OF_MEMORY);
|
2006-05-11 16:06:35 +00:00
|
|
|
|
2012-03-09 09:48:50 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsNullPrincipal::GetScriptLocation(nsACString &aStr)
|
|
|
|
{
|
|
|
|
mURI->GetSpec(aStr);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
void nsNullPrincipal::dumpImpl()
|
|
|
|
{
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString str;
|
2012-03-09 09:48:50 +00:00
|
|
|
mURI->GetSpec(str);
|
|
|
|
fprintf(stderr, "nsNullPrincipal (%p) = %s\n", this, str.get());
|
2006-04-02 20:58:26 +00:00
|
|
|
}
|
2012-03-09 09:48:50 +00:00
|
|
|
#endif
|
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;
|
|
|
|
}
|
|
|
|
|
2011-05-19 11:31:54 +00:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsNullPrincipal::EqualsIgnoringDomain(nsIPrincipal *aOther, bool *aResult)
|
2011-05-19 11:31:54 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-01-22 21:38:21 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNullPrincipal::GetCsp(nsIContentSecurityPolicy** aCsp)
|
|
|
|
{
|
2013-11-22 23:12:00 +00:00
|
|
|
NS_IF_ADDREF(*aCsp = mCSP);
|
2010-01-22 21:38:21 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNullPrincipal::SetCsp(nsIContentSecurityPolicy* aCsp)
|
|
|
|
{
|
2013-11-22 23:12:00 +00:00
|
|
|
// If CSP was already set, it should not be destroyed! Instead, it should
|
|
|
|
// get set anew when a new principal is created.
|
|
|
|
if (mCSP)
|
|
|
|
return NS_ERROR_ALREADY_INITIALIZED;
|
|
|
|
|
|
|
|
mCSP = aCsp;
|
|
|
|
return NS_OK;
|
2010-01-22 21:38:21 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNullPrincipal::GetOrigin(char** aOrigin)
|
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
*aOrigin = nullptr;
|
2006-04-02 20:58:26 +00:00
|
|
|
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString str;
|
2006-04-02 20:58:26 +00:00
|
|
|
nsresult rv = mURI->GetSpec(str);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
*aOrigin = ToNewCString(str);
|
|
|
|
NS_ENSURE_TRUE(*aOrigin, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-02-23 17:41:25 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNullPrincipal::SubsumesIgnoringDomain(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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Also allow the load if the principal of the URI being checked is exactly
|
|
|
|
// us ie this.
|
|
|
|
nsCOMPtr<nsIURIWithPrincipal> uriPrinc = do_QueryInterface(aURI);
|
|
|
|
if (uriPrinc) {
|
|
|
|
nsCOMPtr<nsIPrincipal> principal;
|
|
|
|
uriPrinc->GetPrincipal(getter_AddRefs(principal));
|
|
|
|
|
|
|
|
if (principal && principal == this) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
*aAppStatus = nsIPrincipal::APP_STATUS_NOT_INSTALLED;
|
|
|
|
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
|
|
|
{
|
|
|
|
*aAppId = nsIScriptSecurityManager::NO_APP_ID;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-07-31 15:47:20 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNullPrincipal::GetIsInBrowserElement(bool* aIsInBrowserElement)
|
|
|
|
{
|
|
|
|
*aIsInBrowserElement = false;
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
// no-op: CID is sufficient to create a useful nsNullPrincipal, since the URI
|
|
|
|
// is not really relevant.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNullPrincipal::Write(nsIObjectOutputStream* aStream)
|
|
|
|
{
|
|
|
|
// no-op: CID is sufficient to create a useful nsNullPrincipal, since the URI
|
|
|
|
// is not really relevant.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|