2018-10-01 09:09:44 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 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 "FeaturePolicy.h"
|
|
|
|
#include "mozilla/dom/FeaturePolicyBinding.h"
|
|
|
|
#include "mozilla/dom/FeaturePolicyParser.h"
|
|
|
|
#include "mozilla/dom/FeaturePolicyUtils.h"
|
|
|
|
#include "nsContentUtils.h"
|
2018-10-12 10:36:33 +03:00
|
|
|
#include "nsNetUtil.h"
|
2018-10-01 09:09:44 +03:00
|
|
|
|
2018-10-12 10:36:33 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2018-10-01 09:09:44 +03:00
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(FeaturePolicy, mParentNode)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(FeaturePolicy)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(FeaturePolicy)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(FeaturePolicy)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
FeaturePolicy::FeaturePolicy(nsINode* aNode) : mParentNode(aNode) {}
|
|
|
|
|
|
|
|
void FeaturePolicy::InheritPolicy(FeaturePolicy* aParentPolicy) {
|
|
|
|
MOZ_ASSERT(aParentPolicy);
|
|
|
|
|
|
|
|
mInheritedDeniedFeatureNames.Clear();
|
|
|
|
|
|
|
|
RefPtr<FeaturePolicy> dest = this;
|
|
|
|
RefPtr<FeaturePolicy> src = aParentPolicy;
|
2018-10-25 20:03:38 +03:00
|
|
|
FeaturePolicyUtils::ForEachFeature([dest, src](const char* aFeatureName) {
|
2018-10-01 09:09:44 +03:00
|
|
|
nsString featureName;
|
|
|
|
featureName.AppendASCII(aFeatureName);
|
|
|
|
|
|
|
|
// If the destination has a declared feature (via the HTTP header or 'allow'
|
2018-10-25 20:03:38 +03:00
|
|
|
// attribute) we allow the feature if the destination allows it and the
|
|
|
|
// parent allows its origin or the destinations' one.
|
|
|
|
if (dest->HasDeclaredFeature(featureName) &&
|
|
|
|
dest->AllowsFeatureInternal(featureName, dest->mDefaultOrigin)) {
|
|
|
|
if (!src->AllowsFeatureInternal(featureName, src->mDefaultOrigin) &&
|
|
|
|
!src->AllowsFeatureInternal(featureName, dest->mDefaultOrigin)) {
|
2018-10-01 09:09:44 +03:00
|
|
|
dest->SetInheritedDeniedFeature(featureName);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there was not a declared feature, we allow the feature if the parent
|
|
|
|
// FeaturePolicy allows the current origin.
|
2018-10-25 20:03:38 +03:00
|
|
|
if (!src->AllowsFeatureInternal(featureName, dest->mDefaultOrigin)) {
|
2018-10-01 09:09:44 +03:00
|
|
|
dest->SetInheritedDeniedFeature(featureName);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void FeaturePolicy::SetInheritedDeniedFeature(const nsAString& aFeatureName) {
|
|
|
|
MOZ_ASSERT(!HasInheritedDeniedFeature(aFeatureName));
|
|
|
|
mInheritedDeniedFeatureNames.AppendElement(aFeatureName);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FeaturePolicy::HasInheritedDeniedFeature(
|
|
|
|
const nsAString& aFeatureName) const {
|
|
|
|
return mInheritedDeniedFeatureNames.Contains(aFeatureName);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FeaturePolicy::HasDeclaredFeature(const nsAString& aFeatureName) const {
|
|
|
|
for (const Feature& feature : mFeatures) {
|
|
|
|
if (feature.Name().Equals(aFeatureName)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
void FeaturePolicy::SetDeclaredPolicy(Document* aDocument,
|
2018-10-01 09:09:44 +03:00
|
|
|
const nsAString& aPolicyString,
|
2018-10-12 10:36:33 +03:00
|
|
|
nsIPrincipal* aSelfOrigin,
|
|
|
|
nsIPrincipal* aSrcOrigin) {
|
2018-10-01 09:09:44 +03:00
|
|
|
ResetDeclaredPolicy();
|
|
|
|
|
|
|
|
Unused << NS_WARN_IF(!FeaturePolicyParser::ParseString(
|
|
|
|
aPolicyString, aDocument, aSelfOrigin, aSrcOrigin, mFeatures));
|
|
|
|
}
|
|
|
|
|
|
|
|
void FeaturePolicy::ResetDeclaredPolicy() { mFeatures.Clear(); }
|
|
|
|
|
|
|
|
JSObject* FeaturePolicy::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
2019-07-08 03:10:56 +03:00
|
|
|
return FeaturePolicy_Binding::Wrap(aCx, this, aGivenProto);
|
2018-10-01 09:09:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FeaturePolicy::AllowsFeature(const nsAString& aFeatureName,
|
|
|
|
const Optional<nsAString>& aOrigin) const {
|
2018-10-12 10:36:33 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> origin;
|
|
|
|
if (aOrigin.WasPassed()) {
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(uri), aOrigin.Value());
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
origin = BasePrincipal::CreateCodebasePrincipal(
|
|
|
|
uri, BasePrincipal::Cast(mDefaultOrigin)->OriginAttributesRef());
|
|
|
|
} else {
|
|
|
|
origin = mDefaultOrigin;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!origin)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return AllowsFeatureInternal(aFeatureName, origin);
|
2018-10-01 09:09:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FeaturePolicy::AllowsFeatureInternal(const nsAString& aFeatureName,
|
2018-10-12 10:36:33 +03:00
|
|
|
nsIPrincipal* aOrigin) const {
|
|
|
|
MOZ_ASSERT(aOrigin);
|
|
|
|
|
2018-10-01 09:09:44 +03:00
|
|
|
// Let's see if have to disable this feature because inherited policy.
|
|
|
|
if (HasInheritedDeniedFeature(aFeatureName)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const Feature& feature : mFeatures) {
|
|
|
|
if (feature.Name().Equals(aFeatureName)) {
|
|
|
|
return feature.Allows(aOrigin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-12 10:36:33 +03:00
|
|
|
switch (FeaturePolicyUtils::DefaultAllowListFeature(aFeatureName)) {
|
|
|
|
case FeaturePolicyUtils::FeaturePolicyValue::eAll:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case FeaturePolicyUtils::FeaturePolicyValue::eSelf:
|
|
|
|
return BasePrincipal::Cast(mDefaultOrigin)
|
|
|
|
->Subsumes(aOrigin, BasePrincipal::ConsiderDocumentDomain);
|
|
|
|
|
|
|
|
case FeaturePolicyUtils::FeaturePolicyValue::eNone:
|
|
|
|
return false;
|
|
|
|
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Unknown default value");
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2018-10-01 09:09:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void FeaturePolicy::AllowedFeatures(nsTArray<nsString>& aAllowedFeatures) {
|
|
|
|
RefPtr<FeaturePolicy> self = this;
|
|
|
|
FeaturePolicyUtils::ForEachFeature(
|
|
|
|
[self, &aAllowedFeatures](const char* aFeatureName) {
|
|
|
|
nsString featureName;
|
|
|
|
featureName.AppendASCII(aFeatureName);
|
|
|
|
|
|
|
|
if (self->AllowsFeatureInternal(featureName, self->mDefaultOrigin)) {
|
|
|
|
aAllowedFeatures.AppendElement(featureName);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void FeaturePolicy::GetAllowlistForFeature(const nsAString& aFeatureName,
|
|
|
|
nsTArray<nsString>& aList) const {
|
|
|
|
if (!AllowsFeatureInternal(aFeatureName, mDefaultOrigin)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const Feature& feature : mFeatures) {
|
|
|
|
if (feature.Name().Equals(aFeatureName)) {
|
|
|
|
if (feature.AllowsAll()) {
|
|
|
|
aList.AppendElement(NS_LITERAL_STRING("*"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-12 10:36:33 +03:00
|
|
|
nsTArray<nsCOMPtr<nsIPrincipal>> list;
|
|
|
|
feature.GetAllowList(list);
|
|
|
|
|
|
|
|
for (nsIPrincipal* principal : list) {
|
|
|
|
nsAutoCString originNoSuffix;
|
|
|
|
nsresult rv = principal->GetOriginNoSuffix(originNoSuffix);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aList.AppendElement(NS_ConvertUTF8toUTF16(originNoSuffix));
|
|
|
|
}
|
2018-10-01 09:09:44 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-12 10:36:33 +03:00
|
|
|
switch (FeaturePolicyUtils::DefaultAllowListFeature(aFeatureName)) {
|
|
|
|
case FeaturePolicyUtils::FeaturePolicyValue::eAll:
|
|
|
|
aList.AppendElement(NS_LITERAL_STRING("*"));
|
|
|
|
return;
|
|
|
|
|
|
|
|
case FeaturePolicyUtils::FeaturePolicyValue::eSelf: {
|
|
|
|
nsAutoCString originNoSuffix;
|
|
|
|
nsresult rv = mDefaultOrigin->GetOriginNoSuffix(originNoSuffix);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aList.AppendElement(NS_ConvertUTF8toUTF16(originNoSuffix));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case FeaturePolicyUtils::FeaturePolicyValue::eNone:
|
|
|
|
return;
|
|
|
|
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Unknown default value");
|
2018-10-01 09:09:44 +03:00
|
|
|
}
|
|
|
|
}
|
2018-10-01 12:59:57 +03:00
|
|
|
|
|
|
|
void FeaturePolicy::MaybeSetAllowedPolicy(const nsAString& aFeatureName) {
|
|
|
|
MOZ_ASSERT(FeaturePolicyUtils::IsSupportedFeature(aFeatureName));
|
|
|
|
|
|
|
|
if (HasDeclaredFeature(aFeatureName)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Feature feature(aFeatureName);
|
|
|
|
feature.SetAllowsAll();
|
|
|
|
|
|
|
|
mFeatures.AppendElement(feature);
|
|
|
|
}
|
2018-10-12 10:36:33 +03:00
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|