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