Bug 1487600 - Report to console any content blocking, r=ehsan a=pascalc l10n=flod
authorAndrea Marchesini <amarchesini@mozilla.com>
Tue, 04 Sep 2018 18:50:16 +0200
changeset 492365 235f087b804385340139b23ff2c3022f3838e565
parent 492364 1b236230b856b1aaf499723efce2dd1b7ca83a89
child 492366 6a3f605011caf91001dd26e4156c136ac7316c0e
push id1815
push userffxbld-merge
push dateMon, 15 Oct 2018 10:40:45 +0000
treeherdermozilla-release@18d4c09e9378 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersehsan, pascalc
bugs1487600
milestone63.0
Bug 1487600 - Report to console any content blocking, r=ehsan a=pascalc l10n=flod
netwerk/locales/en-US/necko.properties
toolkit/components/antitracking/AntiTrackingCommon.cpp
uriloader/base/nsIWebProgressListener.idl
--- a/netwerk/locales/en-US/necko.properties
+++ b/netwerk/locales/en-US/necko.properties
@@ -37,12 +37,17 @@ DirFileLabel=File:
 
 PhishingAuth=You are about to visit “%1$S”. This site may be attempting to trick you into thinking you are visiting a different site. Use extreme caution.
 PhishingAuthAccept=I understand and will be very careful
 SuperfluousAuth=You are about to log in to the site “%1$S” with the username “%2$S”, but the website does not require authentication. This may be an attempt to trick you.\n\nIs “%1$S” the site you want to visit?
 AutomaticAuth=You are about to log in to the site “%1$S” with the username “%2$S”.
 
 TrackerUriBlocked=The resource at “%1$S” was blocked because content blocking is enabled.
 UnsafeUriBlocked=The resource at “%1$S” was blocked by Safe Browsing.
+CookieBlockedByPermission=Request to access cookies or storage on “%1$S” was blocked because of custom cookie permission.
+CookieBlockedTracker=Request to access cookie or storage on “%1$S” was blocked because it came from a tracker and content blocking is enabled.
+CookieBlockedAll=Request to access cookie or storage on “%1$S” was blocked because we are blocking all storage access requests.
+CookieBlockedForeign=Request to access cookie or storage on “%1$S” was blocked because we are blocking all third-party storage access requests and content blocking is enabled.
+CookieBlockedSlowTrackingContent=The resource at “%1$S” was blocked because content blocking is enabled and the resource was classified as a slow tracking resource.
 
 # LOCALIZATION NOTE (nsICookieManagerAPIDeprecated): don't localize originAttributes.
 # %1$S is the deprecated API; %2$S is the interface suffix that the given deprecated API belongs to.
 nsICookieManagerAPIDeprecated=“%1$S” is changed. Update your code and pass the correct originAttributes. Read more on MDN: https://developer.mozilla.org/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsICookieManager%2$S
--- a/toolkit/components/antitracking/AntiTrackingCommon.cpp
+++ b/toolkit/components/antitracking/AntiTrackingCommon.cpp
@@ -12,21 +12,23 @@
 #include "mozilla/IntegerPrintfMacros.h"
 #include "mozilla/Logging.h"
 #include "mozilla/StaticPrefs.h"
 #include "mozIThirdPartyUtil.h"
 #include "nsContentUtils.h"
 #include "nsGlobalWindowInner.h"
 #include "nsICookiePermission.h"
 #include "nsICookieService.h"
+#include "nsIDocShell.h"
 #include "nsIHttpChannelInternal.h"
 #include "nsIIOService.h"
 #include "nsIParentChannel.h"
 #include "nsIPermissionManager.h"
 #include "nsIPrincipal.h"
+#include "nsIScriptError.h"
 #include "nsIURI.h"
 #include "nsIURL.h"
 #include "nsIWebProgressListener.h"
 #include "nsNetUtil.h"
 #include "nsPIDOMWindow.h"
 #include "nsScriptSecurityManager.h"
 #include "prtime.h"
 
@@ -181,16 +183,81 @@ CheckContentBlockingAllowList(nsIHttpCha
     }
   }
 
   LOG(("Could not check the content blocking allow list because the top "
        "window wasn't accessible"));
   return false;
 }
 
+void
+ReportBlockingToConsole(nsPIDOMWindowOuter* aWindow, nsIHttpChannel* aChannel,
+                        uint32_t aRejectedReason)
+{
+  MOZ_ASSERT(aWindow && aChannel);
+  MOZ_ASSERT(aRejectedReason == nsIWebProgressListener::STATE_COOKIES_BLOCKED_BY_PERMISSION ||
+             aRejectedReason == nsIWebProgressListener::STATE_COOKIES_BLOCKED_TRACKER ||
+             aRejectedReason == nsIWebProgressListener::STATE_COOKIES_BLOCKED_ALL ||
+             aRejectedReason == nsIWebProgressListener::STATE_COOKIES_BLOCKED_FOREIGN ||
+             aRejectedReason == nsIWebProgressListener::STATE_BLOCKED_SLOW_TRACKING_CONTENT);
+
+  if (!StaticPrefs::browser_contentblocking_enabled()) {
+    return;
+  }
+
+  nsCOMPtr<nsIDocShell> docShell = aWindow->GetDocShell();
+  if (NS_WARN_IF(!docShell)) {
+    return;
+  }
+
+  nsCOMPtr<nsIDocument> doc = docShell->GetDocument();
+  if (NS_WARN_IF(!doc)) {
+    return;
+  }
+
+  const char* message = nullptr;
+  switch (aRejectedReason) {
+    case nsIWebProgressListener::STATE_COOKIES_BLOCKED_BY_PERMISSION:
+      message = "CookieBlockedByPermission";
+      break;
+
+    case nsIWebProgressListener::STATE_COOKIES_BLOCKED_TRACKER:
+      message = "CookieBlockedTracker";
+      break;
+
+    case nsIWebProgressListener::STATE_COOKIES_BLOCKED_ALL:
+      message = "CookieBlockedAll";
+      break;
+
+    case nsIWebProgressListener::STATE_COOKIES_BLOCKED_FOREIGN:
+      message = "CookieBlockedForeign";
+      break;
+
+    case nsIWebProgressListener::STATE_BLOCKED_SLOW_TRACKING_CONTENT:
+      message = "CookieBlockedSlowTrackingContent";
+      break;
+
+    default:
+      return;
+  }
+
+  MOZ_ASSERT(message);
+
+  nsCOMPtr<nsIURI> uri;
+  aChannel->GetURI(getter_AddRefs(uri));
+  NS_ConvertUTF8toUTF16 spec(uri->GetSpecOrDefault());
+  const char16_t* params[] = { spec.get() };
+
+  nsContentUtils::ReportToConsole(nsIScriptError::warningFlag,
+                                  NS_LITERAL_CSTRING("Content Blocking"),
+                                  doc,
+                                  nsContentUtils::eNECKO_PROPERTIES,
+                                  message,
+                                  params, ArrayLength(params));
+}
 } // anonymous
 
 /* static */ RefPtr<AntiTrackingCommon::StorageAccessGrantPromise>
 AntiTrackingCommon::AddFirstPartyStorageAccessGrantedFor(const nsAString& aOrigin,
                                                          nsPIDOMWindowInner* aParentWindow)
 {
   MOZ_ASSERT(aParentWindow);
 
@@ -921,16 +988,18 @@ AntiTrackingCommon::NotifyRejection(nsIC
   NS_ENSURE_SUCCESS_VOID(rv);
 
   nsCOMPtr<nsPIDOMWindowOuter> pwin = nsPIDOMWindowOuter::From(win);
   if (!pwin) {
     return;
   }
 
   pwin->NotifyContentBlockingState(aRejectedReason, aChannel);
+
+  ReportBlockingToConsole(pwin, httpChannel, aRejectedReason);
 }
 
 /* static */ void
 AntiTrackingCommon::NotifyRejection(nsPIDOMWindowInner* aWindow,
                                     uint32_t aRejectedReason)
 {
   MOZ_ASSERT(aWindow);
   MOZ_ASSERT(aRejectedReason == nsIWebProgressListener::STATE_COOKIES_BLOCKED_BY_PERMISSION ||
@@ -951,12 +1020,16 @@ AntiTrackingCommon::NotifyRejection(nsPI
   }
 
   nsCOMPtr<nsPIDOMWindowOuter> pwin;
   auto* outer = nsGlobalWindowOuter::Cast(aWindow->GetOuterWindow());
   if (outer) {
     pwin = outer->GetTopOuter();
   }
 
-  if (pwin) {
-    pwin->NotifyContentBlockingState(aRejectedReason, httpChannel);
+  if (!pwin) {
+    return;
   }
+
+  pwin->NotifyContentBlockingState(aRejectedReason, httpChannel);
+
+  ReportBlockingToConsole(pwin, httpChannel, aRejectedReason);
 }
--- a/uriloader/base/nsIWebProgressListener.idl
+++ b/uriloader/base/nsIWebProgressListener.idl
@@ -310,17 +310,17 @@ interface nsIWebProgressListener : nsISu
    *   Rejected because cookie policy blocks 3rd party cookies.
    *
    * STATE_BLOCKED_SLOW_TRACKING_CONTENT
    *   Rejected because of the FastBlock feature.
    */
   const unsigned long STATE_COOKIES_BLOCKED_BY_PERMISSION = 0x10000000;
   const unsigned long STATE_COOKIES_BLOCKED_TRACKER       = 0x20000000;
   const unsigned long STATE_COOKIES_BLOCKED_ALL           = 0x40000000;
-  const unsigned long STATE_COOKIES_BLOCKED_FOREIGN       = 0x80000000;
+  const unsigned long STATE_COOKIES_BLOCKED_FOREIGN       = 0x00000080;
   const unsigned long STATE_BLOCKED_SLOW_TRACKING_CONTENT = 0x00000040;
 
   /**
    * Notification indicating the state has changed for one of the requests
    * associated with aWebProgress.
    *
    * @param aWebProgress
    *        The nsIWebProgress instance that fired the notification