2017-02-22 09:01:43 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 sw=2 et tw=80: */
|
|
|
|
/* 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/. */
|
|
|
|
|
2017-03-22 10:38:17 +00:00
|
|
|
#include "ExpandedPrincipal.h"
|
2017-02-22 09:01:43 +00:00
|
|
|
#include "nsIClassInfoImpl.h"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
|
2017-03-22 10:38:17 +00:00
|
|
|
NS_IMPL_CLASSINFO(ExpandedPrincipal, nullptr, nsIClassInfo::MAIN_THREAD_ONLY,
|
2017-02-22 09:01:43 +00:00
|
|
|
NS_EXPANDEDPRINCIPAL_CID)
|
2017-03-22 10:38:17 +00:00
|
|
|
NS_IMPL_QUERY_INTERFACE_CI(ExpandedPrincipal,
|
2017-02-22 09:01:43 +00:00
|
|
|
nsIPrincipal,
|
2018-05-24 06:43:14 +00:00
|
|
|
nsIExpandedPrincipal,
|
|
|
|
nsISerializable)
|
2017-03-22 10:38:17 +00:00
|
|
|
NS_IMPL_CI_INTERFACE_GETTER(ExpandedPrincipal,
|
|
|
|
nsIPrincipal,
|
2018-05-24 06:43:14 +00:00
|
|
|
nsIExpandedPrincipal,
|
|
|
|
nsISerializable)
|
2017-02-22 09:01:43 +00:00
|
|
|
|
|
|
|
struct OriginComparator
|
|
|
|
{
|
|
|
|
bool LessThan(nsIPrincipal* a, nsIPrincipal* b) const
|
|
|
|
{
|
|
|
|
nsAutoCString originA;
|
2017-03-29 06:24:01 +00:00
|
|
|
DebugOnly<nsresult> rv = a->GetOrigin(originA);
|
2017-03-29 06:22:26 +00:00
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
2017-02-22 09:01:43 +00:00
|
|
|
nsAutoCString originB;
|
|
|
|
rv = b->GetOrigin(originB);
|
2017-03-29 06:22:26 +00:00
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
2017-02-22 09:01:43 +00:00
|
|
|
return originA < originB;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Equals(nsIPrincipal* a, nsIPrincipal* b) const
|
|
|
|
{
|
|
|
|
nsAutoCString originA;
|
2017-03-29 06:24:01 +00:00
|
|
|
DebugOnly<nsresult> rv = a->GetOrigin(originA);
|
2017-03-29 06:22:26 +00:00
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
2017-02-22 09:01:43 +00:00
|
|
|
nsAutoCString originB;
|
|
|
|
rv = b->GetOrigin(originB);
|
2017-03-29 06:22:26 +00:00
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
2017-02-22 09:01:43 +00:00
|
|
|
return a == b;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-03-29 06:21:03 +00:00
|
|
|
ExpandedPrincipal::ExpandedPrincipal(nsTArray<nsCOMPtr<nsIPrincipal>> &aWhiteList)
|
2017-02-24 02:33:40 +00:00
|
|
|
: BasePrincipal(eExpandedPrincipal)
|
2017-02-22 09:01:43 +00:00
|
|
|
{
|
2017-03-22 10:38:17 +00:00
|
|
|
// We force the principals to be sorted by origin so that ExpandedPrincipal
|
2017-02-22 09:01:43 +00:00
|
|
|
// origins can have a canonical form.
|
|
|
|
OriginComparator c;
|
|
|
|
for (size_t i = 0; i < aWhiteList.Length(); ++i) {
|
|
|
|
mPrincipals.InsertElementSorted(aWhiteList[i], c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-24 06:43:14 +00:00
|
|
|
ExpandedPrincipal::ExpandedPrincipal()
|
|
|
|
: BasePrincipal(eExpandedPrincipal)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-03-22 10:38:17 +00:00
|
|
|
ExpandedPrincipal::~ExpandedPrincipal()
|
2017-02-22 09:01:43 +00:00
|
|
|
{ }
|
|
|
|
|
2017-03-22 10:38:17 +00:00
|
|
|
already_AddRefed<ExpandedPrincipal>
|
|
|
|
ExpandedPrincipal::Create(nsTArray<nsCOMPtr<nsIPrincipal>>& aWhiteList,
|
|
|
|
const OriginAttributes& aAttrs)
|
2017-02-24 22:02:24 +00:00
|
|
|
{
|
2017-03-29 06:21:03 +00:00
|
|
|
RefPtr<ExpandedPrincipal> ep = new ExpandedPrincipal(aWhiteList);
|
2017-03-29 06:22:26 +00:00
|
|
|
|
|
|
|
nsAutoCString origin;
|
|
|
|
origin.AssignLiteral("[Expanded Principal [");
|
|
|
|
for (size_t i = 0; i < ep->mPrincipals.Length(); ++i) {
|
|
|
|
if (i != 0) {
|
|
|
|
origin.AppendLiteral(", ");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString subOrigin;
|
2017-03-29 06:24:01 +00:00
|
|
|
DebugOnly<nsresult> rv = ep->mPrincipals.ElementAt(i)->GetOrigin(subOrigin);
|
2017-03-29 06:22:26 +00:00
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
origin.Append(subOrigin);
|
|
|
|
}
|
2017-09-04 05:14:11 +00:00
|
|
|
origin.AppendLiteral("]]");
|
2017-03-29 06:22:26 +00:00
|
|
|
|
|
|
|
ep->FinishInit(origin, aAttrs);
|
2017-02-24 22:02:24 +00:00
|
|
|
return ep.forget();
|
|
|
|
}
|
|
|
|
|
2017-02-22 09:01:43 +00:00
|
|
|
NS_IMETHODIMP
|
2017-03-22 10:38:17 +00:00
|
|
|
ExpandedPrincipal::GetDomain(nsIURI** aDomain)
|
2017-02-22 09:01:43 +00:00
|
|
|
{
|
|
|
|
*aDomain = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2017-03-22 10:38:17 +00:00
|
|
|
ExpandedPrincipal::SetDomain(nsIURI* aDomain)
|
2017-02-22 09:01:43 +00:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-03-22 10:38:17 +00:00
|
|
|
ExpandedPrincipal::SubsumesInternal(nsIPrincipal* aOther,
|
|
|
|
BasePrincipal::DocumentDomainConsideration aConsideration)
|
2017-02-22 09:01:43 +00:00
|
|
|
{
|
|
|
|
// If aOther is an ExpandedPrincipal too, we break it down into its component
|
|
|
|
// nsIPrincipals, and check subsumes on each one.
|
2017-10-10 22:00:16 +00:00
|
|
|
if (Cast(aOther)->Is<ExpandedPrincipal>()) {
|
|
|
|
auto* expanded = Cast(aOther)->As<ExpandedPrincipal>();
|
|
|
|
|
|
|
|
for (auto& other : expanded->WhiteList()) {
|
2017-02-22 09:01:43 +00:00
|
|
|
// Use SubsumesInternal rather than Subsumes here, since OriginAttribute
|
|
|
|
// checks are only done between non-expanded sub-principals, and we don't
|
|
|
|
// need to incur the extra virtual call overhead.
|
2017-10-10 22:00:16 +00:00
|
|
|
if (!SubsumesInternal(other, aConsideration)) {
|
2017-02-22 09:01:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're dealing with a regular principal. One of our principals must subsume
|
|
|
|
// it.
|
|
|
|
for (uint32_t i = 0; i < mPrincipals.Length(); ++i) {
|
|
|
|
if (Cast(mPrincipals[i])->Subsumes(aOther, aConsideration)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-03-22 10:38:17 +00:00
|
|
|
ExpandedPrincipal::MayLoadInternal(nsIURI* uri)
|
2017-02-22 09:01:43 +00:00
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < mPrincipals.Length(); ++i){
|
|
|
|
if (BasePrincipal::Cast(mPrincipals[i])->MayLoadInternal(uri)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2017-03-22 10:38:17 +00:00
|
|
|
ExpandedPrincipal::GetHashValue(uint32_t* result)
|
2017-02-22 09:01:43 +00:00
|
|
|
{
|
|
|
|
MOZ_CRASH("extended principal should never be used as key in a hash map");
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2017-03-22 10:38:17 +00:00
|
|
|
ExpandedPrincipal::GetURI(nsIURI** aURI)
|
2017-02-22 09:01:43 +00:00
|
|
|
{
|
|
|
|
*aURI = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-10-10 22:00:16 +00:00
|
|
|
const nsTArray<nsCOMPtr<nsIPrincipal>>&
|
|
|
|
ExpandedPrincipal::WhiteList()
|
2017-02-22 09:01:43 +00:00
|
|
|
{
|
2017-10-10 22:00:16 +00:00
|
|
|
return mPrincipals;
|
2017-02-22 09:01:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2017-03-22 10:38:17 +00:00
|
|
|
ExpandedPrincipal::GetBaseDomain(nsACString& aBaseDomain)
|
2017-02-22 09:01:43 +00:00
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2016-11-06 05:38:17 +00:00
|
|
|
NS_IMETHODIMP
|
2017-03-22 10:38:17 +00:00
|
|
|
ExpandedPrincipal::GetAddonId(nsAString& aAddonId)
|
2016-11-06 05:38:17 +00:00
|
|
|
{
|
|
|
|
aAddonId.Truncate();
|
|
|
|
return NS_OK;
|
|
|
|
};
|
|
|
|
|
2017-02-22 09:01:43 +00:00
|
|
|
bool
|
2017-10-02 22:05:19 +00:00
|
|
|
ExpandedPrincipal::AddonHasPermission(const nsAtom* aPerm)
|
2017-02-22 09:01:43 +00:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < mPrincipals.Length(); ++i) {
|
|
|
|
if (BasePrincipal::Cast(mPrincipals[i])->AddonHasPermission(aPerm)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-04-04 14:54:26 +00:00
|
|
|
bool
|
|
|
|
ExpandedPrincipal::AddonAllowsLoad(nsIURI* aURI, bool aExplicit /* = false */)
|
|
|
|
{
|
|
|
|
for (const auto& principal : mPrincipals) {
|
|
|
|
if (Cast(principal)->AddonAllowsLoad(aURI, aExplicit)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-03 02:56:27 +00:00
|
|
|
nsIPrincipal*
|
2017-11-22 22:20:26 +00:00
|
|
|
ExpandedPrincipal::PrincipalToInherit(nsIURI* aRequestedURI)
|
2017-11-03 02:56:27 +00:00
|
|
|
{
|
|
|
|
if (aRequestedURI) {
|
2017-11-22 22:20:26 +00:00
|
|
|
// If a given sub-principal subsumes the given URI, use that principal for
|
|
|
|
// inheritance. In general, this only happens with certain CORS modes, loads
|
|
|
|
// with forced principal inheritance, and creation of XML documents from
|
|
|
|
// XMLHttpRequests or fetch requests. For URIs that normally inherit a
|
|
|
|
// principal (such as data: URIs), we fall back to the last principal in the
|
|
|
|
// whitelist.
|
2017-11-03 02:56:27 +00:00
|
|
|
for (const auto& principal : mPrincipals) {
|
2017-11-22 22:20:26 +00:00
|
|
|
if (Cast(principal)->MayLoadInternal(aRequestedURI)) {
|
2017-11-03 02:56:27 +00:00
|
|
|
return principal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mPrincipals.LastElement();
|
|
|
|
}
|
|
|
|
|
2017-02-22 09:01:43 +00:00
|
|
|
nsresult
|
2017-03-22 10:38:17 +00:00
|
|
|
ExpandedPrincipal::GetScriptLocation(nsACString& aStr)
|
2017-02-22 09:01:43 +00:00
|
|
|
{
|
2017-09-04 05:12:56 +00:00
|
|
|
aStr.AssignLiteral("[Expanded Principal [");
|
2017-02-22 09:01:43 +00:00
|
|
|
for (size_t i = 0; i < mPrincipals.Length(); ++i) {
|
|
|
|
if (i != 0) {
|
|
|
|
aStr.AppendLiteral(", ");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString spec;
|
|
|
|
nsresult rv =
|
|
|
|
nsJSPrincipals::get(mPrincipals.ElementAt(i))->GetScriptLocation(spec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
aStr.Append(spec);
|
|
|
|
}
|
2017-09-04 05:14:11 +00:00
|
|
|
aStr.AppendLiteral("]]");
|
2017-02-22 09:01:43 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////
|
|
|
|
// Methods implementing nsISerializable //
|
|
|
|
//////////////////////////////////////////
|
|
|
|
|
2018-05-24 06:43:14 +00:00
|
|
|
// We've had way too many issues with unversioned serializations, so
|
|
|
|
// explicitly version this one.
|
|
|
|
static const uint32_t kSerializationVersion = 1;
|
|
|
|
|
2017-02-22 09:01:43 +00:00
|
|
|
NS_IMETHODIMP
|
2017-03-22 10:38:17 +00:00
|
|
|
ExpandedPrincipal::Read(nsIObjectInputStream* aStream)
|
2017-02-22 09:01:43 +00:00
|
|
|
{
|
2018-05-24 06:43:14 +00:00
|
|
|
uint32_t version;
|
|
|
|
nsresult rv = aStream->Read32(&version);
|
|
|
|
if (version != kSerializationVersion) {
|
|
|
|
MOZ_ASSERT(false,
|
|
|
|
"We really need to add handling of the old(?) version here");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t count;
|
|
|
|
rv = aStream->Read32(&count);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mPrincipals.SetCapacity(count, fallible)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
OriginComparator c;
|
|
|
|
for (uint32_t i = 0; i < count; ++i) {
|
|
|
|
nsCOMPtr<nsISupports> read;
|
|
|
|
rv = aStream->ReadObject(true, getter_AddRefs(read));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> principal = do_QueryInterface(read);
|
|
|
|
if (!principal) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Play it safe and InsertElementSorted, in case the sort order
|
|
|
|
// changed for some bizarre reason.
|
2018-05-30 19:15:35 +00:00
|
|
|
mPrincipals.InsertElementSorted(std::move(principal), c);
|
2018-05-24 06:43:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2017-02-22 09:01:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2017-03-22 10:38:17 +00:00
|
|
|
ExpandedPrincipal::Write(nsIObjectOutputStream* aStream)
|
2017-02-22 09:01:43 +00:00
|
|
|
{
|
2018-05-24 06:43:14 +00:00
|
|
|
nsresult rv = aStream->Write32(kSerializationVersion);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = aStream->Write32(mPrincipals.Length());
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& principal : mPrincipals) {
|
|
|
|
rv = aStream->WriteObject(principal, true);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2017-02-22 09:01:43 +00:00
|
|
|
}
|
2018-09-11 09:01:14 +00:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
ExpandedPrincipal::GetSiteIdentifier(SiteIdentifier& aSite)
|
|
|
|
{
|
|
|
|
// Call GetSiteIdentifier on each of our principals and return a new
|
|
|
|
// ExpandedPrincipal.
|
|
|
|
|
|
|
|
nsTArray<nsCOMPtr<nsIPrincipal>> whitelist;
|
|
|
|
for (const auto& principal : mPrincipals) {
|
|
|
|
SiteIdentifier site;
|
|
|
|
nsresult rv = Cast(principal)->GetSiteIdentifier(site);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
whitelist.AppendElement(site.GetPrincipal());
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<ExpandedPrincipal> expandedPrincipal =
|
|
|
|
ExpandedPrincipal::Create(whitelist, OriginAttributesRef());
|
|
|
|
MOZ_ASSERT(expandedPrincipal, "ExpandedPrincipal::Create returned nullptr?");
|
|
|
|
|
|
|
|
aSite.Init(expandedPrincipal);
|
|
|
|
return NS_OK;
|
|
|
|
}
|