2014-10-28 16:23:00 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2015-05-03 22:32:37 +03:00
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2010-10-19 20:37:03 +04:00
|
|
|
|
|
|
|
#include "ThirdPartyUtil.h"
|
2019-08-19 22:38:50 +03:00
|
|
|
#include "nsDocShell.h"
|
2019-06-28 10:48:52 +03:00
|
|
|
#include "nsGlobalWindowOuter.h"
|
2015-07-07 05:17:00 +03:00
|
|
|
#include "nsNetCID.h"
|
2010-10-19 20:37:03 +04:00
|
|
|
#include "nsNetUtil.h"
|
2015-07-07 05:17:00 +03:00
|
|
|
#include "nsIChannel.h"
|
2020-01-08 13:25:44 +03:00
|
|
|
#include "nsIClassifiedChannel.h"
|
2010-10-19 20:37:03 +04:00
|
|
|
#include "nsIHttpChannelInternal.h"
|
|
|
|
#include "nsILoadContext.h"
|
|
|
|
#include "nsIPrincipal.h"
|
|
|
|
#include "nsIScriptObjectPrincipal.h"
|
2014-10-30 03:22:22 +03:00
|
|
|
#include "nsIURI.h"
|
2019-02-08 23:01:46 +03:00
|
|
|
#include "nsReadableUtils.h"
|
2010-10-19 20:37:03 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2019-02-04 18:41:28 +03:00
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2020-03-10 02:36:39 +03:00
|
|
|
#include "mozilla/ContentBlocking.h"
|
2020-03-06 19:36:01 +03:00
|
|
|
#include "mozilla/ContentBlockingAllowList.h"
|
2019-06-17 07:36:02 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2019-02-04 18:41:28 +03:00
|
|
|
#include "mozilla/StaticPtr.h"
|
2019-09-18 11:26:34 +03:00
|
|
|
#include "mozilla/TextUtils.h"
|
2018-11-24 03:49:03 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2019-06-17 07:36:02 +03:00
|
|
|
#include "nsGlobalWindowOuter.h"
|
2010-10-19 20:37:03 +04:00
|
|
|
|
2019-08-24 01:26:51 +03:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(ThirdPartyUtil, mozIThirdPartyUtil)
|
2010-10-19 20:37:03 +04:00
|
|
|
|
2014-10-30 03:22:22 +03:00
|
|
|
//
|
2016-05-26 21:08:47 +03:00
|
|
|
// MOZ_LOG=thirdPartyUtil:5
|
2014-10-30 03:22:22 +03:00
|
|
|
//
|
2015-11-23 22:09:25 +03:00
|
|
|
static mozilla::LazyLogModule gThirdPartyLog("thirdPartyUtil");
|
2014-10-30 03:22:22 +03:00
|
|
|
#undef LOG
|
2015-06-04 01:25:57 +03:00
|
|
|
#define LOG(args) MOZ_LOG(gThirdPartyLog, mozilla::LogLevel::Debug, args)
|
2014-10-30 03:22:22 +03:00
|
|
|
|
2019-02-04 18:41:28 +03:00
|
|
|
static mozilla::StaticRefPtr<ThirdPartyUtil> gService;
|
|
|
|
|
2019-02-08 23:01:46 +03:00
|
|
|
// static
|
|
|
|
void ThirdPartyUtil::Startup() {
|
|
|
|
nsCOMPtr<mozIThirdPartyUtil> tpu;
|
|
|
|
if (NS_WARN_IF(!(tpu = do_GetService(THIRDPARTYUTIL_CONTRACTID)))) {
|
|
|
|
NS_WARNING("Failed to get third party util!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-19 20:37:03 +04:00
|
|
|
nsresult ThirdPartyUtil::Init() {
|
|
|
|
NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
2019-02-04 18:41:28 +03:00
|
|
|
MOZ_ASSERT(!gService);
|
|
|
|
gService = this;
|
|
|
|
mozilla::ClearOnShutdown(&gService);
|
|
|
|
|
2019-02-04 21:43:43 +03:00
|
|
|
mTLDService = nsEffectiveTLDService::GetInstance();
|
|
|
|
return mTLDService ? NS_OK : NS_ERROR_FAILURE;
|
2010-10-19 20:37:03 +04:00
|
|
|
}
|
|
|
|
|
2019-02-04 18:41:28 +03:00
|
|
|
ThirdPartyUtil::~ThirdPartyUtil() { gService = nullptr; }
|
|
|
|
|
|
|
|
// static
|
|
|
|
ThirdPartyUtil* ThirdPartyUtil::GetInstance() {
|
|
|
|
if (gService) {
|
|
|
|
return gService;
|
|
|
|
}
|
|
|
|
nsCOMPtr<mozIThirdPartyUtil> tpuService =
|
|
|
|
mozilla::services::GetThirdPartyUtil();
|
|
|
|
if (!tpuService) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(
|
|
|
|
gService,
|
|
|
|
"gService must have been initialized in nsEffectiveTLDService::Init");
|
|
|
|
return gService;
|
|
|
|
}
|
|
|
|
|
2010-10-19 20:37:03 +04:00
|
|
|
// Determine if aFirstDomain is a different base domain to aSecondURI; or, if
|
|
|
|
// the concept of base domain does not apply, determine if the two hosts are not
|
|
|
|
// string-identical.
|
|
|
|
nsresult ThirdPartyUtil::IsThirdPartyInternal(const nsCString& aFirstDomain,
|
|
|
|
nsIURI* aSecondURI,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool* aResult) {
|
2015-12-04 23:01:43 +03:00
|
|
|
if (!aSecondURI) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
2010-10-19 20:37:03 +04:00
|
|
|
|
2020-05-04 19:02:38 +03:00
|
|
|
// BlobURLs are always first-party.
|
|
|
|
if (aSecondURI->SchemeIs("blob")) {
|
|
|
|
*aResult = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-10-19 20:37:03 +04:00
|
|
|
// Get the base domain for aSecondURI.
|
2019-02-04 18:43:45 +03:00
|
|
|
nsAutoCString secondDomain;
|
2010-10-19 20:37:03 +04:00
|
|
|
nsresult rv = GetBaseDomain(aSecondURI, secondDomain);
|
2015-03-31 23:18:38 +03:00
|
|
|
LOG(("ThirdPartyUtil::IsThirdPartyInternal %s =? %s", aFirstDomain.get(),
|
|
|
|
secondDomain.get()));
|
2010-10-19 20:37:03 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2019-02-05 19:38:56 +03:00
|
|
|
*aResult = IsThirdPartyInternal(aFirstDomain, secondDomain);
|
2010-10-19 20:37:03 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-06-17 07:36:02 +03:00
|
|
|
nsCString ThirdPartyUtil::GetBaseDomainFromWindow(nsPIDOMWindowOuter* aWindow) {
|
|
|
|
mozilla::dom::Document* doc = aWindow ? aWindow->GetExtantDoc() : nullptr;
|
|
|
|
|
|
|
|
if (!doc) {
|
|
|
|
return EmptyCString();
|
|
|
|
}
|
|
|
|
|
|
|
|
return doc->GetBaseDomain();
|
|
|
|
}
|
|
|
|
|
2014-08-09 00:52:28 +04:00
|
|
|
NS_IMETHODIMP
|
2019-02-16 01:28:56 +03:00
|
|
|
ThirdPartyUtil::GetPrincipalFromWindow(mozIDOMWindowProxy* aWin,
|
|
|
|
nsIPrincipal** result) {
|
2010-10-19 20:37:03 +04:00
|
|
|
nsCOMPtr<nsIScriptObjectPrincipal> scriptObjPrin = do_QueryInterface(aWin);
|
2014-08-09 00:52:28 +04:00
|
|
|
if (!scriptObjPrin) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
2010-10-19 20:37:03 +04:00
|
|
|
|
2019-02-16 01:28:56 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> prin = scriptObjPrin->GetPrincipal();
|
2014-08-09 00:52:28 +04:00
|
|
|
if (!prin) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
2010-10-19 20:37:03 +04:00
|
|
|
|
2019-02-16 01:28:56 +03:00
|
|
|
prin.forget(result);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the URI associated with a window.
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ThirdPartyUtil::GetURIFromWindow(mozIDOMWindowProxy* aWin, nsIURI** result) {
|
|
|
|
nsCOMPtr<nsIPrincipal> prin;
|
|
|
|
nsresult rv = GetPrincipalFromWindow(aWin, getter_AddRefs(prin));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-10-30 03:22:22 +03:00
|
|
|
if (prin->GetIsNullPrincipal()) {
|
|
|
|
LOG(("ThirdPartyUtil::GetURIFromWindow can't use null principal\n"));
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
2020-04-13 21:50:32 +03:00
|
|
|
auto* basePrin = BasePrincipal::Cast(prin);
|
|
|
|
return basePrin->GetURI(result);
|
2010-10-19 20:37:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Determine if aFirstURI is third party with respect to aSecondURI. See docs
|
|
|
|
// for mozIThirdPartyUtil.
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ThirdPartyUtil::IsThirdPartyURI(nsIURI* aFirstURI, nsIURI* aSecondURI,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool* aResult) {
|
2010-10-19 20:37:03 +04:00
|
|
|
NS_ENSURE_ARG(aFirstURI);
|
|
|
|
NS_ENSURE_ARG(aSecondURI);
|
|
|
|
NS_ASSERTION(aResult, "null outparam pointer");
|
|
|
|
|
2019-02-04 18:43:45 +03:00
|
|
|
nsAutoCString firstHost;
|
2010-10-19 20:37:03 +04:00
|
|
|
nsresult rv = GetBaseDomain(aFirstURI, firstHost);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
return IsThirdPartyInternal(firstHost, aSecondURI, aResult);
|
|
|
|
}
|
|
|
|
|
2019-10-31 11:31:39 +03:00
|
|
|
// If the optional aURI is provided, determine whether aWindow is foreign with
|
|
|
|
// respect to aURI. If the optional aURI is not provided, determine whether the
|
|
|
|
// given "window hierarchy" is third party. See docs for mozIThirdPartyUtil.
|
2010-10-19 20:37:03 +04:00
|
|
|
NS_IMETHODIMP
|
2016-01-30 20:05:36 +03:00
|
|
|
ThirdPartyUtil::IsThirdPartyWindow(mozIDOMWindowProxy* aWindow, nsIURI* aURI,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool* aResult) {
|
2010-10-19 20:37:03 +04:00
|
|
|
NS_ENSURE_ARG(aWindow);
|
|
|
|
NS_ASSERTION(aResult, "null outparam pointer");
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool result;
|
2010-10-19 20:37:03 +04:00
|
|
|
|
2019-06-12 12:08:00 +03:00
|
|
|
// Ignore about:blank URIs here since they have no domain and attempting to
|
|
|
|
// compare against them will fail.
|
|
|
|
if (aURI && !NS_IsAboutBlank(aURI)) {
|
2019-10-02 18:10:40 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> prin;
|
|
|
|
nsresult rv = GetPrincipalFromWindow(aWindow, getter_AddRefs(prin));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
// Determine whether aURI is foreign with respect to the current principal.
|
|
|
|
rv = prin->IsThirdPartyURI(aURI, &result);
|
2019-10-24 12:19:07 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2010-10-19 20:37:03 +04:00
|
|
|
|
|
|
|
if (result) {
|
|
|
|
*aResult = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-04 23:44:03 +03:00
|
|
|
nsPIDOMWindowOuter* current = nsPIDOMWindowOuter::From(aWindow);
|
2020-06-16 22:16:58 +03:00
|
|
|
do {
|
|
|
|
// We use GetInProcessScriptableParent rather than GetParent because we
|
|
|
|
// consider <iframe mozbrowser> to be a top-level frame.
|
|
|
|
nsPIDOMWindowOuter* parent = current->GetInProcessScriptableParent();
|
|
|
|
// We don't use SameCOMIdentity here since we know that nsPIDOMWindowOuter
|
|
|
|
// is only implemented by nsGlobalWindowOuter, so different objects of that
|
|
|
|
// type will not have different nsISupports COM identities, and checking the
|
|
|
|
// actual COM identity using SameCOMIdentity is expensive due to the virtual
|
|
|
|
// calls involved.
|
|
|
|
if (parent == current) {
|
|
|
|
auto* const browsingContext = current->GetBrowsingContext();
|
|
|
|
MOZ_ASSERT(browsingContext);
|
|
|
|
|
|
|
|
// We're either at the topmost content window (i.e. no third party), or,
|
|
|
|
// with fission, we may be an out-of-process content subframe (i.e. third
|
|
|
|
// party), since GetInProcessScriptableParent above explicitly does not
|
|
|
|
// go beyond process boundaries. In either case, we already know the
|
|
|
|
// result.
|
|
|
|
*aResult = browsingContext->IsContentSubframe();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-10-19 20:37:03 +04:00
|
|
|
|
2020-06-16 22:16:58 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> currentPrincipal;
|
|
|
|
nsresult rv =
|
|
|
|
GetPrincipalFromWindow(current, getter_AddRefs(currentPrincipal));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
nsCOMPtr<nsIPrincipal> parentPrincipal;
|
|
|
|
rv = GetPrincipalFromWindow(parent, getter_AddRefs(parentPrincipal));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-10-19 20:37:03 +04:00
|
|
|
|
2020-06-16 22:16:58 +03:00
|
|
|
rv = currentPrincipal->IsThirdPartyPrincipal(parentPrincipal, &result);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result) {
|
|
|
|
*aResult = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
current = parent;
|
|
|
|
} while (1);
|
|
|
|
|
|
|
|
MOZ_ASSERT_UNREACHABLE("should've returned");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2010-10-19 20:37:03 +04:00
|
|
|
}
|
|
|
|
|
2020-04-15 21:53:06 +03:00
|
|
|
nsresult ThirdPartyUtil::IsThirdPartyGlobal(
|
|
|
|
mozilla::dom::WindowGlobalParent* aWindowGlobal, bool* aResult) {
|
|
|
|
NS_ENSURE_ARG(aWindowGlobal);
|
|
|
|
NS_ASSERTION(aResult, "null outparam pointer");
|
|
|
|
|
|
|
|
auto* currentWGP = aWindowGlobal;
|
|
|
|
do {
|
|
|
|
MOZ_ASSERT(currentWGP->BrowsingContext());
|
|
|
|
if (currentWGP->BrowsingContext()->IsTop()) {
|
|
|
|
*aResult = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIPrincipal> currentPrincipal = currentWGP->DocumentPrincipal();
|
|
|
|
RefPtr<WindowGlobalParent> parent =
|
|
|
|
currentWGP->BrowsingContext()->GetEmbedderWindowGlobal();
|
|
|
|
if (!parent) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIPrincipal> parentPrincipal = parent->DocumentPrincipal();
|
|
|
|
nsresult rv =
|
|
|
|
currentPrincipal->IsThirdPartyPrincipal(parentPrincipal, aResult);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*aResult) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
currentWGP = parent;
|
|
|
|
} while (true);
|
|
|
|
}
|
|
|
|
|
2010-10-19 20:37:03 +04:00
|
|
|
// Determine if the URI associated with aChannel or any URI of the window
|
|
|
|
// hierarchy associated with the channel is foreign with respect to aSecondURI.
|
|
|
|
// See docs for mozIThirdPartyUtil.
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ThirdPartyUtil::IsThirdPartyChannel(nsIChannel* aChannel, nsIURI* aURI,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool* aResult) {
|
2015-03-31 23:18:38 +03:00
|
|
|
LOG(("ThirdPartyUtil::IsThirdPartyChannel [channel=%p]", aChannel));
|
2010-10-19 20:37:03 +04:00
|
|
|
NS_ENSURE_ARG(aChannel);
|
|
|
|
NS_ASSERTION(aResult, "null outparam pointer");
|
|
|
|
|
|
|
|
nsresult rv;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool doForce = false;
|
2010-10-19 20:37:03 +04:00
|
|
|
nsCOMPtr<nsIHttpChannelInternal> httpChannelInternal =
|
|
|
|
do_QueryInterface(aChannel);
|
|
|
|
if (httpChannelInternal) {
|
2018-11-24 03:49:03 +03:00
|
|
|
uint32_t flags = 0;
|
|
|
|
// Avoid checking the return value here since some channel implementations
|
|
|
|
// may return NS_ERROR_NOT_IMPLEMENTED.
|
|
|
|
mozilla::Unused << httpChannelInternal->GetThirdPartyFlags(&flags);
|
2010-10-19 20:37:03 +04:00
|
|
|
|
2014-10-28 16:23:00 +03:00
|
|
|
doForce = (flags & nsIHttpChannelInternal::THIRD_PARTY_FORCE_ALLOW);
|
|
|
|
|
2010-10-19 20:37:03 +04:00
|
|
|
// If aURI was not supplied, and we're forcing, then we're by definition
|
|
|
|
// not foreign. If aURI was supplied, we still want to check whether it's
|
|
|
|
// foreign with respect to the channel URI. (The forcing only applies to
|
|
|
|
// whatever window hierarchy exists above the channel.)
|
|
|
|
if (doForce && !aURI) {
|
|
|
|
*aResult = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-01 00:25:29 +03:00
|
|
|
bool parentIsThird = false;
|
|
|
|
|
2010-10-19 20:37:03 +04:00
|
|
|
// Obtain the URI from the channel, and its base domain.
|
|
|
|
nsCOMPtr<nsIURI> channelURI;
|
2015-12-01 00:25:29 +03:00
|
|
|
rv = NS_GetFinalChannelURI(aChannel, getter_AddRefs(channelURI));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2010-10-19 20:37:03 +04:00
|
|
|
|
2019-02-04 18:43:45 +03:00
|
|
|
nsAutoCString channelDomain;
|
2010-10-19 20:37:03 +04:00
|
|
|
rv = GetBaseDomain(channelURI, channelDomain);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2015-12-01 00:25:29 +03:00
|
|
|
if (!doForce) {
|
2020-05-02 00:35:59 +03:00
|
|
|
nsCOMPtr<nsILoadInfo> loadInfo = aChannel->LoadInfo();
|
|
|
|
parentIsThird = loadInfo->GetIsInThirdPartyContext();
|
|
|
|
BasePrincipal* loadingPrincipal =
|
|
|
|
BasePrincipal::Cast(loadInfo->GetLoadingPrincipal());
|
|
|
|
if (!parentIsThird &&
|
|
|
|
loadInfo->GetExternalContentPolicyType() !=
|
|
|
|
nsIContentPolicy::TYPE_DOCUMENT &&
|
|
|
|
(!loadingPrincipal->AddonPolicy() ||
|
|
|
|
!loadingPrincipal->AddonAllowsLoad(channelURI))) {
|
|
|
|
// Check if the channel itself is third-party to its own requestor.
|
|
|
|
// Unforunately, we have to go through the loading principal.
|
|
|
|
|
|
|
|
rv = loadingPrincipal->IsThirdPartyURI(channelURI, &parentIsThird);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
2015-12-01 00:25:29 +03:00
|
|
|
}
|
2010-10-19 20:37:03 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-01 00:25:29 +03:00
|
|
|
// If we're not comparing to a URI, we have our answer. Otherwise, if
|
|
|
|
// parentIsThird, we're not forcing and we know that we're a third-party
|
|
|
|
// request.
|
|
|
|
if (!aURI || parentIsThird) {
|
2014-10-28 16:23:00 +03:00
|
|
|
*aResult = parentIsThird;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-12-01 00:25:29 +03:00
|
|
|
// Determine whether aURI is foreign with respect to channelURI.
|
|
|
|
return IsThirdPartyInternal(channelDomain, aURI, aResult);
|
2010-10-19 20:37:03 +04:00
|
|
|
}
|
|
|
|
|
2014-08-09 00:52:28 +04:00
|
|
|
NS_IMETHODIMP
|
2016-01-30 20:05:36 +03:00
|
|
|
ThirdPartyUtil::GetTopWindowForChannel(nsIChannel* aChannel,
|
2019-01-23 17:55:49 +03:00
|
|
|
nsIURI* aURIBeingLoaded,
|
2016-01-30 20:05:36 +03:00
|
|
|
mozIDOMWindowProxy** aWin) {
|
2014-08-09 00:52:28 +04:00
|
|
|
NS_ENSURE_ARG(aWin);
|
|
|
|
|
|
|
|
// Find the associated window and its parent window.
|
|
|
|
nsCOMPtr<nsILoadContext> ctx;
|
|
|
|
NS_QueryNotificationCallbacks(aChannel, ctx);
|
|
|
|
if (!ctx) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<mozIDOMWindowProxy> window;
|
2015-10-27 00:37:32 +03:00
|
|
|
ctx->GetAssociatedWindow(getter_AddRefs(window));
|
2016-01-30 20:05:36 +03:00
|
|
|
if (!window) {
|
2014-08-09 00:52:28 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
2016-01-30 20:05:36 +03:00
|
|
|
|
2019-01-23 17:55:49 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowOuter> top =
|
|
|
|
nsGlobalWindowOuter::Cast(window)
|
|
|
|
->GetTopExcludingExtensionAccessibleContentFrames(aURIBeingLoaded);
|
2015-10-27 00:37:32 +03:00
|
|
|
top.forget(aWin);
|
|
|
|
return NS_OK;
|
2014-08-09 00:52:28 +04:00
|
|
|
}
|
|
|
|
|
2011-10-14 01:45:22 +04:00
|
|
|
// Get the base domain for aHostURI; e.g. for "www.bbc.co.uk", this would be
|
|
|
|
// "bbc.co.uk". Only properly-formed URI's are tolerated, though a trailing
|
|
|
|
// dot may be present. If aHostURI is an IP address, an alias such as
|
|
|
|
// 'localhost', an eTLD such as 'co.uk', or the empty string, aBaseDomain will
|
|
|
|
// be the exact host. The result of this function should only be used in exact
|
|
|
|
// string comparisons, since substring comparisons will not be valid for the
|
|
|
|
// special cases elided above.
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ThirdPartyUtil::GetBaseDomain(nsIURI* aHostURI, nsACString& aBaseDomain) {
|
2015-07-23 21:02:45 +03:00
|
|
|
if (!aHostURI) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2011-10-14 01:45:22 +04:00
|
|
|
// Get the base domain. this will fail if the host contains a leading dot,
|
|
|
|
// more than one trailing dot, or is otherwise malformed.
|
|
|
|
nsresult rv = mTLDService->GetBaseDomain(aHostURI, 0, aBaseDomain);
|
|
|
|
if (rv == NS_ERROR_HOST_IS_IP_ADDRESS ||
|
|
|
|
rv == NS_ERROR_INSUFFICIENT_DOMAIN_LEVELS) {
|
|
|
|
// aHostURI is either an IP address, an alias such as 'localhost', an eTLD
|
|
|
|
// such as 'co.uk', or the empty string. Uses the normalized host in such
|
|
|
|
// cases.
|
|
|
|
rv = aHostURI->GetAsciiHost(aBaseDomain);
|
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// aHostURI (and thus aBaseDomain) may be the string '.'. If so, fail.
|
|
|
|
if (aBaseDomain.Length() == 1 && aBaseDomain.Last() == '.')
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
|
|
|
// Reject any URIs without a host that aren't file:// URIs. This makes it the
|
|
|
|
// only way we can get a base domain consisting of the empty string, which
|
|
|
|
// means we can safely perform foreign tests on such URIs where "not foreign"
|
|
|
|
// means "the involved URIs are all file://".
|
2019-07-30 20:51:37 +03:00
|
|
|
if (aBaseDomain.IsEmpty() && !aHostURI->SchemeIs("file")) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
2011-10-14 01:45:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2019-02-08 23:01:46 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ThirdPartyUtil::GetBaseDomainFromSchemeHost(const nsACString& aScheme,
|
|
|
|
const nsACString& aAsciiHost,
|
|
|
|
nsACString& aBaseDomain) {
|
2019-09-18 11:26:34 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(IsAscii(aAsciiHost));
|
2019-02-08 23:01:46 +03:00
|
|
|
|
|
|
|
// Get the base domain. this will fail if the host contains a leading dot,
|
|
|
|
// more than one trailing dot, or is otherwise malformed.
|
|
|
|
nsresult rv = mTLDService->GetBaseDomainFromHost(aAsciiHost, 0, aBaseDomain);
|
|
|
|
if (rv == NS_ERROR_HOST_IS_IP_ADDRESS ||
|
|
|
|
rv == NS_ERROR_INSUFFICIENT_DOMAIN_LEVELS) {
|
|
|
|
// aMozURL is either an IP address, an alias such as 'localhost', an eTLD
|
|
|
|
// such as 'co.uk', or the empty string. Uses the normalized host in such
|
|
|
|
// cases.
|
|
|
|
aBaseDomain = aAsciiHost;
|
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// aMozURL (and thus aBaseDomain) may be the string '.'. If so, fail.
|
|
|
|
if (aBaseDomain.Length() == 1 && aBaseDomain.Last() == '.')
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
|
|
|
// Reject any URLs without a host that aren't file:// URLs. This makes it the
|
|
|
|
// only way we can get a base domain consisting of the empty string, which
|
|
|
|
// means we can safely perform foreign tests on such URLs where "not foreign"
|
|
|
|
// means "the involved URLs are all file://".
|
|
|
|
if (aBaseDomain.IsEmpty() && !aScheme.EqualsLiteral("file")) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2020-01-08 13:25:44 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP_(ThirdPartyAnalysisResult)
|
|
|
|
ThirdPartyUtil::AnalyzeChannel(nsIChannel* aChannel, bool aNotify, nsIURI* aURI,
|
|
|
|
RequireThirdPartyCheck aRequireThirdPartyCheck,
|
|
|
|
uint32_t* aRejectedReason) {
|
|
|
|
MOZ_ASSERT_IF(aNotify, aRejectedReason);
|
|
|
|
|
|
|
|
ThirdPartyAnalysisResult result;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
if (!aURI && aChannel) {
|
|
|
|
aChannel->GetURI(getter_AddRefs(uri));
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsILoadInfo> loadInfo = aChannel ? aChannel->LoadInfo() : nullptr;
|
|
|
|
|
|
|
|
bool isForeign = true;
|
|
|
|
if (aChannel &&
|
|
|
|
(!aRequireThirdPartyCheck || aRequireThirdPartyCheck(loadInfo))) {
|
|
|
|
IsThirdPartyChannel(aChannel, aURI ? aURI : uri.get(), &isForeign);
|
|
|
|
}
|
|
|
|
if (isForeign) {
|
|
|
|
result += ThirdPartyAnalysis::IsForeign;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIClassifiedChannel> classifiedChannel =
|
|
|
|
do_QueryInterface(aChannel);
|
|
|
|
if (classifiedChannel) {
|
2020-02-20 10:27:24 +03:00
|
|
|
if (classifiedChannel->IsThirdPartyTrackingResource()) {
|
|
|
|
result += ThirdPartyAnalysis::IsThirdPartyTrackingResource;
|
2020-01-08 13:25:44 +03:00
|
|
|
}
|
2020-02-20 10:27:24 +03:00
|
|
|
if (classifiedChannel->IsThirdPartySocialTrackingResource()) {
|
|
|
|
result += ThirdPartyAnalysis::IsThirdPartySocialTrackingResource;
|
2020-01-08 13:25:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check first-party storage access even for non-tracking resources, since
|
|
|
|
// we will need the result when computing the access rights for the reject
|
|
|
|
// foreign cookie behavior mode.
|
|
|
|
|
|
|
|
// If the caller has requested third-party checks, we will only perform the
|
|
|
|
// storage access check once we know we're in the third-party context.
|
|
|
|
bool performStorageChecks =
|
|
|
|
aRequireThirdPartyCheck ? result.contains(ThirdPartyAnalysis::IsForeign)
|
|
|
|
: true;
|
|
|
|
if (performStorageChecks &&
|
2020-03-10 02:36:39 +03:00
|
|
|
ContentBlocking::ShouldAllowAccessFor(aChannel, aURI ? aURI : uri.get(),
|
|
|
|
aRejectedReason)) {
|
2020-06-03 09:12:06 +03:00
|
|
|
result += ThirdPartyAnalysis::IsStorageAccessPermissionGranted;
|
2020-01-08 13:25:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aNotify && !result.contains(
|
2020-06-03 09:12:06 +03:00
|
|
|
ThirdPartyAnalysis::IsStorageAccessPermissionGranted)) {
|
2020-03-10 02:36:37 +03:00
|
|
|
ContentBlockingNotifier::OnDecision(
|
|
|
|
aChannel, ContentBlockingNotifier::BlockingDecision::eBlock,
|
2020-01-08 13:25:44 +03:00
|
|
|
*aRejectedReason);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|