Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(110)

Side by Side Diff: chrome/browser/safe_browsing/threat_details.cc

Issue 1414343007: Collect threat details for phishing and UwS (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: nit Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 // Implementation of the MalwareDetails class. 5 // Implementation of the ThreatDetails class.
6 6
7 #include "chrome/browser/safe_browsing/threat_details.h" 7 #include "chrome/browser/safe_browsing/threat_details.h"
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/lazy_instance.h" 10 #include "base/lazy_instance.h"
11 #include "chrome/browser/profiles/profile.h" 11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/safe_browsing/report.pb.h"
13 #include "chrome/browser/safe_browsing/threat_details_cache.h" 12 #include "chrome/browser/safe_browsing/threat_details_cache.h"
14 #include "chrome/browser/safe_browsing/threat_details_history.h" 13 #include "chrome/browser/safe_browsing/threat_details_history.h"
15 #include "chrome/common/safe_browsing/safebrowsing_messages.h" 14 #include "chrome/common/safe_browsing/safebrowsing_messages.h"
16 #include "content/public/browser/browser_thread.h" 15 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/navigation_controller.h" 16 #include "content/public/browser/navigation_controller.h"
18 #include "content/public/browser/navigation_entry.h" 17 #include "content/public/browser/navigation_entry.h"
19 #include "content/public/browser/render_view_host.h" 18 #include "content/public/browser/render_view_host.h"
20 #include "content/public/browser/web_contents.h" 19 #include "content/public/browser/web_contents.h"
21 #include "net/url_request/url_request_context_getter.h" 20 #include "net/url_request/url_request_context_getter.h"
22 21
23 using content::BrowserThread; 22 using content::BrowserThread;
24 using content::NavigationEntry; 23 using content::NavigationEntry;
25 using content::WebContents; 24 using content::WebContents;
26 using safe_browsing::ClientMalwareReportRequest; 25 using safe_browsing::ClientSafeBrowsingReportRequest;
27 26
28 // Keep in sync with KMaxNodes in renderer/safe_browsing/malware_dom_details 27 // Keep in sync with KMaxNodes in renderer/safe_browsing/threat_dom_details
29 static const uint32 kMaxDomNodes = 500; 28 static const uint32 kMaxDomNodes = 500;
30 29
31 // static 30 // static
32 ThreatDetailsFactory* ThreatDetails::factory_ = NULL; 31 ThreatDetailsFactory* ThreatDetails::factory_ = NULL;
33 32
33 namespace {
34
35 // Helper function that converts SBThreatType to
36 // ClientSafeBrowsingReportRequest::ReportType.
37 ClientSafeBrowsingReportRequest::ReportType GetReportTypeFromSBThreatType(
38 SBThreatType threat_type) {
39 switch (threat_type) {
40 case SB_THREAT_TYPE_URL_PHISHING:
41 return ClientSafeBrowsingReportRequest::URL_PHISHING;
42 case SB_THREAT_TYPE_URL_MALWARE:
43 return ClientSafeBrowsingReportRequest::URL_MALWARE;
44 case SB_THREAT_TYPE_URL_UNWANTED:
45 return ClientSafeBrowsingReportRequest::URL_UNWANTED;
46 case SB_THREAT_TYPE_CLIENT_SIDE_PHISHING_URL:
47 return ClientSafeBrowsingReportRequest::CLIENT_SIDE_PHISHING_URL;
48 case SB_THREAT_TYPE_CLIENT_SIDE_MALWARE_URL:
49 return ClientSafeBrowsingReportRequest::CLIENT_SIDE_MALWARE_URL;
50 default: // Gated by SafeBrowsingBlockingPage::ShouldReportThreatDetails.
51 NOTREACHED() << "We should not send report for threat type "
52 << threat_type;
53 return ClientSafeBrowsingReportRequest::UNKNOWN;
54 }
55 }
56
57 } // namespace
58
34 // The default ThreatDetailsFactory. Global, made a singleton so we 59 // The default ThreatDetailsFactory. Global, made a singleton so we
35 // don't leak it. 60 // don't leak it.
36 class ThreatDetailsFactoryImpl : public ThreatDetailsFactory { 61 class ThreatDetailsFactoryImpl : public ThreatDetailsFactory {
37 public: 62 public:
38 ThreatDetails* CreateThreatDetails( 63 ThreatDetails* CreateThreatDetails(
39 SafeBrowsingUIManager* ui_manager, 64 SafeBrowsingUIManager* ui_manager,
40 WebContents* web_contents, 65 WebContents* web_contents,
41 const SafeBrowsingUIManager::UnsafeResource& unsafe_resource) override { 66 const SafeBrowsingUIManager::UnsafeResource& unsafe_resource) override {
42 return new ThreatDetails(ui_manager, web_contents, unsafe_resource); 67 return new ThreatDetails(ui_manager, web_contents, unsafe_resource);
43 } 68 }
44 69
45 private: 70 private:
46 friend struct base::DefaultLazyInstanceTraits<ThreatDetailsFactoryImpl>; 71 friend struct base::DefaultLazyInstanceTraits<ThreatDetailsFactoryImpl>;
47 72
48 ThreatDetailsFactoryImpl() {} 73 ThreatDetailsFactoryImpl() {}
49 74
50 DISALLOW_COPY_AND_ASSIGN(ThreatDetailsFactoryImpl); 75 DISALLOW_COPY_AND_ASSIGN(ThreatDetailsFactoryImpl);
51 }; 76 };
52 77
53 static base::LazyInstance<ThreatDetailsFactoryImpl> 78 static base::LazyInstance<ThreatDetailsFactoryImpl>
54 g_malware_details_factory_impl = LAZY_INSTANCE_INITIALIZER; 79 g_threat_details_factory_impl = LAZY_INSTANCE_INITIALIZER;
55 80
56 // Create a ThreatDetails for the given tab. 81 // Create a ThreatDetails for the given tab.
57 /* static */ 82 /* static */
58 ThreatDetails* ThreatDetails::NewThreatDetails( 83 ThreatDetails* ThreatDetails::NewThreatDetails(
59 SafeBrowsingUIManager* ui_manager, 84 SafeBrowsingUIManager* ui_manager,
60 WebContents* web_contents, 85 WebContents* web_contents,
61 const UnsafeResource& resource) { 86 const UnsafeResource& resource) {
62 // Set up the factory if this has not been done already (tests do that 87 // Set up the factory if this has not been done already (tests do that
63 // before this method is called). 88 // before this method is called).
64 if (!factory_) 89 if (!factory_)
65 factory_ = g_malware_details_factory_impl.Pointer(); 90 factory_ = g_threat_details_factory_impl.Pointer();
66 return factory_->CreateThreatDetails(ui_manager, web_contents, resource); 91 return factory_->CreateThreatDetails(ui_manager, web_contents, resource);
67 } 92 }
68 93
69 // Create a ThreatDetails for the given tab. Runs in the UI thread. 94 // Create a ThreatDetails for the given tab. Runs in the UI thread.
70 ThreatDetails::ThreatDetails(SafeBrowsingUIManager* ui_manager, 95 ThreatDetails::ThreatDetails(SafeBrowsingUIManager* ui_manager,
71 content::WebContents* web_contents, 96 content::WebContents* web_contents,
72 const UnsafeResource& resource) 97 const UnsafeResource& resource)
73 : content::WebContentsObserver(web_contents), 98 : content::WebContentsObserver(web_contents),
74 profile_(Profile::FromBrowserContext(web_contents->GetBrowserContext())), 99 profile_(Profile::FromBrowserContext(web_contents->GetBrowserContext())),
75 request_context_getter_(profile_->GetRequestContext()), 100 request_context_getter_(profile_->GetRequestContext()),
76 ui_manager_(ui_manager), 101 ui_manager_(ui_manager),
77 resource_(resource), 102 resource_(resource),
78 cache_result_(false), 103 cache_result_(false),
79 cache_collector_(new ThreatDetailsCacheCollector), 104 cache_collector_(new ThreatDetailsCacheCollector),
80 redirects_collector_(new ThreatDetailsRedirectsCollector(profile_)) { 105 redirects_collector_(new ThreatDetailsRedirectsCollector(profile_)) {
81 StartCollection(); 106 StartCollection();
82 } 107 }
83 108
84 ThreatDetails::~ThreatDetails() {} 109 ThreatDetails::~ThreatDetails() {}
85 110
86 bool ThreatDetails::OnMessageReceived(const IPC::Message& message) { 111 bool ThreatDetails::OnMessageReceived(const IPC::Message& message) {
87 bool handled = true; 112 bool handled = true;
88 IPC_BEGIN_MESSAGE_MAP(ThreatDetails, message) 113 IPC_BEGIN_MESSAGE_MAP(ThreatDetails, message)
89 IPC_MESSAGE_HANDLER(SafeBrowsingHostMsg_MalwareDOMDetails, 114 IPC_MESSAGE_HANDLER(SafeBrowsingHostMsg_ThreatDOMDetails,
90 OnReceivedThreatDOMDetails) 115 OnReceivedThreatDOMDetails)
91 IPC_MESSAGE_UNHANDLED(handled = false) 116 IPC_MESSAGE_UNHANDLED(handled = false)
92 IPC_END_MESSAGE_MAP() 117 IPC_END_MESSAGE_MAP()
93 return handled; 118 return handled;
94 } 119 }
95 120
96 bool ThreatDetails::IsReportableUrl(const GURL& url) const { 121 bool ThreatDetails::IsReportableUrl(const GURL& url) const {
97 // TODO(panayiotis): also skip internal urls. 122 // TODO(panayiotis): also skip internal urls.
98 return url.SchemeIs("http") || url.SchemeIs("https"); 123 return url.SchemeIs("http") || url.SchemeIs("https");
99 } 124 }
100 125
101 // Looks for a Resource for the given url in resources_. If found, it 126 // Looks for a Resource for the given url in resources_. If found, it
102 // updates |resource|. Otherwise, it creates a new message, adds it to 127 // updates |resource|. Otherwise, it creates a new message, adds it to
103 // resources_ and updates |resource| to point to it. 128 // resources_ and updates |resource| to point to it.
104 // 129 //
105 ClientMalwareReportRequest::Resource* ThreatDetails::FindOrCreateResource( 130 ClientSafeBrowsingReportRequest::Resource* ThreatDetails::FindOrCreateResource(
106 const GURL& url) { 131 const GURL& url) {
107 safe_browsing::ResourceMap::iterator it = resources_.find(url.spec()); 132 safe_browsing::ResourceMap::iterator it = resources_.find(url.spec());
108 if (it != resources_.end()) 133 if (it != resources_.end())
109 return it->second.get(); 134 return it->second.get();
110 135
111 // Create the resource for |url|. 136 // Create the resource for |url|.
112 int id = resources_.size(); 137 int id = resources_.size();
113 linked_ptr<ClientMalwareReportRequest::Resource> new_resource( 138 linked_ptr<ClientSafeBrowsingReportRequest::Resource> new_resource(
114 new ClientMalwareReportRequest::Resource()); 139 new ClientSafeBrowsingReportRequest::Resource());
115 new_resource->set_url(url.spec()); 140 new_resource->set_url(url.spec());
116 new_resource->set_id(id); 141 new_resource->set_id(id);
117 resources_[url.spec()] = new_resource; 142 resources_[url.spec()] = new_resource;
118 return new_resource.get(); 143 return new_resource.get();
119 } 144 }
120 145
121 void ThreatDetails::AddUrl(const GURL& url, 146 void ThreatDetails::AddUrl(const GURL& url,
122 const GURL& parent, 147 const GURL& parent,
123 const std::string& tagname, 148 const std::string& tagname,
124 const std::vector<GURL>* children) { 149 const std::vector<GURL>* children) {
125 if (!url.is_valid() || !IsReportableUrl(url)) 150 if (!url.is_valid() || !IsReportableUrl(url))
126 return; 151 return;
127 152
128 // Find (or create) the resource for the url. 153 // Find (or create) the resource for the url.
129 ClientMalwareReportRequest::Resource* url_resource = 154 ClientSafeBrowsingReportRequest::Resource* url_resource =
130 FindOrCreateResource(url); 155 FindOrCreateResource(url);
131 if (!tagname.empty()) 156 if (!tagname.empty())
132 url_resource->set_tag_name(tagname); 157 url_resource->set_tag_name(tagname);
133 if (!parent.is_empty() && IsReportableUrl(parent)) { 158 if (!parent.is_empty() && IsReportableUrl(parent)) {
134 // Add the resource for the parent. 159 // Add the resource for the parent.
135 ClientMalwareReportRequest::Resource* parent_resource = 160 ClientSafeBrowsingReportRequest::Resource* parent_resource =
136 FindOrCreateResource(parent); 161 FindOrCreateResource(parent);
137 // Update the parent-child relation 162 // Update the parent-child relation
138 url_resource->set_parent_id(parent_resource->id()); 163 url_resource->set_parent_id(parent_resource->id());
139 } 164 }
140 if (children) { 165 if (children) {
141 for (std::vector<GURL>::const_iterator it = children->begin(); 166 for (std::vector<GURL>::const_iterator it = children->begin();
142 it != children->end(); ++it) { 167 it != children->end(); ++it) {
143 ClientMalwareReportRequest::Resource* child_resource = 168 ClientSafeBrowsingReportRequest::Resource* child_resource =
144 FindOrCreateResource(*it); 169 FindOrCreateResource(*it);
145 url_resource->add_child_ids(child_resource->id()); 170 url_resource->add_child_ids(child_resource->id());
146 } 171 }
147 } 172 }
148 } 173 }
149 174
150 void ThreatDetails::StartCollection() { 175 void ThreatDetails::StartCollection() {
151 DVLOG(1) << "Starting to compute malware details."; 176 DVLOG(1) << "Starting to compute threat details.";
152 report_.reset(new ClientMalwareReportRequest()); 177 report_.reset(new ClientSafeBrowsingReportRequest());
153 178
154 if (IsReportableUrl(resource_.url)) 179 if (IsReportableUrl(resource_.url)) {
155 report_->set_malware_url(resource_.url.spec()); 180 report_->set_url(resource_.url.spec());
181 report_->set_type(GetReportTypeFromSBThreatType(resource_.threat_type));
182 }
156 183
157 GURL page_url = web_contents()->GetURL(); 184 GURL page_url = web_contents()->GetURL();
158 if (IsReportableUrl(page_url)) 185 if (IsReportableUrl(page_url))
159 report_->set_page_url(page_url.spec()); 186 report_->set_page_url(page_url.spec());
160 187
161 GURL referrer_url; 188 GURL referrer_url;
162 NavigationEntry* nav_entry = web_contents()->GetController().GetActiveEntry(); 189 NavigationEntry* nav_entry = web_contents()->GetController().GetActiveEntry();
163 if (nav_entry) { 190 if (nav_entry) {
164 referrer_url = nav_entry->GetReferrer().url; 191 referrer_url = nav_entry->GetReferrer().url;
165 if (IsReportableUrl(referrer_url)) { 192 if (IsReportableUrl(referrer_url)) {
(...skipping 29 matching lines...) Expand all
195 parent_url = resource_.redirect_urls[i]; 222 parent_url = resource_.redirect_urls[i];
196 } 223 }
197 224
198 // Add the referrer url. 225 // Add the referrer url.
199 if (nav_entry && !referrer_url.is_empty()) 226 if (nav_entry && !referrer_url.is_empty())
200 AddUrl(referrer_url, GURL(), std::string(), NULL); 227 AddUrl(referrer_url, GURL(), std::string(), NULL);
201 228
202 // Get URLs of frames, scripts etc from the DOM. 229 // Get URLs of frames, scripts etc from the DOM.
203 // OnReceivedThreatDOMDetails will be called when the renderer replies. 230 // OnReceivedThreatDOMDetails will be called when the renderer replies.
204 content::RenderViewHost* view = web_contents()->GetRenderViewHost(); 231 content::RenderViewHost* view = web_contents()->GetRenderViewHost();
205 view->Send(new SafeBrowsingMsg_GetMalwareDOMDetails(view->GetRoutingID())); 232 view->Send(new SafeBrowsingMsg_GetThreatDOMDetails(view->GetRoutingID()));
206 } 233 }
207 234
208 // When the renderer is done, this is called. 235 // When the renderer is done, this is called.
209 void ThreatDetails::OnReceivedThreatDOMDetails( 236 void ThreatDetails::OnReceivedThreatDOMDetails(
210 const std::vector<SafeBrowsingHostMsg_MalwareDOMDetails_Node>& params) { 237 const std::vector<SafeBrowsingHostMsg_ThreatDOMDetails_Node>& params) {
211 // Schedule this in IO thread, so it doesn't conflict with future users 238 // Schedule this in IO thread, so it doesn't conflict with future users
212 // of our data structures (eg GetSerializedReport). 239 // of our data structures (eg GetSerializedReport).
213 BrowserThread::PostTask( 240 BrowserThread::PostTask(
214 BrowserThread::IO, FROM_HERE, 241 BrowserThread::IO, FROM_HERE,
215 base::Bind(&ThreatDetails::AddDOMDetails, this, params)); 242 base::Bind(&ThreatDetails::AddDOMDetails, this, params));
216 } 243 }
217 244
218 void ThreatDetails::AddDOMDetails( 245 void ThreatDetails::AddDOMDetails(
219 const std::vector<SafeBrowsingHostMsg_MalwareDOMDetails_Node>& params) { 246 const std::vector<SafeBrowsingHostMsg_ThreatDOMDetails_Node>& params) {
220 DCHECK_CURRENTLY_ON(BrowserThread::IO); 247 DCHECK_CURRENTLY_ON(BrowserThread::IO);
221 DVLOG(1) << "Nodes from the DOM: " << params.size(); 248 DVLOG(1) << "Nodes from the DOM: " << params.size();
222 249
223 // If we have already started getting redirects from history service, 250 // If we have already started getting redirects from history service,
224 // don't modify state, otherwise will invalidate the iterators. 251 // don't modify state, otherwise will invalidate the iterators.
225 if (redirects_collector_->HasStarted()) 252 if (redirects_collector_->HasStarted())
226 return; 253 return;
227 254
228 // If we have already started collecting data from the HTTP cache, don't 255 // If we have already started collecting data from the HTTP cache, don't
229 // modify our state. 256 // modify our state.
230 if (cache_collector_->HasStarted()) 257 if (cache_collector_->HasStarted())
231 return; 258 return;
232 259
233 // Add the urls from the DOM to |resources_|. The renderer could be 260 // Add the urls from the DOM to |resources_|. The renderer could be
234 // sending bogus messages, so limit the number of nodes we accept. 261 // sending bogus messages, so limit the number of nodes we accept.
235 for (size_t i = 0; i < params.size() && i < kMaxDomNodes; ++i) { 262 for (size_t i = 0; i < params.size() && i < kMaxDomNodes; ++i) {
236 SafeBrowsingHostMsg_MalwareDOMDetails_Node node = params[i]; 263 SafeBrowsingHostMsg_ThreatDOMDetails_Node node = params[i];
237 DVLOG(1) << node.url << ", " << node.tag_name << ", " << node.parent; 264 DVLOG(1) << node.url << ", " << node.tag_name << ", " << node.parent;
238 AddUrl(node.url, node.parent, node.tag_name, &(node.children)); 265 AddUrl(node.url, node.parent, node.tag_name, &(node.children));
239 } 266 }
240 } 267 }
241 268
242 // Called from the SB Service on the IO thread, after the user has 269 // Called from the SB Service on the IO thread, after the user has
243 // closed the tab, or clicked proceed or goback. Since the user needs 270 // closed the tab, or clicked proceed or goback. Since the user needs
244 // to take an action, we expect this to be called after 271 // to take an action, we expect this to be called after
245 // OnReceivedThreatDOMDetails in most cases. If not, we don't include 272 // OnReceivedThreatDOMDetails in most cases. If not, we don't include
246 // the DOM data in our report. 273 // the DOM data in our report.
(...skipping 30 matching lines...) Expand all
277 for (size_t i = 0; i < urls.size() - 1; ++i) { 304 for (size_t i = 0; i < urls.size() - 1; ++i) {
278 AddUrl(urls[i], urls[i + 1], std::string(), NULL); 305 AddUrl(urls[i], urls[i + 1], std::string(), NULL);
279 } 306 }
280 } 307 }
281 308
282 void ThreatDetails::OnCacheCollectionReady() { 309 void ThreatDetails::OnCacheCollectionReady() {
283 DVLOG(1) << "OnCacheCollectionReady."; 310 DVLOG(1) << "OnCacheCollectionReady.";
284 // Add all the urls in our |resources_| maps to the |report_| protocol buffer. 311 // Add all the urls in our |resources_| maps to the |report_| protocol buffer.
285 for (safe_browsing::ResourceMap::const_iterator it = resources_.begin(); 312 for (safe_browsing::ResourceMap::const_iterator it = resources_.begin();
286 it != resources_.end(); ++it) { 313 it != resources_.end(); ++it) {
287 ClientMalwareReportRequest::Resource* pb_resource = 314 ClientSafeBrowsingReportRequest::Resource* pb_resource =
288 report_->add_resources(); 315 report_->add_resources();
289 pb_resource->CopyFrom(*(it->second)); 316 pb_resource->CopyFrom(*(it->second));
290 const GURL url(pb_resource->url()); 317 const GURL url(pb_resource->url());
291 if (url.SchemeIs("https")) { 318 if (url.SchemeIs("https")) {
292 // Don't report headers of HTTPS requests since they may contain private 319 // Don't report headers of HTTPS requests since they may contain private
293 // cookies. We still retain the full URL. 320 // cookies. We still retain the full URL.
294 DVLOG(1) << "Clearing out HTTPS resource: " << pb_resource->url(); 321 DVLOG(1) << "Clearing out HTTPS resource: " << pb_resource->url();
295 pb_resource->clear_request(); 322 pb_resource->clear_request();
296 pb_resource->clear_response(); 323 pb_resource->clear_response();
297 // Keep id, parent_id, child_ids, and tag_name. 324 // Keep id, parent_id, child_ids, and tag_name.
298 } 325 }
299 } 326 }
300 report_->set_did_proceed(did_proceed_); 327 report_->set_did_proceed(did_proceed_);
301 // Only sets repeat_visit if num_visits_ >= 0. 328 // Only sets repeat_visit if num_visits_ >= 0.
302 if (num_visits_ >= 0) { 329 if (num_visits_ >= 0) {
303 report_->set_repeat_visit(num_visits_ > 0); 330 report_->set_repeat_visit(num_visits_ > 0);
304 } 331 }
305 report_->set_complete(cache_result_); 332 report_->set_complete(cache_result_);
306 333
307 // Send the report, using the SafeBrowsingService. 334 // Send the report, using the SafeBrowsingService.
308 std::string serialized; 335 std::string serialized;
309 if (!report_->SerializeToString(&serialized)) { 336 if (!report_->SerializeToString(&serialized)) {
310 DLOG(ERROR) << "Unable to serialize the malware report."; 337 DLOG(ERROR) << "Unable to serialize the threat report.";
311 return; 338 return;
312 } 339 }
313
314 ui_manager_->SendSerializedThreatDetails(serialized); 340 ui_manager_->SendSerializedThreatDetails(serialized);
315 } 341 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698