Bug 1633851 - Part 4: Introduce AntiTrakcingUtils::IsThirdPartyChannel, AntiTrackingUtils::IsThirdPartyWindow(). r=baku
authorTim Huang <tihuang@mozilla.com>
Wed, 13 May 2020 13:39:09 +0000
changeset 529613 9b070149d2eea1ee0d53ab35f4f3e8d03814cdb5
parent 529612 46073712f17e191bf1c34f0d394e15546a8a57e4
child 529614 ecbd52188ae8f8dfb9a7d1aa3a5f564dc9ec6014
push id37412
push usercbrindusan@mozilla.com
push dateWed, 13 May 2020 21:46:27 +0000
treeherdermozilla-central@ea102a496187 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbaku
bugs1633851
milestone78.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1633851 - Part 4: Introduce AntiTrakcingUtils::IsThirdPartyChannel, AntiTrackingUtils::IsThirdPartyWindow(). r=baku This patch introduces two APIs that are used to determined third-partyness of a channel and a window. Differential Revision: https://phabricator.services.mozilla.com/D73202
toolkit/components/antitracking/AntiTrackingUtils.cpp
toolkit/components/antitracking/AntiTrackingUtils.h
--- a/toolkit/components/antitracking/AntiTrackingUtils.cpp
+++ b/toolkit/components/antitracking/AntiTrackingUtils.cpp
@@ -10,16 +10,17 @@
 #include "mozilla/dom/BrowsingContext.h"
 #include "mozilla/dom/CanonicalBrowsingContext.h"
 #include "mozilla/net/CookieJarSettings.h"
 #include "mozilla/dom/Document.h"
 #include "mozilla/dom/WindowGlobalParent.h"
 #include "mozilla/dom/WindowContext.h"
 #include "mozilla/net/NeckoChannelParams.h"
 #include "mozilla/PermissionManager.h"
+#include "mozIThirdPartyUtil.h"
 #include "nsIChannel.h"
 #include "nsIPermission.h"
 #include "nsIURI.h"
 #include "nsNetUtil.h"
 #include "nsPIDOMWindow.h"
 #include "nsSandboxFlags.h"
 #include "nsScriptSecurityManager.h"
 
@@ -561,8 +562,50 @@ void AntiTrackingUtils::ComputeIsThirdPa
   if (topWindowPrincipal && !topWindowPrincipal->GetIsNullPrincipal()) {
     auto* basePrin = BasePrincipal::Cast(topWindowPrincipal);
     bool isThirdParty = true;
     basePrin->IsThirdPartyURI(uri, &isThirdParty);
 
     loadInfo->SetIsThirdPartyContextToTopWindow(isThirdParty);
   }
 }
+
+/* static */
+bool AntiTrackingUtils::IsThirdPartyChannel(nsIChannel* aChannel) {
+  MOZ_ASSERT(aChannel);
+
+  // We assume that the channel is a third-party by default.
+  bool thirdParty = true;
+
+  nsCOMPtr<mozIThirdPartyUtil> thirdPartyUtil = services::GetThirdPartyUtil();
+  if (!thirdPartyUtil) {
+    return thirdParty;
+  }
+
+  // Check that if the channel is a third-party to its parent.
+  nsresult rv =
+      thirdPartyUtil->IsThirdPartyChannel(aChannel, nullptr, &thirdParty);
+  if (NS_FAILED(rv)) {
+    // Assume third-party in case of failure
+    thirdParty = true;
+  }
+
+  // We check the top-level window to prevent the top-level navigation from
+  // being detected as third-party.
+  nsCOMPtr<nsILoadInfo> loadInfo = aChannel->LoadInfo();
+  return thirdParty && loadInfo->GetIsThirdPartyContextToTopWindow();
+}
+
+/* static */
+bool AntiTrackingUtils::IsThirdPartyWindow(nsPIDOMWindowInner* aWindow,
+                                           nsIURI* aURI) {
+  MOZ_ASSERT(aWindow);
+  MOZ_ASSERT(aURI);
+
+  // We assume that the window is foreign to the URI by default.
+  bool thirdParty = true;
+
+  nsCOMPtr<mozIThirdPartyUtil> thirdPartyUtil = services::GetThirdPartyUtil();
+  Unused << thirdPartyUtil->IsThirdPartyWindow(aWindow->GetOuterWindow(), aURI,
+                                               &thirdParty);
+
+  return thirdParty;
+}
--- a/toolkit/components/antitracking/AntiTrackingUtils.h
+++ b/toolkit/components/antitracking/AntiTrackingUtils.h
@@ -100,13 +100,24 @@ class AntiTrackingUtils final {
   // nsGlobalWindowOuter::GetTopExcludingExtensionAccessibleContentFrames
   static already_AddRefed<dom::WindowGlobalParent>
   GetTopWindowExcludingExtensionAccessibleContentFrames(
       dom::CanonicalBrowsingContext* aBrowsingContext, nsIURI* aURIBeingLoaded);
 
   // Given a channel, compute and set the IsThirdPartyContextToTopWindow for
   // this channel. This function is supposed to be called in the parent process.
   static void ComputeIsThirdPartyToTopWindow(nsIChannel* aChannel);
+
+  // Given a channel, this function determines if this channel is a third party.
+  // Note that this function also considers the top-level window. The channel
+  // will be considered as a third party only when it's a third party to both
+  // its parent and the top-level window.
+  static bool IsThirdPartyChannel(nsIChannel* aChannel);
+
+  // Given a window and a URI, this function first determines if the window is
+  // third-party with respect to the URI. The function returns if it's true.
+  // Otherwise, it will continue to check if the window is third-party.
+  static bool IsThirdPartyWindow(nsPIDOMWindowInner* aWindow, nsIURI* aURI);
 };
 
 }  // namespace mozilla
 
 #endif  // mozilla_antitrackingutils_h