OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/safe_browsing/ping_manager.h" | 5 #include "components/safe_browsing/base_ping_manager.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/base64.h" | 9 #include "base/base64.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
14 #include "base/values.h" | 14 #include "base/values.h" |
15 #include "chrome/browser/safe_browsing/notification_image_reporter.h" | |
16 #include "chrome/browser/safe_browsing/permission_reporter.h" | |
17 #include "components/data_use_measurement/core/data_use_user_data.h" | 15 #include "components/data_use_measurement/core/data_use_user_data.h" |
18 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
19 #include "google_apis/google_api_keys.h" | 17 #include "google_apis/google_api_keys.h" |
20 #include "net/base/escape.h" | 18 #include "net/base/escape.h" |
21 #include "net/base/load_flags.h" | 19 #include "net/base/load_flags.h" |
22 #include "net/log/net_log_source_type.h" | 20 #include "net/log/net_log_source_type.h" |
23 #include "net/ssl/ssl_info.h" | |
24 #include "net/url_request/url_fetcher.h" | 21 #include "net/url_request/url_fetcher.h" |
25 #include "net/url_request/url_request_context.h" | 22 #include "net/url_request/url_request_context.h" |
26 #include "net/url_request/url_request_context_getter.h" | 23 #include "net/url_request/url_request_context_getter.h" |
27 #include "net/url_request/url_request_status.h" | 24 #include "net/url_request/url_request_status.h" |
28 #include "third_party/skia/include/core/SkBitmap.h" | |
29 #include "url/gurl.h" | 25 #include "url/gurl.h" |
30 | 26 |
31 using content::BrowserThread; | 27 using content::BrowserThread; |
32 | 28 |
33 namespace { | 29 namespace { |
34 // Returns a dictionary with "url"=|url-spec| and "data"=|payload| for | 30 // Returns a dictionary with "url"=|url-spec| and "data"=|payload| for |
35 // netlogging the start phase of a ping. | 31 // netlogging the start phase of a ping. |
36 std::unique_ptr<base::Value> NetLogPingStartCallback( | 32 std::unique_ptr<base::Value> NetLogPingStartCallback( |
37 const net::NetLogWithSource& net_log, | 33 const net::NetLogWithSource& net_log, |
38 const GURL& url, | 34 const GURL& url, |
(...skipping 21 matching lines...) Expand all Loading... |
60 return std::move(event_params); | 56 return std::move(event_params); |
61 } | 57 } |
62 | 58 |
63 } // namespace | 59 } // namespace |
64 | 60 |
65 namespace safe_browsing { | 61 namespace safe_browsing { |
66 | 62 |
67 // SafeBrowsingPingManager implementation ---------------------------------- | 63 // SafeBrowsingPingManager implementation ---------------------------------- |
68 | 64 |
69 // static | 65 // static |
70 std::unique_ptr<SafeBrowsingPingManager> SafeBrowsingPingManager::Create( | 66 std::unique_ptr<BasePingManager> BasePingManager::Create( |
71 net::URLRequestContextGetter* request_context_getter, | 67 net::URLRequestContextGetter* request_context_getter, |
72 const SafeBrowsingProtocolConfig& config) { | 68 const SafeBrowsingProtocolConfig& config) { |
73 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 69 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
74 return base::WrapUnique( | 70 return base::WrapUnique(new BasePingManager(request_context_getter, config)); |
75 new SafeBrowsingPingManager(request_context_getter, config)); | |
76 } | 71 } |
77 | 72 |
78 SafeBrowsingPingManager::SafeBrowsingPingManager( | 73 BasePingManager::BasePingManager( |
79 net::URLRequestContextGetter* request_context_getter, | 74 net::URLRequestContextGetter* request_context_getter, |
80 const SafeBrowsingProtocolConfig& config) | 75 const SafeBrowsingProtocolConfig& config) |
81 : client_name_(config.client_name), | 76 : client_name_(config.client_name), |
82 request_context_getter_(request_context_getter), | 77 request_context_getter_(request_context_getter), |
83 url_prefix_(config.url_prefix) { | 78 url_prefix_(config.url_prefix) { |
84 DCHECK(!url_prefix_.empty()); | 79 DCHECK(!url_prefix_.empty()); |
85 | 80 |
86 if (request_context_getter) { | 81 if (request_context_getter) { |
87 permission_reporter_ = base::MakeUnique<PermissionReporter>( | |
88 request_context_getter->GetURLRequestContext()); | |
89 notification_image_reporter_ = base::MakeUnique<NotificationImageReporter>( | |
90 request_context_getter->GetURLRequestContext()); | |
91 | |
92 net_log_ = net::NetLogWithSource::Make( | 82 net_log_ = net::NetLogWithSource::Make( |
93 request_context_getter->GetURLRequestContext()->net_log(), | 83 request_context_getter->GetURLRequestContext()->net_log(), |
94 net::NetLogSourceType::SAFE_BROWSING); | 84 net::NetLogSourceType::SAFE_BROWSING); |
95 } | 85 } |
96 | 86 |
97 version_ = SafeBrowsingProtocolManagerHelper::Version(); | 87 version_ = ProtocolManagerHelper::Version(); |
98 } | 88 } |
99 | 89 |
100 SafeBrowsingPingManager::~SafeBrowsingPingManager() { | 90 BasePingManager::~BasePingManager() {} |
101 } | |
102 | 91 |
103 // net::URLFetcherDelegate implementation ---------------------------------- | 92 // net::URLFetcherDelegate implementation ---------------------------------- |
104 | 93 |
105 // All SafeBrowsing request responses are handled here. | 94 // All SafeBrowsing request responses are handled here. |
106 void SafeBrowsingPingManager::OnURLFetchComplete( | 95 void BasePingManager::OnURLFetchComplete(const net::URLFetcher* source) { |
107 const net::URLFetcher* source) { | |
108 net_log_.EndEvent( | 96 net_log_.EndEvent( |
109 net::NetLogEventType::SAFE_BROWSING_PING, | 97 net::NetLogEventType::SAFE_BROWSING_PING, |
110 base::Bind(&NetLogPingEndCallback, net_log_, source->GetStatus())); | 98 base::Bind(&NetLogPingEndCallback, net_log_, source->GetStatus())); |
111 auto it = | 99 auto it = |
112 std::find_if(safebrowsing_reports_.begin(), safebrowsing_reports_.end(), | 100 std::find_if(safebrowsing_reports_.begin(), safebrowsing_reports_.end(), |
113 [source](const std::unique_ptr<net::URLFetcher>& ptr) { | 101 [source](const std::unique_ptr<net::URLFetcher>& ptr) { |
114 return ptr.get() == source; | 102 return ptr.get() == source; |
115 }); | 103 }); |
116 DCHECK(it != safebrowsing_reports_.end()); | 104 DCHECK(it != safebrowsing_reports_.end()); |
117 safebrowsing_reports_.erase(it); | 105 safebrowsing_reports_.erase(it); |
118 } | 106 } |
119 | 107 |
120 // Sends a SafeBrowsing "hit" report. | 108 // Sends a SafeBrowsing "hit" report. |
121 void SafeBrowsingPingManager::ReportSafeBrowsingHit( | 109 void BasePingManager::ReportSafeBrowsingHit( |
122 const safe_browsing::HitReport& hit_report) { | 110 const safe_browsing::HitReport& hit_report) { |
123 GURL report_url = SafeBrowsingHitUrl(hit_report); | 111 GURL report_url = SafeBrowsingHitUrl(hit_report); |
124 std::unique_ptr<net::URLFetcher> report_ptr = net::URLFetcher::Create( | 112 std::unique_ptr<net::URLFetcher> report_ptr = net::URLFetcher::Create( |
125 report_url, hit_report.post_data.empty() ? net::URLFetcher::GET | 113 report_url, |
126 : net::URLFetcher::POST, | 114 hit_report.post_data.empty() ? net::URLFetcher::GET |
| 115 : net::URLFetcher::POST, |
127 this); | 116 this); |
128 net::URLFetcher* report = report_ptr.get(); | 117 net::URLFetcher* report = report_ptr.get(); |
129 data_use_measurement::DataUseUserData::AttachToFetcher( | 118 data_use_measurement::DataUseUserData::AttachToFetcher( |
130 report, data_use_measurement::DataUseUserData::SAFE_BROWSING); | 119 report, data_use_measurement::DataUseUserData::SAFE_BROWSING); |
131 report_ptr->SetLoadFlags(net::LOAD_DISABLE_CACHE); | 120 report_ptr->SetLoadFlags(net::LOAD_DISABLE_CACHE); |
132 report_ptr->SetRequestContext(request_context_getter_.get()); | 121 report_ptr->SetRequestContext(request_context_getter_.get()); |
133 std::string post_data_base64; | 122 std::string post_data_base64; |
134 if (!hit_report.post_data.empty()) { | 123 if (!hit_report.post_data.empty()) { |
135 report_ptr->SetUploadData("text/plain", hit_report.post_data); | 124 report_ptr->SetUploadData("text/plain", hit_report.post_data); |
136 base::Base64Encode(hit_report.post_data, &post_data_base64); | 125 base::Base64Encode(hit_report.post_data, &post_data_base64); |
137 } | 126 } |
138 | 127 |
139 net_log_.BeginEvent( | 128 net_log_.BeginEvent( |
140 net::NetLogEventType::SAFE_BROWSING_PING, | 129 net::NetLogEventType::SAFE_BROWSING_PING, |
141 base::Bind(&NetLogPingStartCallback, net_log_, | 130 base::Bind(&NetLogPingStartCallback, net_log_, |
142 report_ptr->GetOriginalURL(), post_data_base64)); | 131 report_ptr->GetOriginalURL(), post_data_base64)); |
143 | 132 |
144 report->Start(); | 133 report->Start(); |
145 safebrowsing_reports_.insert(std::move(report_ptr)); | 134 safebrowsing_reports_.insert(std::move(report_ptr)); |
146 } | 135 } |
147 | 136 |
148 // Sends threat details for users who opt-in. | 137 // Sends threat details for users who opt-in. |
149 void SafeBrowsingPingManager::ReportThreatDetails(const std::string& report) { | 138 void BasePingManager::ReportThreatDetails(const std::string& report) { |
150 GURL report_url = ThreatDetailsUrl(); | 139 GURL report_url = ThreatDetailsUrl(); |
151 std::unique_ptr<net::URLFetcher> fetcher = | 140 std::unique_ptr<net::URLFetcher> fetcher = |
152 net::URLFetcher::Create(report_url, net::URLFetcher::POST, this); | 141 net::URLFetcher::Create(report_url, net::URLFetcher::POST, this); |
153 data_use_measurement::DataUseUserData::AttachToFetcher( | 142 data_use_measurement::DataUseUserData::AttachToFetcher( |
154 fetcher.get(), data_use_measurement::DataUseUserData::SAFE_BROWSING); | 143 fetcher.get(), data_use_measurement::DataUseUserData::SAFE_BROWSING); |
155 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE); | 144 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE); |
156 fetcher->SetRequestContext(request_context_getter_.get()); | 145 fetcher->SetRequestContext(request_context_getter_.get()); |
157 fetcher->SetUploadData("application/octet-stream", report); | 146 fetcher->SetUploadData("application/octet-stream", report); |
158 // Don't try too hard to send reports on failures. | 147 // Don't try too hard to send reports on failures. |
159 fetcher->SetAutomaticallyRetryOn5xx(false); | 148 fetcher->SetAutomaticallyRetryOn5xx(false); |
160 | 149 |
161 std::string report_base64; | 150 std::string report_base64; |
162 base::Base64Encode(report, &report_base64); | 151 base::Base64Encode(report, &report_base64); |
163 net_log_.BeginEvent( | 152 net_log_.BeginEvent(net::NetLogEventType::SAFE_BROWSING_PING, |
164 net::NetLogEventType::SAFE_BROWSING_PING, | 153 base::Bind(&NetLogPingStartCallback, net_log_, |
165 base::Bind(&NetLogPingStartCallback, net_log_, fetcher->GetOriginalURL(), | 154 fetcher->GetOriginalURL(), report_base64)); |
166 report_base64)); | |
167 | 155 |
168 fetcher->Start(); | 156 fetcher->Start(); |
169 safebrowsing_reports_.insert(std::move(fetcher)); | 157 safebrowsing_reports_.insert(std::move(fetcher)); |
170 } | 158 } |
171 | 159 |
172 void SafeBrowsingPingManager::ReportPermissionAction( | 160 GURL BasePingManager::SafeBrowsingHitUrl( |
173 const PermissionReportInfo& report_info) { | |
174 permission_reporter_->SendReport(report_info); | |
175 } | |
176 | |
177 void SafeBrowsingPingManager::ReportNotificationImage( | |
178 Profile* profile, | |
179 const scoped_refptr<SafeBrowsingDatabaseManager>& database_manager, | |
180 const GURL& origin, | |
181 const SkBitmap& image) { | |
182 notification_image_reporter_->ReportNotificationImageOnIO( | |
183 profile, database_manager, origin, image); | |
184 } | |
185 | |
186 GURL SafeBrowsingPingManager::SafeBrowsingHitUrl( | |
187 const safe_browsing::HitReport& hit_report) const { | 161 const safe_browsing::HitReport& hit_report) const { |
188 DCHECK(hit_report.threat_type == SB_THREAT_TYPE_URL_MALWARE || | 162 DCHECK(hit_report.threat_type == SB_THREAT_TYPE_URL_MALWARE || |
189 hit_report.threat_type == SB_THREAT_TYPE_URL_PHISHING || | 163 hit_report.threat_type == SB_THREAT_TYPE_URL_PHISHING || |
190 hit_report.threat_type == SB_THREAT_TYPE_URL_UNWANTED || | 164 hit_report.threat_type == SB_THREAT_TYPE_URL_UNWANTED || |
191 hit_report.threat_type == SB_THREAT_TYPE_BINARY_MALWARE_URL || | 165 hit_report.threat_type == SB_THREAT_TYPE_BINARY_MALWARE_URL || |
192 hit_report.threat_type == SB_THREAT_TYPE_CLIENT_SIDE_PHISHING_URL || | 166 hit_report.threat_type == SB_THREAT_TYPE_CLIENT_SIDE_PHISHING_URL || |
193 hit_report.threat_type == SB_THREAT_TYPE_CLIENT_SIDE_MALWARE_URL); | 167 hit_report.threat_type == SB_THREAT_TYPE_CLIENT_SIDE_MALWARE_URL); |
194 std::string url = SafeBrowsingProtocolManagerHelper::ComposeUrl( | 168 std::string url = ProtocolManagerHelper::ComposeUrl( |
195 url_prefix_, "report", client_name_, version_, std::string(), | 169 url_prefix_, "report", client_name_, version_, std::string(), |
196 hit_report.extended_reporting_level); | 170 hit_report.extended_reporting_level); |
197 | 171 |
198 std::string threat_list = "none"; | 172 std::string threat_list = "none"; |
199 switch (hit_report.threat_type) { | 173 switch (hit_report.threat_type) { |
200 case SB_THREAT_TYPE_URL_MALWARE: | 174 case SB_THREAT_TYPE_URL_MALWARE: |
201 threat_list = "malblhit"; | 175 threat_list = "malblhit"; |
202 break; | 176 break; |
203 case SB_THREAT_TYPE_URL_PHISHING: | 177 case SB_THREAT_TYPE_URL_PHISHING: |
204 threat_list = "phishblhit"; | 178 threat_list = "phishblhit"; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 return GURL(base::StringPrintf( | 232 return GURL(base::StringPrintf( |
259 "%s&evts=%s&evtd=%s&evtr=%s&evhr=%s&evtb=%d&src=%s&m=%d%s", url.c_str(), | 233 "%s&evts=%s&evtd=%s&evtr=%s&evhr=%s&evtb=%d&src=%s&m=%d%s", url.c_str(), |
260 threat_list.c_str(), | 234 threat_list.c_str(), |
261 net::EscapeQueryParamValue(hit_report.malicious_url.spec(), true).c_str(), | 235 net::EscapeQueryParamValue(hit_report.malicious_url.spec(), true).c_str(), |
262 net::EscapeQueryParamValue(hit_report.page_url.spec(), true).c_str(), | 236 net::EscapeQueryParamValue(hit_report.page_url.spec(), true).c_str(), |
263 net::EscapeQueryParamValue(hit_report.referrer_url.spec(), true).c_str(), | 237 net::EscapeQueryParamValue(hit_report.referrer_url.spec(), true).c_str(), |
264 hit_report.is_subresource, threat_source.c_str(), | 238 hit_report.is_subresource, threat_source.c_str(), |
265 hit_report.is_metrics_reporting_active, user_population_comp.c_str())); | 239 hit_report.is_metrics_reporting_active, user_population_comp.c_str())); |
266 } | 240 } |
267 | 241 |
268 GURL SafeBrowsingPingManager::ThreatDetailsUrl() const { | 242 GURL BasePingManager::ThreatDetailsUrl() const { |
269 std::string url = base::StringPrintf( | 243 std::string url = base::StringPrintf( |
270 "%s/clientreport/malware?client=%s&appver=%s&pver=1.0", | 244 "%s/clientreport/malware?client=%s&appver=%s&pver=1.0", |
271 url_prefix_.c_str(), | 245 url_prefix_.c_str(), client_name_.c_str(), version_.c_str()); |
272 client_name_.c_str(), | |
273 version_.c_str()); | |
274 std::string api_key = google_apis::GetAPIKey(); | 246 std::string api_key = google_apis::GetAPIKey(); |
275 if (!api_key.empty()) { | 247 if (!api_key.empty()) { |
276 base::StringAppendF(&url, "&key=%s", | 248 base::StringAppendF(&url, "&key=%s", |
277 net::EscapeQueryParamValue(api_key, true).c_str()); | 249 net::EscapeQueryParamValue(api_key, true).c_str()); |
278 } | 250 } |
279 return GURL(url); | 251 return GURL(url); |
280 } | 252 } |
281 | 253 |
282 } // namespace safe_browsing | 254 } // namespace safe_browsing |
OLD | NEW |