1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/blacklist_state_fetcher.h"
#include "base/strings/stringprintf.h"
#include "base/threading/thread_task_runner_handle.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/safe_browsing/safe_browsing_service.h"
#include "chrome/common/safe_browsing/crx_info.pb.h"
#include "components/safe_browsing/db/v4_protocol_manager_util.h"
#include "content/public/browser/browser_thread.h"
#include "net/base/escape.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "net/url_request/url_request_status.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/simple_url_loader.h"
#include "url/gurl.h"
using content::BrowserThread;
namespace extensions {
BlacklistStateFetcher::BlacklistStateFetcher() : weak_ptr_factory_(this) {}
BlacklistStateFetcher::~BlacklistStateFetcher() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
}
void BlacklistStateFetcher::Request(const std::string& id,
const RequestCallback& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (!safe_browsing_config_) {
if (g_browser_process && g_browser_process->safe_browsing_service()) {
SetSafeBrowsingConfig(
g_browser_process->safe_browsing_service()->GetV4ProtocolConfig());
} else {
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(callback, BLACKLISTED_UNKNOWN));
return;
}
}
bool request_already_sent = base::ContainsKey(callbacks_, id);
callbacks_.insert(std::make_pair(id, callback));
if (request_already_sent)
return;
if (g_browser_process && g_browser_process->safe_browsing_service()) {
url_loader_factory_ =
g_browser_process->safe_browsing_service()->GetURLLoaderFactory();
}
SendRequest(id);
}
void BlacklistStateFetcher::SendRequest(const std::string& id) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
ClientCRXListInfoRequest request;
request.set_id(id);
std::string request_str;
request.SerializeToString(&request_str);
GURL request_url = GURL(safe_browsing::GetReportUrl(
*safe_browsing_config_, "clientreport/crx-list-info"));
net::NetworkTrafficAnnotationTag traffic_annotation =
net::DefineNetworkTrafficAnnotation("extension_blacklist", R"(
semantics {
sender: "Extension Blacklist"
description:
"Chromium protects the users from malicious extensions by checking "
"extensions that are being installed or have been installed "
"against a list of known malwares. Chromium sends the identifiers "
"of extensions to Google and Google responds with whether it "
"believes each extension is malware or not. Only extensions that "
"match the safe browsing blacklist can trigger this request."
trigger:
"When extensions are being installed and at startup when existing "
"extensions are scanned."
data: "The identifier of the installed extension."
destination: GOOGLE_OWNED_SERVICE
}
policy {
cookies_allowed: YES
cookies_store: "Safe Browsing cookies store"
setting:
"Users can enable or disable this feature by toggling 'Protect you "
"and your device from dangerous sites' in Chromium settings under "
"Privacy. This feature is enabled by default."
chrome_policy {
SafeBrowsingEnabled {
policy_options {mode: MANDATORY}
SafeBrowsingEnabled: false
}
}
})");
auto resource_request = std::make_unique<network::ResourceRequest>();
resource_request->url = request_url;
resource_request->method = "POST";
std::unique_ptr<network::SimpleURLLoader> fetcher_ptr =
network::SimpleURLLoader::Create(std::move(resource_request),
traffic_annotation);
auto* fetcher = fetcher_ptr.get();
fetcher->AttachStringForUpload(request_str, "application/octet-stream");
requests_[fetcher] = {std::move(fetcher_ptr), id};
fetcher->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
url_loader_factory_.get(),
base::BindOnce(&BlacklistStateFetcher::OnURLLoaderComplete,
base::Unretained(this), fetcher));
}
void BlacklistStateFetcher::SetSafeBrowsingConfig(
const safe_browsing::V4ProtocolConfig& config) {
safe_browsing_config_ =
std::make_unique<safe_browsing::V4ProtocolConfig>(config);
}
void BlacklistStateFetcher::OnURLLoaderComplete(
network::SimpleURLLoader* url_loader,
std::unique_ptr<std::string> response_body) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
int response_code = 0;
if (url_loader->ResponseInfo() && url_loader->ResponseInfo()->headers)
response_code = url_loader->ResponseInfo()->headers->response_code();
std::string response_body_str;
if (response_body.get())
response_body_str = std::move(*response_body.get());
OnURLLoaderCompleteInternal(url_loader, response_body_str, response_code,
url_loader->NetError());
}
void BlacklistStateFetcher::OnURLLoaderCompleteInternal(
network::SimpleURLLoader* url_loader,
const std::string& response_body,
int response_code,
int net_error) {
auto it = requests_.find(url_loader);
if (it == requests_.end()) {
NOTREACHED();
return;
}
std::unique_ptr<network::SimpleURLLoader> loader =
std::move(it->second.first);
std::string id = it->second.second;
requests_.erase(it);
BlacklistState state;
if (net_error == net::OK && response_code == 200) {
ClientCRXListInfoResponse response;
if (response.ParseFromString(response_body)) {
state = static_cast<BlacklistState>(response.verdict());
} else {
state = BLACKLISTED_UNKNOWN;
}
} else {
if (net_error != net::OK) {
VLOG(1) << "Blacklist request for: " << id
<< " failed with error: " << net_error;
} else {
VLOG(1) << "Blacklist request for: " << id
<< " failed with error: " << response_code;
}
state = BLACKLISTED_UNKNOWN;
}
std::pair<CallbackMultiMap::iterator, CallbackMultiMap::iterator> range =
callbacks_.equal_range(id);
for (CallbackMultiMap::const_iterator callback_it = range.first;
callback_it != range.second; ++callback_it) {
callback_it->second.Run(state);
}
callbacks_.erase(range.first, range.second);
}
} // namespace extensions
|