OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/safe_browsing/ping_manager.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
| 9 #include "base/base64.h" |
9 #include "base/logging.h" | 10 #include "base/logging.h" |
10 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
11 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
12 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 14 #include "base/values.h" |
13 #include "chrome/browser/safe_browsing/permission_reporter.h" | 15 #include "chrome/browser/safe_browsing/permission_reporter.h" |
14 #include "components/certificate_reporting/error_reporter.h" | 16 #include "components/certificate_reporting/error_reporter.h" |
15 #include "components/data_use_measurement/core/data_use_user_data.h" | 17 #include "components/data_use_measurement/core/data_use_user_data.h" |
16 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
17 #include "google_apis/google_api_keys.h" | 19 #include "google_apis/google_api_keys.h" |
18 #include "net/base/escape.h" | 20 #include "net/base/escape.h" |
19 #include "net/base/load_flags.h" | 21 #include "net/base/load_flags.h" |
| 22 #include "net/log/net_log_source_type.h" |
20 #include "net/ssl/ssl_info.h" | 23 #include "net/ssl/ssl_info.h" |
21 #include "net/url_request/report_sender.h" | 24 #include "net/url_request/report_sender.h" |
22 #include "net/url_request/url_fetcher.h" | 25 #include "net/url_request/url_fetcher.h" |
| 26 #include "net/url_request/url_request_context.h" |
23 #include "net/url_request/url_request_context_getter.h" | 27 #include "net/url_request/url_request_context_getter.h" |
24 #include "net/url_request/url_request_status.h" | 28 #include "net/url_request/url_request_status.h" |
25 #include "url/gurl.h" | 29 #include "url/gurl.h" |
26 | 30 |
27 using content::BrowserThread; | 31 using content::BrowserThread; |
28 | 32 |
29 namespace { | 33 namespace { |
30 // URL to upload invalid certificate chain reports. An HTTP URL is | 34 // URL to upload invalid certificate chain reports. An HTTP URL is |
31 // used because a client seeing an invalid cert might not be able to | 35 // used because a client seeing an invalid cert might not be able to |
32 // make an HTTPS connection to report it. | 36 // make an HTTPS connection to report it. |
33 const char kExtendedReportingUploadUrlInsecure[] = | 37 const char kExtendedReportingUploadUrlInsecure[] = |
34 "http://safebrowsing.googleusercontent.com/safebrowsing/clientreport/" | 38 "http://safebrowsing.googleusercontent.com/safebrowsing/clientreport/" |
35 "chrome-certs"; | 39 "chrome-certs"; |
| 40 |
| 41 // Returns a dictionary with "url"=|url-spec| and "data"=|payload| for |
| 42 // netlogging the start phase of a ping. |
| 43 std::unique_ptr<base::Value> NetLogPingStartCallback( |
| 44 const net::NetLogWithSource& net_log, |
| 45 const GURL& url, |
| 46 const std::string& payload, |
| 47 net::NetLogCaptureMode) { |
| 48 std::unique_ptr<base::DictionaryValue> event_params( |
| 49 new base::DictionaryValue()); |
| 50 event_params->SetString("url", url.spec()); |
| 51 event_params->SetString("payload", payload); |
| 52 net_log.source().AddToEventParameters(event_params.get()); |
| 53 return std::move(event_params); |
| 54 } |
| 55 |
| 56 // Returns a dictionary with "url"=|url-spec|, "status"=|status| and |
| 57 // "error"=|error| for netlogging the end phase of a ping. |
| 58 std::unique_ptr<base::Value> NetLogPingEndCallback( |
| 59 const net::NetLogWithSource& net_log, |
| 60 const net::URLRequestStatus& status, |
| 61 net::NetLogCaptureMode) { |
| 62 std::unique_ptr<base::DictionaryValue> event_params( |
| 63 new base::DictionaryValue()); |
| 64 event_params->SetInteger("status", status.status()); |
| 65 event_params->SetInteger("error", status.error()); |
| 66 net_log.source().AddToEventParameters(event_params.get()); |
| 67 return std::move(event_params); |
| 68 } |
| 69 |
36 } // namespace | 70 } // namespace |
37 | 71 |
38 namespace safe_browsing { | 72 namespace safe_browsing { |
39 | 73 |
40 // SafeBrowsingPingManager implementation ---------------------------------- | 74 // SafeBrowsingPingManager implementation ---------------------------------- |
41 | 75 |
42 // static | 76 // static |
43 std::unique_ptr<SafeBrowsingPingManager> SafeBrowsingPingManager::Create( | 77 std::unique_ptr<SafeBrowsingPingManager> SafeBrowsingPingManager::Create( |
44 net::URLRequestContextGetter* request_context_getter, | 78 net::URLRequestContextGetter* request_context_getter, |
45 const SafeBrowsingProtocolConfig& config) { | 79 const SafeBrowsingProtocolConfig& config) { |
(...skipping 15 matching lines...) Expand all Loading... |
61 GURL certificate_upload_url; | 95 GURL certificate_upload_url; |
62 cookies_preference = net::ReportSender::DO_NOT_SEND_COOKIES; | 96 cookies_preference = net::ReportSender::DO_NOT_SEND_COOKIES; |
63 certificate_upload_url = GURL(kExtendedReportingUploadUrlInsecure); | 97 certificate_upload_url = GURL(kExtendedReportingUploadUrlInsecure); |
64 | 98 |
65 certificate_error_reporter_.reset(new certificate_reporting::ErrorReporter( | 99 certificate_error_reporter_.reset(new certificate_reporting::ErrorReporter( |
66 request_context_getter->GetURLRequestContext(), certificate_upload_url, | 100 request_context_getter->GetURLRequestContext(), certificate_upload_url, |
67 cookies_preference)); | 101 cookies_preference)); |
68 | 102 |
69 permission_reporter_.reset( | 103 permission_reporter_.reset( |
70 new PermissionReporter(request_context_getter->GetURLRequestContext())); | 104 new PermissionReporter(request_context_getter->GetURLRequestContext())); |
| 105 |
| 106 net_log_ = net::NetLogWithSource::Make( |
| 107 request_context_getter->GetURLRequestContext()->net_log(), |
| 108 net::NetLogSourceType::SAFE_BROWSING); |
71 } | 109 } |
72 | 110 |
73 version_ = SafeBrowsingProtocolManagerHelper::Version(); | 111 version_ = SafeBrowsingProtocolManagerHelper::Version(); |
74 } | 112 } |
75 | 113 |
76 SafeBrowsingPingManager::~SafeBrowsingPingManager() { | 114 SafeBrowsingPingManager::~SafeBrowsingPingManager() { |
77 } | 115 } |
78 | 116 |
79 // net::URLFetcherDelegate implementation ---------------------------------- | 117 // net::URLFetcherDelegate implementation ---------------------------------- |
80 | 118 |
81 // All SafeBrowsing request responses are handled here. | 119 // All SafeBrowsing request responses are handled here. |
82 void SafeBrowsingPingManager::OnURLFetchComplete( | 120 void SafeBrowsingPingManager::OnURLFetchComplete( |
83 const net::URLFetcher* source) { | 121 const net::URLFetcher* source) { |
| 122 net_log_.EndEvent( |
| 123 net::NetLogEventType::SAFE_BROWSING_PING, |
| 124 base::Bind(&NetLogPingEndCallback, net_log_, source->GetStatus())); |
84 auto it = | 125 auto it = |
85 std::find_if(safebrowsing_reports_.begin(), safebrowsing_reports_.end(), | 126 std::find_if(safebrowsing_reports_.begin(), safebrowsing_reports_.end(), |
86 [source](const std::unique_ptr<net::URLFetcher>& ptr) { | 127 [source](const std::unique_ptr<net::URLFetcher>& ptr) { |
87 return ptr.get() == source; | 128 return ptr.get() == source; |
88 }); | 129 }); |
89 DCHECK(it != safebrowsing_reports_.end()); | 130 DCHECK(it != safebrowsing_reports_.end()); |
90 safebrowsing_reports_.erase(it); | 131 safebrowsing_reports_.erase(it); |
91 } | 132 } |
92 | 133 |
93 // Sends a SafeBrowsing "hit" report. | 134 // Sends a SafeBrowsing "hit" report. |
94 void SafeBrowsingPingManager::ReportSafeBrowsingHit( | 135 void SafeBrowsingPingManager::ReportSafeBrowsingHit( |
95 const safe_browsing::HitReport& hit_report) { | 136 const safe_browsing::HitReport& hit_report) { |
96 GURL report_url = SafeBrowsingHitUrl(hit_report); | 137 GURL report_url = SafeBrowsingHitUrl(hit_report); |
97 std::unique_ptr<net::URLFetcher> report_ptr = net::URLFetcher::Create( | 138 std::unique_ptr<net::URLFetcher> report_ptr = net::URLFetcher::Create( |
98 report_url, hit_report.post_data.empty() ? net::URLFetcher::GET | 139 report_url, hit_report.post_data.empty() ? net::URLFetcher::GET |
99 : net::URLFetcher::POST, | 140 : net::URLFetcher::POST, |
100 this); | 141 this); |
101 net::URLFetcher* report = report_ptr.get(); | 142 net::URLFetcher* report = report_ptr.get(); |
102 data_use_measurement::DataUseUserData::AttachToFetcher( | 143 data_use_measurement::DataUseUserData::AttachToFetcher( |
103 report, data_use_measurement::DataUseUserData::SAFE_BROWSING); | 144 report, data_use_measurement::DataUseUserData::SAFE_BROWSING); |
104 report_ptr->SetLoadFlags(net::LOAD_DISABLE_CACHE); | 145 report_ptr->SetLoadFlags(net::LOAD_DISABLE_CACHE); |
105 report_ptr->SetRequestContext(request_context_getter_.get()); | 146 report_ptr->SetRequestContext(request_context_getter_.get()); |
106 if (!hit_report.post_data.empty()) | 147 std::string post_data_base64; |
| 148 if (!hit_report.post_data.empty()) { |
107 report_ptr->SetUploadData("text/plain", hit_report.post_data); | 149 report_ptr->SetUploadData("text/plain", hit_report.post_data); |
| 150 base::Base64Encode(hit_report.post_data, &post_data_base64); |
| 151 } |
| 152 |
| 153 net_log_.BeginEvent( |
| 154 net::NetLogEventType::SAFE_BROWSING_PING, |
| 155 base::Bind(&NetLogPingStartCallback, net_log_, |
| 156 report_ptr->GetOriginalURL(), post_data_base64)); |
| 157 |
| 158 report->Start(); |
108 safebrowsing_reports_.insert(std::move(report_ptr)); | 159 safebrowsing_reports_.insert(std::move(report_ptr)); |
109 report->Start(); | |
110 } | 160 } |
111 | 161 |
112 // Sends threat details for users who opt-in. | 162 // Sends threat details for users who opt-in. |
113 void SafeBrowsingPingManager::ReportThreatDetails(const std::string& report) { | 163 void SafeBrowsingPingManager::ReportThreatDetails(const std::string& report) { |
114 GURL report_url = ThreatDetailsUrl(); | 164 GURL report_url = ThreatDetailsUrl(); |
115 std::unique_ptr<net::URLFetcher> fetcher = | 165 std::unique_ptr<net::URLFetcher> fetcher = |
116 net::URLFetcher::Create(report_url, net::URLFetcher::POST, this); | 166 net::URLFetcher::Create(report_url, net::URLFetcher::POST, this); |
117 data_use_measurement::DataUseUserData::AttachToFetcher( | 167 data_use_measurement::DataUseUserData::AttachToFetcher( |
118 fetcher.get(), data_use_measurement::DataUseUserData::SAFE_BROWSING); | 168 fetcher.get(), data_use_measurement::DataUseUserData::SAFE_BROWSING); |
119 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE); | 169 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE); |
120 fetcher->SetRequestContext(request_context_getter_.get()); | 170 fetcher->SetRequestContext(request_context_getter_.get()); |
121 fetcher->SetUploadData("application/octet-stream", report); | 171 fetcher->SetUploadData("application/octet-stream", report); |
122 // Don't try too hard to send reports on failures. | 172 // Don't try too hard to send reports on failures. |
123 fetcher->SetAutomaticallyRetryOn5xx(false); | 173 fetcher->SetAutomaticallyRetryOn5xx(false); |
| 174 |
| 175 std::string report_base64; |
| 176 base::Base64Encode(report, &report_base64); |
| 177 net_log_.BeginEvent( |
| 178 net::NetLogEventType::SAFE_BROWSING_PING, |
| 179 base::Bind(&NetLogPingStartCallback, net_log_, fetcher->GetOriginalURL(), |
| 180 report_base64)); |
| 181 |
124 fetcher->Start(); | 182 fetcher->Start(); |
125 safebrowsing_reports_.insert(std::move(fetcher)); | 183 safebrowsing_reports_.insert(std::move(fetcher)); |
126 } | 184 } |
127 | 185 |
128 void SafeBrowsingPingManager::ReportInvalidCertificateChain( | 186 void SafeBrowsingPingManager::ReportInvalidCertificateChain( |
129 const std::string& serialized_report) { | 187 const std::string& serialized_report) { |
130 DCHECK(certificate_error_reporter_); | 188 DCHECK(certificate_error_reporter_); |
131 certificate_error_reporter_->SendExtendedReportingReport(serialized_report); | 189 certificate_error_reporter_->SendExtendedReportingReport(serialized_report); |
132 } | 190 } |
133 | 191 |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 version_.c_str()); | 290 version_.c_str()); |
233 std::string api_key = google_apis::GetAPIKey(); | 291 std::string api_key = google_apis::GetAPIKey(); |
234 if (!api_key.empty()) { | 292 if (!api_key.empty()) { |
235 base::StringAppendF(&url, "&key=%s", | 293 base::StringAppendF(&url, "&key=%s", |
236 net::EscapeQueryParamValue(api_key, true).c_str()); | 294 net::EscapeQueryParamValue(api_key, true).c_str()); |
237 } | 295 } |
238 return GURL(url); | 296 return GURL(url); |
239 } | 297 } |
240 | 298 |
241 } // namespace safe_browsing | 299 } // namespace safe_browsing |
OLD | NEW |