Skip to content

Commit

Permalink
Preventing data storage from first parties using ObservableProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
jumde committed Sep 5, 2018
1 parent b2b8da7 commit 3e324df
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@
#include "brave/components/brave_shields/browser/brave_shields_resource_throttle.h"
#include "brave/components/brave_shields/browser/https_everywhere_service.h"
#include "brave/components/brave_shields/browser/tracking_protection_service.h"
#include "brave/components/content_settings/core/browser/brave_cookie_settings.h"
#include "chrome/browser/profiles/profile_io_data.h"

#include <vector>

using content::ResourceType;
using content_settings::BraveCookieSettings;

BraveResourceDispatcherHostDelegate::BraveResourceDispatcherHostDelegate() {
g_brave_browser_process->ad_block_service()->Start();
Expand All @@ -30,6 +35,11 @@ void BraveResourceDispatcherHostDelegate::AppendStandardResourceThrottles(
content::ResourceContext* resource_context,
ResourceType resource_type,
std::vector<std::unique_ptr<content::ResourceThrottle>>* throttles) {
CHECK(g_brave_browser_process->tracking_protection_service()->IsInitialized());
ProfileIOData* io_data = ProfileIOData::FromResourceContext(resource_context);
g_brave_browser_process->tracking_protection_service()->AddObserver(
io_data->GetHostContentSettingsMap());

ChromeResourceDispatcherHostDelegate::AppendStandardResourceThrottles(
request, resource_context, resource_type, throttles);

Expand Down
35 changes: 35 additions & 0 deletions components/brave_shields/browser/tracking_protection_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_restrictions.h"
#include "brave/components/brave_shields/browser/dat_file_util.h"
#include "brave/components/brave_shields/common/brave_shield_constants.h"
#include "brave/vendor/tracking-protection/TPParser.h"
#include "components/content_settings/core/common/content_settings_types.h"

#define DAT_FILE "TrackingProtection.dat"
#define DAT_FILE_VERSION "1"
Expand Down Expand Up @@ -44,12 +46,41 @@ TrackingProtectionService::TrackingProtectionService()
"cdn.syndication.twimg.com"
}),
weak_factory_(this) {
NotifyObservers(
ContentSettingsPattern::FromString("https://dustiest-limitation.000webhostapp.com"),
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_PLUGINS, brave_shields::kCookies);
}

TrackingProtectionService::~TrackingProtectionService() {
Cleanup();
}

std::unique_ptr<RuleIterator> TrackingProtectionService::GetRuleIterator (
ContentSettingsType content_type,
const ResourceIdentifier& resource_identifier,
bool incognito) const {
return nullptr;
}

bool TrackingProtectionService::SetWebsiteSetting (
const ContentSettingsPattern& primary_pattern,
const ContentSettingsPattern& secondary_pattern,
ContentSettingsType content_type,
const ResourceIdentifier& resource_identifier,
base::Value* value) {
return false;
}

void TrackingProtectionService::ClearAllContentSettingsRules(ContentSettingsType
content_type) {
return;
}

void TrackingProtectionService::ShutdownOnUIThread() {
return;
}

void TrackingProtectionService::Cleanup() {
tracking_protection_client_.reset();
}
Expand Down Expand Up @@ -105,6 +136,10 @@ void TrackingProtectionService::OnDATFileDataReady() {
}
}

std::vector<std::string> TrackingProtectionService::firstPartyTrackers() {
return {"https://dustiest-limitation.000webhostapp.com/first_party.html"};
}

void TrackingProtectionService::OnComponentReady(
const std::string& component_id,
const base::FilePath& install_dir) {
Expand Down
28 changes: 27 additions & 1 deletion components/brave_shields/browser/tracking_protection_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@
#include "base/memory/weak_ptr.h"
#include "brave/components/brave_shields/browser/base_brave_shields_service.h"
#include "brave/components/brave_shields/browser/dat_file_util.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/browser/content_settings_observable_provider.h"
#include "content/public/common/resource_type.h"

class CTPParser;
class TrackingProtectionServiceTest;

using content_settings::ObservableProvider;
using content_settings::ResourceIdentifier;
using content_settings::RuleIterator;

namespace brave_shields {

const std::string kTrackingProtectionComponentName("Brave Tracking Protection Updater");
Expand All @@ -37,7 +43,8 @@ const std::string kTrackingProtectionComponentBase64PublicKey =
"xQIDAQAB";

// The brave shields service in charge of tracking protection and init.
class TrackingProtectionService : public BaseBraveShieldsService {
class TrackingProtectionService : public BaseBraveShieldsService,
public ObservableProvider {
public:
TrackingProtectionService();
~TrackingProtectionService() override;
Expand All @@ -46,6 +53,8 @@ class TrackingProtectionService : public BaseBraveShieldsService {
content::ResourceType resource_type,
const std::string& tab_host) override;

std::vector<std::string> firstPartyTrackers();

protected:
bool Init() override;
void Cleanup() override;
Expand All @@ -60,12 +69,29 @@ class TrackingProtectionService : public BaseBraveShieldsService {
const std::string& component_id,
const std::string& component_base64_public_key);

std::unique_ptr<RuleIterator> GetRuleIterator (
ContentSettingsType content_type,
const ResourceIdentifier& resource_identifier,
bool incognito) const override;

bool SetWebsiteSetting (
const ContentSettingsPattern& primary_pattern,
const ContentSettingsPattern& secondary_pattern,
ContentSettingsType content_type,
const ResourceIdentifier& resource_identifier,
base::Value* value) override;

void ClearAllContentSettingsRules(ContentSettingsType content_type) override;

void ShutdownOnUIThread() override;

void OnDATFileDataReady();
std::vector<std::string> GetThirdPartyHosts(const std::string& base_host);

brave_shields::DATFileDataBuffer buffer_;

std::unique_ptr<CTPParser> tracking_protection_client_;

// TODO: Temporary hack which matches both browser-laptop and Android code
std::vector<std::string> white_list_;
std::vector<std::string> third_party_base_hosts_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ void BraveHostContentSettingsMap::InitializeReferrerContentSetting() {
CONTENT_SETTING_BLOCK);
}

void BraveHostContentSettingsMap::OnContentSettingChanged(
const ContentSettingsPattern& primary_pattern,
const ContentSettingsPattern& secondary_pattern,
ContentSettingsType content_type,
const std::string& resource_identifier) {
SetContentSettingCustomScope(
primary_pattern,
secondary_pattern,
content_type,
resource_identifier,
CONTENT_SETTING_BLOCK);
}

void BraveHostContentSettingsMap::InitializeCookieContentSetting() {
// We intentionally do not use the cookies content settings so that
// these special rules do not show up in Chromium UI.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#define BRAVE_COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_BRAVE_HOST_CONTENT_SETTINGS_MAP_H_

#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/browser/content_settings_observer.h"



class BraveHostContentSettingsMap : public HostContentSettingsMap {
public:
Expand All @@ -20,6 +23,11 @@ class BraveHostContentSettingsMap : public HostContentSettingsMap {
void InitializeBraveShieldsContentSetting();
void InitializeFlashContentSetting();
~BraveHostContentSettingsMap() override;

void OnContentSettingChanged(const ContentSettingsPattern& primary_pattern,
const ContentSettingsPattern& secondary_pattern,
ContentSettingsType content_type,
const std::string& resource_identifier) override;
};

#endif // BRAVE_COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_BRAVE_HOST_CONTENT_SETTINGS_MAP_H_
2 changes: 2 additions & 0 deletions renderer/brave_content_settings_observer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

#include "brave/renderer/brave_content_settings_observer.h"

#include "brave/browser/brave_browser_process_impl.h"
#include "base/strings/utf_string_conversions.h"
#include "brave/common/render_messages.h"
#include "brave/content/common/frame_messages.h"
#include "brave/components/brave_shields/browser/tracking_protection_service.h"
#include "components/content_settings/core/common/content_settings_pattern.h"
#include "content/public/renderer/render_frame.h"
#include "services/service_manager/public/cpp/interface_provider.h"
Expand Down

0 comments on commit 3e324df

Please sign in to comment.