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"
|
2017-03-22 13:38:40 +03:00
|
|
|
#include "NullPrincipal.h"
|
|
|
|
#include "NullPrincipalURI.h"
|
2006-04-03 00:58:26 +04:00
|
|
|
#include "nsMemory.h"
|
|
|
|
#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"
|
2017-03-22 13:39:31 +03:00
|
|
|
#include "ContentPrincipal.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;
|
|
|
|
|
2017-03-22 13:38:40 +03:00
|
|
|
NS_IMPL_CLASSINFO(NullPrincipal, nullptr, nsIClassInfo::MAIN_THREAD_ONLY,
|
2010-06-22 20:59:57 +04:00
|
|
|
NS_NULLPRINCIPAL_CID)
|
2017-03-22 13:38:40 +03:00
|
|
|
NS_IMPL_QUERY_INTERFACE_CI(NullPrincipal,
|
2014-04-27 11:06:00 +04:00
|
|
|
nsIPrincipal,
|
|
|
|
nsISerializable)
|
2017-03-22 13:38:40 +03:00
|
|
|
NS_IMPL_CI_INTERFACE_GETTER(NullPrincipal,
|
2006-04-03 00:58:26 +04:00
|
|
|
nsIPrincipal,
|
|
|
|
nsISerializable)
|
|
|
|
|
2017-03-22 13:38:40 +03:00
|
|
|
/* static */ already_AddRefed<NullPrincipal>
|
|
|
|
NullPrincipal::CreateWithInheritedAttributes(nsIPrincipal* aInheritFrom)
|
2014-07-29 19:47:52 +04:00
|
|
|
{
|
2018-10-30 03:13:29 +03:00
|
|
|
MOZ_ASSERT(aInheritFrom);
|
|
|
|
return CreateWithInheritedAttributes(Cast(aInheritFrom)->OriginAttributesRef(), false);
|
2014-07-29 19:47:52 +04:00
|
|
|
}
|
|
|
|
|
2017-03-22 13:38:40 +03:00
|
|
|
/* static */ already_AddRefed<NullPrincipal>
|
|
|
|
NullPrincipal::CreateWithInheritedAttributes(nsIDocShell* aDocShell, bool aIsFirstParty)
|
2016-04-21 11:51:25 +03:00
|
|
|
{
|
2018-10-30 03:13:29 +03:00
|
|
|
MOZ_ASSERT(aDocShell);
|
|
|
|
|
2017-03-01 13:59:57 +03:00
|
|
|
OriginAttributes attrs = nsDocShell::Cast(aDocShell)->GetOriginAttributes();
|
2018-10-30 03:13:29 +03:00
|
|
|
return CreateWithInheritedAttributes(attrs, aIsFirstParty);
|
|
|
|
}
|
2017-03-01 13:59:57 +03:00
|
|
|
|
2018-10-30 03:13:29 +03:00
|
|
|
/* static */ already_AddRefed<NullPrincipal>
|
|
|
|
NullPrincipal::CreateWithInheritedAttributes(const OriginAttributes& aOriginAttributes, bool aIsFirstParty)
|
|
|
|
{
|
2017-03-22 13:38:40 +03:00
|
|
|
RefPtr<NullPrincipal> nullPrin = new NullPrincipal();
|
2018-10-30 03:13:29 +03:00
|
|
|
nsresult rv = nullPrin->Init(aOriginAttributes, aIsFirstParty);
|
2016-04-21 11:51:25 +03:00
|
|
|
MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
return nullPrin.forget();
|
|
|
|
}
|
|
|
|
|
2017-03-22 13:38:40 +03:00
|
|
|
/* static */ already_AddRefed<NullPrincipal>
|
|
|
|
NullPrincipal::Create(const OriginAttributes& aOriginAttributes, nsIURI* aURI)
|
2015-03-31 20:11:00 +03:00
|
|
|
{
|
2017-03-22 13:38:40 +03:00
|
|
|
RefPtr<NullPrincipal> nullPrin = new NullPrincipal();
|
2016-11-30 17:13:27 +03:00
|
|
|
nsresult rv = nullPrin->Init(aOriginAttributes, aURI);
|
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
|
|
|
|
2018-03-22 21:36:20 +03:00
|
|
|
/* static */ already_AddRefed<NullPrincipal>
|
|
|
|
NullPrincipal::CreateWithoutOriginAttributes()
|
|
|
|
{
|
2018-07-17 22:37:48 +03:00
|
|
|
return NullPrincipal::Create(OriginAttributes(), nullptr);
|
2018-03-22 21:36:20 +03:00
|
|
|
}
|
|
|
|
|
2006-04-03 00:58:26 +04:00
|
|
|
nsresult
|
2017-03-22 13:38:40 +03:00
|
|
|
NullPrincipal::Init(const OriginAttributes& aOriginAttributes, nsIURI* aURI)
|
2006-04-03 00:58:26 +04:00
|
|
|
{
|
2016-11-30 17:13:27 +03:00
|
|
|
if (aURI) {
|
|
|
|
nsAutoCString scheme;
|
|
|
|
nsresult rv = aURI->GetScheme(scheme);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
NS_ENSURE_TRUE(scheme.EqualsLiteral(NS_NULLPRINCIPAL_SCHEME),
|
|
|
|
NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
|
|
|
mURI = aURI;
|
|
|
|
} else {
|
2017-03-22 13:38:40 +03:00
|
|
|
mURI = NullPrincipalURI::Create();
|
2016-11-30 17:13:27 +03:00
|
|
|
NS_ENSURE_TRUE(mURI, NS_ERROR_NOT_AVAILABLE);
|
|
|
|
}
|
2015-02-24 23:54:40 +03:00
|
|
|
|
2017-03-29 09:22:26 +03:00
|
|
|
nsAutoCString originNoSuffix;
|
2017-03-29 09:24:01 +03:00
|
|
|
DebugOnly<nsresult> rv = mURI->GetSpec(originNoSuffix);
|
2017-03-29 09:22:26 +03:00
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
|
|
|
|
FinishInit(originNoSuffix, aOriginAttributes);
|
2017-02-25 01:02:24 +03:00
|
|
|
|
2015-02-24 23:54:40 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-04-10 13:45:36 +03:00
|
|
|
nsresult
|
|
|
|
NullPrincipal::Init(const OriginAttributes& aOriginAttributes, bool aIsFirstParty)
|
|
|
|
{
|
|
|
|
mURI = NullPrincipalURI::Create();
|
|
|
|
NS_ENSURE_TRUE(mURI, NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
|
|
|
nsAutoCString originNoSuffix;
|
|
|
|
DebugOnly<nsresult> rv = mURI->GetSpec(originNoSuffix);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
|
|
|
|
nsAutoCString path;
|
2017-07-29 14:50:21 +03:00
|
|
|
rv = mURI->GetPathQueryRef(path);
|
2017-04-10 13:45:36 +03:00
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
|
|
|
|
OriginAttributes attrs(aOriginAttributes);
|
|
|
|
if (aIsFirstParty) {
|
|
|
|
// remove the '{}' characters from both ends.
|
|
|
|
path.Mid(path, 1, path.Length() - 2);
|
|
|
|
path.AppendLiteral(".mozilla");
|
|
|
|
attrs.SetFirstPartyDomain(true, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
FinishInit(originNoSuffix, attrs);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-08-30 07:22:04 +03:00
|
|
|
nsresult
|
2017-03-22 13:38:40 +03:00
|
|
|
NullPrincipal::GetScriptLocation(nsACString &aStr)
|
2015-02-24 23:54:40 +03:00
|
|
|
{
|
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
|
2017-03-22 13:38:40 +03:00
|
|
|
NullPrincipal::GetHashValue(uint32_t *aResult)
|
2006-04-03 00:58:26 +04:00
|
|
|
{
|
|
|
|
*aResult = (NS_PTR_TO_INT32(this) >> 2);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-01-30 16:09:37 +03:00
|
|
|
NS_IMETHODIMP
|
2017-03-22 13:38:40 +03:00
|
|
|
NullPrincipal::SetCsp(nsIContentSecurityPolicy* aCsp)
|
|
|
|
{
|
2017-01-30 16:09:37 +03:00
|
|
|
// Never destroy an existing CSP on the principal.
|
|
|
|
// This method should only be called in rare cases.
|
|
|
|
|
|
|
|
MOZ_ASSERT(!mCSP, "do not destroy an existing CSP");
|
|
|
|
if (mCSP) {
|
|
|
|
return NS_ERROR_ALREADY_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
mCSP = aCsp;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-06-04 21:51:57 +03:00
|
|
|
NS_IMETHODIMP
|
2017-03-22 13:38:40 +03:00
|
|
|
NullPrincipal::GetURI(nsIURI** aURI)
|
2006-04-03 00:58:26 +04:00
|
|
|
{
|
2018-05-09 19:21:24 +03:00
|
|
|
nsCOMPtr<nsIURI> uri = mURI;
|
|
|
|
uri.forget(aURI);
|
|
|
|
return NS_OK;
|
2006-04-03 00:58:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2017-03-22 13:38:40 +03:00
|
|
|
NullPrincipal::GetDomain(nsIURI** aDomain)
|
2006-04-03 00:58:26 +04:00
|
|
|
{
|
2018-05-09 19:21:24 +03:00
|
|
|
nsCOMPtr<nsIURI> uri = mURI;
|
|
|
|
uri.forget(aDomain);
|
|
|
|
return NS_OK;
|
2006-04-03 00:58:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2017-03-22 13:38:40 +03:00
|
|
|
NullPrincipal::SetDomain(nsIURI* aDomain)
|
2006-04-03 00:58:26 +04:00
|
|
|
{
|
|
|
|
// I think the right thing to do here is to just throw... Silently failing
|
|
|
|
// seems counterproductive.
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2015-10-01 06:03:36 +03:00
|
|
|
bool
|
2017-03-22 13:38:40 +03:00
|
|
|
NullPrincipal::MayLoadInternal(nsIURI* aURI)
|
2015-10-01 06:03:36 +03:00
|
|
|
{
|
2014-09-30 12:09:36 +04:00
|
|
|
// Also allow the load if we are the principal of the URI being checked.
|
2018-07-24 23:15:57 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> blobPrincipal;
|
|
|
|
if (dom::BlobURLProtocolHandler::GetBlobURLPrincipal(aURI,
|
|
|
|
getter_AddRefs(blobPrincipal))) {
|
|
|
|
return blobPrincipal == this;
|
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
|
2017-03-22 13:38:40 +03:00
|
|
|
NullPrincipal::GetBaseDomain(nsACString& aBaseDomain)
|
2013-01-09 01:53:32 +04:00
|
|
|
{
|
|
|
|
// For a null principal, we use our unique uuid as the base domain.
|
2017-07-29 14:50:21 +03:00
|
|
|
return mURI->GetPathQueryRef(aBaseDomain);
|
2013-01-09 01:53:32 +04:00
|
|
|
}
|
|
|
|
|
2016-11-06 08:38:17 +03:00
|
|
|
NS_IMETHODIMP
|
2017-03-22 13:38:40 +03:00
|
|
|
NullPrincipal::GetAddonId(nsAString& aAddonId)
|
2016-11-06 08:38:17 +03:00
|
|
|
{
|
|
|
|
aAddonId.Truncate();
|
|
|
|
return NS_OK;
|
|
|
|
};
|
|
|
|
|
2006-04-03 00:58:26 +04:00
|
|
|
/**
|
|
|
|
* nsISerializable implementation
|
|
|
|
*/
|
|
|
|
NS_IMETHODIMP
|
2017-03-22 13:38:40 +03:00
|
|
|
NullPrincipal::Read(nsIObjectInputStream* aStream)
|
2006-04-03 00:58:26 +04:00
|
|
|
{
|
2017-03-22 13:38:40 +03:00
|
|
|
// Note - NullPrincipal use NS_GENERIC_FACTORY_CONSTRUCTOR_INIT, which means
|
2014-07-29 19:47:52 +04:00
|
|
|
// that the Init() method has already been invoked by the time we deserialize.
|
2017-03-22 13:39:31 +03:00
|
|
|
// This is in contrast to ContentPrincipal, which uses NS_GENERIC_FACTORY_CONSTRUCTOR,
|
2014-07-29 19:47:52 +04:00
|
|
|
// in which case ::Read needs to invoke Init().
|
2017-02-23 16:30:23 +03:00
|
|
|
|
|
|
|
nsAutoCString spec;
|
|
|
|
nsresult rv = aStream->ReadCString(spec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
rv = NS_NewURI(getter_AddRefs(uri), spec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2015-06-04 21:51:57 +03:00
|
|
|
nsAutoCString suffix;
|
2017-02-23 16:30:23 +03:00
|
|
|
rv = aStream->ReadCString(suffix);
|
2015-06-04 21:51:57 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2017-02-23 16:30:23 +03:00
|
|
|
OriginAttributes attrs;
|
|
|
|
bool ok = attrs.PopulateFromSuffix(suffix);
|
2015-06-04 21:51:57 +03:00
|
|
|
NS_ENSURE_TRUE(ok, NS_ERROR_FAILURE);
|
|
|
|
|
2017-02-23 16:30:23 +03:00
|
|
|
return Init(attrs, uri);
|
2006-04-03 00:58:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2017-03-22 13:38:40 +03:00
|
|
|
NullPrincipal::Write(nsIObjectOutputStream* aStream)
|
2006-04-03 00:58:26 +04:00
|
|
|
{
|
2017-02-23 16:30:23 +03:00
|
|
|
NS_ENSURE_STATE(mURI);
|
|
|
|
|
|
|
|
nsAutoCString spec;
|
|
|
|
nsresult rv = mURI->GetSpec(spec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aStream->WriteStringZ(spec.get());
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2015-06-04 21:51:57 +03:00
|
|
|
nsAutoCString suffix;
|
|
|
|
OriginAttributesRef().CreateSuffix(suffix);
|
|
|
|
|
2017-02-23 16:30:23 +03:00
|
|
|
rv = aStream->WriteStringZ(suffix.get());
|
2015-06-04 21:51:57 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2006-04-03 00:58:26 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|