netwerk/url-classifier/UrlClassifierFeatureBase.cpp
author Dorel Luca <dluca@mozilla.com>
Thu, 14 Mar 2019 09:14:15 +0200
changeset 521851 4d0c32fbf17c2146a1c1a51afc458de79c10b705
parent 521846 38470d2dd98c7c7528c2756b006205f9c454ac0f
child 521916 83534ac4c0eb43b8cfa1c6f93431978fe11ba120
permissions -rw-r--r--
Backed out 5 changesets (bug 1533074) for eslint failure Backed out changeset 36c6a7178a5c (bug 1533074) Backed out changeset 7e6a8fadff5b (bug 1533074) Backed out changeset 2a0494fed543 (bug 1533074) Backed out changeset 38470d2dd98c (bug 1533074) Backed out changeset af4e03d1f5c8 (bug 1533074)

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */

#include "UrlClassifierFeatureBase.h"
#include "Classifier.h"
#include "mozilla/Preferences.h"

namespace mozilla {

using namespace safebrowsing;

namespace net {

namespace {

void OnPrefsChange(const char* aPrefName, nsTArray<nsCString>* aArray) {
  MOZ_ASSERT(aArray);

  nsAutoCString value;
  Preferences::GetCString(aPrefName, value);
  Classifier::SplitTables(value, *aArray);
}

}  // namespace

NS_INTERFACE_MAP_BEGIN(UrlClassifierFeatureBase)
  NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIUrlClassifierFeature)
  NS_INTERFACE_MAP_ENTRY(nsIUrlClassifierFeature)
  NS_INTERFACE_MAP_ENTRY(nsIUrlClassifierSkipListObserver)
NS_INTERFACE_MAP_END

NS_IMPL_ADDREF(UrlClassifierFeatureBase)
NS_IMPL_RELEASE(UrlClassifierFeatureBase)

UrlClassifierFeatureBase::UrlClassifierFeatureBase(
    const nsACString& aName, const nsACString& aPrefBlacklistTables,
    const nsACString& aPrefWhitelistTables,
    const nsACString& aPrefBlacklistHosts,
    const nsACString& aPrefWhitelistHosts,
    const nsACString& aPrefBlacklistTableName,
    const nsACString& aPrefWhitelistTableName, const nsACString& aPrefSkipHosts)
    : mName(aName), mPrefSkipHosts(aPrefSkipHosts) {
  static_assert(nsIUrlClassifierFeature::blacklist == 0,
                "nsIUrlClassifierFeature::blacklist must be 0");
  static_assert(nsIUrlClassifierFeature::whitelist == 1,
                "nsIUrlClassifierFeature::whitelist must be 1");

  mPrefTables[nsIUrlClassifierFeature::blacklist] = aPrefBlacklistTables;
  mPrefTables[nsIUrlClassifierFeature::whitelist] = aPrefWhitelistTables;

  mPrefHosts[nsIUrlClassifierFeature::blacklist] = aPrefBlacklistHosts;
  mPrefHosts[nsIUrlClassifierFeature::whitelist] = aPrefWhitelistHosts;

  mPrefTableNames[nsIUrlClassifierFeature::blacklist] = aPrefBlacklistTableName;
  mPrefTableNames[nsIUrlClassifierFeature::whitelist] = aPrefWhitelistTableName;
}

UrlClassifierFeatureBase::~UrlClassifierFeatureBase() = default;

void UrlClassifierFeatureBase::InitializePreferences() {
  for (uint32_t i = 0; i < 2; ++i) {
    if (!mPrefTables[i].IsEmpty()) {
      Preferences::RegisterCallbackAndCall(OnPrefsChange, mPrefTables[i],
                                           &mTables[i]);
    }

    if (!mPrefHosts[i].IsEmpty()) {
      Preferences::RegisterCallbackAndCall(OnPrefsChange, mPrefHosts[i],
                                           &mHosts[i]);
    }
  }

  nsCOMPtr<nsIUrlClassifierSkipListService> skipListService =
      do_GetService("@mozilla.org/url-classifier/skip-list-service;1");
  if (NS_WARN_IF(!skipListService)) {
    return;
  }

  skipListService->RegisterAndRunSkipListObserver(mName, mPrefSkipHosts, this);
}

void UrlClassifierFeatureBase::ShutdownPreferences() {
  for (uint32_t i = 0; i < 2; ++i) {
    if (!mPrefTables[i].IsEmpty()) {
      Preferences::UnregisterCallback(OnPrefsChange, mPrefTables[i],
                                      &mTables[i]);
    }

    if (!mPrefHosts[i].IsEmpty()) {
      Preferences::UnregisterCallback(OnPrefsChange, mPrefHosts[i], &mHosts[i]);
    }
  }

  nsCOMPtr<nsIUrlClassifierSkipListService> skipListService =
      do_GetService("@mozilla.org/url-classifier/skip-list-service;1");
  if (skipListService) {
    skipListService->UnregisterSkipListObserver(mName, this);
  }
}

NS_IMETHODIMP
UrlClassifierFeatureBase::OnSkipListUpdate(const nsACString& aList) {
  mSkipHosts = aList;
  return NS_OK;
}

NS_IMETHODIMP
UrlClassifierFeatureBase::GetName(nsACString& aName) {
  aName = mName;
  return NS_OK;
}

NS_IMETHODIMP
UrlClassifierFeatureBase::GetTables(nsIUrlClassifierFeature::listType aListType,
                                    nsTArray<nsCString>& aTables) {
  if (aListType != nsIUrlClassifierFeature::blacklist &&
      aListType != nsIUrlClassifierFeature::whitelist) {
    return NS_ERROR_INVALID_ARG;
  }

  aTables = mTables[aListType];
  return NS_OK;
}

NS_IMETHODIMP
UrlClassifierFeatureBase::HasTable(const nsACString& aTable,
                                   nsIUrlClassifierFeature::listType aListType,
                                   bool* aResult) {
  NS_ENSURE_ARG_POINTER(aResult);

  if (aListType != nsIUrlClassifierFeature::blacklist &&
      aListType != nsIUrlClassifierFeature::whitelist) {
    return NS_ERROR_INVALID_ARG;
  }

  *aResult = mTables[aListType].Contains(aTable);
  return NS_OK;
}

NS_IMETHODIMP
UrlClassifierFeatureBase::HasHostInPreferences(
    const nsACString& aHost, nsIUrlClassifierFeature::listType aListType,
    nsACString& aPrefTableName, bool* aResult) {
  NS_ENSURE_ARG_POINTER(aResult);

  if (aListType != nsIUrlClassifierFeature::blacklist &&
      aListType != nsIUrlClassifierFeature::whitelist) {
    return NS_ERROR_INVALID_ARG;
  }

  *aResult = mHosts[aListType].Contains(aHost);
  if (*aResult) {
    aPrefTableName = mPrefTableNames[aListType];
  }
  return NS_OK;
}

NS_IMETHODIMP
UrlClassifierFeatureBase::GetSkipHostList(nsACString& aList) {
  aList = mSkipHosts;
  return NS_OK;
}

bool UrlClassifierFeatureBase::IsAllowListed(
    nsIChannel* aChannel,
    AntiTrackingCommon::ContentBlockingAllowListPurpose aPurpose) {
  MOZ_ASSERT(aPurpose == AntiTrackingCommon::eTrackingProtection ||
             aPurpose == AntiTrackingCommon::eTrackingAnnotations ||
             aPurpose == AntiTrackingCommon::eFingerprinting ||
             aPurpose == AntiTrackingCommon::eCryptomining);

  nsCOMPtr<nsIHttpChannelInternal> channel = do_QueryInterface(aChannel);
  if (!channel) {
    UC_LOG(("nsChannelClassifier: Not an HTTP channel"));
    return false;
  }

  nsCOMPtr<nsIURI> topWinURI;
  nsresult rv = channel->GetTopWindowURI(getter_AddRefs(topWinURI));
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return false;
  }

  if (!topWinURI && StaticPrefs::channelclassifier_allowlist_example()) {
    UC_LOG(("nsChannelClassifier: Allowlisting test domain"));
    nsCOMPtr<nsIIOService> ios = services::GetIOService();
    if (NS_WARN_IF(!ios)) {
      return false;
    }

    rv = ios->NewURI(NS_LITERAL_CSTRING("http://allowlisted.example.com"),
                     nullptr, nullptr, getter_AddRefs(topWinURI));
    if (NS_WARN_IF(NS_FAILED(rv))) {
      return false;
    }
  }

  bool isAllowListed = false;
  rv = AntiTrackingCommon::IsOnContentBlockingAllowList(
      topWinURI, NS_UsePrivateBrowsing(aChannel), aPurpose, isAllowListed);
  if (NS_FAILED(rv)) {  // normal for some loads, no need to print a warning
    return false;
  }

  if (isAllowListed) {
    if (UC_LOG_ENABLED()) {
      nsCOMPtr<nsIURI> chanURI;
      nsresult rv = aChannel->GetURI(getter_AddRefs(chanURI));
      if (NS_WARN_IF(NS_FAILED(rv))) {
        return isAllowListed;
      }

      nsCString chanSpec = chanURI->GetSpecOrDefault();
      chanSpec.Truncate(
          std::min(chanSpec.Length(), UrlClassifierCommon::sMaxSpecLength));
      UC_LOG(("nsChannelClassifier: User override on channel[%p] (%s)",
              aChannel, chanSpec.get()));
    }
  }

  return isAllowListed;
}

}  // namespace net
}  // namespace mozilla