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
|
|
|
|
2021-05-17 23:50:09 +03:00
|
|
|
#include "mozilla/dom/BlobURLProtocolHandler.h"
|
2021-07-06 11:23:11 +03:00
|
|
|
#include "mozilla/StaticPrefs_network.h"
|
2016-04-21 11:51:25 +03:00
|
|
|
#include "nsDocShell.h"
|
2017-03-22 13:38:40 +03:00
|
|
|
#include "NullPrincipal.h"
|
2021-07-06 11:23:11 +03:00
|
|
|
#include "DefaultURI.h"
|
|
|
|
#include "nsSimpleURI.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"
|
2022-02-08 18:18:21 +03:00
|
|
|
#include "nsEscape.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"
|
2021-05-17 23:50:09 +03:00
|
|
|
#include "nsIObjectInputStream.h"
|
2006-05-11 20:06:35 +04:00
|
|
|
|
2019-06-03 15:37:12 +03:00
|
|
|
#include "json/json.h"
|
|
|
|
|
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)
|
2021-05-17 23:50:09 +03:00
|
|
|
NS_IMPL_QUERY_INTERFACE_CI(NullPrincipal, nsIPrincipal)
|
|
|
|
NS_IMPL_CI_INTERFACE_GETTER(NullPrincipal, nsIPrincipal)
|
|
|
|
|
|
|
|
NullPrincipal::NullPrincipal(nsIURI* aURI, const nsACString& aOriginNoSuffix,
|
|
|
|
const OriginAttributes& aOriginAttributes)
|
|
|
|
: BasePrincipal(eNullPrincipal, aOriginNoSuffix, aOriginAttributes),
|
|
|
|
mURI(aURI) {}
|
2006-04-03 00:58:26 +04:00
|
|
|
|
2019-02-26 01:03:35 +03:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<NullPrincipal> NullPrincipal::CreateWithInheritedAttributes(
|
|
|
|
nsIPrincipal* aInheritFrom) {
|
2018-10-30 03:13:29 +03:00
|
|
|
MOZ_ASSERT(aInheritFrom);
|
2021-07-16 00:09:14 +03:00
|
|
|
return CreateInternal(Cast(aInheritFrom)->OriginAttributesRef(), false,
|
|
|
|
nullptr, aInheritFrom);
|
2014-07-29 19:47:52 +04:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:03:35 +03:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<NullPrincipal> NullPrincipal::CreateWithInheritedAttributes(
|
|
|
|
nsIDocShell* aDocShell, bool aIsFirstParty) {
|
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
|
|
|
|
2019-02-26 01:03:35 +03:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<NullPrincipal> NullPrincipal::CreateWithInheritedAttributes(
|
2018-10-30 03:13:29 +03:00
|
|
|
const OriginAttributes& aOriginAttributes, bool aIsFirstParty) {
|
2021-05-17 23:50:09 +03:00
|
|
|
return CreateInternal(aOriginAttributes, aIsFirstParty);
|
2016-04-21 11:51:25 +03:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:03:35 +03:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<NullPrincipal> NullPrincipal::Create(
|
2017-03-22 13:38:40 +03:00
|
|
|
const OriginAttributes& aOriginAttributes, nsIURI* aURI) {
|
2021-05-17 23:50:09 +03:00
|
|
|
return CreateInternal(aOriginAttributes, false, aURI);
|
2015-03-31 20:11:00 +03:00
|
|
|
}
|
2007-09-28 18:31:04 +04:00
|
|
|
|
2019-02-26 01:03:35 +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
|
|
|
}
|
|
|
|
|
2022-03-28 18:12:36 +03:00
|
|
|
void NullPrincipal::EscapePrecursorQuery(nsACString& aPrecursorQuery) {
|
2022-02-08 18:18:21 +03:00
|
|
|
// origins should not contain existing escape sequences, so set `esc_Forced`
|
|
|
|
// to force any `%` in the input to be escaped in addition to non-ascii,
|
|
|
|
// control characters and DEL.
|
|
|
|
nsCString modified;
|
2022-03-28 18:12:36 +03:00
|
|
|
if (NS_EscapeURLSpan(aPrecursorQuery, esc_Query | esc_Forced, modified)) {
|
2022-02-08 18:18:21 +03:00
|
|
|
aPrecursorQuery.Assign(std::move(modified));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-28 18:12:36 +03:00
|
|
|
void NullPrincipal::UnescapePrecursorQuery(nsACString& aPrecursorQuery) {
|
2022-02-08 18:18:21 +03:00
|
|
|
nsCString modified;
|
|
|
|
if (NS_UnescapeURL(aPrecursorQuery.BeginReading(), aPrecursorQuery.Length(),
|
|
|
|
/* aFlags */ 0, modified)) {
|
|
|
|
aPrecursorQuery.Assign(std::move(modified));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 00:09:15 +03:00
|
|
|
already_AddRefed<nsIURI> NullPrincipal::CreateURI(
|
|
|
|
nsIPrincipal* aPrecursor, const nsID* aNullPrincipalID) {
|
2021-07-16 00:09:13 +03:00
|
|
|
nsCOMPtr<nsIURIMutator> iMutator;
|
2021-07-06 11:23:11 +03:00
|
|
|
if (StaticPrefs::network_url_useDefaultURI()) {
|
2021-07-16 00:09:13 +03:00
|
|
|
iMutator = new mozilla::net::DefaultURI::Mutator();
|
2021-07-06 11:23:11 +03:00
|
|
|
} else {
|
2021-07-16 00:09:13 +03:00
|
|
|
iMutator = new mozilla::net::nsSimpleURI::Mutator();
|
2021-07-06 11:23:11 +03:00
|
|
|
}
|
|
|
|
|
2021-11-04 11:55:46 +03:00
|
|
|
nsID uuid = aNullPrincipalID ? *aNullPrincipalID : nsID::GenerateUUID();
|
2021-07-06 11:23:11 +03:00
|
|
|
|
2021-07-16 00:09:13 +03:00
|
|
|
NS_MutateURI mutator(iMutator);
|
2021-11-04 11:55:46 +03:00
|
|
|
mutator.SetSpec(NS_NULLPRINCIPAL_SCHEME ":"_ns +
|
|
|
|
nsDependentCString(nsIDToCString(uuid).get()));
|
2021-07-16 00:09:13 +03:00
|
|
|
|
|
|
|
// If there's a precursor URI, encode it in the null principal URI's query.
|
|
|
|
if (aPrecursor) {
|
|
|
|
nsAutoCString precursorOrigin;
|
|
|
|
switch (BasePrincipal::Cast(aPrecursor)->Kind()) {
|
2022-02-08 18:18:21 +03:00
|
|
|
case eNullPrincipal: {
|
2021-07-16 00:09:13 +03:00
|
|
|
// If the precursor null principal has a precursor, inherit it.
|
|
|
|
if (nsCOMPtr<nsIURI> nullPrecursorURI = aPrecursor->GetURI()) {
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(nullPrecursorURI->GetQuery(precursorOrigin));
|
|
|
|
}
|
|
|
|
break;
|
2022-02-08 18:18:21 +03:00
|
|
|
}
|
|
|
|
case eContentPrincipal: {
|
2021-07-16 00:09:13 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(aPrecursor->GetOriginNoSuffix(precursorOrigin));
|
2022-02-08 18:18:21 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
nsAutoCString original(precursorOrigin);
|
|
|
|
#endif
|
|
|
|
EscapePrecursorQuery(precursorOrigin);
|
|
|
|
#ifdef DEBUG
|
|
|
|
nsAutoCString unescaped(precursorOrigin);
|
|
|
|
UnescapePrecursorQuery(unescaped);
|
|
|
|
MOZ_ASSERT(unescaped == original,
|
|
|
|
"cannot recover original precursor origin after escape");
|
|
|
|
#endif
|
2021-07-16 00:09:13 +03:00
|
|
|
break;
|
2022-02-08 18:18:21 +03:00
|
|
|
}
|
2021-07-16 00:09:13 +03:00
|
|
|
|
|
|
|
// For now, we won't track expanded or system principal precursors. We may
|
|
|
|
// want to track expanded principal precursors in the future, but it's
|
|
|
|
// unlikely we'll want to track system principal precursors.
|
|
|
|
case eExpandedPrincipal:
|
|
|
|
case eSystemPrincipal:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!precursorOrigin.IsEmpty()) {
|
|
|
|
mutator.SetQuery(precursorOrigin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-06 11:23:11 +03:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2021-07-16 00:09:13 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(mutator.Finalize(getter_AddRefs(uri)));
|
2021-07-06 11:23:11 +03:00
|
|
|
return uri.forget();
|
|
|
|
}
|
|
|
|
|
2021-05-17 23:50:09 +03:00
|
|
|
already_AddRefed<NullPrincipal> NullPrincipal::CreateInternal(
|
2021-07-16 00:09:13 +03:00
|
|
|
const OriginAttributes& aOriginAttributes, bool aIsFirstParty, nsIURI* aURI,
|
|
|
|
nsIPrincipal* aPrecursor) {
|
|
|
|
MOZ_ASSERT_IF(aPrecursor, !aURI);
|
2021-05-17 23:50:09 +03:00
|
|
|
nsCOMPtr<nsIURI> uri = aURI;
|
|
|
|
if (!uri) {
|
2021-07-16 00:09:13 +03:00
|
|
|
uri = NullPrincipal::CreateURI(aPrecursor);
|
2020-05-28 11:55:04 +03:00
|
|
|
}
|
2021-07-06 11:23:11 +03:00
|
|
|
|
2021-05-17 23:50:09 +03:00
|
|
|
MOZ_RELEASE_ASSERT(uri->SchemeIs(NS_NULLPRINCIPAL_SCHEME));
|
2017-04-10 13:45:36 +03:00
|
|
|
|
|
|
|
nsAutoCString originNoSuffix;
|
2021-05-17 23:50:09 +03:00
|
|
|
DebugOnly<nsresult> rv = uri->GetSpec(originNoSuffix);
|
2017-04-10 13:45:36 +03:00
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
|
|
|
|
OriginAttributes attrs(aOriginAttributes);
|
|
|
|
if (aIsFirstParty) {
|
2021-07-16 00:09:13 +03:00
|
|
|
// The FirstPartyDomain attribute will not include information about the
|
|
|
|
// precursor origin.
|
2021-05-17 23:50:09 +03:00
|
|
|
nsAutoCString path;
|
2021-07-16 00:09:13 +03:00
|
|
|
rv = uri->GetFilePath(path);
|
2021-05-17 23:50:09 +03:00
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
|
2017-04-10 13:45:36 +03:00
|
|
|
// remove the '{}' characters from both ends.
|
|
|
|
path.Mid(path, 1, path.Length() - 2);
|
|
|
|
path.AppendLiteral(".mozilla");
|
|
|
|
attrs.SetFirstPartyDomain(true, path);
|
|
|
|
}
|
|
|
|
|
2021-05-17 23:50:09 +03:00
|
|
|
RefPtr<NullPrincipal> nullPrin =
|
|
|
|
new NullPrincipal(uri, originNoSuffix, attrs);
|
|
|
|
return nullPrin.forget();
|
2017-04-10 13:45:36 +03:00
|
|
|
}
|
|
|
|
|
2017-03-22 13:38:40 +03:00
|
|
|
nsresult NullPrincipal::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
|
|
|
|
*/
|
|
|
|
|
2018-11-20 04:17:52 +03:00
|
|
|
uint32_t NullPrincipal::GetHashValue() { return (NS_PTR_TO_INT32(this) >> 2); }
|
2006-04-03 00:58:26 +04:00
|
|
|
|
2015-06-04 21:51:57 +03:00
|
|
|
NS_IMETHODIMP
|
2017-03-22 13:38:40 +03:00
|
|
|
NullPrincipal::GetURI(nsIURI** aURI) {
|
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
|
|
|
}
|
2019-12-05 19:04:53 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
NullPrincipal::GetIsOriginPotentiallyTrustworthy(bool* aResult) {
|
|
|
|
*aResult = false;
|
|
|
|
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) {
|
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;
|
|
|
|
}
|
|
|
|
|
2017-03-22 13:38:40 +03:00
|
|
|
bool NullPrincipal::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.
|
2018-07-24 23:15:57 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> blobPrincipal;
|
|
|
|
if (dom::BlobURLProtocolHandler::GetBlobURLPrincipal(
|
|
|
|
aURI, getter_AddRefs(blobPrincipal))) {
|
2019-02-11 21:03:12 +03:00
|
|
|
MOZ_ASSERT(blobPrincipal);
|
|
|
|
return SubsumesInternal(blobPrincipal,
|
|
|
|
BasePrincipal::ConsiderDocumentDomain);
|
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
|
2021-05-17 23:50:09 +03:00
|
|
|
NullPrincipal::Deserializer::Read(nsIObjectInputStream* aStream) {
|
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);
|
|
|
|
|
2021-05-17 23:50:09 +03:00
|
|
|
mPrincipal = NullPrincipal::Create(attrs, uri);
|
|
|
|
NS_ENSURE_TRUE(mPrincipal, NS_ERROR_FAILURE);
|
2006-04-03 00:58:26 +04:00
|
|
|
|
2019-06-03 15:37:12 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2017-02-23 16:30:23 +03:00
|
|
|
|
2019-06-03 15:37:12 +03:00
|
|
|
nsresult NullPrincipal::PopulateJSONObject(Json::Value& aObject) {
|
2019-07-08 19:37:45 +03:00
|
|
|
nsAutoCString principalURI;
|
|
|
|
nsresult rv = mURI->GetSpec(principalURI);
|
2017-02-23 16:30:23 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2019-07-08 19:37:45 +03:00
|
|
|
aObject[std::to_string(eSpec)] = principalURI.get();
|
2017-02-23 16:30:23 +03:00
|
|
|
|
2015-06-04 21:51:57 +03:00
|
|
|
nsAutoCString suffix;
|
|
|
|
OriginAttributesRef().CreateSuffix(suffix);
|
2019-06-03 15:37:12 +03:00
|
|
|
if (suffix.Length() > 0) {
|
|
|
|
aObject[std::to_string(eSuffix)] = suffix.get();
|
|
|
|
}
|
2015-06-04 21:51:57 +03:00
|
|
|
|
2006-04-03 00:58:26 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2019-06-03 15:37:12 +03:00
|
|
|
|
|
|
|
already_AddRefed<BasePrincipal> NullPrincipal::FromProperties(
|
|
|
|
nsTArray<NullPrincipal::KeyVal>& aFields) {
|
|
|
|
MOZ_ASSERT(aFields.Length() == eMax + 1, "Must have all the keys");
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
OriginAttributes attrs;
|
|
|
|
|
|
|
|
// The odd structure here is to make the code to not compile
|
|
|
|
// if all the switch enum cases haven't been codified
|
|
|
|
for (const auto& field : aFields) {
|
|
|
|
switch (field.key) {
|
|
|
|
case NullPrincipal::eSpec:
|
|
|
|
if (!field.valueWasSerialized) {
|
|
|
|
MOZ_ASSERT(false,
|
|
|
|
"Null principals require a spec URI in serialized JSON");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
rv = NS_NewURI(getter_AddRefs(uri), field.value);
|
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
|
|
|
break;
|
|
|
|
case NullPrincipal::eSuffix:
|
|
|
|
bool ok = attrs.PopulateFromSuffix(field.value);
|
|
|
|
if (!ok) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!uri) {
|
|
|
|
MOZ_ASSERT(false, "No URI deserialized");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-05-17 23:50:09 +03:00
|
|
|
return NullPrincipal::Create(attrs, uri);
|
2019-06-03 15:37:12 +03:00
|
|
|
}
|
2021-07-16 00:09:15 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
NullPrincipal::GetPrecursorPrincipal(nsIPrincipal** aPrincipal) {
|
|
|
|
*aPrincipal = nullptr;
|
|
|
|
|
|
|
|
nsAutoCString query;
|
|
|
|
if (NS_FAILED(mURI->GetQuery(query)) || query.IsEmpty()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2022-02-08 18:18:21 +03:00
|
|
|
UnescapePrecursorQuery(query);
|
2021-07-16 00:09:15 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> precursorURI;
|
|
|
|
if (NS_FAILED(NS_NewURI(getter_AddRefs(precursorURI), query))) {
|
|
|
|
MOZ_ASSERT_UNREACHABLE(
|
|
|
|
"Failed to parse precursor from nullprincipal query");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If our precursor is another null principal, re-construct it. This can
|
|
|
|
// happen if a null principal without a precursor causes another principal to
|
|
|
|
// be created.
|
|
|
|
if (precursorURI->SchemeIs(NS_NULLPRINCIPAL_SCHEME)) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
nsAutoCString precursorQuery;
|
|
|
|
precursorURI->GetQuery(precursorQuery);
|
|
|
|
MOZ_ASSERT(precursorQuery.IsEmpty(),
|
|
|
|
"Null principal with nested precursors?");
|
|
|
|
#endif
|
|
|
|
*aPrincipal =
|
|
|
|
NullPrincipal::Create(OriginAttributesRef(), precursorURI).take();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<BasePrincipal> contentPrincipal =
|
|
|
|
BasePrincipal::CreateContentPrincipal(precursorURI,
|
|
|
|
OriginAttributesRef());
|
|
|
|
// If `CreateContentPrincipal` failed, it will create a new NullPrincipal and
|
|
|
|
// return that instead. We only want to return real content principals here.
|
|
|
|
if (!contentPrincipal || !contentPrincipal->Is<ContentPrincipal>()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
contentPrincipal.forget(aPrincipal);
|
|
|
|
return NS_OK;
|
|
|
|
}
|