2019-09-21 11:38:26 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; 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 "nsGlobalWindowInner.h"
|
|
|
|
#include "PermissionDelegateHandler.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
2019-11-07 17:29:02 +03:00
|
|
|
#include "nsPermissionManager.h"
|
2019-09-21 11:38:26 +03:00
|
|
|
#include "nsIPrincipal.h"
|
2019-11-07 17:29:02 +03:00
|
|
|
#include "nsContentPermissionHelper.h"
|
2019-09-21 11:38:26 +03:00
|
|
|
|
2019-12-05 07:44:32 +03:00
|
|
|
#include "mozilla/BasePrincipal.h"
|
2019-11-07 17:29:02 +03:00
|
|
|
#include "mozilla/StaticPrefs_permissions.h"
|
2019-09-21 11:38:26 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
|
|
|
#include "mozilla/dom/FeaturePolicyUtils.h"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
typedef PermissionDelegateHandler::PermissionDelegatePolicy DelegatePolicy;
|
|
|
|
typedef PermissionDelegateHandler::PermissionDelegateInfo DelegateInfo;
|
|
|
|
|
|
|
|
// Particular type of permissions to care about. We decide cases by case and
|
|
|
|
// give various types of controls over each of these.
|
|
|
|
static const DelegateInfo sPermissionsMap[] = {
|
|
|
|
// Permissions API map
|
|
|
|
{"geo", u"geolocation", DelegatePolicy::eDelegateUseFeaturePolicy},
|
2019-11-07 17:29:02 +03:00
|
|
|
// The same with geo, but we support both to save some conversions between
|
|
|
|
// "geo" and "geolocation"
|
|
|
|
{"geolocation", u"geolocation", DelegatePolicy::eDelegateUseFeaturePolicy},
|
2019-09-21 11:38:26 +03:00
|
|
|
{"desktop-notification", nullptr,
|
|
|
|
DelegatePolicy::ePersistDeniedCrossOrigin},
|
2019-11-07 17:29:02 +03:00
|
|
|
{"persistent-storage", nullptr, DelegatePolicy::ePersistDeniedCrossOrigin},
|
2019-11-12 11:42:02 +03:00
|
|
|
{"vibration", nullptr, DelegatePolicy::ePersistDeniedCrossOrigin},
|
2019-12-04 18:39:03 +03:00
|
|
|
{"midi", nullptr, DelegatePolicy::eDelegateUseIframeOrigin},
|
|
|
|
{"storage-access", nullptr, DelegatePolicy::eDelegateUseIframeOrigin},
|
|
|
|
{"camera", u"camera", DelegatePolicy::eDelegateUseFeaturePolicy},
|
|
|
|
{"microphone", u"microphone", DelegatePolicy::eDelegateUseFeaturePolicy},
|
|
|
|
{"screen", u"display-capture", DelegatePolicy::eDelegateUseFeaturePolicy},
|
2019-12-12 06:10:51 +03:00
|
|
|
{"xr", nullptr, DelegatePolicy::ePersistDeniedCrossOrigin},
|
2019-09-21 11:38:26 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION(PermissionDelegateHandler)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(PermissionDelegateHandler)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(PermissionDelegateHandler)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PermissionDelegateHandler)
|
2019-12-04 18:39:26 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIPermissionDelegateHandler)
|
2019-09-21 11:38:26 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
PermissionDelegateHandler::PermissionDelegateHandler(dom::Document* aDocument)
|
|
|
|
: mDocument(aDocument) {
|
|
|
|
MOZ_ASSERT(aDocument);
|
|
|
|
}
|
|
|
|
|
2019-12-04 18:39:03 +03:00
|
|
|
/* static */
|
2019-09-21 11:38:26 +03:00
|
|
|
const DelegateInfo* PermissionDelegateHandler::GetPermissionDelegateInfo(
|
2019-12-04 18:39:03 +03:00
|
|
|
const nsAString& aPermissionName) {
|
2019-09-21 11:38:26 +03:00
|
|
|
nsAutoString lowerContent(aPermissionName);
|
|
|
|
ToLowerCase(lowerContent);
|
|
|
|
|
|
|
|
for (const auto& perm : sPermissionsMap) {
|
|
|
|
if (lowerContent.EqualsASCII(perm.mPermissionName)) {
|
|
|
|
return &perm;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-12-04 18:39:26 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
PermissionDelegateHandler::MaybeUnsafePermissionDelegate(
|
|
|
|
const nsTArray<nsCString>& aTypes, bool* aMaybeUnsafe) {
|
|
|
|
*aMaybeUnsafe = false;
|
|
|
|
if (!StaticPrefs::permissions_delegation_enabled()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& type : aTypes) {
|
|
|
|
const DelegateInfo* info =
|
|
|
|
GetPermissionDelegateInfo(NS_ConvertUTF8toUTF16(type));
|
|
|
|
if (!info) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString featureName(info->mFeatureName);
|
|
|
|
if (FeaturePolicyUtils::IsFeatureUnsafeAllowedAll(mDocument, featureName)) {
|
|
|
|
*aMaybeUnsafe = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-01-06 18:28:12 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
PermissionDelegateHandler::GetPermissionDelegateFPEnabled(bool* aEnabled) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
*aEnabled = StaticPrefs::permissions_delegation_enabled() &&
|
|
|
|
StaticPrefs::dom_security_featurePolicy_enabled();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-12-04 18:39:03 +03:00
|
|
|
/* static */
|
|
|
|
nsresult PermissionDelegateHandler::GetDelegatePrincipal(
|
|
|
|
const nsACString& aType, nsIContentPermissionRequest* aRequest,
|
|
|
|
nsIPrincipal** aResult) {
|
|
|
|
MOZ_ASSERT(aRequest);
|
|
|
|
|
|
|
|
if (!StaticPrefs::permissions_delegation_enabled()) {
|
|
|
|
return aRequest->GetPrincipal(aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
const DelegateInfo* info =
|
|
|
|
GetPermissionDelegateInfo(NS_ConvertUTF8toUTF16(aType));
|
|
|
|
if (!info) {
|
|
|
|
*aResult = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info->mPolicy == DelegatePolicy::eDelegateUseTopOrigin ||
|
|
|
|
(info->mPolicy == DelegatePolicy::eDelegateUseFeaturePolicy &&
|
|
|
|
StaticPrefs::dom_security_featurePolicy_enabled())) {
|
|
|
|
return aRequest->GetTopLevelPrincipal(aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
return aRequest->GetPrincipal(aResult);
|
|
|
|
}
|
|
|
|
|
2019-11-07 17:29:02 +03:00
|
|
|
bool PermissionDelegateHandler::Initialize() {
|
2019-09-21 11:38:26 +03:00
|
|
|
MOZ_ASSERT(mDocument);
|
|
|
|
|
2019-11-07 17:29:02 +03:00
|
|
|
mPermissionManager = nsPermissionManager::GetInstance();
|
|
|
|
if (!mPermissionManager) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mPrincipal = mDocument->NodePrincipal();
|
|
|
|
nsPIDOMWindowInner* window = mDocument->GetInnerWindow();
|
|
|
|
nsGlobalWindowInner* innerWindow = nsGlobalWindowInner::Cast(window);
|
|
|
|
if (innerWindow) {
|
|
|
|
mTopLevelPrincipal = innerWindow->GetTopLevelAntiTrackingPrincipal();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool IsTopWindowContent(Document* aDocument) {
|
|
|
|
MOZ_ASSERT(aDocument);
|
|
|
|
|
|
|
|
BrowsingContext* browsingContext = aDocument->GetBrowsingContext();
|
|
|
|
return browsingContext && browsingContext->IsTopContent();
|
|
|
|
}
|
|
|
|
|
2019-12-04 18:39:03 +03:00
|
|
|
bool PermissionDelegateHandler::HasFeaturePolicyAllowed(
|
|
|
|
const DelegateInfo* info) const {
|
|
|
|
if (info->mPolicy != DelegatePolicy::eDelegateUseFeaturePolicy ||
|
|
|
|
!info->mFeatureName) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString featureName(info->mFeatureName);
|
|
|
|
return FeaturePolicyUtils::IsFeatureAllowed(mDocument, featureName);
|
|
|
|
}
|
|
|
|
|
2019-11-07 17:29:02 +03:00
|
|
|
bool PermissionDelegateHandler::HasPermissionDelegated(
|
|
|
|
const nsACString& aType) {
|
|
|
|
MOZ_ASSERT(mDocument);
|
|
|
|
|
|
|
|
// System principal should have right to make permission request
|
|
|
|
if (mPrincipal->IsSystemPrincipal()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-09-21 11:38:26 +03:00
|
|
|
const DelegateInfo* info =
|
|
|
|
GetPermissionDelegateInfo(NS_ConvertUTF8toUTF16(aType));
|
2019-12-04 18:39:03 +03:00
|
|
|
if (!info || !HasFeaturePolicyAllowed(info)) {
|
2019-11-07 17:29:02 +03:00
|
|
|
return false;
|
2019-09-21 11:38:26 +03:00
|
|
|
}
|
|
|
|
|
2019-12-04 18:39:03 +03:00
|
|
|
if (!StaticPrefs::permissions_delegation_enabled()) {
|
|
|
|
return true;
|
2019-09-21 11:38:26 +03:00
|
|
|
}
|
|
|
|
|
2019-11-07 17:29:02 +03:00
|
|
|
if (info->mPolicy == DelegatePolicy::ePersistDeniedCrossOrigin &&
|
|
|
|
!IsTopWindowContent(mDocument) &&
|
|
|
|
!mPrincipal->Subsumes(mTopLevelPrincipal)) {
|
|
|
|
return false;
|
2019-09-21 11:38:26 +03:00
|
|
|
}
|
|
|
|
|
2019-11-07 17:29:02 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult PermissionDelegateHandler::GetPermission(const nsACString& aType,
|
|
|
|
uint32_t* aPermission,
|
|
|
|
bool aExactHostMatch) {
|
|
|
|
MOZ_ASSERT(mDocument);
|
|
|
|
|
|
|
|
if (mPrincipal->IsSystemPrincipal()) {
|
|
|
|
*aPermission = nsIPermissionManager::ALLOW_ACTION;
|
|
|
|
return NS_OK;
|
2019-11-07 15:17:02 +03:00
|
|
|
}
|
2019-09-21 11:38:26 +03:00
|
|
|
|
2019-12-04 18:39:03 +03:00
|
|
|
const DelegateInfo* info =
|
|
|
|
GetPermissionDelegateInfo(NS_ConvertUTF8toUTF16(aType));
|
|
|
|
if (!info || !HasFeaturePolicyAllowed(info)) {
|
|
|
|
*aPermission = nsIPermissionManager::DENY_ACTION;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-11-07 17:29:02 +03:00
|
|
|
nsresult (NS_STDCALL nsIPermissionManager::*testPermission)(
|
|
|
|
nsIPrincipal*, const nsACString&, uint32_t*) =
|
|
|
|
aExactHostMatch ? &nsIPermissionManager::TestExactPermissionFromPrincipal
|
|
|
|
: &nsIPermissionManager::TestPermissionFromPrincipal;
|
2019-09-21 11:38:26 +03:00
|
|
|
|
2019-12-04 18:39:03 +03:00
|
|
|
if (!StaticPrefs::permissions_delegation_enabled()) {
|
2019-11-07 17:29:02 +03:00
|
|
|
return (mPermissionManager->*testPermission)(mPrincipal, aType,
|
|
|
|
aPermission);
|
2019-09-21 11:38:26 +03:00
|
|
|
}
|
|
|
|
|
2019-11-07 17:29:02 +03:00
|
|
|
if (info->mPolicy == DelegatePolicy::ePersistDeniedCrossOrigin &&
|
|
|
|
!IsTopWindowContent(mDocument) &&
|
|
|
|
!mPrincipal->Subsumes(mTopLevelPrincipal)) {
|
|
|
|
*aPermission = nsIPermissionManager::DENY_ACTION;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIPrincipal* principal = mPrincipal;
|
|
|
|
if (mTopLevelPrincipal &&
|
|
|
|
(info->mPolicy == DelegatePolicy::eDelegateUseTopOrigin ||
|
|
|
|
(info->mPolicy == DelegatePolicy::eDelegateUseFeaturePolicy &&
|
|
|
|
StaticPrefs::dom_security_featurePolicy_enabled()))) {
|
|
|
|
principal = mTopLevelPrincipal;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (mPermissionManager->*testPermission)(principal, aType, aPermission);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult PermissionDelegateHandler::GetPermissionForPermissionsAPI(
|
|
|
|
const nsACString& aType, uint32_t* aPermission) {
|
|
|
|
return GetPermission(aType, aPermission, false);
|
2019-09-21 11:38:26 +03:00
|
|
|
}
|