2015-05-12 00:25:59 +03: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/. */
|
|
|
|
|
|
|
|
#include "mozilla/BasePrincipal.h"
|
2015-05-14 23:56:37 +03:00
|
|
|
|
2015-09-23 11:10:21 +03:00
|
|
|
#include "nsDocShell.h"
|
2015-05-15 23:43:11 +03:00
|
|
|
|
2017-11-03 05:56:27 +03:00
|
|
|
#include "ExpandedPrincipal.h"
|
2015-05-15 23:43:11 +03:00
|
|
|
#include "nsNetUtil.h"
|
2019-06-03 15:37:12 +03:00
|
|
|
#include "nsContentUtils.h"
|
2020-03-13 15:44:36 +03:00
|
|
|
#include "nsIOService.h"
|
2018-07-31 11:27:00 +03:00
|
|
|
#include "nsIURIWithSpecialOrigin.h"
|
2015-05-14 04:25:40 +03:00
|
|
|
#include "nsScriptSecurityManager.h"
|
2015-07-08 03:53:15 +03:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2019-10-17 16:54:41 +03:00
|
|
|
#include "nsAboutProtocolUtils.h"
|
2019-10-02 18:10:40 +03:00
|
|
|
#include "ThirdPartyUtil.h"
|
2018-07-17 22:38:48 +03:00
|
|
|
#include "mozilla/ContentPrincipal.h"
|
|
|
|
#include "mozilla/NullPrincipal.h"
|
2018-07-24 23:15:57 +03:00
|
|
|
#include "mozilla/dom/BlobURLProtocolHandler.h"
|
2016-11-02 20:04:13 +03:00
|
|
|
#include "mozilla/dom/ChromeUtils.h"
|
2020-11-23 19:21:38 +03:00
|
|
|
#include "mozilla/dom/ReferrerInfo.h"
|
2015-05-15 02:24:25 +03:00
|
|
|
#include "mozilla/dom/ToJSValue.h"
|
2019-12-05 19:04:53 +03:00
|
|
|
#include "mozilla/dom/nsMixedContentBlocker.h"
|
2020-01-22 20:14:08 +03:00
|
|
|
#include "mozilla/Components.h"
|
2020-02-27 03:22:27 +03:00
|
|
|
#include "mozilla/dom/StorageUtils.h"
|
2020-03-29 18:31:24 +03:00
|
|
|
#include "mozilla/dom/StorageUtils.h"
|
|
|
|
#include "nsIURL.h"
|
2020-04-20 12:53:40 +03:00
|
|
|
#include "nsEffectiveTLDService.h"
|
|
|
|
#include "nsIURIMutator.h"
|
|
|
|
#include "mozilla/StaticPrefs_permissions.h"
|
|
|
|
#include "nsIURIMutator.h"
|
2020-02-27 02:15:43 +03:00
|
|
|
#include "prnetdb.h"
|
2020-04-15 00:48:38 +03:00
|
|
|
#include "nsIURIFixup.h"
|
|
|
|
#include "mozilla/dom/StorageUtils.h"
|
2020-05-19 17:51:40 +03:00
|
|
|
#include "mozilla/ContentBlocking.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
2020-04-15 00:48:38 +03:00
|
|
|
#include "nsIURIMutator.h"
|
2021-07-30 18:01:07 +03:00
|
|
|
#include "mozilla/PermissionManager.h"
|
2020-02-27 02:15:43 +03:00
|
|
|
|
2019-06-03 15:37:12 +03:00
|
|
|
#include "json/json.h"
|
|
|
|
#include "nsSerializationHelper.h"
|
|
|
|
|
2015-05-12 00:25:59 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
2021-05-17 23:50:09 +03:00
|
|
|
BasePrincipal::BasePrincipal(PrincipalKind aKind,
|
|
|
|
const nsACString& aOriginNoSuffix,
|
|
|
|
const OriginAttributes& aOriginAttributes)
|
|
|
|
: mOriginNoSuffix(NS_Atomize(aOriginNoSuffix)),
|
|
|
|
mOriginSuffix(aOriginAttributes.CreateSuffixAtom()),
|
|
|
|
mOriginAttributes(aOriginAttributes),
|
|
|
|
mKind(aKind),
|
|
|
|
mHasExplicitDomain(false) {}
|
|
|
|
|
|
|
|
BasePrincipal::BasePrincipal(BasePrincipal* aOther,
|
|
|
|
const OriginAttributes& aOriginAttributes)
|
|
|
|
: mOriginNoSuffix(aOther->mOriginNoSuffix),
|
|
|
|
mOriginSuffix(aOriginAttributes.CreateSuffixAtom()),
|
|
|
|
mOriginAttributes(aOriginAttributes),
|
|
|
|
mKind(aOther->mKind),
|
|
|
|
mHasExplicitDomain(aOther->mHasExplicitDomain) {}
|
|
|
|
|
|
|
|
BasePrincipal::~BasePrincipal() = default;
|
2015-06-04 21:51:57 +03:00
|
|
|
|
2015-05-15 02:50:44 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::GetOrigin(nsACString& aOrigin) {
|
2017-02-25 01:02:24 +03:00
|
|
|
nsresult rv = GetOriginNoSuffix(aOrigin);
|
2015-05-15 02:55:52 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2015-07-16 00:50:12 +03:00
|
|
|
|
2015-05-15 02:55:52 +03:00
|
|
|
nsAutoCString suffix;
|
2017-02-25 01:02:24 +03:00
|
|
|
rv = GetOriginSuffix(suffix);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2015-05-15 02:55:52 +03:00
|
|
|
aOrigin.Append(suffix);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-11-28 12:22:17 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::GetAsciiOrigin(nsACString& aOrigin) {
|
|
|
|
aOrigin.Truncate();
|
|
|
|
nsCOMPtr<nsIURI> prinURI;
|
|
|
|
nsresult rv = GetURI(getter_AddRefs(prinURI));
|
|
|
|
if (NS_FAILED(rv) || !prinURI) {
|
2020-01-22 20:14:08 +03:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2019-11-28 12:22:17 +03:00
|
|
|
}
|
|
|
|
return nsContentUtils::GetASCIIOrigin(prinURI, aOrigin);
|
|
|
|
}
|
|
|
|
|
2020-02-10 18:10:52 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::GetHostPort(nsACString& aRes) {
|
|
|
|
aRes.Truncate();
|
|
|
|
nsCOMPtr<nsIURI> prinURI;
|
|
|
|
nsresult rv = GetURI(getter_AddRefs(prinURI));
|
|
|
|
if (NS_FAILED(rv) || !prinURI) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return prinURI->GetHostPort(aRes);
|
|
|
|
}
|
|
|
|
|
2020-02-24 19:57:40 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::GetHost(nsACString& aRes) {
|
|
|
|
aRes.Truncate();
|
|
|
|
nsCOMPtr<nsIURI> prinURI;
|
|
|
|
nsresult rv = GetURI(getter_AddRefs(prinURI));
|
|
|
|
if (NS_FAILED(rv) || !prinURI) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return prinURI->GetHost(aRes);
|
|
|
|
}
|
|
|
|
|
2015-05-15 02:55:52 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::GetOriginNoSuffix(nsACString& aOrigin) {
|
2017-09-25 09:38:18 +03:00
|
|
|
mOriginNoSuffix->ToUTF8String(aOrigin);
|
|
|
|
return NS_OK;
|
2015-05-15 02:50:44 +03:00
|
|
|
}
|
|
|
|
|
2018-09-05 06:22:16 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::GetSiteOrigin(nsACString& aSiteOrigin) {
|
2020-11-28 10:21:32 +03:00
|
|
|
nsresult rv = GetSiteOriginNoSuffix(aSiteOrigin);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsAutoCString suffix;
|
|
|
|
rv = GetOriginSuffix(suffix);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
aSiteOrigin.Append(suffix);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::GetSiteOriginNoSuffix(nsACString& aSiteOrigin) {
|
|
|
|
return GetOriginNoSuffix(aSiteOrigin);
|
2018-09-05 06:22:16 +03:00
|
|
|
}
|
|
|
|
|
2019-06-03 15:37:12 +03:00
|
|
|
// Returns the inner Json::value of the serialized principal
|
|
|
|
// Example input and return values:
|
|
|
|
// Null principal:
|
|
|
|
// {"0":{"0":"moz-nullprincipal:{56cac540-864d-47e7-8e25-1614eab5155e}"}} ->
|
|
|
|
// {"0":"moz-nullprincipal:{56cac540-864d-47e7-8e25-1614eab5155e}"}
|
|
|
|
//
|
2019-07-08 19:37:45 +03:00
|
|
|
// Content principal:
|
2019-06-03 15:37:12 +03:00
|
|
|
// {"1":{"0":"https://mozilla.com"}} -> {"0":"https://mozilla.com"}
|
|
|
|
//
|
|
|
|
// Expanded principal:
|
|
|
|
// {"2":{"0":"<base64principal1>,<base64principal2>"}} ->
|
|
|
|
// {"0":"<base64principal1>,<base64principal2>"}
|
|
|
|
//
|
|
|
|
// System principal:
|
|
|
|
// {"3":{}} -> {}
|
|
|
|
// The aKey passed in also returns the corresponding PrincipalKind enum
|
|
|
|
//
|
|
|
|
// Warning: The Json::Value* pointer is into the aRoot object
|
|
|
|
static const Json::Value* GetPrincipalObject(const Json::Value& aRoot,
|
|
|
|
int& aOutPrincipalKind) {
|
|
|
|
const Json::Value::Members members = aRoot.getMemberNames();
|
|
|
|
// We only support one top level key in the object
|
|
|
|
if (members.size() != 1) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
// members[0] here is the "0", "1", "2", "3" principalKind
|
|
|
|
// that is the top level of the serialized JSON principal
|
|
|
|
const std::string stringPrincipalKind = members[0];
|
|
|
|
|
|
|
|
// Next we take the string value from the JSON
|
|
|
|
// and convert it into the int for the BasePrincipal::PrincipalKind enum
|
|
|
|
|
|
|
|
// Verify that the key is within the valid range
|
|
|
|
int principalKind = std::stoi(stringPrincipalKind);
|
|
|
|
MOZ_ASSERT(BasePrincipal::eNullPrincipal == 0,
|
|
|
|
"We need to rely on 0 being a bounds check for the first "
|
|
|
|
"principal kind.");
|
|
|
|
if (principalKind < 0 || principalKind > BasePrincipal::eKindMax) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(principalKind == BasePrincipal::eNullPrincipal ||
|
2019-07-08 19:37:45 +03:00
|
|
|
principalKind == BasePrincipal::eContentPrincipal ||
|
2019-06-03 15:37:12 +03:00
|
|
|
principalKind == BasePrincipal::eExpandedPrincipal ||
|
|
|
|
principalKind == BasePrincipal::eSystemPrincipal);
|
|
|
|
aOutPrincipalKind = principalKind;
|
|
|
|
|
|
|
|
if (!aRoot[stringPrincipalKind].isObject()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the inner value of the principal object
|
|
|
|
return &aRoot[stringPrincipalKind];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accepts the JSON inner object without the wrapping principalKind
|
|
|
|
// (See GetPrincipalObject for the inner object response examples)
|
|
|
|
// Creates an array of KeyVal objects that are all defined on the principal
|
|
|
|
// Each principal type (null, content, expanded) has a KeyVal that stores the
|
|
|
|
// fields of the JSON
|
|
|
|
//
|
|
|
|
// This simplifies deserializing elsewhere as we do the checking for presence
|
|
|
|
// and string values here for the complete set of serializable keys that the
|
|
|
|
// corresponding principal supports.
|
|
|
|
//
|
|
|
|
// The KeyVal object has the following fields:
|
|
|
|
// - valueWasSerialized: is true if the deserialized JSON contained a string
|
|
|
|
// value
|
|
|
|
// - value: The string that was serialized for this key
|
|
|
|
// - key: an SerializableKeys enum value specific to the principal.
|
2019-07-08 19:37:45 +03:00
|
|
|
// For example content principal is an enum of: eURI, eDomain,
|
2019-06-03 15:37:12 +03:00
|
|
|
// eSuffix, eCSP
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Given an inner content principal:
|
|
|
|
// {"0": "https://mozilla.com", "2": "^privateBrowsingId=1"}
|
|
|
|
// | | | |
|
|
|
|
// ----------------------------- |
|
|
|
|
// | | |
|
|
|
|
// Key ----------------------
|
|
|
|
// |
|
|
|
|
// Value
|
|
|
|
//
|
2019-07-08 19:37:45 +03:00
|
|
|
// They Key "0" corresponds to ContentPrincipal::eURI
|
2019-06-03 15:37:12 +03:00
|
|
|
// They Key "1" corresponds to ContentPrincipal::eSuffix
|
|
|
|
template <typename T>
|
|
|
|
static nsTArray<typename T::KeyVal> GetJSONKeys(const Json::Value* aInput) {
|
|
|
|
int size = T::eMax + 1;
|
|
|
|
nsTArray<typename T::KeyVal> fields;
|
|
|
|
for (int i = 0; i != size; i++) {
|
2019-12-03 20:37:43 +03:00
|
|
|
typename T::KeyVal* field = fields.AppendElement();
|
|
|
|
// field->valueWasSerialized returns if the field was found in the
|
2019-06-03 15:37:12 +03:00
|
|
|
// deserialized code. This simplifies the consumers from having to check
|
|
|
|
// length.
|
2019-12-03 20:37:43 +03:00
|
|
|
field->valueWasSerialized = false;
|
|
|
|
field->key = static_cast<typename T::SerializableKeys>(i);
|
|
|
|
const std::string key = std::to_string(field->key);
|
2019-12-03 20:52:40 +03:00
|
|
|
if (aInput->isMember(key)) {
|
|
|
|
const Json::Value& val = (*aInput)[key];
|
|
|
|
if (val.isString()) {
|
2019-12-03 20:37:43 +03:00
|
|
|
field->value.Append(nsDependentCString(val.asCString()));
|
|
|
|
field->valueWasSerialized = true;
|
2019-12-03 20:52:40 +03:00
|
|
|
}
|
2019-06-03 15:37:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Takes a JSON string and parses it turning it into a principal of the
|
|
|
|
// corresponding type
|
|
|
|
//
|
|
|
|
// Given a content principal:
|
|
|
|
//
|
|
|
|
// inner JSON object
|
|
|
|
// |
|
|
|
|
// ---------------------------------------------------------
|
|
|
|
// | |
|
|
|
|
// {"1": {"0": "https://mozilla.com", "2": "^privateBrowsingId=1"}}
|
|
|
|
// | | | | |
|
|
|
|
// | ----------------------------- |
|
|
|
|
// | | | |
|
|
|
|
// PrincipalKind | | |
|
|
|
|
// | ----------------------------
|
|
|
|
// SerializableKeys |
|
|
|
|
// Value
|
|
|
|
//
|
|
|
|
// The string is first deserialized with jsoncpp to get the Json::Value of the
|
|
|
|
// object. The inner JSON object is parsed with GetPrincipalObject which returns
|
|
|
|
// a KeyVal array of the inner object's fields. PrincipalKind is returned by
|
|
|
|
// GetPrincipalObject which is then used to decide which principal
|
|
|
|
// implementation of FromProperties to call. The corresponding FromProperties
|
|
|
|
// call takes the KeyVal fields and turns it into a principal.
|
|
|
|
already_AddRefed<BasePrincipal> BasePrincipal::FromJSON(
|
|
|
|
const nsACString& aJSON) {
|
|
|
|
Json::Value root;
|
2019-08-09 01:39:07 +03:00
|
|
|
Json::CharReaderBuilder builder;
|
|
|
|
std::unique_ptr<Json::CharReader> const reader(builder.newCharReader());
|
|
|
|
bool parseSuccess =
|
|
|
|
reader->parse(aJSON.BeginReading(), aJSON.EndReading(), &root, nullptr);
|
2019-06-03 15:37:12 +03:00
|
|
|
if (!parseSuccess) {
|
|
|
|
MOZ_ASSERT(false,
|
|
|
|
"Unable to parse string as JSON to deserialize as a principal");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
int principalKind = -1;
|
|
|
|
const Json::Value* value = GetPrincipalObject(root, principalKind);
|
|
|
|
if (!value) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "Unexpected JSON principal %s\n",
|
|
|
|
root.toStyledString().c_str());
|
|
|
|
#endif
|
|
|
|
MOZ_ASSERT(false, "Unexpected JSON to deserialize as a principal");
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(principalKind != -1,
|
|
|
|
"PrincipalKind should always be >=0 by this point");
|
|
|
|
|
|
|
|
if (principalKind == eSystemPrincipal) {
|
|
|
|
RefPtr<BasePrincipal> principal =
|
|
|
|
BasePrincipal::Cast(nsContentUtils::GetSystemPrincipal());
|
|
|
|
return principal.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (principalKind == eNullPrincipal) {
|
|
|
|
nsTArray<NullPrincipal::KeyVal> res = GetJSONKeys<NullPrincipal>(value);
|
|
|
|
return NullPrincipal::FromProperties(res);
|
|
|
|
}
|
|
|
|
|
2019-07-08 19:37:45 +03:00
|
|
|
if (principalKind == eContentPrincipal) {
|
2019-06-03 15:37:12 +03:00
|
|
|
nsTArray<ContentPrincipal::KeyVal> res =
|
|
|
|
GetJSONKeys<ContentPrincipal>(value);
|
|
|
|
return ContentPrincipal::FromProperties(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (principalKind == eExpandedPrincipal) {
|
|
|
|
nsTArray<ExpandedPrincipal::KeyVal> res =
|
|
|
|
GetJSONKeys<ExpandedPrincipal>(value);
|
|
|
|
return ExpandedPrincipal::FromProperties(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_RELEASE_ASSERT(false, "Unexpected enum to deserialize as a principal");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult BasePrincipal::PopulateJSONObject(Json::Value& aObject) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a JSON representation of the principal.
|
|
|
|
// Calling BasePrincipal::FromJSON will deserialize the JSON into
|
|
|
|
// the corresponding principal type.
|
|
|
|
nsresult BasePrincipal::ToJSON(nsACString& aResult) {
|
|
|
|
MOZ_ASSERT(aResult.IsEmpty(), "ToJSON only supports an empty result input");
|
|
|
|
aResult.Truncate();
|
|
|
|
|
2019-08-09 01:39:07 +03:00
|
|
|
Json::StreamWriterBuilder builder;
|
|
|
|
builder["indentation"] = "";
|
2019-06-03 15:37:12 +03:00
|
|
|
Json::Value innerJSONObject = Json::objectValue;
|
|
|
|
|
|
|
|
nsresult rv = PopulateJSONObject(innerJSONObject);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
Json::Value root = Json::objectValue;
|
|
|
|
std::string key = std::to_string(Kind());
|
|
|
|
root[key] = innerJSONObject;
|
2019-08-09 01:39:07 +03:00
|
|
|
std::string result = Json::writeString(builder, root);
|
2019-06-03 15:37:12 +03:00
|
|
|
aResult.Append(result);
|
|
|
|
if (aResult.Length() == 0) {
|
|
|
|
MOZ_ASSERT(false, "JSON writer failed to output a principal serialization");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-11-23 19:10:16 +03:00
|
|
|
bool BasePrincipal::FastSubsumesIgnoringFPD(
|
|
|
|
nsIPrincipal* aOther, DocumentDomainConsideration aConsideration) {
|
|
|
|
MOZ_ASSERT(aOther);
|
|
|
|
|
|
|
|
if (Kind() == eContentPrincipal &&
|
|
|
|
!dom::ChromeUtils::IsOriginAttributesEqualIgnoringFPD(
|
|
|
|
mOriginAttributes, Cast(aOther)->mOriginAttributes)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SubsumesInternal(aOther, aConsideration);
|
|
|
|
}
|
|
|
|
|
2015-05-14 21:15:56 +03:00
|
|
|
bool BasePrincipal::Subsumes(nsIPrincipal* aOther,
|
|
|
|
DocumentDomainConsideration aConsideration) {
|
2015-06-02 01:33:35 +03:00
|
|
|
MOZ_ASSERT(aOther);
|
2019-07-08 19:37:45 +03:00
|
|
|
MOZ_ASSERT_IF(Kind() == eContentPrincipal, mOriginSuffix);
|
2016-11-02 20:04:13 +03:00
|
|
|
|
|
|
|
// Expanded principals handle origin attributes for each of their
|
|
|
|
// sub-principals individually, null principals do only simple checks for
|
|
|
|
// pointer equality, and system principals are immune to origin attributes
|
2019-07-08 19:37:45 +03:00
|
|
|
// checks, so only do this check for content principals.
|
|
|
|
if (Kind() == eContentPrincipal &&
|
2017-02-25 04:27:06 +03:00
|
|
|
mOriginSuffix != Cast(aOther)->mOriginSuffix) {
|
2016-11-02 20:04:13 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-05-14 21:15:56 +03:00
|
|
|
return SubsumesInternal(aOther, aConsideration);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::Equals(nsIPrincipal* aOther, bool* aResult) {
|
2019-09-09 19:45:29 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(aOther);
|
2017-02-25 02:05:44 +03:00
|
|
|
|
|
|
|
*aResult = FastEquals(aOther);
|
|
|
|
|
2015-05-14 21:15:56 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-04-23 16:11:56 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::EqualsForPermission(nsIPrincipal* aOther, bool aExactHost,
|
|
|
|
bool* aResult) {
|
|
|
|
*aResult = false;
|
|
|
|
NS_ENSURE_ARG_POINTER(aOther);
|
|
|
|
NS_ENSURE_ARG_POINTER(aResult);
|
|
|
|
|
2021-07-30 18:01:07 +03:00
|
|
|
auto* other = Cast(aOther);
|
|
|
|
if (Kind() != other->Kind()) {
|
|
|
|
// Principals of different kinds can't be equal.
|
2020-04-23 16:11:56 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2021-07-30 18:01:07 +03:00
|
|
|
if (Kind() == eSystemPrincipal) {
|
|
|
|
*aResult = this == other;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Kind() == eNullPrincipal) {
|
|
|
|
// We don't store permissions for NullPrincipals.
|
2020-04-23 16:11:56 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2021-07-30 18:01:07 +03:00
|
|
|
MOZ_ASSERT(Kind() == eExpandedPrincipal || Kind() == eContentPrincipal);
|
|
|
|
|
|
|
|
// Certain origin attributes should not be used to isolate permissions.
|
|
|
|
// Create a stripped copy of both OA sets to compare.
|
|
|
|
mozilla::OriginAttributes ourAttrs = mOriginAttributes;
|
|
|
|
PermissionManager::MaybeStripOriginAttributes(false, ourAttrs);
|
|
|
|
mozilla::OriginAttributes theirAttrs = aOther->OriginAttributesRef();
|
|
|
|
PermissionManager::MaybeStripOriginAttributes(false, theirAttrs);
|
2020-04-23 16:11:56 +03:00
|
|
|
|
2021-07-30 18:01:07 +03:00
|
|
|
if (ourAttrs != theirAttrs) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mOriginNoSuffix == other->mOriginNoSuffix) {
|
|
|
|
*aResult = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we are matching with an exact host, we're done now - the permissions
|
|
|
|
// don't match otherwise, we need to start comparing subdomains!
|
|
|
|
if (aExactHost) {
|
2020-04-23 16:11:56 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> ourURI;
|
|
|
|
nsresult rv = GetURI(getter_AddRefs(ourURI));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2021-07-30 18:01:07 +03:00
|
|
|
// Some principal types may indicate success, but still return nullptr for
|
|
|
|
// URI.
|
|
|
|
NS_ENSURE_TRUE(ourURI, NS_ERROR_FAILURE);
|
2020-04-23 16:11:56 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> otherURI;
|
2021-07-30 18:01:07 +03:00
|
|
|
rv = other->GetURI(getter_AddRefs(otherURI));
|
2020-04-23 16:11:56 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2021-07-30 18:01:07 +03:00
|
|
|
NS_ENSURE_TRUE(otherURI, NS_ERROR_FAILURE);
|
|
|
|
|
2020-04-23 16:11:56 +03:00
|
|
|
// Compare schemes
|
|
|
|
nsAutoCString otherScheme;
|
|
|
|
rv = otherURI->GetScheme(otherScheme);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsAutoCString ourScheme;
|
|
|
|
rv = ourURI->GetScheme(ourScheme);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (otherScheme != ourScheme) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare ports
|
|
|
|
int32_t otherPort;
|
|
|
|
rv = otherURI->GetPort(&otherPort);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
int32_t ourPort;
|
|
|
|
rv = ourURI->GetPort(&ourPort);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (otherPort != ourPort) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the host or any subdomain of their host matches.
|
|
|
|
nsAutoCString otherHost;
|
|
|
|
rv = otherURI->GetHost(otherHost);
|
|
|
|
if (NS_FAILED(rv) || otherHost.IsEmpty()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString ourHost;
|
|
|
|
rv = ourURI->GetHost(ourHost);
|
|
|
|
if (NS_FAILED(rv) || ourHost.IsEmpty()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIEffectiveTLDService> tldService =
|
|
|
|
do_GetService(NS_EFFECTIVETLDSERVICE_CONTRACTID);
|
|
|
|
if (!tldService) {
|
|
|
|
NS_ERROR("Should have a tld service!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This loop will not loop forever, as GetNextSubDomain will eventually fail
|
|
|
|
// with NS_ERROR_INSUFFICIENT_DOMAIN_LEVELS.
|
|
|
|
while (otherHost != ourHost) {
|
|
|
|
rv = tldService->GetNextSubDomain(otherHost, otherHost);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
if (rv == NS_ERROR_INSUFFICIENT_DOMAIN_LEVELS) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResult = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-03-07 06:50:10 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::EqualsConsideringDomain(nsIPrincipal* aOther, bool* aResult) {
|
2019-09-09 19:45:29 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(aOther);
|
2017-03-07 06:50:10 +03:00
|
|
|
|
|
|
|
*aResult = FastEqualsConsideringDomain(aOther);
|
|
|
|
|
2015-05-14 21:15:56 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-01-22 20:14:08 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::EqualsURI(nsIURI* aOtherURI, bool* aResult) {
|
|
|
|
*aResult = false;
|
|
|
|
nsCOMPtr<nsIURI> prinURI;
|
|
|
|
nsresult rv = GetURI(getter_AddRefs(prinURI));
|
|
|
|
if (NS_FAILED(rv) || !prinURI) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return prinURI->EqualsExceptRef(aOtherURI, aResult);
|
|
|
|
}
|
|
|
|
|
2017-02-23 05:29:47 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::Subsumes(nsIPrincipal* aOther, bool* aResult) {
|
2019-09-09 19:45:29 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(aOther);
|
2017-02-23 05:29:47 +03:00
|
|
|
|
|
|
|
*aResult = FastSubsumes(aOther);
|
|
|
|
|
2015-05-14 21:15:56 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-01-18 15:17:19 +03:00
|
|
|
NS_IMETHODIMP
|
2017-03-07 06:50:10 +03:00
|
|
|
BasePrincipal::SubsumesConsideringDomain(nsIPrincipal* aOther, bool* aResult) {
|
2019-09-09 19:45:29 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(aOther);
|
2017-01-18 15:17:19 +03:00
|
|
|
|
2017-03-07 06:50:10 +03:00
|
|
|
*aResult = FastSubsumesConsideringDomain(aOther);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::SubsumesConsideringDomainIgnoringFPD(nsIPrincipal* aOther,
|
|
|
|
bool* aResult) {
|
2019-09-09 19:45:29 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(aOther);
|
2017-03-07 06:50:10 +03:00
|
|
|
|
|
|
|
*aResult = FastSubsumesConsideringDomainIgnoringFPD(aOther);
|
2017-01-18 15:17:19 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-10-01 06:03:36 +03:00
|
|
|
NS_IMETHODIMP
|
2019-12-13 09:24:12 +03:00
|
|
|
BasePrincipal::CheckMayLoad(nsIURI* aURI, bool aAllowIfInheritsPrincipal) {
|
|
|
|
return CheckMayLoadHelper(aURI, aAllowIfInheritsPrincipal, false, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::CheckMayLoadWithReporting(nsIURI* aURI,
|
|
|
|
bool aAllowIfInheritsPrincipal,
|
|
|
|
uint64_t aInnerWindowID) {
|
|
|
|
return CheckMayLoadHelper(aURI, aAllowIfInheritsPrincipal, true,
|
|
|
|
aInnerWindowID);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult BasePrincipal::CheckMayLoadHelper(nsIURI* aURI,
|
|
|
|
bool aAllowIfInheritsPrincipal,
|
|
|
|
bool aReport,
|
|
|
|
uint64_t aInnerWindowID) {
|
2019-09-09 19:45:29 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(aURI);
|
2019-12-13 09:24:12 +03:00
|
|
|
MOZ_ASSERT(
|
|
|
|
aReport || aInnerWindowID == 0,
|
|
|
|
"Why do we have an inner window id if we're not supposed to report?");
|
2019-09-09 19:45:29 +03:00
|
|
|
|
2015-10-01 06:03:36 +03:00
|
|
|
// Check the internal method first, which allows us to quickly approve loads
|
|
|
|
// for the System Principal.
|
|
|
|
if (MayLoadInternal(aURI)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
if (aAllowIfInheritsPrincipal) {
|
|
|
|
// If the caller specified to allow loads of URIs that inherit
|
|
|
|
// our principal, allow the load if this URI inherits its principal.
|
|
|
|
bool doesInheritSecurityContext;
|
|
|
|
rv = NS_URIChainHasFlags(aURI,
|
|
|
|
nsIProtocolHandler::URI_INHERITS_SECURITY_CONTEXT,
|
|
|
|
&doesInheritSecurityContext);
|
|
|
|
if (NS_SUCCEEDED(rv) && doesInheritSecurityContext) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-08 22:56:34 +03:00
|
|
|
bool fetchableByAnyone;
|
|
|
|
rv = NS_URIChainHasFlags(aURI, nsIProtocolHandler::URI_FETCHABLE_BY_ANYONE,
|
|
|
|
&fetchableByAnyone);
|
|
|
|
if (NS_SUCCEEDED(rv) && fetchableByAnyone) {
|
|
|
|
return NS_OK;
|
2015-10-01 06:03:36 +03:00
|
|
|
}
|
|
|
|
|
2021-06-24 00:52:38 +03:00
|
|
|
if (aReport) {
|
2021-07-08 22:56:34 +03:00
|
|
|
nsCOMPtr<nsIURI> prinURI;
|
|
|
|
rv = GetURI(getter_AddRefs(prinURI));
|
|
|
|
if (NS_SUCCEEDED(rv) && prinURI) {
|
|
|
|
nsScriptSecurityManager::ReportError(
|
|
|
|
"CheckSameOriginError", prinURI, aURI,
|
|
|
|
mOriginAttributes.mPrivateBrowsingId > 0, aInnerWindowID);
|
|
|
|
}
|
2021-06-24 00:52:38 +03:00
|
|
|
}
|
|
|
|
|
2015-10-01 06:03:36 +03:00
|
|
|
return NS_ERROR_DOM_BAD_URI;
|
|
|
|
}
|
|
|
|
|
2019-10-02 18:10:40 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::IsThirdPartyURI(nsIURI* aURI, bool* aRes) {
|
2020-07-09 12:23:52 +03:00
|
|
|
if (IsSystemPrincipal() || (AddonPolicy() && AddonAllowsLoad(aURI))) {
|
2020-06-23 14:52:57 +03:00
|
|
|
*aRes = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-10-02 18:10:40 +03:00
|
|
|
*aRes = true;
|
|
|
|
// If we do not have a URI its always 3rd party.
|
|
|
|
nsCOMPtr<nsIURI> prinURI;
|
|
|
|
nsresult rv = GetURI(getter_AddRefs(prinURI));
|
|
|
|
if (NS_FAILED(rv) || !prinURI) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
ThirdPartyUtil* thirdPartyUtil = ThirdPartyUtil::GetInstance();
|
|
|
|
return thirdPartyUtil->IsThirdPartyURI(prinURI, aURI, aRes);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::IsThirdPartyPrincipal(nsIPrincipal* aPrin, bool* aRes) {
|
|
|
|
*aRes = true;
|
|
|
|
nsCOMPtr<nsIURI> prinURI;
|
|
|
|
nsresult rv = GetURI(getter_AddRefs(prinURI));
|
|
|
|
if (NS_FAILED(rv) || !prinURI) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return aPrin->IsThirdPartyURI(prinURI, aRes);
|
|
|
|
}
|
2020-04-27 13:54:47 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::IsThirdPartyChannel(nsIChannel* aChan, bool* aRes) {
|
2020-07-09 12:23:52 +03:00
|
|
|
if (IsSystemPrincipal()) {
|
|
|
|
// Nothing is 3rd party to the system principal.
|
|
|
|
*aRes = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-04-27 13:54:47 +03:00
|
|
|
nsCOMPtr<nsIURI> prinURI;
|
|
|
|
GetURI(getter_AddRefs(prinURI));
|
|
|
|
ThirdPartyUtil* thirdPartyUtil = ThirdPartyUtil::GetInstance();
|
|
|
|
return thirdPartyUtil->IsThirdPartyChannel(aChan, prinURI, aRes);
|
|
|
|
}
|
2019-10-02 18:10:40 +03:00
|
|
|
|
2019-12-06 15:32:27 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::IsSameOrigin(nsIURI* aURI, bool aIsPrivateWin, bool* aRes) {
|
|
|
|
*aRes = false;
|
|
|
|
nsCOMPtr<nsIURI> prinURI;
|
|
|
|
nsresult rv = GetURI(getter_AddRefs(prinURI));
|
|
|
|
if (NS_FAILED(rv) || !prinURI) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
nsIScriptSecurityManager* ssm = nsContentUtils::GetSecurityManager();
|
|
|
|
if (!ssm) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2019-12-12 20:40:03 +03:00
|
|
|
*aRes = NS_SUCCEEDED(
|
|
|
|
ssm->CheckSameOriginURI(prinURI, aURI, false, aIsPrivateWin));
|
2019-12-06 15:32:27 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2020-03-26 16:18:40 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::IsL10nAllowed(nsIURI* aURI, bool* aRes) {
|
|
|
|
*aRes = false;
|
|
|
|
|
|
|
|
if (nsContentUtils::IsErrorPage(aURI)) {
|
|
|
|
*aRes = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The system principal is always allowed.
|
|
|
|
if (IsSystemPrincipal()) {
|
|
|
|
*aRes = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsresult rv = GetURI(getter_AddRefs(uri));
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_OK);
|
|
|
|
|
|
|
|
bool hasFlags;
|
|
|
|
|
|
|
|
// Allow access to uris that cannot be loaded by web content.
|
|
|
|
rv = NS_URIChainHasFlags(uri, nsIProtocolHandler::URI_DANGEROUS_TO_LOAD,
|
|
|
|
&hasFlags);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_OK);
|
|
|
|
if (hasFlags) {
|
|
|
|
*aRes = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// UI resources also get access.
|
|
|
|
rv = NS_URIChainHasFlags(uri, nsIProtocolHandler::URI_IS_UI_RESOURCE,
|
|
|
|
&hasFlags);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_OK);
|
|
|
|
if (hasFlags) {
|
|
|
|
*aRes = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto policy = AddonPolicy();
|
|
|
|
*aRes = (policy && policy->IsPrivileged());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-03-03 15:58:38 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::AllowsRelaxStrictFileOriginPolicy(nsIURI* aURI, bool* aRes) {
|
|
|
|
*aRes = false;
|
|
|
|
nsCOMPtr<nsIURI> prinURI;
|
|
|
|
nsresult rv = GetURI(getter_AddRefs(prinURI));
|
|
|
|
if (NS_FAILED(rv) || !prinURI) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
*aRes = NS_RelaxStrictFileOriginPolicy(aURI, prinURI);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2019-12-06 15:32:27 +03:00
|
|
|
|
2020-02-17 16:57:41 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::GetPrefLightCacheKey(nsIURI* aURI, bool aWithCredentials,
|
2020-10-26 16:39:12 +03:00
|
|
|
const OriginAttributes& aOriginAttributes,
|
2020-02-17 16:57:41 +03:00
|
|
|
nsACString& _retval) {
|
|
|
|
_retval.Truncate();
|
|
|
|
constexpr auto space = " "_ns;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsresult rv = GetURI(getter_AddRefs(uri));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsAutoCString scheme, host, port;
|
|
|
|
if (uri) {
|
|
|
|
uri->GetScheme(scheme);
|
|
|
|
uri->GetHost(host);
|
|
|
|
port.AppendInt(NS_GetRealPort(uri));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aWithCredentials) {
|
|
|
|
_retval.AssignLiteral("cred");
|
|
|
|
} else {
|
|
|
|
_retval.AssignLiteral("nocred");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString spec;
|
|
|
|
rv = aURI->GetSpec(spec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2020-10-26 16:39:12 +03:00
|
|
|
nsAutoCString originAttributesSuffix;
|
|
|
|
aOriginAttributes.CreateSuffix(originAttributesSuffix);
|
|
|
|
|
|
|
|
_retval.Append(space + scheme + space + host + space + port + space + spec +
|
|
|
|
space + originAttributesSuffix);
|
2020-02-17 16:57:41 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-05-19 17:51:40 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::HasFirstpartyStorageAccess(mozIDOMWindow* aCheckWindow,
|
|
|
|
uint32_t* aRejectedReason,
|
|
|
|
bool* aOutAllowed) {
|
|
|
|
*aRejectedReason = 0;
|
|
|
|
*aOutAllowed = false;
|
|
|
|
|
|
|
|
nsPIDOMWindowInner* win = nsPIDOMWindowInner::From(aCheckWindow);
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsresult rv = GetURI(getter_AddRefs(uri));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
*aOutAllowed =
|
|
|
|
ContentBlocking::ShouldAllowAccessFor(win, uri, aRejectedReason);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-05-12 00:36:56 +03:00
|
|
|
NS_IMETHODIMP
|
2015-10-24 03:58:21 +03:00
|
|
|
BasePrincipal::GetIsNullPrincipal(bool* aResult) {
|
|
|
|
*aResult = Kind() == eNullPrincipal;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2019-07-08 19:37:45 +03:00
|
|
|
BasePrincipal::GetIsContentPrincipal(bool* aResult) {
|
|
|
|
*aResult = Kind() == eContentPrincipal;
|
2015-10-24 03:58:21 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::GetIsExpandedPrincipal(bool* aResult) {
|
|
|
|
*aResult = Kind() == eExpandedPrincipal;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-10-22 19:03:27 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::GetAsciiSpec(nsACString& aSpec) {
|
|
|
|
aSpec.Truncate();
|
|
|
|
nsCOMPtr<nsIURI> prinURI;
|
|
|
|
nsresult rv = GetURI(getter_AddRefs(prinURI));
|
|
|
|
if (NS_FAILED(rv) || !prinURI) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return prinURI->GetAsciiSpec(aSpec);
|
|
|
|
}
|
|
|
|
|
2020-08-04 14:10:41 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::GetSpec(nsACString& aSpec) {
|
|
|
|
aSpec.Truncate();
|
|
|
|
nsCOMPtr<nsIURI> prinURI;
|
|
|
|
nsresult rv = GetURI(getter_AddRefs(prinURI));
|
|
|
|
if (NS_FAILED(rv) || !prinURI) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return prinURI->GetSpec(aSpec);
|
|
|
|
}
|
|
|
|
|
2020-02-17 16:57:41 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::GetAsciiHost(nsACString& aHost) {
|
|
|
|
aHost.Truncate();
|
|
|
|
nsCOMPtr<nsIURI> prinURI;
|
|
|
|
nsresult rv = GetURI(getter_AddRefs(prinURI));
|
|
|
|
if (NS_FAILED(rv) || !prinURI) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return prinURI->GetAsciiHost(aHost);
|
|
|
|
}
|
|
|
|
|
2020-01-22 20:14:08 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::GetExposablePrePath(nsACString& aPrepath) {
|
|
|
|
aPrepath.Truncate();
|
|
|
|
nsCOMPtr<nsIURI> prinURI;
|
|
|
|
nsresult rv = GetURI(getter_AddRefs(prinURI));
|
|
|
|
if (NS_FAILED(rv) || !prinURI) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-03-13 15:44:36 +03:00
|
|
|
nsCOMPtr<nsIURI> exposableURI = net::nsIOService::CreateExposableURI(prinURI);
|
|
|
|
return exposableURI->GetDisplayPrePath(aPrepath);
|
2020-01-22 20:14:08 +03:00
|
|
|
}
|
2020-04-15 00:48:38 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::GetExposableSpec(nsACString& aSpec) {
|
|
|
|
aSpec.Truncate();
|
|
|
|
nsCOMPtr<nsIURI> prinURI;
|
|
|
|
nsresult rv = GetURI(getter_AddRefs(prinURI));
|
|
|
|
if (NS_FAILED(rv) || !prinURI) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIURI> clone;
|
|
|
|
rv = NS_MutateURI(prinURI)
|
2020-09-23 18:17:15 +03:00
|
|
|
.SetQuery(""_ns)
|
|
|
|
.SetRef(""_ns)
|
|
|
|
.SetUserPass(""_ns)
|
2020-04-15 00:48:38 +03:00
|
|
|
.Finalize(clone);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
return clone->GetAsciiSpec(aSpec);
|
|
|
|
}
|
|
|
|
|
2020-02-12 19:13:19 +03:00
|
|
|
NS_IMETHODIMP
|
2021-04-30 14:16:31 +03:00
|
|
|
BasePrincipal::GetPrePath(nsACString& aPath) {
|
2020-02-12 19:13:19 +03:00
|
|
|
aPath.Truncate();
|
|
|
|
nsCOMPtr<nsIURI> prinURI;
|
|
|
|
nsresult rv = GetURI(getter_AddRefs(prinURI));
|
|
|
|
if (NS_FAILED(rv) || !prinURI) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return prinURI->GetPrePath(aPath);
|
|
|
|
}
|
2020-01-22 20:14:08 +03:00
|
|
|
|
2020-04-20 13:01:54 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::GetFilePath(nsACString& aPath) {
|
|
|
|
aPath.Truncate();
|
|
|
|
nsCOMPtr<nsIURI> prinURI;
|
|
|
|
nsresult rv = GetURI(getter_AddRefs(prinURI));
|
|
|
|
if (NS_FAILED(rv) || !prinURI) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return prinURI->GetFilePath(aPath);
|
|
|
|
}
|
|
|
|
|
2015-10-24 03:58:21 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::GetIsSystemPrincipal(bool* aResult) {
|
2019-01-03 23:08:55 +03:00
|
|
|
*aResult = IsSystemPrincipal();
|
2015-05-12 00:36:56 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-07-14 05:26:15 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::GetIsAddonOrExpandedAddonPrincipal(bool* aResult) {
|
|
|
|
*aResult = AddonPolicy() || ContentScriptAddonPolicy();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-11-28 12:22:17 +03:00
|
|
|
NS_IMETHODIMP BasePrincipal::GetIsOnion(bool* aIsOnion) {
|
|
|
|
*aIsOnion = false;
|
|
|
|
nsCOMPtr<nsIURI> prinURI;
|
|
|
|
nsresult rv = GetURI(getter_AddRefs(prinURI));
|
|
|
|
if (NS_FAILED(rv) || !prinURI) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString host;
|
|
|
|
rv = prinURI->GetHost(host);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
*aIsOnion = StringEndsWith(host, ".onion"_ns);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-02-27 02:15:43 +03:00
|
|
|
NS_IMETHODIMP BasePrincipal::GetIsIpAddress(bool* aIsIpAddress) {
|
|
|
|
*aIsIpAddress = false;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> prinURI;
|
|
|
|
nsresult rv = GetURI(getter_AddRefs(prinURI));
|
|
|
|
if (NS_FAILED(rv) || !prinURI) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString host;
|
|
|
|
rv = prinURI->GetHost(host);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRNetAddr prAddr;
|
|
|
|
memset(&prAddr, 0, sizeof(prAddr));
|
|
|
|
|
|
|
|
if (PR_StringToNetAddr(host.get(), &prAddr) == PR_SUCCESS) {
|
|
|
|
*aIsIpAddress = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-08-24 19:28:26 +03:00
|
|
|
NS_IMETHODIMP BasePrincipal::GetIsLocalIpAddress(bool* aIsIpAddress) {
|
|
|
|
*aIsIpAddress = false;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> prinURI;
|
|
|
|
nsresult rv = GetURI(getter_AddRefs(prinURI));
|
|
|
|
if (NS_FAILED(rv) || !prinURI) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIIOService> ioService = do_GetIOService(&rv);
|
|
|
|
if (NS_FAILED(rv) || !ioService) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
rv = ioService->HostnameIsLocalIPAddress(prinURI, aIsIpAddress);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
*aIsIpAddress = false;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-06-02 16:50:12 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::GetScheme(nsACString& aScheme) {
|
|
|
|
aScheme.Truncate();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> prinURI;
|
|
|
|
nsresult rv = GetURI(getter_AddRefs(prinURI));
|
|
|
|
if (NS_FAILED(rv) || !prinURI) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return prinURI->GetScheme(aScheme);
|
|
|
|
}
|
|
|
|
|
2019-09-26 13:47:03 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::SchemeIs(const char* aScheme, bool* aResult) {
|
|
|
|
*aResult = false;
|
|
|
|
nsCOMPtr<nsIURI> prinURI;
|
|
|
|
nsresult rv = GetURI(getter_AddRefs(prinURI));
|
2021-10-05 18:16:19 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv)) || !prinURI) {
|
2019-09-26 13:47:03 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
*aResult = prinURI->SchemeIs(aScheme);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-11-07 16:42:37 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::IsURIInPrefList(const char* aPref, bool* aResult) {
|
|
|
|
*aResult = false;
|
|
|
|
nsCOMPtr<nsIURI> prinURI;
|
|
|
|
nsresult rv = GetURI(getter_AddRefs(prinURI));
|
|
|
|
if (NS_FAILED(rv) || !prinURI) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
*aResult = nsContentUtils::IsURIInPrefList(prinURI, aPref);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2021-10-05 18:16:19 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::IsURIInList(const nsACString& aList, bool* aResult) {
|
|
|
|
*aResult = false;
|
|
|
|
nsCOMPtr<nsIURI> prinURI;
|
|
|
|
|
|
|
|
nsresult rv = GetURI(getter_AddRefs(prinURI));
|
|
|
|
if (NS_FAILED(rv) || !prinURI) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResult = nsContentUtils::IsURIInList(prinURI, nsCString(aList));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-12-05 19:04:53 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::GetIsOriginPotentiallyTrustworthy(bool* aResult) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
*aResult = false;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsresult rv = GetURI(getter_AddRefs(uri));
|
|
|
|
if (NS_FAILED(rv) || !uri) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResult = nsMixedContentBlocker::IsPotentiallyTrustworthyOrigin(uri);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-10-17 16:54:41 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::GetAboutModuleFlags(uint32_t* flags) {
|
|
|
|
*flags = 0;
|
|
|
|
nsCOMPtr<nsIURI> prinURI;
|
|
|
|
nsresult rv = GetURI(getter_AddRefs(prinURI));
|
|
|
|
if (NS_FAILED(rv) || !prinURI) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
if (!prinURI->SchemeIs("about")) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIAboutModule> aboutModule;
|
|
|
|
rv = NS_GetAboutModule(prinURI, getter_AddRefs(aboutModule));
|
|
|
|
if (NS_FAILED(rv) || !aboutModule) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return aboutModule->GetURIFlags(prinURI, flags);
|
|
|
|
}
|
|
|
|
|
2015-05-15 02:24:25 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::GetOriginAttributes(JSContext* aCx,
|
|
|
|
JS::MutableHandle<JS::Value> aVal) {
|
|
|
|
if (NS_WARN_IF(!ToJSValue(aCx, mOriginAttributes, aVal))) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::GetOriginSuffix(nsACString& aOriginAttributes) {
|
2017-02-25 01:02:24 +03:00
|
|
|
MOZ_ASSERT(mOriginSuffix);
|
2017-09-25 09:38:18 +03:00
|
|
|
mOriginSuffix->ToUTF8String(aOriginAttributes);
|
|
|
|
return NS_OK;
|
2015-05-15 02:24:25 +03:00
|
|
|
}
|
|
|
|
|
2015-07-30 21:15:00 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::GetUserContextId(uint32_t* aUserContextId) {
|
|
|
|
*aUserContextId = UserContextId();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-06-29 15:01:00 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::GetPrivateBrowsingId(uint32_t* aPrivateBrowsingId) {
|
|
|
|
*aPrivateBrowsingId = PrivateBrowsingId();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-05-14 04:25:40 +03:00
|
|
|
NS_IMETHODIMP
|
2016-02-05 04:42:44 +03:00
|
|
|
BasePrincipal::GetIsInIsolatedMozBrowserElement(
|
|
|
|
bool* aIsInIsolatedMozBrowserElement) {
|
|
|
|
*aIsInIsolatedMozBrowserElement = IsInIsolatedMozBrowserElement();
|
2015-05-14 04:25:40 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2021-08-05 21:16:17 +03:00
|
|
|
nsresult BasePrincipal::GetAddonPolicy(
|
|
|
|
extensions::WebExtensionPolicy** aResult) {
|
2018-06-26 10:19:46 +03:00
|
|
|
RefPtr<extensions::WebExtensionPolicy> policy(AddonPolicy());
|
|
|
|
policy.forget(aResult);
|
2017-09-05 21:04:43 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2016-11-05 01:16:50 +03:00
|
|
|
|
2017-09-05 21:04:43 +03:00
|
|
|
extensions::WebExtensionPolicy* BasePrincipal::AddonPolicy() {
|
|
|
|
if (Is<ContentPrincipal>()) {
|
|
|
|
return As<ContentPrincipal>()->AddonPolicy();
|
2016-07-09 03:19:17 +03:00
|
|
|
}
|
2017-09-05 21:04:43 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
2016-11-05 01:16:50 +03:00
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
bool BasePrincipal::AddonHasPermission(const nsAtom* aPerm) {
|
2017-09-05 21:04:43 +03:00
|
|
|
if (auto policy = AddonPolicy()) {
|
|
|
|
return policy->HasPermission(aPerm);
|
|
|
|
}
|
|
|
|
return false;
|
2016-07-09 03:19:17 +03:00
|
|
|
}
|
|
|
|
|
2017-11-23 01:20:26 +03:00
|
|
|
nsIPrincipal* BasePrincipal::PrincipalToInherit(nsIURI* aRequestedURI) {
|
2017-11-03 05:56:27 +03:00
|
|
|
if (Is<ExpandedPrincipal>()) {
|
2017-11-23 01:20:26 +03:00
|
|
|
return As<ExpandedPrincipal>()->PrincipalToInherit(aRequestedURI);
|
2017-11-03 05:56:27 +03:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-07-08 19:37:45 +03:00
|
|
|
already_AddRefed<BasePrincipal> BasePrincipal::CreateContentPrincipal(
|
2017-03-29 09:24:01 +03:00
|
|
|
nsIURI* aURI, const OriginAttributes& aAttrs) {
|
|
|
|
MOZ_ASSERT(aURI);
|
|
|
|
|
|
|
|
nsAutoCString originNoSuffix;
|
|
|
|
nsresult rv =
|
|
|
|
ContentPrincipal::GenerateOriginNoSuffixFromURI(aURI, originNoSuffix);
|
2017-04-19 09:00:11 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2017-03-29 09:24:01 +03:00
|
|
|
// If the generation of the origin fails, we still want to have a valid
|
|
|
|
// principal. Better to return a null principal here.
|
|
|
|
return NullPrincipal::Create(aAttrs);
|
|
|
|
}
|
|
|
|
|
2019-07-08 19:37:45 +03:00
|
|
|
return CreateContentPrincipal(aURI, aAttrs, originNoSuffix);
|
2017-03-29 09:24:01 +03:00
|
|
|
}
|
|
|
|
|
2019-07-08 19:37:45 +03:00
|
|
|
already_AddRefed<BasePrincipal> BasePrincipal::CreateContentPrincipal(
|
2017-03-29 09:24:01 +03:00
|
|
|
nsIURI* aURI, const OriginAttributes& aAttrs,
|
|
|
|
const nsACString& aOriginNoSuffix) {
|
|
|
|
MOZ_ASSERT(aURI);
|
|
|
|
MOZ_ASSERT(!aOriginNoSuffix.IsEmpty());
|
|
|
|
|
2015-05-15 23:43:11 +03:00
|
|
|
// If the URI is supposed to inherit the security context of whoever loads it,
|
2019-07-08 19:37:45 +03:00
|
|
|
// we shouldn't make a content principal for it.
|
2015-05-15 23:43:11 +03:00
|
|
|
bool inheritsPrincipal;
|
|
|
|
nsresult rv = NS_URIChainHasFlags(
|
|
|
|
aURI, nsIProtocolHandler::URI_INHERITS_SECURITY_CONTEXT,
|
|
|
|
&inheritsPrincipal);
|
|
|
|
if (NS_FAILED(rv) || inheritsPrincipal) {
|
2017-03-22 13:38:40 +03:00
|
|
|
return NullPrincipal::Create(aAttrs);
|
2015-05-15 23:43:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether the URI knows what its principal is supposed to be.
|
2018-07-31 11:27:00 +03:00
|
|
|
#if defined(MOZ_THUNDERBIRD) || defined(MOZ_SUITE)
|
|
|
|
nsCOMPtr<nsIURIWithSpecialOrigin> uriWithSpecialOrigin =
|
|
|
|
do_QueryInterface(aURI);
|
|
|
|
if (uriWithSpecialOrigin) {
|
|
|
|
nsCOMPtr<nsIURI> origin;
|
|
|
|
rv = uriWithSpecialOrigin->GetOrigin(getter_AddRefs(origin));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(origin);
|
|
|
|
OriginAttributes attrs;
|
2019-07-08 19:37:45 +03:00
|
|
|
RefPtr<BasePrincipal> principal = CreateContentPrincipal(origin, attrs);
|
2018-07-31 11:27:00 +03:00
|
|
|
return principal.forget();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-07-24 23:15:57 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> blobPrincipal;
|
|
|
|
if (dom::BlobURLProtocolHandler::GetBlobURLPrincipal(
|
|
|
|
aURI, getter_AddRefs(blobPrincipal))) {
|
|
|
|
MOZ_ASSERT(blobPrincipal);
|
|
|
|
RefPtr<BasePrincipal> principal = Cast(blobPrincipal);
|
|
|
|
return principal.forget();
|
2015-05-15 23:43:11 +03:00
|
|
|
}
|
|
|
|
|
2019-07-08 19:37:45 +03:00
|
|
|
// Mint a content principal.
|
2021-05-17 23:50:09 +03:00
|
|
|
RefPtr<ContentPrincipal> principal =
|
|
|
|
new ContentPrincipal(aURI, aAttrs, aOriginNoSuffix);
|
2019-07-08 19:37:45 +03:00
|
|
|
return principal.forget();
|
2015-05-15 23:43:11 +03:00
|
|
|
}
|
|
|
|
|
2019-07-08 19:37:45 +03:00
|
|
|
already_AddRefed<BasePrincipal> BasePrincipal::CreateContentPrincipal(
|
2015-08-26 14:12:13 +03:00
|
|
|
const nsACString& aOrigin) {
|
|
|
|
MOZ_ASSERT(!StringBeginsWith(aOrigin, "["_ns),
|
2019-07-08 19:37:45 +03:00
|
|
|
"CreateContentPrincipal does not support System and Expanded "
|
2015-08-26 14:12:13 +03:00
|
|
|
"principals");
|
|
|
|
|
|
|
|
MOZ_ASSERT(
|
|
|
|
!StringBeginsWith(aOrigin, nsLiteralCString(NS_NULLPRINCIPAL_SCHEME ":")),
|
2019-07-08 19:37:45 +03:00
|
|
|
"CreateContentPrincipal does not support NullPrincipal");
|
2015-08-26 14:12:13 +03:00
|
|
|
|
|
|
|
nsAutoCString originNoSuffix;
|
2018-07-24 23:15:57 +03:00
|
|
|
OriginAttributes attrs;
|
2015-08-26 14:12:13 +03:00
|
|
|
if (!attrs.PopulateFromOrigin(aOrigin, originNoSuffix)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(uri), originNoSuffix);
|
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
|
|
|
|
2019-07-08 19:37:45 +03:00
|
|
|
return BasePrincipal::CreateContentPrincipal(uri, attrs);
|
2015-08-26 14:12:13 +03:00
|
|
|
}
|
|
|
|
|
2019-04-12 08:30:19 +03:00
|
|
|
already_AddRefed<BasePrincipal> BasePrincipal::CloneForcingOriginAttributes(
|
|
|
|
const OriginAttributes& aOriginAttributes) {
|
2019-07-08 19:37:45 +03:00
|
|
|
if (NS_WARN_IF(!IsContentPrincipal())) {
|
2019-04-12 08:30:19 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-04-12 08:31:32 +03:00
|
|
|
nsAutoCString originNoSuffix;
|
|
|
|
nsresult rv = GetOriginNoSuffix(originNoSuffix);
|
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
|
|
|
|
2021-05-17 23:50:09 +03:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(GetURI(getter_AddRefs(uri)));
|
2019-04-12 08:31:32 +03:00
|
|
|
|
2021-05-17 23:50:09 +03:00
|
|
|
RefPtr<ContentPrincipal> copy =
|
|
|
|
new ContentPrincipal(uri, aOriginAttributes, originNoSuffix);
|
2019-04-12 08:31:32 +03:00
|
|
|
return copy.forget();
|
|
|
|
}
|
|
|
|
|
2018-05-14 21:49:32 +03:00
|
|
|
extensions::WebExtensionPolicy* BasePrincipal::ContentScriptAddonPolicy() {
|
|
|
|
if (!Is<ExpandedPrincipal>()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto expanded = As<ExpandedPrincipal>();
|
2018-10-31 20:56:43 +03:00
|
|
|
for (auto& prin : expanded->AllowList()) {
|
2018-05-14 21:49:32 +03:00
|
|
|
if (auto policy = BasePrincipal::Cast(prin)->AddonPolicy()) {
|
|
|
|
return policy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-02-10 08:08:06 +03:00
|
|
|
bool BasePrincipal::AddonAllowsLoad(nsIURI* aURI,
|
|
|
|
bool aExplicit /* = false */) {
|
2018-04-04 17:54:26 +03:00
|
|
|
if (Is<ExpandedPrincipal>()) {
|
|
|
|
return As<ExpandedPrincipal>()->AddonAllowsLoad(aURI, aExplicit);
|
|
|
|
}
|
2017-09-05 21:04:43 +03:00
|
|
|
if (auto policy = AddonPolicy()) {
|
|
|
|
return policy->CanAccessURI(aURI, aExplicit);
|
2015-07-08 03:53:15 +03:00
|
|
|
}
|
2017-09-05 21:04:43 +03:00
|
|
|
return false;
|
2015-07-08 03:53:15 +03:00
|
|
|
}
|
|
|
|
|
2020-02-27 03:22:27 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::GetLocalStorageQuotaKey(nsACString& aKey) {
|
|
|
|
aKey.Truncate();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
2020-11-13 22:51:08 +03:00
|
|
|
nsresult rv = GetURI(getter_AddRefs(uri));
|
2020-02-27 03:22:27 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
NS_ENSURE_TRUE(uri, NS_ERROR_UNEXPECTED);
|
|
|
|
|
2020-11-13 22:51:08 +03:00
|
|
|
// The special handling of the file scheme should be consistent with
|
|
|
|
// GetStorageOriginKey.
|
|
|
|
|
|
|
|
nsAutoCString baseDomain;
|
|
|
|
rv = uri->GetAsciiHost(baseDomain);
|
2020-02-27 03:22:27 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2020-11-13 22:51:08 +03:00
|
|
|
if (baseDomain.IsEmpty() && uri->SchemeIs("file")) {
|
|
|
|
nsCOMPtr<nsIURL> url = do_QueryInterface(uri, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = url->GetDirectory(baseDomain);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
} else {
|
|
|
|
nsCOMPtr<nsIEffectiveTLDService> eTLDService(
|
|
|
|
do_GetService(NS_EFFECTIVETLDSERVICE_CONTRACTID, &rv));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsAutoCString eTLDplusOne;
|
|
|
|
rv = eTLDService->GetBaseDomain(uri, 0, eTLDplusOne);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
baseDomain = eTLDplusOne;
|
|
|
|
} else if (rv == NS_ERROR_HOST_IS_IP_ADDRESS ||
|
|
|
|
rv == NS_ERROR_INSUFFICIENT_DOMAIN_LEVELS) {
|
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2020-02-27 03:22:27 +03:00
|
|
|
OriginAttributesRef().CreateSuffix(aKey);
|
|
|
|
|
|
|
|
nsAutoCString subdomainsDBKey;
|
2020-11-13 22:51:08 +03:00
|
|
|
rv = dom::StorageUtils::CreateReversedDomain(baseDomain, subdomainsDBKey);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2020-02-27 03:22:27 +03:00
|
|
|
|
|
|
|
aKey.Append(':');
|
|
|
|
aKey.Append(subdomainsDBKey);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2020-11-13 22:51:08 +03:00
|
|
|
|
2020-04-20 12:53:40 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::GetNextSubDomainPrincipal(
|
|
|
|
nsIPrincipal** aNextSubDomainPrincipal) {
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsresult rv = GetURI(getter_AddRefs(uri));
|
|
|
|
if (NS_FAILED(rv) || !uri) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString host;
|
|
|
|
rv = uri->GetHost(host);
|
|
|
|
if (NS_FAILED(rv) || host.IsEmpty()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCString subDomain;
|
|
|
|
rv = nsEffectiveTLDService::GetInstance()->GetNextSubDomain(host, subDomain);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv) || subDomain.IsEmpty()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> subDomainURI;
|
|
|
|
rv = NS_MutateURI(uri).SetHost(subDomain).Finalize(subDomainURI);
|
|
|
|
if (NS_FAILED(rv) || !subDomainURI) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
// Copy the attributes over
|
|
|
|
mozilla::OriginAttributes attrs = OriginAttributesRef();
|
|
|
|
|
|
|
|
if (!StaticPrefs::permissions_isolateBy_userContext()) {
|
|
|
|
// Disable userContext for permissions.
|
|
|
|
attrs.StripAttributes(mozilla::OriginAttributes::STRIP_USER_CONTEXT_ID);
|
|
|
|
}
|
|
|
|
RefPtr<nsIPrincipal> principal =
|
|
|
|
mozilla::BasePrincipal::CreateContentPrincipal(subDomainURI, attrs);
|
|
|
|
|
|
|
|
if (!principal) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
principal.forget(aNextSubDomainPrincipal);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2020-02-27 03:22:27 +03:00
|
|
|
|
2020-03-29 18:31:24 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::GetStorageOriginKey(nsACString& aOriginKey) {
|
|
|
|
aOriginKey.Truncate();
|
2020-11-13 22:51:08 +03:00
|
|
|
|
2020-03-29 18:31:24 +03:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsresult rv = GetURI(getter_AddRefs(uri));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2020-11-13 22:51:08 +03:00
|
|
|
NS_ENSURE_TRUE(uri, NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
// The special handling of the file scheme should be consistent with
|
|
|
|
// GetLocalStorageQuotaKey.
|
2020-03-29 18:31:24 +03:00
|
|
|
|
|
|
|
nsAutoCString domainOrigin;
|
|
|
|
rv = uri->GetAsciiHost(domainOrigin);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (domainOrigin.IsEmpty()) {
|
|
|
|
// For the file:/// protocol use the exact directory as domain.
|
|
|
|
if (uri->SchemeIs("file")) {
|
|
|
|
nsCOMPtr<nsIURL> url = do_QueryInterface(uri, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = url->GetDirectory(domainOrigin);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append reversed domain
|
|
|
|
nsAutoCString reverseDomain;
|
|
|
|
rv = dom::StorageUtils::CreateReversedDomain(domainOrigin, reverseDomain);
|
2020-11-13 22:51:08 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2020-03-29 18:31:24 +03:00
|
|
|
|
|
|
|
aOriginKey.Append(reverseDomain);
|
|
|
|
|
|
|
|
// Append scheme
|
|
|
|
nsAutoCString scheme;
|
|
|
|
rv = uri->GetScheme(scheme);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
aOriginKey.Append(':');
|
|
|
|
aOriginKey.Append(scheme);
|
|
|
|
|
|
|
|
// Append port if any
|
|
|
|
int32_t port = NS_GetRealPort(uri);
|
|
|
|
if (port != -1) {
|
|
|
|
aOriginKey.Append(nsPrintfCString(":%d", port));
|
|
|
|
}
|
2020-11-13 22:51:08 +03:00
|
|
|
|
2020-03-29 18:31:24 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-03-09 16:28:03 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::GetIsScriptAllowedByPolicy(bool* aIsScriptAllowedByPolicy) {
|
|
|
|
*aIsScriptAllowedByPolicy = false;
|
|
|
|
nsCOMPtr<nsIURI> prinURI;
|
|
|
|
nsresult rv = GetURI(getter_AddRefs(prinURI));
|
|
|
|
if (NS_FAILED(rv) || !prinURI) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
nsIScriptSecurityManager* ssm = nsContentUtils::GetSecurityManager();
|
|
|
|
if (!ssm) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
return ssm->PolicyAllowsScript(prinURI, aIsScriptAllowedByPolicy);
|
|
|
|
}
|
|
|
|
|
2018-09-11 12:01:14 +03:00
|
|
|
bool SiteIdentifier::Equals(const SiteIdentifier& aOther) const {
|
|
|
|
MOZ_ASSERT(IsInitialized());
|
|
|
|
MOZ_ASSERT(aOther.IsInitialized());
|
|
|
|
return mPrincipal->FastEquals(aOther.mPrincipal);
|
|
|
|
}
|
|
|
|
|
2020-03-05 16:42:13 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::CreateReferrerInfo(mozilla::dom::ReferrerPolicy aReferrerPolicy,
|
|
|
|
nsIReferrerInfo** _retval) {
|
|
|
|
nsCOMPtr<nsIURI> prinURI;
|
2020-03-25 17:38:42 +03:00
|
|
|
RefPtr<dom::ReferrerInfo> info;
|
2020-03-05 16:42:13 +03:00
|
|
|
nsresult rv = GetURI(getter_AddRefs(prinURI));
|
|
|
|
if (NS_FAILED(rv) || !prinURI) {
|
2020-03-25 17:38:42 +03:00
|
|
|
info = new dom::ReferrerInfo(nullptr);
|
|
|
|
info.forget(_retval);
|
|
|
|
return NS_OK;
|
2020-03-05 16:42:13 +03:00
|
|
|
}
|
2020-03-25 17:38:42 +03:00
|
|
|
info = new dom::ReferrerInfo(prinURI, aReferrerPolicy);
|
2020-03-05 16:42:13 +03:00
|
|
|
info.forget(_retval);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2021-07-16 00:09:15 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::GetPrecursorPrincipal(nsIPrincipal** aPrecursor) {
|
|
|
|
*aPrecursor = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2021-05-17 23:50:09 +03:00
|
|
|
NS_IMPL_ADDREF(BasePrincipal::Deserializer)
|
|
|
|
NS_IMPL_RELEASE(BasePrincipal::Deserializer)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(BasePrincipal::Deserializer)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISerializable)
|
|
|
|
if (mPrincipal) {
|
|
|
|
return mPrincipal->QueryInterface(aIID, aInstancePtr);
|
|
|
|
} else
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BasePrincipal::Deserializer::Write(nsIObjectOutputStream* aStream) {
|
|
|
|
// Read is used still for legacy principals
|
|
|
|
MOZ_RELEASE_ASSERT(false, "Old style serialization is removed");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-05-12 00:25:59 +03:00
|
|
|
} // namespace mozilla
|