2014-11-12 02:05:55 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set sw=2 sts=2 ts=8 et 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-05-22 01:03:02 +04:00
|
|
|
|
|
|
|
#include "nsChannelClassifier.h"
|
|
|
|
|
2014-08-09 00:52:28 +04:00
|
|
|
#include "mozIThirdPartyUtil.h"
|
2016-03-12 00:57:38 +03:00
|
|
|
#include "nsCharSeparatedTokenizer.h"
|
2014-10-10 23:19:14 +04:00
|
|
|
#include "nsContentUtils.h"
|
2017-02-10 08:08:06 +03:00
|
|
|
#include "nsIAddonPolicyService.h"
|
2014-08-23 02:17:32 +04:00
|
|
|
#include "nsICacheEntry.h"
|
2014-08-09 00:52:28 +04:00
|
|
|
#include "nsICachingChannel.h"
|
2010-05-22 01:03:02 +04:00
|
|
|
#include "nsIChannel.h"
|
2017-06-22 02:55:00 +03:00
|
|
|
#include "nsIClassOfService.h"
|
2014-08-13 00:53:15 +04:00
|
|
|
#include "nsIDocShell.h"
|
|
|
|
#include "nsIDocument.h"
|
2014-08-09 00:52:28 +04:00
|
|
|
#include "nsIDOMDocument.h"
|
2016-12-16 23:19:19 +03:00
|
|
|
#include "nsIHttpChannel.h"
|
2014-10-30 03:22:22 +03:00
|
|
|
#include "nsIHttpChannelInternal.h"
|
2014-08-09 00:52:28 +04:00
|
|
|
#include "nsIIOService.h"
|
2014-11-12 02:05:55 +03:00
|
|
|
#include "nsIParentChannel.h"
|
2014-08-09 00:52:28 +04:00
|
|
|
#include "nsIPermissionManager.h"
|
2015-07-24 13:55:54 +03:00
|
|
|
#include "nsIPrivateBrowsingTrackingProtectionWhitelist.h"
|
2010-05-22 01:03:02 +04:00
|
|
|
#include "nsIProtocolHandler.h"
|
2014-10-10 23:19:14 +04:00
|
|
|
#include "nsIScriptError.h"
|
2012-07-20 20:46:59 +04:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
2014-08-13 00:53:15 +04:00
|
|
|
#include "nsISecureBrowserUI.h"
|
|
|
|
#include "nsISecurityEventSink.h"
|
2016-12-01 07:13:00 +03:00
|
|
|
#include "nsISupportsPriority.h"
|
2015-07-07 05:17:00 +03:00
|
|
|
#include "nsIURL.h"
|
2014-08-13 00:53:15 +04:00
|
|
|
#include "nsIWebProgressListener.h"
|
2016-01-09 04:20:50 +03:00
|
|
|
#include "nsNetUtil.h"
|
2014-08-13 00:53:15 +04:00
|
|
|
#include "nsPIDOMWindow.h"
|
2014-11-12 02:05:55 +03:00
|
|
|
#include "nsXULAppAPI.h"
|
2016-12-16 23:19:19 +03:00
|
|
|
#include "nsQueryObject.h"
|
2014-08-09 00:52:28 +04:00
|
|
|
|
2015-09-26 06:39:49 +03:00
|
|
|
#include "mozilla/ErrorNames.h"
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2015-09-26 06:39:49 +03:00
|
|
|
#include "mozilla/Preferences.h"
|
2016-12-16 23:19:19 +03:00
|
|
|
#include "mozilla/net/HttpBaseChannel.h"
|
2017-05-04 13:14:04 +03:00
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2017-06-13 02:16:00 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2014-08-09 00:52:28 +04:00
|
|
|
|
2016-05-19 05:02:57 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
2010-05-22 01:03:02 +04:00
|
|
|
|
|
|
|
//
|
2016-05-26 02:13:54 +03:00
|
|
|
// MOZ_LOG=nsChannelClassifier:5
|
2010-05-22 01:03:02 +04:00
|
|
|
//
|
2016-05-19 05:02:57 +03:00
|
|
|
static LazyLogModule gChannelClassifierLog("nsChannelClassifier");
|
2015-11-03 07:35:29 +03:00
|
|
|
|
2016-12-16 22:05:08 +03:00
|
|
|
|
2013-11-21 02:55:44 +04:00
|
|
|
#undef LOG
|
2016-05-19 05:02:57 +03:00
|
|
|
#define LOG(args) MOZ_LOG(gChannelClassifierLog, LogLevel::Debug, args)
|
|
|
|
#define LOG_ENABLED() MOZ_LOG_TEST(gChannelClassifierLog, LogLevel::Debug)
|
2010-05-22 01:03:02 +04:00
|
|
|
|
2017-04-18 12:00:27 +03:00
|
|
|
#define URLCLASSIFIER_SKIP_HOSTNAMES "urlclassifier.skipHostnames"
|
|
|
|
#define URLCLASSIFIER_TRACKING_WHITELIST "urlclassifier.trackingWhitelistTable"
|
2017-06-13 02:16:00 +03:00
|
|
|
#define URLCLASSIFIER_TRACKING_TABLE "urlclassifier.trackingTable"
|
2017-04-18 12:00:27 +03:00
|
|
|
|
|
|
|
// Put CachedPrefs in anonymous namespace to avoid any collision from outside of
|
|
|
|
// this file.
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* It is not recommended to read from Preference everytime a channel is
|
|
|
|
* connected.
|
|
|
|
* That is not fast and we should cache preference values and reuse them
|
|
|
|
*/
|
|
|
|
class CachedPrefs final
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static CachedPrefs* GetInstance();
|
|
|
|
|
|
|
|
void Init();
|
|
|
|
bool IsAllowListExample() { return sAllowListExample;}
|
|
|
|
bool IsLowerNetworkPriority() { return sLowerNetworkPriority;}
|
|
|
|
bool IsAnnotateChannelEnabled() { return sAnnotateChannelEnabled;}
|
|
|
|
nsCString GetTrackingWhiteList() { return mTrackingWhitelist; }
|
|
|
|
void SetTrackingWhiteList(const nsACString& aList) { mTrackingWhitelist = aList; }
|
|
|
|
nsCString GetSkipHostnames() { return mSkipHostnames; }
|
|
|
|
void SetSkipHostnames(const nsACString& aHostnames) { mSkipHostnames = aHostnames; }
|
2017-06-13 02:16:00 +03:00
|
|
|
void SetTrackingBlackList(const nsACString& aList) { mTrackingBlacklist = aList; }
|
|
|
|
nsCString GetTrackingBlackList() { return mTrackingBlacklist; }
|
2017-04-18 12:00:27 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
friend class StaticAutoPtr<CachedPrefs>;
|
|
|
|
CachedPrefs();
|
|
|
|
~CachedPrefs();
|
|
|
|
|
|
|
|
static void OnPrefsChange(const char* aPrefName, void* );
|
|
|
|
|
|
|
|
// Whether channels should be annotated as being on the tracking protection
|
|
|
|
// list.
|
|
|
|
static bool sAnnotateChannelEnabled;
|
|
|
|
// Whether the priority of the channels annotated as being on the tracking
|
|
|
|
// protection list should be lowered.
|
|
|
|
static bool sLowerNetworkPriority;
|
|
|
|
static bool sAllowListExample;
|
|
|
|
|
|
|
|
nsCString mTrackingWhitelist;
|
|
|
|
nsCString mSkipHostnames;
|
2017-06-13 02:16:00 +03:00
|
|
|
nsCString mTrackingBlacklist;
|
2017-04-18 12:00:27 +03:00
|
|
|
|
|
|
|
static StaticAutoPtr<CachedPrefs> sInstance;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool CachedPrefs::sAllowListExample = false;
|
|
|
|
bool CachedPrefs::sLowerNetworkPriority = false;
|
|
|
|
bool CachedPrefs::sAnnotateChannelEnabled = false;
|
|
|
|
|
|
|
|
StaticAutoPtr<CachedPrefs> CachedPrefs::sInstance;
|
|
|
|
|
|
|
|
// static
|
|
|
|
void
|
|
|
|
CachedPrefs::OnPrefsChange(const char* aPref, void* aClosure)
|
|
|
|
{
|
|
|
|
CachedPrefs* prefs = static_cast<CachedPrefs*> (aClosure);
|
|
|
|
|
|
|
|
if (!strcmp(aPref, URLCLASSIFIER_SKIP_HOSTNAMES)) {
|
2017-07-31 07:28:48 +03:00
|
|
|
nsCString skipHostnames;
|
|
|
|
Preferences::GetCString(URLCLASSIFIER_SKIP_HOSTNAMES, skipHostnames);
|
2017-04-18 12:00:27 +03:00
|
|
|
ToLowerCase(skipHostnames);
|
|
|
|
prefs->SetSkipHostnames(skipHostnames);
|
|
|
|
} else if (!strcmp(aPref, URLCLASSIFIER_TRACKING_WHITELIST)) {
|
2017-07-31 07:28:48 +03:00
|
|
|
nsCString trackingWhitelist;
|
|
|
|
Preferences::GetCString(URLCLASSIFIER_TRACKING_WHITELIST,
|
|
|
|
trackingWhitelist);
|
2017-04-18 12:00:27 +03:00
|
|
|
prefs->SetTrackingWhiteList(trackingWhitelist);
|
2017-06-13 02:16:00 +03:00
|
|
|
} else if (!strcmp(aPref, URLCLASSIFIER_TRACKING_TABLE)) {
|
2017-07-31 07:28:48 +03:00
|
|
|
nsCString trackingBlacklist;
|
|
|
|
Preferences::GetCString(URLCLASSIFIER_TRACKING_TABLE, trackingBlacklist);
|
2017-06-13 02:16:00 +03:00
|
|
|
prefs->SetTrackingBlackList(trackingBlacklist);
|
2017-04-18 12:00:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CachedPrefs::Init()
|
|
|
|
{
|
|
|
|
Preferences::AddBoolVarCache(&sAnnotateChannelEnabled,
|
|
|
|
"privacy.trackingprotection.annotate_channels");
|
|
|
|
Preferences::AddBoolVarCache(&sLowerNetworkPriority,
|
|
|
|
"privacy.trackingprotection.lower_network_priority");
|
|
|
|
Preferences::AddBoolVarCache(&sAllowListExample,
|
|
|
|
"channelclassifier.allowlist_example");
|
|
|
|
Preferences::RegisterCallbackAndCall(CachedPrefs::OnPrefsChange,
|
|
|
|
URLCLASSIFIER_SKIP_HOSTNAMES, this);
|
|
|
|
Preferences::RegisterCallbackAndCall(CachedPrefs::OnPrefsChange,
|
|
|
|
URLCLASSIFIER_TRACKING_WHITELIST, this);
|
2017-06-13 02:16:00 +03:00
|
|
|
Preferences::RegisterCallbackAndCall(CachedPrefs::OnPrefsChange,
|
|
|
|
URLCLASSIFIER_TRACKING_TABLE, this);
|
2017-04-18 12:00:27 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
CachedPrefs*
|
|
|
|
CachedPrefs::GetInstance()
|
|
|
|
{
|
|
|
|
if (!sInstance) {
|
|
|
|
sInstance = new CachedPrefs();
|
|
|
|
sInstance->Init();
|
|
|
|
ClearOnShutdown(&sInstance);
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(sInstance);
|
|
|
|
return sInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
CachedPrefs::CachedPrefs()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(CachedPrefs);
|
|
|
|
}
|
|
|
|
|
|
|
|
CachedPrefs::~CachedPrefs()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(CachedPrefs);
|
|
|
|
|
|
|
|
Preferences::UnregisterCallback(CachedPrefs::OnPrefsChange, URLCLASSIFIER_SKIP_HOSTNAMES, this);
|
|
|
|
Preferences::UnregisterCallback(CachedPrefs::OnPrefsChange, URLCLASSIFIER_TRACKING_WHITELIST, this);
|
2017-06-13 02:16:00 +03:00
|
|
|
Preferences::UnregisterCallback(CachedPrefs::OnPrefsChange, URLCLASSIFIER_TRACKING_TABLE, this);
|
2017-04-18 12:00:27 +03:00
|
|
|
}
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2017-06-13 02:16:00 +03:00
|
|
|
static void
|
|
|
|
SetIsTrackingResourceHelper(nsIChannel* aChannel)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aChannel);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIParentChannel> parentChannel;
|
|
|
|
NS_QueryNotificationCallbacks(aChannel, parentChannel);
|
|
|
|
if (parentChannel) {
|
|
|
|
// This channel is a parent-process proxy for a child process
|
|
|
|
// request. We should notify the child process as well.
|
|
|
|
parentChannel->NotifyTrackingResource();
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<HttpBaseChannel> httpChannel = do_QueryObject(aChannel);
|
|
|
|
if (httpChannel) {
|
|
|
|
httpChannel->SetIsTrackingResource();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
LowerPriorityHelper(nsIChannel* aChannel)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aChannel);
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupportsPriority> p = do_QueryInterface(aChannel);
|
|
|
|
if (p) {
|
|
|
|
p->SetPriority(nsISupportsPriority::PRIORITY_LOWEST);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-22 02:55:00 +03:00
|
|
|
static void
|
|
|
|
SetThrottleableHelper(nsIChannel* aChannel)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aChannel);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIClassOfService> cos(do_QueryInterface(aChannel));
|
|
|
|
if (cos) {
|
|
|
|
cos->AddClassFlags(nsIClassOfService::Throttleable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsChannelClassifier,
|
2017-01-12 13:07:22 +03:00
|
|
|
nsIURIClassifierCallback,
|
|
|
|
nsIObserver)
|
2010-05-22 01:03:02 +04:00
|
|
|
|
2016-12-01 07:13:00 +03:00
|
|
|
nsChannelClassifier::nsChannelClassifier(nsIChannel *aChannel)
|
2015-01-10 00:25:13 +03:00
|
|
|
: mIsAllowListed(false),
|
2016-12-01 07:13:00 +03:00
|
|
|
mSuspendedChannel(false),
|
|
|
|
mChannel(aChannel),
|
2017-06-13 02:16:00 +03:00
|
|
|
mTrackingProtectionEnabled(Nothing()),
|
|
|
|
mTrackingAnnotationEnabled(Nothing())
|
2010-05-22 01:03:02 +04:00
|
|
|
{
|
2016-12-01 07:13:00 +03:00
|
|
|
MOZ_ASSERT(mChannel);
|
2010-05-22 01:03:02 +04:00
|
|
|
}
|
|
|
|
|
2017-06-13 02:16:00 +03:00
|
|
|
bool
|
|
|
|
nsChannelClassifier::ShouldEnableTrackingProtection()
|
|
|
|
{
|
|
|
|
if (mTrackingProtectionEnabled) {
|
|
|
|
return mTrackingProtectionEnabled.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
mTrackingProtectionEnabled = Some(false);
|
|
|
|
|
|
|
|
nsCOMPtr<nsILoadContext> loadContext;
|
|
|
|
NS_QueryNotificationCallbacks(mChannel, loadContext);
|
|
|
|
if (loadContext && loadContext->UseTrackingProtection()) {
|
|
|
|
Unused << ShouldEnableTrackingProtectionInternal(
|
|
|
|
mChannel, false, mTrackingProtectionEnabled.ptr());
|
|
|
|
}
|
|
|
|
|
|
|
|
return mTrackingProtectionEnabled.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsChannelClassifier::ShouldEnableTrackingAnnotation()
|
2016-12-01 07:13:00 +03:00
|
|
|
{
|
2017-06-13 02:16:00 +03:00
|
|
|
if (mTrackingAnnotationEnabled) {
|
|
|
|
return mTrackingAnnotationEnabled.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
mTrackingAnnotationEnabled = Some(false);
|
|
|
|
|
|
|
|
if (!CachedPrefs::GetInstance()->IsAnnotateChannelEnabled()) {
|
|
|
|
return mTrackingAnnotationEnabled.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If tracking protection is enabled, no need to do channel annotation.
|
|
|
|
if (ShouldEnableTrackingProtection()) {
|
|
|
|
return mTrackingAnnotationEnabled.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
// To prevent calling ShouldEnableTrackingProtectionInternal() again,
|
|
|
|
// check loadContext->UseTrackingProtection() here.
|
|
|
|
// If loadContext->UseTrackingProtection() is true, here it means
|
|
|
|
// ShouldEnableTrackingProtectionInternal() has been called before in
|
|
|
|
// ShouldEnableTrackingProtection() above and the result is false.
|
|
|
|
// So, we can just return false.
|
|
|
|
nsCOMPtr<nsILoadContext> loadContext;
|
|
|
|
NS_QueryNotificationCallbacks(mChannel, loadContext);
|
|
|
|
if (loadContext && loadContext->UseTrackingProtection()) {
|
|
|
|
return mTrackingAnnotationEnabled.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
Unused << ShouldEnableTrackingProtectionInternal(
|
|
|
|
mChannel, true, mTrackingAnnotationEnabled.ptr());
|
|
|
|
|
|
|
|
return mTrackingAnnotationEnabled.value();
|
2016-12-01 07:13:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2017-06-13 02:16:00 +03:00
|
|
|
nsChannelClassifier::ShouldEnableTrackingProtectionInternal(
|
|
|
|
nsIChannel *aChannel,
|
|
|
|
bool aAnnotationsOnly,
|
|
|
|
bool *result)
|
2014-07-24 21:59:00 +04:00
|
|
|
{
|
2014-11-12 02:05:55 +03:00
|
|
|
// Should only be called in the parent process.
|
2015-07-04 04:29:00 +03:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
2014-11-12 02:05:55 +03:00
|
|
|
|
2014-08-09 00:52:28 +04:00
|
|
|
NS_ENSURE_ARG(result);
|
|
|
|
*result = false;
|
2014-07-24 21:59:00 +04:00
|
|
|
|
2014-08-09 00:52:28 +04:00
|
|
|
nsresult rv;
|
2014-07-24 21:59:00 +04:00
|
|
|
nsCOMPtr<mozIThirdPartyUtil> thirdPartyUtil =
|
|
|
|
do_GetService(THIRDPARTYUTIL_CONTRACTID, &rv);
|
2014-08-09 00:52:28 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2014-10-30 03:22:22 +03:00
|
|
|
nsCOMPtr<nsIHttpChannelInternal> chan = do_QueryInterface(aChannel, &rv);
|
2016-11-11 08:01:00 +03:00
|
|
|
if (NS_FAILED(rv) || !chan) {
|
|
|
|
LOG(("nsChannelClassifier[%p]: Not an HTTP channel", this));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-08-09 00:52:28 +04:00
|
|
|
|
2015-03-31 23:18:38 +03:00
|
|
|
nsCOMPtr<nsIURI> topWinURI;
|
|
|
|
rv = chan->GetTopWindowURI(getter_AddRefs(topWinURI));
|
2014-08-09 00:52:28 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2015-03-31 23:18:38 +03:00
|
|
|
if (!topWinURI) {
|
2014-10-30 03:22:22 +03:00
|
|
|
LOG(("nsChannelClassifier[%p]: No window URI\n", this));
|
|
|
|
}
|
|
|
|
|
2015-03-31 23:18:38 +03:00
|
|
|
nsCOMPtr<nsIURI> chanURI;
|
|
|
|
rv = aChannel->GetURI(getter_AddRefs(chanURI));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
thirdPartyUtil->IsThirdPartyURI(chanURI, topWinURI, &isThirdPartyWindow);
|
|
|
|
thirdPartyUtil->IsThirdPartyChannel(aChannel, nullptr, &isThirdPartyChannel);
|
|
|
|
if (!isThirdPartyWindow || !isThirdPartyChannel) {
|
2015-09-26 06:39:49 +03:00
|
|
|
*result = false;
|
|
|
|
if (LOG_ENABLED()) {
|
|
|
|
LOG(("nsChannelClassifier[%p]: Skipping tracking protection checks "
|
|
|
|
"for first party or top-level load channel[%p] with uri %s",
|
2016-08-26 09:02:31 +03:00
|
|
|
this, aChannel, chanURI->GetSpecOrDefault().get()));
|
2015-09-26 06:39:49 +03:00
|
|
|
}
|
|
|
|
return NS_OK;
|
2015-03-31 23:18:38 +03:00
|
|
|
}
|
|
|
|
|
2017-06-13 02:16:00 +03:00
|
|
|
// Unlike full Tracking Protection, annotations don't block anything
|
|
|
|
// so we don't need to take into account add-ons or user exceptions.
|
|
|
|
if (aAnnotationsOnly) {
|
|
|
|
*result = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-02-10 08:08:06 +03:00
|
|
|
if (AddonMayLoad(aChannel, chanURI)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-31 23:18:38 +03:00
|
|
|
nsCOMPtr<nsIIOService> ios = do_GetService(NS_IOSERVICE_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2017-04-18 12:00:27 +03:00
|
|
|
if (!topWinURI && CachedPrefs::GetInstance()->IsAllowListExample()) {
|
2014-10-30 03:22:22 +03:00
|
|
|
LOG(("nsChannelClassifier[%p]: Allowlisting test domain\n", this));
|
2014-08-09 00:52:28 +04:00
|
|
|
rv = ios->NewURI(NS_LITERAL_CSTRING("http://allowlisted.example.com"),
|
2015-03-31 23:18:38 +03:00
|
|
|
nullptr, nullptr, getter_AddRefs(topWinURI));
|
2014-08-09 00:52:28 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Take the host/port portion so we can allowlist by site. Also ignore the
|
|
|
|
// scheme, since users who put sites on the allowlist probably don't expect
|
|
|
|
// allowlisting to depend on scheme.
|
2015-03-31 23:18:38 +03:00
|
|
|
nsCOMPtr<nsIURL> url = do_QueryInterface(topWinURI, &rv);
|
2015-09-17 02:42:08 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv; // normal for some loads, no need to print a warning
|
|
|
|
}
|
2014-08-09 00:52:28 +04:00
|
|
|
|
|
|
|
nsCString escaped(NS_LITERAL_CSTRING("https://"));
|
|
|
|
nsAutoCString temp;
|
|
|
|
rv = url->GetHostPort(temp);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
escaped.Append(temp);
|
|
|
|
|
|
|
|
// Stuff the whole thing back into a URI for the permission manager.
|
2015-03-31 23:18:38 +03:00
|
|
|
rv = ios->NewURI(escaped, nullptr, nullptr, getter_AddRefs(topWinURI));
|
2014-08-09 00:52:28 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPermissionManager> permMgr =
|
|
|
|
do_GetService(NS_PERMISSIONMANAGER_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
uint32_t permissions = nsIPermissionManager::UNKNOWN_ACTION;
|
2015-03-31 23:18:38 +03:00
|
|
|
rv = permMgr->TestPermission(topWinURI, "trackingprotection", &permissions);
|
2014-08-09 00:52:28 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2014-08-23 02:17:32 +04:00
|
|
|
if (permissions == nsIPermissionManager::ALLOW_ACTION) {
|
2015-09-26 06:39:49 +03:00
|
|
|
LOG(("nsChannelClassifier[%p]: Allowlisting channel[%p] for %s", this,
|
|
|
|
aChannel, escaped.get()));
|
2014-08-23 02:17:32 +04:00
|
|
|
mIsAllowListed = true;
|
|
|
|
*result = false;
|
|
|
|
} else {
|
|
|
|
*result = true;
|
|
|
|
}
|
2014-08-13 00:53:15 +04:00
|
|
|
|
2015-07-24 13:55:54 +03:00
|
|
|
// In Private Browsing Mode we also check against an in-memory list.
|
|
|
|
if (NS_UsePrivateBrowsing(aChannel)) {
|
|
|
|
nsCOMPtr<nsIPrivateBrowsingTrackingProtectionWhitelist> pbmtpWhitelist =
|
|
|
|
do_GetService(NS_PBTRACKINGPROTECTIONWHITELIST_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
bool exists = false;
|
|
|
|
rv = pbmtpWhitelist->ExistsInAllowList(topWinURI, &exists);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (exists) {
|
|
|
|
mIsAllowListed = true;
|
|
|
|
LOG(("nsChannelClassifier[%p]: Allowlisting channel[%p] in PBM for %s",
|
|
|
|
this, aChannel, escaped.get()));
|
|
|
|
}
|
|
|
|
|
|
|
|
*result = !exists;
|
|
|
|
}
|
|
|
|
|
2014-08-13 00:53:15 +04:00
|
|
|
// Tracking protection will be enabled so return without updating
|
|
|
|
// the security state. If any channels are subsequently cancelled
|
|
|
|
// (page elements blocked) the state will be then updated.
|
|
|
|
if (*result) {
|
2015-09-26 06:39:49 +03:00
|
|
|
if (LOG_ENABLED()) {
|
|
|
|
LOG(("nsChannelClassifier[%p]: Enabling tracking protection checks on "
|
|
|
|
"channel[%p] with uri %s for toplevel window %s", this, aChannel,
|
2016-08-26 09:02:31 +03:00
|
|
|
chanURI->GetSpecOrDefault().get(),
|
|
|
|
topWinURI->GetSpecOrDefault().get()));
|
2015-09-26 06:39:49 +03:00
|
|
|
}
|
2014-08-13 00:53:15 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tracking protection will be disabled so update the security state
|
2014-10-30 03:22:22 +03:00
|
|
|
// of the document and fire a secure change event. If we can't get the
|
|
|
|
// window for the channel, then the shield won't show up so we can't send
|
2014-11-12 02:05:55 +03:00
|
|
|
// an event to the securityUI anyway.
|
|
|
|
return NotifyTrackingProtectionDisabled(aChannel);
|
|
|
|
}
|
|
|
|
|
2017-02-10 08:08:06 +03:00
|
|
|
bool
|
|
|
|
nsChannelClassifier::AddonMayLoad(nsIChannel *aChannel, nsIURI *aUri)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsILoadInfo> channelLoadInfo = aChannel->GetLoadInfo();
|
|
|
|
if (!channelLoadInfo)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// loadingPrincipal is used here to ensure we are loading into an
|
|
|
|
// addon principal. This allows an addon, with explicit permission, to
|
|
|
|
// call out to API endpoints that may otherwise get blocked.
|
|
|
|
nsIPrincipal* loadingPrincipal = channelLoadInfo->LoadingPrincipal();
|
|
|
|
if (!loadingPrincipal)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return BasePrincipal::Cast(loadingPrincipal)->AddonAllowsLoad(aUri, true);
|
|
|
|
}
|
|
|
|
|
2014-11-12 02:05:55 +03:00
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
nsChannelClassifier::NotifyTrackingProtectionDisabled(nsIChannel *aChannel)
|
|
|
|
{
|
|
|
|
// Can be called in EITHER the parent or child process.
|
|
|
|
nsCOMPtr<nsIParentChannel> parentChannel;
|
|
|
|
NS_QueryNotificationCallbacks(aChannel, parentChannel);
|
|
|
|
if (parentChannel) {
|
|
|
|
// This channel is a parent-process proxy for a child process request.
|
|
|
|
// Tell the child process channel to do this instead.
|
|
|
|
parentChannel->NotifyTrackingProtectionDisabled();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<mozIThirdPartyUtil> thirdPartyUtil =
|
|
|
|
do_GetService(THIRDPARTYUTIL_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<mozIDOMWindowProxy> win;
|
2014-10-30 03:22:22 +03:00
|
|
|
rv = thirdPartyUtil->GetTopWindowForChannel(aChannel, getter_AddRefs(win));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
auto* pwin = nsPIDOMWindowOuter::From(win);
|
2014-08-13 00:53:15 +04:00
|
|
|
nsCOMPtr<nsIDocShell> docShell = pwin->GetDocShell();
|
|
|
|
if (!docShell) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2016-04-12 10:40:36 +03:00
|
|
|
nsCOMPtr<nsIDocument> doc = docShell->GetDocument();
|
|
|
|
NS_ENSURE_TRUE(doc, NS_OK);
|
2014-08-13 00:53:15 +04:00
|
|
|
|
|
|
|
// Notify nsIWebProgressListeners of this security event.
|
|
|
|
// Can be used to change the UI state.
|
|
|
|
nsCOMPtr<nsISecurityEventSink> eventSink = do_QueryInterface(docShell, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_OK);
|
|
|
|
uint32_t state = 0;
|
|
|
|
nsCOMPtr<nsISecureBrowserUI> securityUI;
|
|
|
|
docShell->GetSecurityUI(getter_AddRefs(securityUI));
|
|
|
|
if (!securityUI) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
doc->SetHasTrackingContentLoaded(true);
|
|
|
|
securityUI->GetState(&state);
|
|
|
|
state |= nsIWebProgressListener::STATE_LOADED_TRACKING_CONTENT;
|
|
|
|
eventSink->OnSecurityChange(nullptr, state);
|
|
|
|
|
2014-08-09 00:52:28 +04:00
|
|
|
return NS_OK;
|
2014-07-24 21:59:00 +04:00
|
|
|
}
|
|
|
|
|
2015-01-10 00:25:13 +03:00
|
|
|
void
|
2016-12-01 07:13:00 +03:00
|
|
|
nsChannelClassifier::Start()
|
2015-01-10 00:25:13 +03:00
|
|
|
{
|
2015-01-21 22:33:07 +03:00
|
|
|
nsresult rv = StartInternal();
|
2015-01-10 00:25:13 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// If we aren't getting a callback for any reason, assume a good verdict and
|
|
|
|
// make sure we resume the channel if necessary.
|
2017-02-21 04:46:36 +03:00
|
|
|
OnClassifyComplete(NS_OK, NS_LITERAL_CSTRING(""),NS_LITERAL_CSTRING(""),
|
|
|
|
NS_LITERAL_CSTRING(""));
|
2015-01-10 00:25:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-01-21 22:33:07 +03:00
|
|
|
nsChannelClassifier::StartInternal()
|
2010-05-22 01:03:02 +04:00
|
|
|
{
|
2014-11-12 02:05:55 +03:00
|
|
|
// Should only be called in the parent process.
|
2015-07-04 04:29:00 +03:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
2014-11-12 02:05:55 +03:00
|
|
|
|
2010-05-22 01:03:02 +04:00
|
|
|
// Don't bother to run the classifier on a load that has already failed.
|
|
|
|
// (this might happen after a redirect)
|
2012-07-27 17:59:29 +04:00
|
|
|
nsresult status;
|
2015-01-21 22:33:07 +03:00
|
|
|
mChannel->GetStatus(&status);
|
2010-05-22 01:03:02 +04:00
|
|
|
if (NS_FAILED(status))
|
2015-01-10 00:25:13 +03:00
|
|
|
return status;
|
2010-05-22 01:03:02 +04:00
|
|
|
|
|
|
|
// Don't bother to run the classifier on a cached load that was
|
2015-01-10 00:25:13 +03:00
|
|
|
// previously classified as good.
|
2015-01-21 22:33:07 +03:00
|
|
|
if (HasBeenClassified(mChannel)) {
|
2015-01-10 00:25:13 +03:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2010-05-22 01:03:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
2015-01-21 22:33:07 +03:00
|
|
|
nsresult rv = mChannel->GetURI(getter_AddRefs(uri));
|
2010-05-22 01:03:02 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Don't bother checking certain types of URIs.
|
2017-05-23 22:59:25 +03:00
|
|
|
bool isAbout = false;
|
|
|
|
rv = uri->SchemeIs("about", &isAbout);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (isAbout) return NS_ERROR_UNEXPECTED;
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool hasFlags;
|
2010-05-22 01:03:02 +04:00
|
|
|
rv = NS_URIChainHasFlags(uri,
|
|
|
|
nsIProtocolHandler::URI_DANGEROUS_TO_LOAD,
|
|
|
|
&hasFlags);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2015-01-10 00:25:13 +03:00
|
|
|
if (hasFlags) return NS_ERROR_UNEXPECTED;
|
2010-05-22 01:03:02 +04:00
|
|
|
|
|
|
|
rv = NS_URIChainHasFlags(uri,
|
|
|
|
nsIProtocolHandler::URI_IS_LOCAL_FILE,
|
|
|
|
&hasFlags);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2015-01-10 00:25:13 +03:00
|
|
|
if (hasFlags) return NS_ERROR_UNEXPECTED;
|
2010-05-22 01:03:02 +04:00
|
|
|
|
|
|
|
rv = NS_URIChainHasFlags(uri,
|
|
|
|
nsIProtocolHandler::URI_IS_UI_RESOURCE,
|
|
|
|
&hasFlags);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2015-01-10 00:25:13 +03:00
|
|
|
if (hasFlags) return NS_ERROR_UNEXPECTED;
|
2010-05-22 01:03:02 +04:00
|
|
|
|
|
|
|
rv = NS_URIChainHasFlags(uri,
|
|
|
|
nsIProtocolHandler::URI_IS_LOCAL_RESOURCE,
|
|
|
|
&hasFlags);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2015-01-10 00:25:13 +03:00
|
|
|
if (hasFlags) return NS_ERROR_UNEXPECTED;
|
2010-05-22 01:03:02 +04:00
|
|
|
|
2017-04-18 12:00:27 +03:00
|
|
|
nsCString skipHostnames = CachedPrefs::GetInstance()->GetSkipHostnames();
|
|
|
|
if (!skipHostnames.IsEmpty()) {
|
2016-03-12 00:57:38 +03:00
|
|
|
LOG(("nsChannelClassifier[%p]:StartInternal whitelisted hostnames = %s",
|
2017-04-18 12:00:27 +03:00
|
|
|
this, skipHostnames.get()));
|
|
|
|
if (IsHostnameWhitelisted(uri, skipHostnames)) {
|
2016-03-12 00:57:38 +03:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-22 01:03:02 +04:00
|
|
|
nsCOMPtr<nsIURIClassifier> uriClassifier =
|
|
|
|
do_GetService(NS_URICLASSIFIERSERVICE_CONTRACTID, &rv);
|
|
|
|
if (rv == NS_ERROR_FACTORY_NOT_REGISTERED ||
|
|
|
|
rv == NS_ERROR_NOT_AVAILABLE) {
|
|
|
|
// no URI classifier, ignore this failure.
|
2015-01-10 00:25:13 +03:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2010-05-22 01:03:02 +04:00
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-07-20 20:46:59 +04:00
|
|
|
nsCOMPtr<nsIScriptSecurityManager> securityManager =
|
|
|
|
do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> principal;
|
2015-02-13 09:23:50 +03:00
|
|
|
rv = securityManager->GetChannelURIPrincipal(mChannel, getter_AddRefs(principal));
|
2012-07-20 20:46:59 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool expectCallback;
|
2015-09-26 06:39:49 +03:00
|
|
|
if (LOG_ENABLED()) {
|
2015-02-13 09:23:50 +03:00
|
|
|
nsCOMPtr<nsIURI> principalURI;
|
|
|
|
principal->GetURI(getter_AddRefs(principalURI));
|
2015-09-26 06:39:49 +03:00
|
|
|
LOG(("nsChannelClassifier[%p]: Classifying principal %s on channel with "
|
2016-08-26 09:02:31 +03:00
|
|
|
"uri %s", this, principalURI->GetSpecOrDefault().get(),
|
|
|
|
uri->GetSpecOrDefault().get()));
|
2015-02-13 09:23:50 +03:00
|
|
|
}
|
2017-03-08 06:06:36 +03:00
|
|
|
// The classify is running in parent process, no need to give a valid event
|
|
|
|
// target
|
2017-04-18 12:00:27 +03:00
|
|
|
rv = uriClassifier->Classify(principal, nullptr,
|
2017-06-13 02:16:00 +03:00
|
|
|
ShouldEnableTrackingProtection(),
|
2016-12-01 07:13:00 +03:00
|
|
|
this, &expectCallback);
|
2015-01-10 00:25:13 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2010-05-22 01:03:02 +04:00
|
|
|
|
|
|
|
if (expectCallback) {
|
|
|
|
// Suspend the channel, it will be resumed when we get the classifier
|
|
|
|
// callback.
|
2015-01-21 22:33:07 +03:00
|
|
|
rv = mChannel->Suspend();
|
2010-05-22 01:03:02 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// Some channels (including nsJSChannel) fail on Suspend. This
|
|
|
|
// shouldn't be fatal, but will prevent malware from being
|
|
|
|
// blocked on these channels.
|
2015-01-10 00:25:13 +03:00
|
|
|
LOG(("nsChannelClassifier[%p]: Couldn't suspend channel", this));
|
|
|
|
return rv;
|
2010-05-22 01:03:02 +04:00
|
|
|
}
|
|
|
|
|
2015-01-10 00:25:13 +03:00
|
|
|
mSuspendedChannel = true;
|
2010-05-22 01:03:02 +04:00
|
|
|
LOG(("nsChannelClassifier[%p]: suspended channel %p",
|
2015-01-10 00:25:13 +03:00
|
|
|
this, mChannel.get()));
|
|
|
|
} else {
|
|
|
|
LOG(("nsChannelClassifier[%p]: not expecting callback", this));
|
|
|
|
return NS_ERROR_FAILURE;
|
2010-05-22 01:03:02 +04:00
|
|
|
}
|
|
|
|
|
2017-01-12 13:07:22 +03:00
|
|
|
// Add an observer for shutdown
|
2017-02-24 05:14:07 +03:00
|
|
|
AddShutdownObserver();
|
2010-05-22 01:03:02 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-03-12 00:57:38 +03:00
|
|
|
bool
|
|
|
|
nsChannelClassifier::IsHostnameWhitelisted(nsIURI *aUri,
|
|
|
|
const nsACString &aWhitelisted)
|
|
|
|
{
|
|
|
|
nsAutoCString host;
|
|
|
|
nsresult rv = aUri->GetHost(host);
|
|
|
|
if (NS_FAILED(rv) || host.IsEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ToLowerCase(host);
|
|
|
|
|
|
|
|
nsCCharSeparatedTokenizer tokenizer(aWhitelisted, ',');
|
|
|
|
while (tokenizer.hasMoreTokens()) {
|
2017-06-20 12:19:52 +03:00
|
|
|
const nsACString& token = tokenizer.nextToken();
|
2016-03-12 00:57:38 +03:00
|
|
|
if (token.Equals(host)) {
|
|
|
|
LOG(("nsChannelClassifier[%p]:StartInternal skipping %s (whitelisted)",
|
|
|
|
this, host.get()));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-05-22 01:03:02 +04:00
|
|
|
// Note in the cache entry that this URL was classified, so that future
|
|
|
|
// cached loads don't need to be checked.
|
|
|
|
void
|
|
|
|
nsChannelClassifier::MarkEntryClassified(nsresult status)
|
|
|
|
{
|
2014-11-12 02:05:55 +03:00
|
|
|
// Should only be called in the parent process.
|
2015-07-04 04:29:00 +03:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
2014-11-12 02:05:55 +03:00
|
|
|
|
2014-08-09 00:52:28 +04:00
|
|
|
// Don't cache tracking classifications because we support allowlisting.
|
2014-08-23 02:17:32 +04:00
|
|
|
if (status == NS_ERROR_TRACKING_URI || mIsAllowListed) {
|
2014-08-09 00:52:28 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-03 01:54:59 +03:00
|
|
|
if (LOG_ENABLED()) {
|
|
|
|
nsAutoCString errorName;
|
2016-05-19 05:02:57 +03:00
|
|
|
GetErrorName(status, errorName);
|
2016-03-03 01:54:59 +03:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
mChannel->GetURI(getter_AddRefs(uri));
|
|
|
|
nsAutoCString spec;
|
|
|
|
uri->GetAsciiSpec(spec);
|
|
|
|
LOG(("nsChannelClassifier::MarkEntryClassified[%s] %s",
|
|
|
|
errorName.get(), spec.get()));
|
|
|
|
}
|
|
|
|
|
2015-01-10 00:25:13 +03:00
|
|
|
nsCOMPtr<nsICachingChannel> cachingChannel = do_QueryInterface(mChannel);
|
2010-05-22 01:03:02 +04:00
|
|
|
if (!cachingChannel) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> cacheToken;
|
|
|
|
cachingChannel->GetCacheToken(getter_AddRefs(cacheToken));
|
|
|
|
if (!cacheToken) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-23 02:17:32 +04:00
|
|
|
nsCOMPtr<nsICacheEntry> cacheEntry =
|
2010-05-22 01:03:02 +04:00
|
|
|
do_QueryInterface(cacheToken);
|
|
|
|
if (!cacheEntry) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cacheEntry->SetMetaDataElement("necko:classified",
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_SUCCEEDED(status) ? "1" : nullptr);
|
2010-05-22 01:03:02 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2010-05-22 01:03:02 +04:00
|
|
|
nsChannelClassifier::HasBeenClassified(nsIChannel *aChannel)
|
|
|
|
{
|
2014-11-12 02:05:55 +03:00
|
|
|
// Should only be called in the parent process.
|
2015-07-04 04:29:00 +03:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
2014-11-12 02:05:55 +03:00
|
|
|
|
2010-05-22 01:03:02 +04:00
|
|
|
nsCOMPtr<nsICachingChannel> cachingChannel =
|
|
|
|
do_QueryInterface(aChannel);
|
|
|
|
if (!cachingChannel) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-05-22 01:03:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Only check the tag if we are loading from the cache without
|
|
|
|
// validation.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool fromCache;
|
2010-05-22 01:03:02 +04:00
|
|
|
if (NS_FAILED(cachingChannel->IsFromCache(&fromCache)) || !fromCache) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-05-22 01:03:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> cacheToken;
|
|
|
|
cachingChannel->GetCacheToken(getter_AddRefs(cacheToken));
|
|
|
|
if (!cacheToken) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-05-22 01:03:02 +04:00
|
|
|
}
|
|
|
|
|
2014-08-23 02:17:32 +04:00
|
|
|
nsCOMPtr<nsICacheEntry> cacheEntry =
|
2010-05-22 01:03:02 +04:00
|
|
|
do_QueryInterface(cacheToken);
|
|
|
|
if (!cacheEntry) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-05-22 01:03:02 +04:00
|
|
|
}
|
|
|
|
|
2017-08-16 06:58:35 +03:00
|
|
|
nsCString tag;
|
2010-05-22 01:03:02 +04:00
|
|
|
cacheEntry->GetMetaDataElement("necko:classified", getter_Copies(tag));
|
|
|
|
return tag.EqualsLiteral("1");
|
|
|
|
}
|
|
|
|
|
2015-08-13 12:14:36 +03:00
|
|
|
//static
|
|
|
|
bool
|
|
|
|
nsChannelClassifier::SameLoadingURI(nsIDocument *aDoc, nsIChannel *aChannel)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> docURI = aDoc->GetDocumentURI();
|
|
|
|
nsCOMPtr<nsILoadInfo> channelLoadInfo = aChannel->GetLoadInfo();
|
|
|
|
if (!channelLoadInfo || !docURI) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-14 02:30:25 +03:00
|
|
|
|
2015-08-13 12:14:36 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> channelLoadingPrincipal = channelLoadInfo->LoadingPrincipal();
|
|
|
|
if (!channelLoadingPrincipal) {
|
2016-04-14 02:30:25 +03:00
|
|
|
// TYPE_DOCUMENT loads will not have a channelLoadingPrincipal. But top level
|
|
|
|
// loads should not be blocked by Tracking Protection, so we will return
|
|
|
|
// false
|
2015-08-13 12:14:36 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIURI> channelLoadingURI;
|
|
|
|
channelLoadingPrincipal->GetURI(getter_AddRefs(channelLoadingURI));
|
|
|
|
if (!channelLoadingURI) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool equals = false;
|
|
|
|
nsresult rv = docURI->EqualsExceptRef(channelLoadingURI, &equals);
|
|
|
|
return NS_SUCCEEDED(rv) && equals;
|
|
|
|
}
|
|
|
|
|
2014-11-12 02:05:55 +03:00
|
|
|
// static
|
2014-08-13 00:53:15 +04:00
|
|
|
nsresult
|
2017-02-21 04:46:36 +03:00
|
|
|
nsChannelClassifier::SetBlockedContent(nsIChannel *channel,
|
|
|
|
nsresult aErrorCode,
|
|
|
|
const nsACString& aList,
|
|
|
|
const nsACString& aProvider,
|
|
|
|
const nsACString& aPrefix)
|
2014-08-13 00:53:15 +04:00
|
|
|
{
|
2017-02-21 04:46:36 +03:00
|
|
|
NS_ENSURE_ARG(!aList.IsEmpty());
|
|
|
|
NS_ENSURE_ARG(!aPrefix.IsEmpty());
|
|
|
|
|
2014-11-12 02:05:55 +03:00
|
|
|
// Can be called in EITHER the parent or child process.
|
|
|
|
nsCOMPtr<nsIParentChannel> parentChannel;
|
|
|
|
NS_QueryNotificationCallbacks(channel, parentChannel);
|
|
|
|
if (parentChannel) {
|
2017-02-21 04:46:36 +03:00
|
|
|
// This channel is a parent-process proxy for a child process request.
|
|
|
|
// Tell the child process channel to do this instead.
|
|
|
|
parentChannel->SetClassifierMatchedInfo(aList, aProvider, aPrefix);
|
2014-11-12 02:05:55 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-08-13 00:53:15 +04:00
|
|
|
nsresult rv;
|
2017-02-21 04:46:36 +03:00
|
|
|
nsCOMPtr<nsIClassifiedChannel> classifiedChannel = do_QueryInterface(channel, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (classifiedChannel) {
|
|
|
|
classifiedChannel->SetMatchedInfo(aList, aProvider, aPrefix);
|
|
|
|
}
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<mozIDOMWindowProxy> win;
|
2014-08-13 00:53:15 +04:00
|
|
|
nsCOMPtr<mozIThirdPartyUtil> thirdPartyUtil =
|
|
|
|
do_GetService(THIRDPARTYUTIL_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_OK);
|
2014-11-12 02:05:55 +03:00
|
|
|
rv = thirdPartyUtil->GetTopWindowForChannel(channel, getter_AddRefs(win));
|
2014-08-13 00:53:15 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_OK);
|
2016-01-30 20:05:36 +03:00
|
|
|
auto* pwin = nsPIDOMWindowOuter::From(win);
|
2014-08-13 00:53:15 +04:00
|
|
|
nsCOMPtr<nsIDocShell> docShell = pwin->GetDocShell();
|
|
|
|
if (!docShell) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2016-04-12 10:40:36 +03:00
|
|
|
nsCOMPtr<nsIDocument> doc = docShell->GetDocument();
|
|
|
|
NS_ENSURE_TRUE(doc, NS_OK);
|
2014-08-13 00:53:15 +04:00
|
|
|
|
2015-08-13 12:14:36 +03:00
|
|
|
// This event might come after the user has navigated to another page.
|
|
|
|
// To prevent showing the TrackingProtection UI on the wrong page, we need to
|
|
|
|
// check that the loading URI for the channel is the same as the URI currently
|
|
|
|
// loaded in the document.
|
|
|
|
if (!SameLoadingURI(doc, channel)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-08-13 00:53:15 +04:00
|
|
|
// Notify nsIWebProgressListeners of this security event.
|
|
|
|
// Can be used to change the UI state.
|
|
|
|
nsCOMPtr<nsISecurityEventSink> eventSink = do_QueryInterface(docShell, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_OK);
|
|
|
|
uint32_t state = 0;
|
|
|
|
nsCOMPtr<nsISecureBrowserUI> securityUI;
|
|
|
|
docShell->GetSecurityUI(getter_AddRefs(securityUI));
|
|
|
|
if (!securityUI) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
securityUI->GetState(&state);
|
2017-02-21 04:46:36 +03:00
|
|
|
if (aErrorCode == NS_ERROR_TRACKING_URI) {
|
|
|
|
doc->SetHasTrackingContentBlocked(true);
|
|
|
|
state |= nsIWebProgressListener::STATE_BLOCKED_TRACKING_CONTENT;
|
|
|
|
} else {
|
|
|
|
state |= nsIWebProgressListener::STATE_BLOCKED_UNSAFE_CONTENT;
|
|
|
|
}
|
|
|
|
|
2014-08-13 00:53:15 +04:00
|
|
|
eventSink->OnSecurityChange(nullptr, state);
|
|
|
|
|
2014-10-10 23:19:14 +04:00
|
|
|
// Log a warning to the web console.
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
channel->GetURI(getter_AddRefs(uri));
|
2016-08-26 09:02:31 +03:00
|
|
|
NS_ConvertUTF8toUTF16 spec(uri->GetSpecOrDefault());
|
2014-10-10 23:19:14 +04:00
|
|
|
const char16_t* params[] = { spec.get() };
|
2017-02-21 04:46:36 +03:00
|
|
|
const char* message = (aErrorCode == NS_ERROR_TRACKING_URI) ?
|
|
|
|
"TrackingUriBlocked" : "UnsafeUriBlocked";
|
|
|
|
nsCString category = (aErrorCode == NS_ERROR_TRACKING_URI) ?
|
|
|
|
NS_LITERAL_CSTRING("Tracking Protection") :
|
|
|
|
NS_LITERAL_CSTRING("Safe Browsing");
|
|
|
|
|
2014-10-10 23:19:14 +04:00
|
|
|
nsContentUtils::ReportToConsole(nsIScriptError::warningFlag,
|
2017-02-21 04:46:36 +03:00
|
|
|
category,
|
2014-10-10 23:19:14 +04:00
|
|
|
doc,
|
|
|
|
nsContentUtils::eNECKO_PROPERTIES,
|
2017-02-21 04:46:36 +03:00
|
|
|
message,
|
2014-10-10 23:19:14 +04:00
|
|
|
params, ArrayLength(params));
|
|
|
|
|
2014-08-13 00:53:15 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-03-01 06:27:51 +03:00
|
|
|
namespace {
|
|
|
|
|
2017-06-13 02:16:00 +03:00
|
|
|
// The purpose of this class is only for implementing all nsISupports methods.
|
|
|
|
// This is a workaround for template derived class.
|
|
|
|
class URIClassifierCallbackBase : public nsIURIClassifierCallback {
|
2017-03-01 06:27:51 +03:00
|
|
|
public:
|
2017-06-13 02:16:00 +03:00
|
|
|
URIClassifierCallbackBase() = default;
|
|
|
|
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual ~URIClassifierCallbackBase() = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(URIClassifierCallbackBase, nsIURIClassifierCallback)
|
|
|
|
|
|
|
|
// A template class for reusing the code.
|
|
|
|
// OnClassifyCompleteInternal will be called to pass the result.
|
|
|
|
template<class T>
|
|
|
|
class IsTrackerWhitelistedCallback final : public URIClassifierCallbackBase {
|
|
|
|
public:
|
|
|
|
explicit IsTrackerWhitelistedCallback(T* aClosure,
|
2017-03-01 06:27:51 +03:00
|
|
|
const nsACString& aList,
|
|
|
|
const nsACString& aProvider,
|
|
|
|
const nsACString& aPrefix,
|
2017-06-13 02:16:00 +03:00
|
|
|
nsIURI* aWhitelistURI)
|
2017-03-01 06:27:51 +03:00
|
|
|
: mClosure(aClosure)
|
2017-06-13 02:16:00 +03:00
|
|
|
, mWhitelistURI(aWhitelistURI)
|
2017-03-01 06:27:51 +03:00
|
|
|
, mList(aList)
|
|
|
|
, mProvider(aProvider)
|
|
|
|
, mPrefix(aPrefix)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-06-13 02:16:00 +03:00
|
|
|
NS_IMETHOD OnClassifyComplete(nsresult /*aErrorCode*/,
|
|
|
|
const nsACString& aLists, // Only this matters.
|
|
|
|
const nsACString& /*aProvider*/,
|
|
|
|
const nsACString& /*aPrefix*/) override
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
if (aLists.IsEmpty()) {
|
|
|
|
if (LOG_ENABLED()) {
|
|
|
|
MOZ_ASSERT(mWhitelistURI);
|
|
|
|
|
|
|
|
LOG(("nsChannelClassifier[%p]: %s is not in the whitelist",
|
|
|
|
mClosure.get(), mWhitelistURI->GetSpecOrDefault().get()));
|
|
|
|
}
|
|
|
|
rv = NS_ERROR_TRACKING_URI;
|
|
|
|
} else {
|
|
|
|
LOG(("nsChannelClassifier[%p]:OnClassifyComplete tracker found "
|
|
|
|
"in whitelist so we won't block it", mClosure.get()));
|
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = mClosure->OnClassifyCompleteInternal(rv, mList, mProvider, mPrefix);
|
|
|
|
mClosure = nullptr;
|
|
|
|
return rv;
|
|
|
|
}
|
2017-03-01 06:27:51 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
~IsTrackerWhitelistedCallback() = default;
|
|
|
|
|
2017-06-13 02:16:00 +03:00
|
|
|
RefPtr<T> mClosure;
|
|
|
|
nsCOMPtr<nsIURI> mWhitelistURI;
|
2017-03-01 06:27:51 +03:00
|
|
|
|
|
|
|
// The following 3 values are for forwarding the callback.
|
|
|
|
nsCString mList;
|
|
|
|
nsCString mProvider;
|
|
|
|
nsCString mPrefix;
|
|
|
|
};
|
|
|
|
|
2017-06-13 02:16:00 +03:00
|
|
|
// This class is designed to get the results of checking blacklist and whitelist.
|
|
|
|
// 1. The result of local blacklist will be sent back via
|
|
|
|
// OnClassifyComplete, which is called by nsIURIClassifier service.
|
|
|
|
// 2. The result of local whitelist is got via OnClassifyCompleteInternal,
|
|
|
|
// which is called by IsTrackerWhitelistedCallback::OnClassifyComplete.
|
|
|
|
class IsTrackerBlacklistedCallback final : public nsIURIClassifierCallback {
|
|
|
|
public:
|
|
|
|
explicit IsTrackerBlacklistedCallback(nsChannelClassifier* aChannelClassifier,
|
|
|
|
nsIURIClassifierCallback* aCallback)
|
|
|
|
: mChannelClassifier(aChannelClassifier)
|
|
|
|
, mChannelCallback(aCallback)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
NS_DECL_NSIURICLASSIFIERCALLBACK
|
|
|
|
|
|
|
|
nsresult OnClassifyCompleteInternal(nsresult aErrorCode,
|
|
|
|
const nsACString& aList,
|
|
|
|
const nsACString& aProvider,
|
|
|
|
const nsACString& aPrefix);
|
2017-03-01 06:27:51 +03:00
|
|
|
|
2017-06-13 02:16:00 +03:00
|
|
|
private:
|
|
|
|
~IsTrackerBlacklistedCallback() = default;
|
|
|
|
|
|
|
|
RefPtr<nsChannelClassifier> mChannelClassifier;
|
|
|
|
nsCOMPtr<nsIURIClassifierCallback> mChannelCallback;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(IsTrackerBlacklistedCallback, nsIURIClassifierCallback)
|
2017-03-01 06:27:51 +03:00
|
|
|
|
|
|
|
/*virtual*/ nsresult
|
2017-06-13 02:16:00 +03:00
|
|
|
IsTrackerBlacklistedCallback::OnClassifyComplete(nsresult aErrorCode,
|
|
|
|
const nsACString& aLists,
|
|
|
|
const nsACString& aProvider,
|
|
|
|
const nsACString& aPrefix)
|
2017-03-01 06:27:51 +03:00
|
|
|
{
|
2017-06-13 02:16:00 +03:00
|
|
|
nsresult status = aLists.IsEmpty() ? NS_OK : NS_ERROR_TRACKING_URI;
|
|
|
|
bool tpEnabled = mChannelClassifier->ShouldEnableTrackingProtection();
|
|
|
|
|
|
|
|
LOG(("IsTrackerBlacklistedCallback[%p]:OnClassifyComplete "
|
|
|
|
" status=0x%" PRIx32 ", tpEnabled=%d",
|
|
|
|
mChannelClassifier.get(), static_cast<uint32_t>(status), tpEnabled));
|
|
|
|
|
|
|
|
// If this is not in local blacklist or tracking protection is enabled,
|
|
|
|
// directly send the status back.
|
|
|
|
// The whitelist will be checked at nsChannelClassifier::OnClassifyComplete
|
|
|
|
// when tracking protection is enabled, so we can just return here.
|
|
|
|
if (NS_SUCCEEDED(status) || tpEnabled) {
|
|
|
|
return mChannelCallback->OnClassifyComplete(
|
|
|
|
status, aLists, aProvider, aPrefix);
|
2017-03-01 06:27:51 +03:00
|
|
|
}
|
|
|
|
|
2017-06-13 02:16:00 +03:00
|
|
|
nsCOMPtr<nsIChannel> channel = mChannelClassifier->GetChannel();
|
|
|
|
if (LOG_ENABLED()) {
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
channel->GetURI(getter_AddRefs(uri));
|
|
|
|
LOG(("IsTrackerBlacklistedCallback[%p]:OnClassifyComplete channel [%p] "
|
|
|
|
"uri=%s, is in blacklist. Start checking whitelist.",
|
|
|
|
mChannelClassifier.get(), channel.get(),
|
|
|
|
uri->GetSpecOrDefault().get()));
|
|
|
|
}
|
2017-03-01 06:27:51 +03:00
|
|
|
|
2017-06-13 02:16:00 +03:00
|
|
|
nsCOMPtr<nsIURI> whitelistURI = mChannelClassifier->CreateWhiteListURI();
|
|
|
|
nsCOMPtr<nsIURIClassifierCallback> callback =
|
|
|
|
new IsTrackerWhitelistedCallback<IsTrackerBlacklistedCallback>(
|
|
|
|
this, aLists, aProvider, aPrefix, whitelistURI);
|
|
|
|
|
|
|
|
// If IsTrackerWhitelisted has failed, it means the uri is not in whitelist.
|
|
|
|
if (NS_FAILED(mChannelClassifier->IsTrackerWhitelisted(whitelistURI, callback))) {
|
|
|
|
LOG(("IsTrackerBlacklistedCallback[%p]:OnClassifyComplete channel [%p] "
|
|
|
|
"IsTrackerWhitelisted has failed.",
|
|
|
|
mChannelClassifier.get(), channel.get()));
|
|
|
|
|
|
|
|
MOZ_ASSERT(mChannelClassifier->ShouldEnableTrackingAnnotation());
|
|
|
|
|
|
|
|
SetIsTrackingResourceHelper(channel);
|
|
|
|
if (CachedPrefs::GetInstance()->IsLowerNetworkPriority()) {
|
|
|
|
LowerPriorityHelper(channel);
|
|
|
|
}
|
2017-06-22 02:55:00 +03:00
|
|
|
SetThrottleableHelper(channel);
|
2017-06-13 02:16:00 +03:00
|
|
|
|
|
|
|
// We don't want to disable speculative connection when tracking protection
|
|
|
|
// is disabled. So, change the status to NS_OK.
|
|
|
|
status = NS_OK;
|
|
|
|
|
|
|
|
return mChannelCallback->OnClassifyComplete(
|
|
|
|
status, aLists, aProvider, aPrefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnClassifyCompleteInternal() will be called once we know
|
|
|
|
// if the tracker is whitelisted.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2017-03-01 06:27:51 +03:00
|
|
|
|
2015-08-07 23:08:22 +03:00
|
|
|
nsresult
|
2017-06-13 02:16:00 +03:00
|
|
|
IsTrackerBlacklistedCallback::OnClassifyCompleteInternal(nsresult aErrorCode,
|
|
|
|
const nsACString& aLists,
|
|
|
|
const nsACString& aProvider,
|
|
|
|
const nsACString& aPrefix)
|
2015-08-07 23:08:22 +03:00
|
|
|
{
|
2017-06-13 02:16:00 +03:00
|
|
|
LOG(("IsTrackerBlacklistedCallback[%p]:OnClassifyCompleteInternal"
|
|
|
|
" status=0x%" PRIx32,
|
|
|
|
mChannelClassifier.get(), static_cast<uint32_t>(aErrorCode)));
|
2015-08-07 23:08:22 +03:00
|
|
|
|
2017-06-13 02:16:00 +03:00
|
|
|
if (NS_SUCCEEDED(aErrorCode)) {
|
|
|
|
return mChannelCallback->OnClassifyComplete(
|
|
|
|
aErrorCode, aLists, aProvider, aPrefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mChannelClassifier->ShouldEnableTrackingAnnotation());
|
|
|
|
MOZ_ASSERT(aErrorCode == NS_ERROR_TRACKING_URI);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIChannel> channel = mChannelClassifier->GetChannel();
|
|
|
|
if (LOG_ENABLED()) {
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
channel->GetURI(getter_AddRefs(uri));
|
|
|
|
LOG(("IsTrackerBlacklistedCallback[%p]:OnClassifyCompleteInternal "
|
|
|
|
"channel [%p] uri=%s, is not in whitelist",
|
|
|
|
mChannelClassifier.get(), channel.get(),
|
|
|
|
uri->GetSpecOrDefault().get()));
|
2015-08-07 23:08:22 +03:00
|
|
|
}
|
|
|
|
|
2017-06-13 02:16:00 +03:00
|
|
|
SetIsTrackingResourceHelper(channel);
|
|
|
|
if (CachedPrefs::GetInstance()->IsLowerNetworkPriority()) {
|
|
|
|
LowerPriorityHelper(channel);
|
|
|
|
}
|
2017-06-22 02:55:00 +03:00
|
|
|
SetThrottleableHelper(channel);
|
2017-06-13 02:16:00 +03:00
|
|
|
|
|
|
|
return mChannelCallback->OnClassifyComplete(
|
|
|
|
NS_OK, aLists, aProvider, aPrefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end of unnamed namespace/
|
|
|
|
|
|
|
|
already_AddRefed<nsIURI>
|
|
|
|
nsChannelClassifier::CreateWhiteListURI() const
|
|
|
|
{
|
|
|
|
nsresult rv;
|
2015-08-07 23:08:22 +03:00
|
|
|
nsCOMPtr<nsIHttpChannelInternal> chan = do_QueryInterface(mChannel, &rv);
|
2017-06-13 02:16:00 +03:00
|
|
|
if (!chan) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-08-07 23:08:22 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> topWinURI;
|
|
|
|
rv = chan->GetTopWindowURI(getter_AddRefs(topWinURI));
|
2017-06-13 02:16:00 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2015-08-07 23:08:22 +03:00
|
|
|
if (!topWinURI) {
|
|
|
|
LOG(("nsChannelClassifier[%p]: No window URI", this));
|
2017-06-13 02:16:00 +03:00
|
|
|
return nullptr;
|
2015-08-07 23:08:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIScriptSecurityManager> securityManager =
|
|
|
|
do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv);
|
2017-06-13 02:16:00 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2015-08-07 23:08:22 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> chanPrincipal;
|
|
|
|
rv = securityManager->GetChannelURIPrincipal(mChannel,
|
|
|
|
getter_AddRefs(chanPrincipal));
|
2017-06-13 02:16:00 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-08-07 23:08:22 +03:00
|
|
|
|
|
|
|
// Craft a whitelist URL like "toplevel.page/?resource=third.party.domain"
|
|
|
|
nsAutoCString pageHostname, resourceDomain;
|
|
|
|
rv = topWinURI->GetHost(pageHostname);
|
2017-06-13 02:16:00 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2015-08-07 23:08:22 +03:00
|
|
|
rv = chanPrincipal->GetBaseDomain(resourceDomain);
|
2017-06-13 02:16:00 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2015-08-07 23:08:22 +03:00
|
|
|
nsAutoCString whitelistEntry = NS_LITERAL_CSTRING("http://") +
|
|
|
|
pageHostname + NS_LITERAL_CSTRING("/?resource=") + resourceDomain;
|
|
|
|
LOG(("nsChannelClassifier[%p]: Looking for %s in the whitelist",
|
|
|
|
this, whitelistEntry.get()));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> whitelistURI;
|
|
|
|
rv = NS_NewURI(getter_AddRefs(whitelistURI), whitelistEntry);
|
2017-06-13 02:16:00 +03:00
|
|
|
|
|
|
|
return NS_SUCCEEDED(rv) ? whitelistURI.forget() : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsChannelClassifier::IsTrackerWhitelisted(nsIURI* aWhiteListURI,
|
|
|
|
nsIURIClassifierCallback *aCallback)
|
|
|
|
{
|
|
|
|
if (!aCallback || !aWhiteListURI) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIURIClassifier> uriClassifier =
|
|
|
|
do_GetService(NS_URICLASSIFIERSERVICE_CONTRACTID, &rv);
|
2015-08-07 23:08:22 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2017-06-13 02:16:00 +03:00
|
|
|
nsCString trackingWhitelist = CachedPrefs::GetInstance()->GetTrackingWhiteList();
|
|
|
|
if (trackingWhitelist.IsEmpty()) {
|
|
|
|
LOG(("nsChannelClassifier[%p]:IsTrackerWhitelisted whitelist disabled",
|
|
|
|
this));
|
|
|
|
return NS_ERROR_TRACKING_URI;
|
|
|
|
}
|
2015-08-07 23:08:22 +03:00
|
|
|
|
2017-06-13 02:16:00 +03:00
|
|
|
return uriClassifier->AsyncClassifyLocalWithTables(aWhiteListURI, trackingWhitelist, aCallback);
|
2015-08-07 23:08:22 +03:00
|
|
|
}
|
|
|
|
|
2010-05-22 01:03:02 +04:00
|
|
|
NS_IMETHODIMP
|
2017-02-21 04:46:36 +03:00
|
|
|
nsChannelClassifier::OnClassifyComplete(nsresult aErrorCode,
|
|
|
|
const nsACString& aList,
|
|
|
|
const nsACString& aProvider,
|
|
|
|
const nsACString& aPrefix)
|
2010-05-22 01:03:02 +04:00
|
|
|
{
|
2017-03-01 06:27:51 +03:00
|
|
|
// Should only be called in the parent process.
|
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
2014-11-12 02:05:55 +03:00
|
|
|
|
2017-06-13 02:16:00 +03:00
|
|
|
if (aErrorCode == NS_ERROR_TRACKING_URI) {
|
|
|
|
nsCOMPtr<nsIURI> whitelistURI = CreateWhiteListURI();
|
|
|
|
nsCOMPtr<nsIURIClassifierCallback> callback =
|
|
|
|
new IsTrackerWhitelistedCallback<nsChannelClassifier>(
|
|
|
|
this, aList, aProvider, aPrefix, whitelistURI);
|
|
|
|
if (whitelistURI &&
|
|
|
|
NS_SUCCEEDED(IsTrackerWhitelisted(whitelistURI, callback))) {
|
|
|
|
// OnClassifyCompleteInternal() will be called once we know
|
|
|
|
// if the tracker is whitelisted.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2017-03-01 06:27:51 +03:00
|
|
|
}
|
2015-08-07 23:08:22 +03:00
|
|
|
|
2017-03-01 06:27:51 +03:00
|
|
|
return OnClassifyCompleteInternal(aErrorCode, aList, aProvider, aPrefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsChannelClassifier::OnClassifyCompleteInternal(nsresult aErrorCode,
|
|
|
|
const nsACString& aList,
|
|
|
|
const nsACString& aProvider,
|
|
|
|
const nsACString& aPrefix)
|
|
|
|
{
|
2010-05-22 01:03:02 +04:00
|
|
|
if (mSuspendedChannel) {
|
2015-09-26 06:39:49 +03:00
|
|
|
nsAutoCString errorName;
|
|
|
|
if (LOG_ENABLED()) {
|
2016-05-19 05:02:57 +03:00
|
|
|
GetErrorName(aErrorCode, errorName);
|
2015-09-26 06:39:49 +03:00
|
|
|
LOG(("nsChannelClassifier[%p]:OnClassifyComplete %s (suspended channel)",
|
|
|
|
this, errorName.get()));
|
|
|
|
}
|
|
|
|
MarkEntryClassified(aErrorCode);
|
|
|
|
|
|
|
|
if (NS_FAILED(aErrorCode)) {
|
|
|
|
if (LOG_ENABLED()) {
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
mChannel->GetURI(getter_AddRefs(uri));
|
|
|
|
LOG(("nsChannelClassifier[%p]: cancelling channel %p for %s "
|
|
|
|
"with error code %s", this, mChannel.get(),
|
2016-08-26 09:02:31 +03:00
|
|
|
uri->GetSpecOrDefault().get(), errorName.get()));
|
2015-01-10 00:25:13 +03:00
|
|
|
}
|
2015-09-26 06:39:49 +03:00
|
|
|
|
2017-02-21 04:46:36 +03:00
|
|
|
// Channel will be cancelled (page element blocked) due to tracking
|
|
|
|
// protection or Safe Browsing.
|
2015-09-26 06:39:49 +03:00
|
|
|
// Do update the security state of the document and fire a security
|
|
|
|
// change event.
|
2017-02-21 04:46:36 +03:00
|
|
|
SetBlockedContent(mChannel, aErrorCode, aList, aProvider, aPrefix);
|
2015-09-26 06:39:49 +03:00
|
|
|
|
|
|
|
mChannel->Cancel(aErrorCode);
|
|
|
|
}
|
|
|
|
LOG(("nsChannelClassifier[%p]: resuming channel %p from "
|
|
|
|
"OnClassifyComplete", this, mChannel.get()));
|
|
|
|
mChannel->Resume();
|
2015-01-10 00:25:13 +03:00
|
|
|
}
|
2015-01-21 22:33:07 +03:00
|
|
|
|
2015-01-10 00:25:13 +03:00
|
|
|
mChannel = nullptr;
|
2017-02-24 05:14:07 +03:00
|
|
|
RemoveShutdownObserver();
|
2010-05-22 01:03:02 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2016-05-19 05:02:57 +03:00
|
|
|
|
2017-06-13 02:16:00 +03:00
|
|
|
nsresult
|
|
|
|
nsChannelClassifier::CheckIsTrackerWithLocalTable(nsIURIClassifierCallback* aCallback)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (!aCallback) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURIClassifier> uriClassifier =
|
|
|
|
do_GetService(NS_URICLASSIFIERSERVICE_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2017-06-20 05:59:00 +03:00
|
|
|
if (!ShouldEnableTrackingProtection() && !ShouldEnableTrackingAnnotation()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2017-06-13 02:16:00 +03:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
rv = mChannel->GetURI(getter_AddRefs(uri));
|
|
|
|
if (NS_FAILED(rv) || !uri) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCString trackingBlacklist =
|
|
|
|
CachedPrefs::GetInstance()->GetTrackingBlackList();
|
|
|
|
if (trackingBlacklist.IsEmpty()) {
|
|
|
|
LOG(("nsChannelClassifier[%p]:CheckIsTrackerWithLocalTable blacklist is empty",
|
|
|
|
this));
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURIClassifierCallback> callback =
|
|
|
|
new IsTrackerBlacklistedCallback(this, aCallback);
|
|
|
|
|
|
|
|
LOG(("nsChannelClassifier[%p]:CheckIsTrackerWithLocalTable for uri=%s\n",
|
|
|
|
this, uri->GetSpecOrDefault().get()));
|
|
|
|
return uriClassifier->AsyncClassifyLocalWithTables(uri,
|
|
|
|
trackingBlacklist,
|
|
|
|
callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsIChannel>
|
|
|
|
nsChannelClassifier::GetChannel()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIChannel> channel = mChannel;
|
|
|
|
return channel.forget();
|
|
|
|
}
|
|
|
|
|
2017-02-24 05:14:07 +03:00
|
|
|
void
|
|
|
|
nsChannelClassifier::AddShutdownObserver()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserverService> observerService = mozilla::services::GetObserverService();
|
|
|
|
if (observerService) {
|
|
|
|
observerService->AddObserver(this, "profile-change-net-teardown", false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsChannelClassifier::RemoveShutdownObserver()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserverService> observerService = mozilla::services::GetObserverService();
|
|
|
|
if (observerService) {
|
|
|
|
observerService->RemoveObserver(this, "profile-change-net-teardown");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-12 13:07:22 +03:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsIObserver implementation
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsChannelClassifier::Observe(nsISupports *aSubject, const char *aTopic,
|
|
|
|
const char16_t *aData)
|
|
|
|
{
|
|
|
|
if (!strcmp(aTopic, "profile-change-net-teardown")) {
|
|
|
|
// If we aren't getting a callback for any reason, make sure
|
|
|
|
// we resume the channel.
|
|
|
|
|
|
|
|
if (mChannel && mSuspendedChannel) {
|
|
|
|
mSuspendedChannel = false;
|
|
|
|
mChannel->Cancel(NS_ERROR_ABORT);
|
|
|
|
mChannel->Resume();
|
2017-06-13 02:16:00 +03:00
|
|
|
mChannel = nullptr;
|
2017-01-12 13:07:22 +03:00
|
|
|
}
|
2017-02-24 05:14:07 +03:00
|
|
|
|
|
|
|
RemoveShutdownObserver();
|
2017-01-12 13:07:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-05-04 13:14:04 +03:00
|
|
|
#undef LOG_ENABLED
|
|
|
|
|
2016-05-19 05:02:57 +03:00
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|