Skip to content
This repository has been archived by the owner on Jan 4, 2019. It is now read-only.

Commit

Permalink
Adding tor browser context support "isolated_storage" and "tor_proxy"…
Browse files Browse the repository at this point in the history
… options

fix #468
fix #464

Auditors: @bridiver, @riastradh-brave, @diracdeltas
  • Loading branch information
darkdh committed Feb 1, 2018
1 parent d6f9ad6 commit a8fab48
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 1 deletion.
8 changes: 8 additions & 0 deletions atom/browser/api/atom_api_web_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,14 @@ mate::Handle<api::Session> SessionFromOptions(v8::Isolate* isolate,
if (options.Get("parent_partition", &parent_partition)) {
session_options.SetString("parent_partition", parent_partition);
}
bool isolated_storage;
if (options.Get("isolated_storage", &isolated_storage)) {
session_options.SetBoolean("isolated_storage", isolated_storage);
}
std::string tor_proxy;
if (options.Get("tor_proxy", &tor_proxy)) {
session_options.SetString("tor_proxy", tor_proxy);
}
session = Session::FromPartition(isolate, partition, session_options);
} else {
// Use the default session if not specified.
Expand Down
69 changes: 69 additions & 0 deletions brave/browser/brave_browser_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,18 @@
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/dom_storage_context.h"
#include "content/public/browser/storage_partition.h"
#include "crypto/random.h"
#include "extensions/browser/pref_names.h"
#include "extensions/features/features.h"
#include "net/base/escape.h"
#include "net/cookies/cookie_store.h"
#include "net/proxy/proxy_config_service_fixed.h"
#include "net/proxy/proxy_service.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_getter.h"
#include "net/url_request/url_request_job_factory_impl.h"
#include "vendor/brightray/browser/browser_client.h"
#include "vendor/brightray/browser/net_log.h"

#if BUILDFLAG(ENABLE_EXTENSIONS)
#include "atom/browser/extensions/atom_browser_client_extensions_part.h"
Expand Down Expand Up @@ -82,6 +88,7 @@ namespace {
const char kPrefExitTypeCrashed[] = "Crashed";
const char kPrefExitTypeSessionEnded[] = "SessionEnded";
const char kPrefExitTypeNormal[] = "Normal";
const int kTorPasswordLength = 16;

#if BUILDFLAG(ENABLE_EXTENSIONS)
// WATCH(bridiver) - chrome/browser/profiles/off_the_record_profile_impl.cc
Expand Down Expand Up @@ -153,6 +160,8 @@ BraveBrowserContext::BraveBrowserContext(
ready_(new base::WaitableEvent(
base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED)),
isolated_storage_(false),
tor_proxy_(std::string()),
io_task_runner_(std::move(io_task_runner)),
delegate_(g_browser_process->profile_manager()) {
std::string parent_partition;
Expand All @@ -162,6 +171,16 @@ BraveBrowserContext::BraveBrowserContext(
atom::AtomBrowserContext::From(parent_partition, false));
}

bool isolated_storage;
if (options.GetBoolean("isolated_storage", &isolated_storage)) {
isolated_storage_ = isolated_storage;
}

std::string tor_proxy;
if (options.GetString("tor_proxy", &tor_proxy)) {
tor_proxy_ = tor_proxy;
}

if (in_memory) {
original_context_ = static_cast<BraveBrowserContext*>(
atom::AtomBrowserContext::From(partition, false));
Expand Down Expand Up @@ -305,6 +324,51 @@ content::BrowserPluginGuestManager* BraveBrowserContext::GetGuestManager() {
return guest_view::GuestViewManager::FromBrowserContext(this);
}

net::URLRequestContextGetter*
BraveBrowserContext::CreateRequestContextForStoragePartition(
const base::FilePath& partition_path,
bool in_memory,
content::ProtocolHandlerMap* protocol_handlers,
content::URLRequestInterceptorScopedVector request_interceptors) {
if (isolated_storage_) {
url_request_getter_ =
new brightray::URLRequestContextGetter(
this,
static_cast<brightray::NetLog*>(brightray::BrowserClient::Get()->
GetNetLog()),
partition_path,
in_memory,
BrowserThread::GetTaskRunnerForThread(BrowserThread::IO),
BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE),
protocol_handlers,
std::move(request_interceptors));
auto proxy_service = url_request_getter_->GetURLRequestContext()->
proxy_service();
if (!tor_proxy_.empty() && GURL(tor_proxy_).is_valid()) {
net::ProxyConfig config;
// Notice CreateRequestContextForStoragePartition will only be called once
// per partition_path so there is no need to cache password per origin
std::string origin = partition_path.DirName().BaseName().value();
std::string encoded_password;
std::vector<uint8_t> password(kTorPasswordLength);
crypto::RandBytes(password.data(), password.size());
encoded_password = base::HexEncode(password.data(), password.size());
std::string url = tor_proxy_;
base::ReplaceFirstSubstringAfterOffset(
&url, 0, "//", "//" + origin + ":" + encoded_password + "@");
// TODO(darkdh): using URL with auth after
// https://github.com/brave/muon/pull/470
config.proxy_rules().ParseFromString(tor_proxy_);
proxy_service->ResetConfigService(base::WrapUnique(
new net::ProxyConfigServiceFixed(config)));
proxy_service->ForceReloadProxyConfig();
}
return url_request_getter_.get();
} else {
return nullptr;
}
}

void BraveBrowserContext::TrackZoomLevelsFromParent() {
// Here we only want to use zoom levels stored in the main-context's default
// storage partition. We're not interested in zoom levels in special
Expand Down Expand Up @@ -402,6 +466,11 @@ BraveBrowserContext::CreateURLRequestJobFactory(
extensions::CreateExtensionProtocolHandler(IsOffTheRecord(),
info_map_));
#endif
if (!protocol_handler_interceptor_.get()) {
protocol_handler_interceptor_ =
ProtocolHandlerRegistryFactory::GetForBrowserContext(this)
->CreateJobInterceptorFactory();
}
protocol_handler_interceptor_->Chain(std::move(job_factory));
return std::move(protocol_handler_interceptor_);
}
Expand Down
11 changes: 11 additions & 0 deletions brave/browser/brave_browser_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ class BraveBrowserContext : public Profile {
override {
return nullptr;
}
net::URLRequestContextGetter* CreateRequestContextForStoragePartition(
const base::FilePath& partition_path,
bool in_memory,
content::ProtocolHandlerMap* protocol_handlers,
content::URLRequestInterceptorScopedVector request_interceptors) override;

// Profile implementation:
scoped_refptr<base::SequencedTaskRunner> GetIOTaskRunner() override;
Expand Down Expand Up @@ -129,6 +134,8 @@ class BraveBrowserContext : public Profile {

void SetExitType(ExitType exit_type) override;

bool IsIsolatedStorage() const { return isolated_storage_; }

private:
void OnPrefsLoaded(bool success);
void TrackZoomLevelsFromParent();
Expand All @@ -152,6 +159,10 @@ class BraveBrowserContext : public Profile {
BraveBrowserContext* otr_context_;
const std::string partition_;
std::unique_ptr<base::WaitableEvent> ready_;
bool isolated_storage_;
std::string tor_proxy_;

scoped_refptr<brightray::URLRequestContextGetter> url_request_getter_;

scoped_refptr<autofill::AutofillWebDataService> autofill_data_;
#if defined(OS_WIN)
Expand Down
18 changes: 18 additions & 0 deletions brave/browser/brave_content_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "base/lazy_instance.h"
#include "base/path_service.h"
#include "base/strings/utf_string_conversions.h"
#include "brave/browser/brave_browser_context.h"
#include "brave/browser/notifications/platform_notification_service_impl.h"
#include "brave/browser/password_manager/brave_password_manager_client.h"
#include "brave/grit/brave_resources.h"
Expand Down Expand Up @@ -224,6 +225,12 @@ std::string BraveContentBrowserClient::GetStoragePartitionIdForSite(
partition_id = site.spec();
#endif

auto brave_browser_context =
BraveBrowserContext::FromBrowserContext(browser_context);
if (brave_browser_context && brave_browser_context->IsIsolatedStorage()) {
partition_id = site.spec();
}

DCHECK(IsValidStoragePartitionId(browser_context, partition_id));
return partition_id;
}
Expand Down Expand Up @@ -282,6 +289,17 @@ void BraveContentBrowserClient::GetStoragePartitionConfigForSite(
// error about which StoragePartition they expect to be in and it is not
// safe to continue.
CHECK(can_be_default || !partition_domain->empty());

auto brave_browser_context =
BraveBrowserContext::FromBrowserContext(browser_context);
if (brave_browser_context && brave_browser_context->IsIsolatedStorage() &&
!site.SchemeIs(extensions::kExtensionScheme)) {
if (!site.is_empty()) {
*partition_domain = site.host();
*partition_name = site.host();
}
*in_memory = true;
}
}

content::WebContentsViewDelegate*
Expand Down
4 changes: 4 additions & 0 deletions brave/browser/guest_view/tab_view/tab_view_guest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ void TabViewGuest::CreateWebContents(
partition_options.SetString("parent_partition", "");
}
}
bool isolated_storage;
if (params.GetBoolean("isolated_storage", &isolated_storage)) {
partition_options.SetBoolean("isolated_storage", isolated_storage);
}
atom::AtomBrowserContext* browser_context =
brave::BraveBrowserContext::FromPartition(partition, partition_options);

Expand Down
4 changes: 3 additions & 1 deletion lib/browser/api/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,9 @@ const createTab = function (createProperties, cb) {
if (!error && createProperties.partition) {
// createProperties.partition always takes precendence
ses = session.fromPartition(createProperties.partition, {
parent_partition: createProperties.parent_partition
parent_partition: createProperties.parent_partition,
isolated_storage: createProperties.isolated_storage,
tor_proxy: createProperties.tor_proxy
})
// don't pass the partition info through
delete createProperties.partition
Expand Down

0 comments on commit a8fab48

Please sign in to comment.