2006-04-03 00:58:26 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2015-02-24 23:54:40 +03:00
|
|
|
/* vim: set sw=2 sts=2 ts=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/. */
|
2006-04-03 00:58:26 +04: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 06:52:54 +04:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2011-10-11 09:50:08 +04:00
|
|
|
|
2016-04-21 11:51:25 +03:00
|
|
|
#include "nsDocShell.h"
|
2006-04-03 00:58:26 +04:00
|
|
|
#include "nsNullPrincipal.h"
|
2008-08-28 02:11:02 +04:00
|
|
|
#include "nsNullPrincipalURI.h"
|
2006-04-03 00:58:26 +04:00
|
|
|
#include "nsMemory.h"
|
2015-07-07 05:17:00 +03:00
|
|
|
#include "nsIURIWithPrincipal.h"
|
2006-04-03 00:58:26 +04:00
|
|
|
#include "nsIClassInfoImpl.h"
|
2006-05-11 20:06:35 +04:00
|
|
|
#include "nsNetCID.h"
|
2012-07-27 18:03:27 +04:00
|
|
|
#include "nsError.h"
|
2012-07-20 09:44:03 +04:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
2014-03-29 23:10:27 +04:00
|
|
|
#include "nsPrincipal.h"
|
2008-02-27 06:45:29 +03:00
|
|
|
#include "nsScriptSecurityManager.h"
|
2013-07-12 00:21:45 +04:00
|
|
|
#include "pratom.h"
|
2006-05-11 20:06:35 +04:00
|
|
|
|
2011-10-11 09:50:08 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2013-04-03 04:16:25 +04:00
|
|
|
NS_IMPL_CLASSINFO(nsNullPrincipal, nullptr, nsIClassInfo::MAIN_THREAD_ONLY,
|
2010-06-22 20:59:57 +04:00
|
|
|
NS_NULLPRINCIPAL_CID)
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_QUERY_INTERFACE_CI(nsNullPrincipal,
|
|
|
|
nsIPrincipal,
|
|
|
|
nsISerializable)
|
|
|
|
NS_IMPL_CI_INTERFACE_GETTER(nsNullPrincipal,
|
2006-04-03 00:58:26 +04:00
|
|
|
nsIPrincipal,
|
|
|
|
nsISerializable)
|
|
|
|
|
2014-07-29 19:47:52 +04:00
|
|
|
/* static */ already_AddRefed<nsNullPrincipal>
|
|
|
|
nsNullPrincipal::CreateWithInheritedAttributes(nsIPrincipal* aInheritFrom)
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsNullPrincipal> nullPrin = new nsNullPrincipal();
|
2015-05-14 22:24:03 +03:00
|
|
|
nsresult rv = nullPrin->Init(Cast(aInheritFrom)->OriginAttributesRef());
|
2016-04-21 11:30:03 +03:00
|
|
|
MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
return nullPrin.forget();
|
2014-07-29 19:47:52 +04:00
|
|
|
}
|
|
|
|
|
2016-04-21 11:51:25 +03:00
|
|
|
/* static */ already_AddRefed<nsNullPrincipal>
|
|
|
|
nsNullPrincipal::CreateWithInheritedAttributes(nsIDocShell* aDocShell)
|
|
|
|
{
|
|
|
|
PrincipalOriginAttributes attrs;
|
|
|
|
attrs.InheritFromDocShellToDoc(nsDocShell::Cast(aDocShell)->GetOriginAttributes(), nullptr);
|
|
|
|
|
|
|
|
RefPtr<nsNullPrincipal> nullPrin = new nsNullPrincipal();
|
|
|
|
nsresult rv = nullPrin->Init(attrs);
|
|
|
|
MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
return nullPrin.forget();
|
|
|
|
}
|
|
|
|
|
2015-03-31 20:11:00 +03:00
|
|
|
/* static */ already_AddRefed<nsNullPrincipal>
|
2015-11-03 04:50:54 +03:00
|
|
|
nsNullPrincipal::Create(const PrincipalOriginAttributes& aOriginAttributes)
|
2015-03-31 20:11:00 +03:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsNullPrincipal> nullPrin = new nsNullPrincipal();
|
2015-05-14 22:24:03 +03:00
|
|
|
nsresult rv = nullPrin->Init(aOriginAttributes);
|
2016-04-21 11:30:03 +03:00
|
|
|
MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv));
|
2015-03-31 20:11:00 +03:00
|
|
|
|
|
|
|
return nullPrin.forget();
|
|
|
|
}
|
2007-09-28 18:31:04 +04:00
|
|
|
|
2006-04-03 00:58:26 +04:00
|
|
|
nsresult
|
2015-11-03 04:50:54 +03:00
|
|
|
nsNullPrincipal::Init(const PrincipalOriginAttributes& aOriginAttributes)
|
2006-04-03 00:58:26 +04:00
|
|
|
{
|
2015-05-14 22:24:03 +03:00
|
|
|
mOriginAttributes = aOriginAttributes;
|
2014-07-29 19:47:52 +04:00
|
|
|
|
2015-03-31 20:11:00 +03:00
|
|
|
mURI = nsNullPrincipalURI::Create();
|
|
|
|
NS_ENSURE_TRUE(mURI, NS_ERROR_NOT_AVAILABLE);
|
2015-02-24 23:54:40 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-08-30 07:22:04 +03:00
|
|
|
nsresult
|
2015-02-24 23:54:40 +03:00
|
|
|
nsNullPrincipal::GetScriptLocation(nsACString &aStr)
|
|
|
|
{
|
2016-08-30 07:22:04 +03:00
|
|
|
return mURI->GetSpec(aStr);
|
2015-02-24 23:54:40 +03:00
|
|
|
}
|
|
|
|
|
2006-04-03 00:58:26 +04:00
|
|
|
/**
|
|
|
|
* nsIPrincipal implementation
|
|
|
|
*/
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsNullPrincipal::GetHashValue(uint32_t *aResult)
|
2006-04-03 00:58:26 +04:00
|
|
|
{
|
|
|
|
*aResult = (NS_PTR_TO_INT32(this) >> 2);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-06-04 21:51:57 +03:00
|
|
|
NS_IMETHODIMP
|
2006-04-03 00:58:26 +04:00
|
|
|
nsNullPrincipal::GetURI(nsIURI** aURI)
|
|
|
|
{
|
2006-06-20 07:17:41 +04:00
|
|
|
return NS_EnsureSafeToReturn(mURI, aURI);
|
2006-04-03 00:58:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNullPrincipal::GetDomain(nsIURI** aDomain)
|
|
|
|
{
|
2006-06-20 07:17:41 +04:00
|
|
|
return NS_EnsureSafeToReturn(mURI, aDomain);
|
2006-04-03 00:58:26 +04: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-15 02:50:44 +03:00
|
|
|
nsresult
|
|
|
|
nsNullPrincipal::GetOriginInternal(nsACString& aOrigin)
|
2006-04-03 00:58:26 +04:00
|
|
|
{
|
2015-05-13 01:08:20 +03:00
|
|
|
return mURI->GetSpec(aOrigin);
|
2006-04-03 00:58:26 +04:00
|
|
|
}
|
|
|
|
|
2015-10-01 06:03:36 +03:00
|
|
|
bool
|
|
|
|
nsNullPrincipal::MayLoadInternal(nsIURI* aURI)
|
|
|
|
{
|
2014-09-30 12:09:36 +04: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 22:34:33 +04:00
|
|
|
|
2014-09-30 12:09:36 +04:00
|
|
|
if (principal == this) {
|
2015-10-01 06:03:36 +03:00
|
|
|
return true;
|
2012-08-20 22:34:33 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-01 06:03:36 +03:00
|
|
|
return false;
|
2008-02-27 06:45:29 +03:00
|
|
|
}
|
|
|
|
|
2013-01-09 01:53:32 +04: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-03 00:58:26 +04:00
|
|
|
/**
|
|
|
|
* nsISerializable implementation
|
|
|
|
*/
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNullPrincipal::Read(nsIObjectInputStream* aStream)
|
|
|
|
{
|
2014-07-29 19:47:52 +04: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().
|
2015-06-04 21:51:57 +03:00
|
|
|
nsAutoCString suffix;
|
|
|
|
nsresult rv = aStream->ReadCString(suffix);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
bool ok = mOriginAttributes.PopulateFromSuffix(suffix);
|
|
|
|
NS_ENSURE_TRUE(ok, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
return NS_OK;
|
2006-04-03 00:58:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNullPrincipal::Write(nsIObjectOutputStream* aStream)
|
|
|
|
{
|
2015-06-04 21:51:57 +03:00
|
|
|
nsAutoCString suffix;
|
|
|
|
OriginAttributesRef().CreateSuffix(suffix);
|
|
|
|
|
|
|
|
nsresult rv = aStream->WriteStringZ(suffix.get());
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2006-04-03 00:58:26 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|