Bug 1494476 - Part 1: Make AntiTrackingCommon::AddFirstPartyStorageAccessGrantedFor() accept a principal argument instead of a string r=baku
authorEhsan Akhgari <ehsan@mozilla.com>
Wed, 10 Oct 2018 15:47:40 +0000
changeset 498992 8c191b926a4395545612361cd38a9874d53a7bb5
parent 498991 cc1d49fd799c50999eca236c3b965e27779d1440
child 498993 84b8383192e2ea46c245878cc3850790bf5e6bce
push id1864
push userffxbld-merge
push dateMon, 03 Dec 2018 15:51:40 +0000
treeherdermozilla-release@f040763d99ad [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbaku
bugs1494476
milestone64.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 1494476 - Part 1: Make AntiTrackingCommon::AddFirstPartyStorageAccessGrantedFor() accept a principal argument instead of a string r=baku Depends on D8238 Differential Revision: https://phabricator.services.mozilla.com/D8155
dom/base/nsDocument.cpp
dom/base/nsGlobalWindowOuter.cpp
toolkit/components/antitracking/AntiTrackingCommon.cpp
toolkit/components/antitracking/AntiTrackingCommon.h
--- a/dom/base/nsDocument.cpp
+++ b/dom/base/nsDocument.cpp
@@ -12811,29 +12811,18 @@ nsIDocument::MaybeAllowStorageForOpener(
   // If the opener is not a 3rd party and if this window is not a 3rd party, we
   // should not continue.
   if (!nsContentUtils::IsThirdPartyWindowOrChannel(inner, nullptr, openerURI) &&
       !nsContentUtils::IsThirdPartyWindowOrChannel(openerInner, nullptr,
                                                    nullptr)) {
     return;
   }
 
-  nsCOMPtr<nsIURI> uri = GetDocumentURI();
-  if (NS_WARN_IF(!uri)) {
-    return;
-  }
-
-  nsAutoString origin;
-  nsresult rv = nsContentUtils::GetUTFOrigin(uri, origin);
-  if (NS_WARN_IF(NS_FAILED(rv))) {
-    return;
-  }
-
   // We don't care when the asynchronous work finishes here.
-  Unused << AntiTrackingCommon::AddFirstPartyStorageAccessGrantedFor(origin,
+  Unused << AntiTrackingCommon::AddFirstPartyStorageAccessGrantedFor(NodePrincipal(),
                                                                      openerInner,
                                                                      AntiTrackingCommon::eHeuristic);
 }
 
 namespace {
 
 // Documents can stay alive for days. We don't want to update the permission
 // value at any user-interaction, and, using a timer triggered any X seconds
@@ -13827,28 +13816,17 @@ nsIDocument::RequestStorageAccess(mozill
   }
 
   // Step 10. Grant the document access to cookies and store that fact for
   //          the purposes of future calls to hasStorageAccess() and
   //          requestStorageAccess().
   if (granted && inner) {
     outer->SetHasStorageAccess(true);
     if (isTrackingWindow) {
-      nsCOMPtr<nsIURI> uri = GetDocumentURI();
-      if (NS_WARN_IF(!uri)) {
-        aRv.Throw(NS_ERROR_NOT_AVAILABLE);
-        return nullptr;
-      }
-      nsAutoString origin;
-      nsresult rv = nsContentUtils::GetUTFOrigin(uri, origin);
-      if (NS_WARN_IF(NS_FAILED(rv))) {
-        aRv.Throw(rv);
-        return nullptr;
-      }
-      AntiTrackingCommon::AddFirstPartyStorageAccessGrantedFor(origin,
+      AntiTrackingCommon::AddFirstPartyStorageAccessGrantedFor(NodePrincipal(),
                                                                inner,
                                                                AntiTrackingCommon::eStorageAccessAPI)
         ->Then(GetCurrentThreadSerialEventTarget(), __func__,
                [promise] (bool) {
                  promise->MaybeResolveWithUndefined();
                },
                [promise] (bool) {
                  promise->MaybeRejectWithUndefined();
--- a/dom/base/nsGlobalWindowOuter.cpp
+++ b/dom/base/nsGlobalWindowOuter.cpp
@@ -7096,24 +7096,26 @@ nsGlobalWindowOuter::MaybeAllowStorageFo
     return;
   }
 
   // No 3rd party URL/window.
   if (!nsContentUtils::IsThirdPartyWindowOrChannel(inner, nullptr, aURI)) {
     return;
   }
 
-  nsAutoString origin;
-  nsresult rv = nsContentUtils::GetUTFOrigin(aURI, origin);
-  if (NS_WARN_IF(NS_FAILED(rv))) {
+  nsIDocument* doc = inner->GetDoc();
+  if (!doc) {
     return;
   }
+  nsCOMPtr<nsIPrincipal> principal =
+    BasePrincipal::CreateCodebasePrincipal(aURI,
+      doc->NodePrincipal()->OriginAttributesRef());
 
   // We don't care when the asynchronous work finishes here.
-  Unused << AntiTrackingCommon::AddFirstPartyStorageAccessGrantedFor(origin,
+  Unused << AntiTrackingCommon::AddFirstPartyStorageAccessGrantedFor(principal,
                                                                      inner,
                                                                      AntiTrackingCommon::eHeuristic);
 }
 
 //*****************************************************************************
 // nsGlobalWindowOuter: Helper Functions
 //*****************************************************************************
 
--- a/toolkit/components/antitracking/AntiTrackingCommon.cpp
+++ b/toolkit/components/antitracking/AntiTrackingCommon.cpp
@@ -356,24 +356,38 @@ GetTopWindow(nsPIDOMWindowInner* aWindow
 
 /* static */ bool
 AntiTrackingCommon::ShouldHonorContentBlockingCookieRestrictions()
 {
   return StaticPrefs::browser_contentblocking_enabled();
 }
 
 /* static */ RefPtr<AntiTrackingCommon::StorageAccessGrantPromise>
-AntiTrackingCommon::AddFirstPartyStorageAccessGrantedFor(const nsAString& aOrigin,
+AntiTrackingCommon::AddFirstPartyStorageAccessGrantedFor(nsIPrincipal* aPrincipal,
                                                          nsPIDOMWindowInner* aParentWindow,
                                                          StorageAccessGrantedReason aReason)
 {
   MOZ_ASSERT(aParentWindow);
 
+  nsCOMPtr<nsIURI> uri;
+  nsresult rv = aPrincipal->GetURI(getter_AddRefs(uri));
+  if (NS_WARN_IF(!uri)) {
+    LOG(("Can't get the URI from the principal"));
+    return StorageAccessGrantPromise::CreateAndReject(false, __func__);
+  }
+
+  nsAutoString origin;
+  rv = nsContentUtils::GetUTFOrigin(uri, origin);
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    LOG(("Can't get the origin from the URI"));
+    return StorageAccessGrantPromise::CreateAndReject(false, __func__);
+  }
+
   LOG(("Adding a first-party storage exception for %s...",
-       NS_ConvertUTF16toUTF8(aOrigin).get()));
+       NS_ConvertUTF16toUTF8(origin).get()));
 
   if (StaticPrefs::network_cookie_cookieBehavior() !=
         nsICookieService::BEHAVIOR_REJECT_TRACKER) {
     LOG(("Disabled by network.cookie.cookieBehavior pref (%d), bailing out early",
          StaticPrefs::network_cookie_cookieBehavior()));
     return StorageAccessGrantPromise::CreateAndResolve(true, __func__);
   }
 
@@ -397,33 +411,33 @@ AntiTrackingCommon::AddFirstPartyStorage
     return StorageAccessGrantPromise::CreateAndReject(false, __func__);
   }
 
   LOG(("The current resource is %s-party",
        outerParentWindow->IsTopLevelWindow() ? "first" : "third"));
 
   // We are a first party resource.
   if (outerParentWindow->IsTopLevelWindow()) {
-    CopyUTF16toUTF8(aOrigin, trackingOrigin);
+    CopyUTF16toUTF8(origin, trackingOrigin);
     topLevelStoragePrincipal = parentWindow->GetPrincipal();
     if (NS_WARN_IF(!topLevelStoragePrincipal)) {
       LOG(("Top-level storage area principal not found, bailing out early"));
       return StorageAccessGrantPromise::CreateAndReject(false, __func__);
     }
 
   // We are a 3rd party source.
   } else if (!GetParentPrincipalAndTrackingOrigin(parentWindow,
                                                   getter_AddRefs(topLevelStoragePrincipal),
                                                   trackingOrigin)) {
     LOG(("Error while computing the parent principal and tracking origin, bailing out early"));
     return StorageAccessGrantPromise::CreateAndReject(false, __func__);
   }
 
-  nsCOMPtr<nsIURI> uri;
-  nsresult rv = NS_NewURI(getter_AddRefs(uri), trackingOrigin);
+  nsCOMPtr<nsIURI> trackingURI;
+  rv = NS_NewURI(getter_AddRefs(trackingURI), trackingOrigin);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     LOG(("Couldn't make a new URI out of the tracking origin"));
     return StorageAccessGrantPromise::CreateAndReject(false, __func__);
   }
 
   nsCOMPtr<nsPIDOMWindowOuter> pwin = GetTopWindow(parentWindow);
   if (!pwin) {
     LOG(("Couldn't get the top window"));
@@ -431,22 +445,22 @@ AntiTrackingCommon::AddFirstPartyStorage
   }
 
   nsIChannel* channel =
     pwin->GetCurrentInnerWindow()->GetExtantDoc()->GetChannel();
 
   // We hardcode this block reason since the first-party storage access permission
   // is granted for the purpose of blocking trackers.
   const uint32_t blockReason = nsIWebProgressListener::STATE_COOKIES_BLOCKED_TRACKER;
-  pwin->NotifyContentBlockingState(blockReason, channel, false, uri);
+  pwin->NotifyContentBlockingState(blockReason, channel, false, trackingURI);
 
-  NS_ConvertUTF16toUTF8 grantedOrigin(aOrigin);
+  NS_ConvertUTF16toUTF8 grantedOrigin(origin);
 
   ReportUnblockingConsole(parentWindow, NS_ConvertUTF8toUTF16(trackingOrigin),
-                          aOrigin, aReason);
+                          origin, aReason);
 
   if (XRE_IsParentProcess()) {
     LOG(("Saving the permission: trackingOrigin=%s, grantedOrigin=%s",
          trackingOrigin.get(), grantedOrigin.get()));
 
     RefPtr<StorageAccessGrantPromise::Private> p = new StorageAccessGrantPromise::Private(__func__);
     SaveFirstPartyStorageAccessGrantedForOriginOnParentProcess(topLevelStoragePrincipal,
                                                                trackingOrigin,
--- a/toolkit/components/antitracking/AntiTrackingCommon.h
+++ b/toolkit/components/antitracking/AntiTrackingCommon.h
@@ -99,17 +99,17 @@ public:
   //   becuase of a script) opens a popup and the user interacts with it. We
   //   want to grant the permission for the 3rd party context to have access to
   //   the first party stoage when loaded in aParentWindow.
   //   Ex: example.net import tracker.com/script.js which does opens a popup and
   //   the user interacts with it. tracker.com is allowed when loaded by
   //   example.net.
   typedef MozPromise<bool, bool, false> StorageAccessGrantPromise;
   static MOZ_MUST_USE RefPtr<StorageAccessGrantPromise>
-  AddFirstPartyStorageAccessGrantedFor(const nsAString& aOrigin,
+  AddFirstPartyStorageAccessGrantedFor(nsIPrincipal* aPrincipal,
                                        nsPIDOMWindowInner* aParentWindow,
                                        StorageAccessGrantedReason aReason);
 
   static void
   StoreUserInteractionFor(nsIPrincipal* aPrincipal);
 
   static bool
   HasUserInteraction(nsIPrincipal* aPrincipal);