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-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-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)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
PermissionDelegateHandler::PermissionDelegateHandler(dom::Document* aDocument)
|
|
|
|
: mDocument(aDocument) {
|
|
|
|
MOZ_ASSERT(aDocument);
|
|
|
|
}
|
|
|
|
|
|
|
|
const DelegateInfo* PermissionDelegateHandler::GetPermissionDelegateInfo(
|
|
|
|
const nsAString& aPermissionName) const {
|
|
|
|
nsAutoString lowerContent(aPermissionName);
|
|
|
|
ToLowerCase(lowerContent);
|
|
|
|
|
|
|
|
for (const auto& perm : sPermissionsMap) {
|
|
|
|
if (lowerContent.EqualsASCII(perm.mPermissionName)) {
|
|
|
|
return &perm;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PermissionDelegateHandler::HasPermissionDelegated(
|
|
|
|
const nsACString& aType) {
|
|
|
|
MOZ_ASSERT(mDocument);
|
|
|
|
|
|
|
|
if (!StaticPrefs::permissions_delegation_enable()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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));
|
|
|
|
|
|
|
|
// If the type is not in the supported list, auto denied
|
|
|
|
if (!info) {
|
2019-11-07 17:29:02 +03:00
|
|
|
return false;
|
2019-09-21 11:38:26 +03:00
|
|
|
}
|
|
|
|
|
2019-11-07 17:29:02 +03:00
|
|
|
if (info->mPolicy == DelegatePolicy::eDelegateUseFeaturePolicy &&
|
|
|
|
info->mFeatureName) {
|
|
|
|
nsAutoString featureName(info->mFeatureName);
|
|
|
|
// Default allowlist for a feature used in permissions delegate should be
|
|
|
|
// set to eSelf, to ensure that permission is denied by default and only
|
|
|
|
// have the opportunity to request permission with allow attribute.
|
|
|
|
if (!FeaturePolicyUtils::IsFeatureAllowed(mDocument, featureName)) {
|
|
|
|
return false;
|
|
|
|
}
|
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-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-11-07 17:29:02 +03:00
|
|
|
if (!StaticPrefs::permissions_delegation_enable()) {
|
|
|
|
return (mPermissionManager->*testPermission)(mPrincipal, aType,
|
|
|
|
aPermission);
|
2019-09-21 11:38:26 +03:00
|
|
|
}
|
|
|
|
|
2019-11-07 17:29:02 +03:00
|
|
|
const DelegateInfo* info =
|
|
|
|
GetPermissionDelegateInfo(NS_ConvertUTF8toUTF16(aType));
|
|
|
|
|
|
|
|
// If the type is not in the supported list, auto denied
|
|
|
|
if (!info) {
|
2019-09-21 11:38:26 +03:00
|
|
|
*aPermission = nsIPermissionManager::DENY_ACTION;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info->mPolicy == DelegatePolicy::eDelegateUseFeaturePolicy &&
|
|
|
|
info->mFeatureName) {
|
|
|
|
nsAutoString featureName(info->mFeatureName);
|
|
|
|
// Default allowlist for a feature used in permissions delegate should be
|
|
|
|
// set to eSelf, to ensure that permission is denied by default and only
|
|
|
|
// have the opportunity to request permission with allow attribute.
|
|
|
|
if (!FeaturePolicyUtils::IsFeatureAllowed(mDocument, featureName)) {
|
|
|
|
*aPermission = nsIPermissionManager::DENY_ACTION;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|