Bug 1508044 - Part 4: Make nsChannelClassifier use nsContentUtils::IsThirdPartyWindowOrChannel() in order to avoid having duplicated logic r=francois
authorEhsan Akhgari <ehsan@mozilla.com>
Tue, 27 Nov 2018 00:46:50 +0000
changeset 507395 e3a074ae3f0c18a5329c23099c5169f135885852
parent 507394 691286d50c87eb419c121c88836a67a8227e611b
child 507396 63cbd79e897de14cb164992d37b15700fec420f0
push id1905
push userffxbld-merge
push dateMon, 21 Jan 2019 12:33:13 +0000
treeherdermozilla-release@c2fca1944d8c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfrancois
bugs1508044
milestone65.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 1508044 - Part 4: Make nsChannelClassifier use nsContentUtils::IsThirdPartyWindowOrChannel() in order to avoid having duplicated logic r=francois Depends on D12853 Differential Revision: https://phabricator.services.mozilla.com/D12854
netwerk/base/nsChannelClassifier.cpp
--- a/netwerk/base/nsChannelClassifier.cpp
+++ b/netwerk/base/nsChannelClassifier.cpp
@@ -273,87 +273,16 @@ CachedPrefs::~CachedPrefs()
   Preferences::UnregisterCallback(CachedPrefs::OnPrefsChange, URLCLASSIFIER_ANNOTATION_WHITELIST_TEST_ENTRIES, this);
   Preferences::UnregisterCallback(CachedPrefs::OnPrefsChange, URLCLASSIFIER_TRACKING_WHITELIST, this);
   Preferences::UnregisterCallback(CachedPrefs::OnPrefsChange, URLCLASSIFIER_TRACKING_WHITELIST_TEST_ENTRIES, this);
   Preferences::UnregisterCallback(CachedPrefs::OnPrefsChange, URLCLASSIFIER_TRACKING_TABLE, this);
   Preferences::UnregisterCallback(CachedPrefs::OnPrefsChange, URLCLASSIFIER_TRACKING_TABLE_TEST_ENTRIES, this);
 }
 } // anonymous namespace
 
-static nsresult
-IsThirdParty(nsIChannel* aChannel, bool* aResult)
-{
-  NS_ENSURE_ARG(aResult);
-  *aResult = false;
-
-  nsCOMPtr<mozIThirdPartyUtil> thirdPartyUtil = services::GetThirdPartyUtil();
-  if (NS_WARN_IF(!thirdPartyUtil)) {
-    return NS_ERROR_FAILURE;
-  }
-
-  nsresult rv;
-  nsCOMPtr<nsIHttpChannelInternal> chan = do_QueryInterface(aChannel, &rv);
-  if (NS_FAILED(rv) || !chan) {
-    LOG(("nsChannelClassifier: Not an HTTP channel"));
-    return NS_OK;
-  }
-  nsCOMPtr<nsIURI> chanURI;
-  rv = aChannel->GetURI(getter_AddRefs(chanURI));
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  nsCOMPtr<nsIURI> topWinURI;
-  rv = chan->GetTopWindowURI(getter_AddRefs(topWinURI));
-  if (NS_FAILED(rv)) {
-    return rv;
-  }
-  if (!topWinURI) {
-    LOG(("nsChannelClassifier: No window URI\n"));
-  }
-
-  // Third party checks don't work for chrome:// URIs in mochitests, so just
-  // default to isThirdParty = true. We check isThirdPartyWindow to expand
-  // the list of domains that are considered first party (e.g., if
-  // facebook.com includes an iframe from fatratgames.com, all subsources
-  // included in that iframe are considered third-party with
-  // isThirdPartyChannel, even if they are not third-party w.r.t.
-  // facebook.com), and isThirdPartyChannel to prevent top-level navigations
-  // from being detected as third-party.
-  bool isThirdPartyChannel = true;
-  bool isThirdPartyWindow = true;
-  if (topWinURI) { // IsThirdPartyURI() will fail if passed a null URI.
-    rv = thirdPartyUtil->IsThirdPartyURI(chanURI, topWinURI, &isThirdPartyWindow);
-    if (NS_WARN_IF(NS_FAILED(rv))) {
-      if (LOG_ENABLED()) {
-        nsAutoCString spec;
-        chanURI->GetAsciiSpec(spec);
-        spec.Truncate(std::min(spec.Length(), sMaxSpecLength));
-        LOG(("IsThirdPartyURI failed (%s)",
-             spec.get()));
-      }
-      return rv;
-    }
-  }
-  rv = thirdPartyUtil->IsThirdPartyChannel(aChannel, nullptr, &isThirdPartyChannel);
-  if (NS_WARN_IF(NS_FAILED(rv))) {
-    if (LOG_ENABLED()) {
-      nsCOMPtr<nsIURI> uri;
-      aChannel->GetURI(getter_AddRefs(uri));
-      nsAutoCString spec;
-      chanURI->GetAsciiSpec(spec);
-      spec.Truncate(std::min(spec.Length(), sMaxSpecLength));
-      LOG(("IsThirdPartyChannel failed (%s)",
-           spec.get()));
-    }
-    return rv;
-  }
-
-  *aResult = isThirdPartyWindow && isThirdPartyChannel;
-  return NS_OK;
-}
-
 static void
 SetIsTrackingResourceHelper(nsIChannel* aChannel, bool aIsThirdParty)
 {
   MOZ_ASSERT(aChannel);
 
   nsCOMPtr<nsIParentChannel> parentChannel;
   NS_QueryNotificationCallbacks(aChannel, parentChannel);
   if (parentChannel) {
@@ -515,22 +444,19 @@ nsChannelClassifier::ShouldEnableTrackin
     }
 
     nsCOMPtr<nsIURI> chanURI;
     rv = aChannel->GetURI(getter_AddRefs(chanURI));
     NS_ENSURE_SUCCESS(rv, rv);
 
     // Only perform third-party checks for tracking protection
     if (!aAnnotationsOnly) {
-      bool isThirdParty = false;
-      rv = IsThirdParty(aChannel, &isThirdParty);
-      if (NS_WARN_IF(NS_FAILED(rv))) {
-        LOG(("nsChannelClassifier[%p]: IsThirdParty() failed", this));
-        return NS_OK;
-      }
+      bool isThirdParty =
+        nsContentUtils::IsThirdPartyWindowOrChannel(nullptr, aChannel,
+                                                    chanURI);
       if (!isThirdParty) {
         *result = false;
         if (LOG_ENABLED()) {
           nsCString spec = chanURI->GetSpecOrDefault();
           spec.Truncate(std::min(spec.Length(), sMaxSpecLength));
           LOG(("nsChannelClassifier[%p]: Skipping tracking protection checks "
                "for first party or top-level load channel[%p] with uri %s",
                this, aChannel, spec.get()));
@@ -1305,23 +1231,26 @@ TrackingURICallback::OnTrackerFound(nsre
       Unused << httpChannel->CancelForTrackingProtection();
     } else {
       Unused << channel->Cancel(aErrorCode);
     }
   } else {
     MOZ_ASSERT(aErrorCode == NS_ERROR_TRACKING_ANNOTATION_URI);
     MOZ_ASSERT(mChannelClassifier->ShouldEnableTrackingAnnotation());
 
-    bool isThirdPartyWithTopLevelWinURI = false;
-    nsresult rv = IsThirdParty(channel, &isThirdPartyWithTopLevelWinURI);
+    nsCOMPtr<nsIURI> chanURI;
+    nsresult rv = channel->GetURI(getter_AddRefs(chanURI));
     if (NS_WARN_IF(NS_FAILED(rv))) {
-      LOG(("TrackingURICallback[%p]::OnTrackerFound IsThirdParty() failed",
-           mChannelClassifier.get()));
+      LOG(("TrackingURICallback[%p]::OnTrackerFound nsIChannel::GetURI(%p) failed",
+           mChannelClassifier.get(), (void*) channel.get()));
       return; // we'll assume the channel is NOT third-party
     }
+    bool isThirdPartyWithTopLevelWinURI =
+      nsContentUtils::IsThirdPartyWindowOrChannel(nullptr, channel,
+                                                  chanURI);
 
     LOG(("TrackingURICallback[%p]::OnTrackerFound, annotating channel[%p]",
          mChannelClassifier.get(), channel.get()));
 
     SetIsTrackingResourceHelper(channel, isThirdPartyWithTopLevelWinURI);
 
     if (isThirdPartyWithTopLevelWinURI) {
       // Even with TP disabled, we still want to show the user that there