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/local_database_manager.h" | 5 #include "chrome/browser/safe_browsing/local_database_manager.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
88 // |full_hashes| should never contain INVALID as a |list_id|. | 88 // |full_hashes| should never contain INVALID as a |list_id|. |
89 NOTREACHED(); | 89 NOTREACHED(); |
90 break; | 90 break; |
91 case MALWARE: // Falls through. | 91 case MALWARE: // Falls through. |
92 case PHISH: // Falls through. | 92 case PHISH: // Falls through. |
93 case BINURL: // Falls through. | 93 case BINURL: // Falls through. |
94 case CSDWHITELIST: // Falls through. | 94 case CSDWHITELIST: // Falls through. |
95 case DOWNLOADWHITELIST: // Falls through. | 95 case DOWNLOADWHITELIST: // Falls through. |
96 case INCLUSIONWHITELIST: // Falls through. | 96 case INCLUSIONWHITELIST: // Falls through. |
97 case EXTENSIONBLACKLIST: // Falls through. | 97 case EXTENSIONBLACKLIST: // Falls through. |
98 case IPBLACKLIST: | 98 case IPBLACKLIST: |
Nathan Parker
2016/02/16 21:42:00
Now that there are three levels, how about turning
veranika
2016/02/17 15:37:53
Done, but in my code smaller numbers correspond to
| |
99 if (index) | 99 if (index) |
100 *index = i; | 100 *index = i; |
101 return threat; | 101 return threat; |
102 case UNWANTEDURL: | 102 case UNWANTEDURL: |
103 // UNWANTEDURL is considered less severe than other threats, keep | 103 // UNWANTEDURL is considered less severe than other threats, keep |
104 // looking. | 104 // looking. |
105 pending_threat = threat; | 105 pending_threat = threat; |
106 if (index) | 106 if (index) |
107 *index = i; | 107 *index = i; |
108 break; | 108 break; |
109 case RESOURCEBLACKLIST: | |
110 // RESOURCEBLACKLIST is even less severe than UNWANTEDURL. | |
111 if (pending_threat == INVALID) { | |
112 pending_threat = threat; | |
113 if (index) | |
114 *index = i; | |
115 } | |
116 break; | |
109 } | 117 } |
110 } | 118 } |
111 } | 119 } |
112 return pending_threat; | 120 return pending_threat; |
113 } | 121 } |
114 | 122 |
115 // Given a URL, compare all the possible host + path full hashes to the set of | 123 // Given a URL, compare all the possible host + path full hashes to the set of |
116 // provided full hashes. Returns the list id of the severest matching result | 124 // provided full hashes. Returns the list id of the severest matching result |
117 // from |full_hashes|, or INVALID if none match. | 125 // from |full_hashes|, or INVALID if none match. |
118 ListType GetUrlSeverestThreatListType( | 126 ListType GetUrlSeverestThreatListType( |
(...skipping 15 matching lines...) Expand all Loading... | |
134 // Ignore patterns with no matching threat. | 142 // Ignore patterns with no matching threat. |
135 break; | 143 break; |
136 case MALWARE: // Falls through. | 144 case MALWARE: // Falls through. |
137 case PHISH: // Falls through. | 145 case PHISH: // Falls through. |
138 case BINURL: // Falls through. | 146 case BINURL: // Falls through. |
139 case CSDWHITELIST: // Falls through. | 147 case CSDWHITELIST: // Falls through. |
140 case DOWNLOADWHITELIST: // Falls through. | 148 case DOWNLOADWHITELIST: // Falls through. |
141 case INCLUSIONWHITELIST: // Falls through. | 149 case INCLUSIONWHITELIST: // Falls through. |
142 case EXTENSIONBLACKLIST: // Falls through. | 150 case EXTENSIONBLACKLIST: // Falls through. |
143 case IPBLACKLIST: | 151 case IPBLACKLIST: |
144 return threat; | 152 return threat; |
Nathan Parker
2016/02/16 21:42:00
Same comment as above.
veranika
2016/02/17 15:37:53
Done.
| |
145 case UNWANTEDURL: | 153 case UNWANTEDURL: |
146 // UNWANTEDURL is considered less severe than other threats, keep | 154 // UNWANTEDURL is considered less severe than other threats, keep |
147 // looking. | 155 // looking. |
148 pending_threat = threat; | 156 pending_threat = threat; |
149 break; | 157 break; |
158 case RESOURCEBLACKLIST: | |
159 // RESOURCEBLACKLIST is even less severe than UNWANTEDURL. | |
160 if (pending_threat == INVALID) { | |
161 pending_threat = threat; | |
162 } | |
163 break; | |
150 } | 164 } |
151 } | 165 } |
152 return pending_threat; | 166 return pending_threat; |
153 } | 167 } |
154 | 168 |
155 SBThreatType GetThreatTypeFromListType(ListType list_type) { | 169 SBThreatType GetThreatTypeFromListType(ListType list_type) { |
156 switch (list_type) { | 170 switch (list_type) { |
157 case PHISH: | 171 case PHISH: |
158 return SB_THREAT_TYPE_URL_PHISHING; | 172 return SB_THREAT_TYPE_URL_PHISHING; |
159 case MALWARE: | 173 case MALWARE: |
160 return SB_THREAT_TYPE_URL_MALWARE; | 174 return SB_THREAT_TYPE_URL_MALWARE; |
161 case UNWANTEDURL: | 175 case UNWANTEDURL: |
162 return SB_THREAT_TYPE_URL_UNWANTED; | 176 return SB_THREAT_TYPE_URL_UNWANTED; |
163 case BINURL: | 177 case BINURL: |
164 return SB_THREAT_TYPE_BINARY_MALWARE_URL; | 178 return SB_THREAT_TYPE_BINARY_MALWARE_URL; |
165 case EXTENSIONBLACKLIST: | 179 case EXTENSIONBLACKLIST: |
166 return SB_THREAT_TYPE_EXTENSION; | 180 return SB_THREAT_TYPE_EXTENSION; |
181 case RESOURCEBLACKLIST: | |
182 return SB_THREAT_TYPE_BLACKLISTED_RESOURCE; | |
167 default: | 183 default: |
168 DVLOG(1) << "Unknown safe browsing list id " << list_type; | 184 DVLOG(1) << "Unknown safe browsing list id " << list_type; |
169 return SB_THREAT_TYPE_SAFE; | 185 return SB_THREAT_TYPE_SAFE; |
170 } | 186 } |
171 } | 187 } |
172 | 188 |
173 } // namespace | 189 } // namespace |
174 | 190 |
175 // static | 191 // static |
176 SBThreatType LocalSafeBrowsingDatabaseManager::GetHashSeverestThreatType( | 192 SBThreatType LocalSafeBrowsingDatabaseManager::GetHashSeverestThreatType( |
(...skipping 14 matching lines...) Expand all Loading... | |
191 | 207 |
192 LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck::SafeBrowsingCheck( | 208 LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck::SafeBrowsingCheck( |
193 const std::vector<GURL>& urls, | 209 const std::vector<GURL>& urls, |
194 const std::vector<SBFullHash>& full_hashes, | 210 const std::vector<SBFullHash>& full_hashes, |
195 Client* client, | 211 Client* client, |
196 ListType check_type, | 212 ListType check_type, |
197 const std::vector<SBThreatType>& expected_threats) | 213 const std::vector<SBThreatType>& expected_threats) |
198 : urls(urls), | 214 : urls(urls), |
199 url_results(urls.size(), SB_THREAT_TYPE_SAFE), | 215 url_results(urls.size(), SB_THREAT_TYPE_SAFE), |
200 url_metadata(urls.size()), | 216 url_metadata(urls.size()), |
217 url_hit_hash(urls.size()), | |
201 full_hashes(full_hashes), | 218 full_hashes(full_hashes), |
202 full_hash_results(full_hashes.size(), SB_THREAT_TYPE_SAFE), | 219 full_hash_results(full_hashes.size(), SB_THREAT_TYPE_SAFE), |
203 client(client), | 220 client(client), |
204 need_get_hash(false), | 221 need_get_hash(false), |
205 check_type(check_type), | 222 check_type(check_type), |
206 expected_threats(expected_threats) { | 223 expected_threats(expected_threats) { |
207 DCHECK_EQ(urls.empty(), !full_hashes.empty()) | 224 DCHECK_EQ(urls.empty(), !full_hashes.empty()) |
208 << "Exactly one of urls and full_hashes must be set"; | 225 << "Exactly one of urls and full_hashes must be set"; |
209 } | 226 } |
210 | 227 |
(...skipping 15 matching lines...) Expand all Loading... | |
226 case UNWANTEDURL: | 243 case UNWANTEDURL: |
227 DCHECK_EQ(1u, urls.size()); | 244 DCHECK_EQ(1u, urls.size()); |
228 client->OnCheckBrowseUrlResult(urls[0], url_results[0], | 245 client->OnCheckBrowseUrlResult(urls[0], url_results[0], |
229 url_metadata[0]); | 246 url_metadata[0]); |
230 break; | 247 break; |
231 case BINURL: | 248 case BINURL: |
232 DCHECK_EQ(urls.size(), url_results.size()); | 249 DCHECK_EQ(urls.size(), url_results.size()); |
233 client->OnCheckDownloadUrlResult( | 250 client->OnCheckDownloadUrlResult( |
234 urls, *std::max_element(url_results.begin(), url_results.end())); | 251 urls, *std::max_element(url_results.begin(), url_results.end())); |
235 break; | 252 break; |
253 case RESOURCEBLACKLIST: | |
254 DCHECK_EQ(1u, urls.size()); | |
255 client->OnCheckResourceUrlResult(urls[0], url_results[0], | |
256 url_hit_hash[0]); | |
257 break; | |
236 default: | 258 default: |
237 NOTREACHED(); | 259 NOTREACHED(); |
238 } | 260 } |
239 } else if (!full_hashes.empty()) { | 261 } else if (!full_hashes.empty()) { |
240 switch (check_type) { | 262 switch (check_type) { |
241 case EXTENSIONBLACKLIST: { | 263 case EXTENSIONBLACKLIST: { |
242 std::set<std::string> unsafe_extension_ids; | 264 std::set<std::string> unsafe_extension_ids; |
243 for (size_t i = 0; i < full_hashes.size(); ++i) { | 265 for (size_t i = 0; i < full_hashes.size(); ++i) { |
244 std::string extension_id = | 266 std::string extension_id = |
245 SBFullHashToString(full_hashes[i]); | 267 SBFullHashToString(full_hashes[i]); |
(...skipping 15 matching lines...) Expand all Loading... | |
261 const scoped_refptr<SafeBrowsingService>& service) | 283 const scoped_refptr<SafeBrowsingService>& service) |
262 : sb_service_(service), | 284 : sb_service_(service), |
263 database_(NULL), | 285 database_(NULL), |
264 enabled_(false), | 286 enabled_(false), |
265 enable_download_protection_(false), | 287 enable_download_protection_(false), |
266 enable_csd_whitelist_(false), | 288 enable_csd_whitelist_(false), |
267 enable_download_whitelist_(false), | 289 enable_download_whitelist_(false), |
268 enable_extension_blacklist_(false), | 290 enable_extension_blacklist_(false), |
269 enable_ip_blacklist_(false), | 291 enable_ip_blacklist_(false), |
270 enable_unwanted_software_blacklist_(true), | 292 enable_unwanted_software_blacklist_(true), |
293 enable_resource_blacklist_(true), | |
271 update_in_progress_(false), | 294 update_in_progress_(false), |
272 database_update_in_progress_(false), | 295 database_update_in_progress_(false), |
273 closing_database_(false), | 296 closing_database_(false), |
274 check_timeout_(base::TimeDelta::FromMilliseconds(kCheckTimeoutMs)) { | 297 check_timeout_(base::TimeDelta::FromMilliseconds(kCheckTimeoutMs)) { |
275 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 298 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
276 DCHECK(sb_service_.get() != NULL); | 299 DCHECK(sb_service_.get() != NULL); |
277 | 300 |
278 base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess(); | 301 base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess(); |
279 enable_download_protection_ = | 302 enable_download_protection_ = |
280 !cmdline->HasSwitch(switches::kSbDisableDownloadProtection); | 303 !cmdline->HasSwitch(switches::kSbDisableDownloadProtection); |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
379 client, | 402 client, |
380 EXTENSIONBLACKLIST, | 403 EXTENSIONBLACKLIST, |
381 std::vector<SBThreatType>(1, SB_THREAT_TYPE_EXTENSION)); | 404 std::vector<SBThreatType>(1, SB_THREAT_TYPE_EXTENSION)); |
382 StartSafeBrowsingCheck( | 405 StartSafeBrowsingCheck( |
383 check, | 406 check, |
384 base::Bind(&LocalSafeBrowsingDatabaseManager::CheckExtensionIDsOnSBThread, | 407 base::Bind(&LocalSafeBrowsingDatabaseManager::CheckExtensionIDsOnSBThread, |
385 this, prefixes)); | 408 this, prefixes)); |
386 return false; | 409 return false; |
387 } | 410 } |
388 | 411 |
412 bool LocalSafeBrowsingDatabaseManager::CheckResourceUrl( | |
413 const GURL& url, Client* client) { | |
414 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
415 | |
416 if (!enabled_ || !enable_resource_blacklist_ || !CanCheckUrl(url)) | |
417 return true; | |
Nathan Parker
2016/02/16 21:42:00
Do you need to check that the DB is available, lik
veranika
2016/02/17 15:37:54
Done.
| |
418 | |
419 SafeBrowsingCheck* check = | |
420 new SafeBrowsingCheck({url}, std::vector<SBFullHash>(), client, | |
421 RESOURCEBLACKLIST, | |
422 {SB_THREAT_TYPE_BLACKLISTED_RESOURCE}); | |
423 std::vector<SBPrefix> prefixes; | |
424 SafeBrowsingDatabase::GetDownloadUrlPrefixes(check->urls, &prefixes); | |
425 StartSafeBrowsingCheck( | |
426 check, | |
427 base::Bind(&LocalSafeBrowsingDatabaseManager::CheckResourceUrlOnSBThread, | |
428 this, prefixes)); | |
429 return false; | |
430 } | |
431 | |
389 bool LocalSafeBrowsingDatabaseManager::MatchMalwareIP( | 432 bool LocalSafeBrowsingDatabaseManager::MatchMalwareIP( |
390 const std::string& ip_address) { | 433 const std::string& ip_address) { |
391 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 434 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
392 if (!enabled_ || !enable_ip_blacklist_ || !MakeDatabaseAvailable()) { | 435 if (!enabled_ || !enable_ip_blacklist_ || !MakeDatabaseAvailable()) { |
393 return false; // Fail open. | 436 return false; // Fail open. |
394 } | 437 } |
395 return database_->ContainsMalwareIP(ip_address); | 438 return database_->ContainsMalwareIP(ip_address); |
396 } | 439 } |
397 | 440 |
398 bool LocalSafeBrowsingDatabaseManager::MatchCsdWhitelistUrl(const GURL& url) { | 441 bool LocalSafeBrowsingDatabaseManager::MatchCsdWhitelistUrl(const GURL& url) { |
(...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
773 DCHECK(safe_browsing_task_runner_->RunsTasksOnCurrentThread()); | 816 DCHECK(safe_browsing_task_runner_->RunsTasksOnCurrentThread()); |
774 | 817 |
775 if (database_) | 818 if (database_) |
776 return database_; | 819 return database_; |
777 | 820 |
778 const base::TimeTicks before = base::TimeTicks::Now(); | 821 const base::TimeTicks before = base::TimeTicks::Now(); |
779 SafeBrowsingDatabase* database = SafeBrowsingDatabase::Create( | 822 SafeBrowsingDatabase* database = SafeBrowsingDatabase::Create( |
780 safe_browsing_task_runner_, enable_download_protection_, | 823 safe_browsing_task_runner_, enable_download_protection_, |
781 enable_csd_whitelist_, enable_download_whitelist_, | 824 enable_csd_whitelist_, enable_download_whitelist_, |
782 enable_extension_blacklist_, enable_ip_blacklist_, | 825 enable_extension_blacklist_, enable_ip_blacklist_, |
783 enable_unwanted_software_blacklist_); | 826 enable_unwanted_software_blacklist_, |
827 enable_resource_blacklist_); | |
784 | 828 |
785 database->Init(SafeBrowsingService::GetBaseFilename()); | 829 database->Init(SafeBrowsingService::GetBaseFilename()); |
786 { | 830 { |
787 // Acquiring the lock here guarantees correct ordering between the writes to | 831 // Acquiring the lock here guarantees correct ordering between the writes to |
788 // the new database object above, and the setting of |database_| below. | 832 // the new database object above, and the setting of |database_| below. |
789 base::AutoLock lock(database_lock_); | 833 base::AutoLock lock(database_lock_); |
790 database_ = database; | 834 database_ = database; |
791 } | 835 } |
792 | 836 |
793 BrowserThread::PostTask( | 837 BrowserThread::PostTask( |
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1091 } | 1135 } |
1092 | 1136 |
1093 for (size_t i = 0; i < check->urls.size(); ++i) { | 1137 for (size_t i = 0; i < check->urls.size(); ++i) { |
1094 size_t threat_index; | 1138 size_t threat_index; |
1095 SBThreatType threat = GetUrlSeverestThreatType(check->urls[i], | 1139 SBThreatType threat = GetUrlSeverestThreatType(check->urls[i], |
1096 expected_full_hashes, | 1140 expected_full_hashes, |
1097 &threat_index); | 1141 &threat_index); |
1098 if (threat != SB_THREAT_TYPE_SAFE) { | 1142 if (threat != SB_THREAT_TYPE_SAFE) { |
1099 check->url_results[i] = threat; | 1143 check->url_results[i] = threat; |
1100 check->url_metadata[i] = expected_full_hashes[threat_index].metadata; | 1144 check->url_metadata[i] = expected_full_hashes[threat_index].metadata; |
1145 const SBFullHash& hash = expected_full_hashes[threat_index].hash; | |
1146 check->url_hit_hash[i] = std::string(hash.full_hash, | |
1147 arraysize(hash.full_hash)); | |
1101 is_threat = true; | 1148 is_threat = true; |
1102 } | 1149 } |
1103 } | 1150 } |
1104 | 1151 |
1105 for (size_t i = 0; i < check->full_hashes.size(); ++i) { | 1152 for (size_t i = 0; i < check->full_hashes.size(); ++i) { |
1106 SBThreatType threat = | 1153 SBThreatType threat = |
1107 GetHashSeverestThreatType(check->full_hashes[i], expected_full_hashes); | 1154 GetHashSeverestThreatType(check->full_hashes[i], expected_full_hashes); |
1108 if (threat != SB_THREAT_TYPE_SAFE) { | 1155 if (threat != SB_THREAT_TYPE_SAFE) { |
1109 check->full_hash_results[i] = threat; | 1156 check->full_hash_results[i] = threat; |
1110 is_threat = true; | 1157 is_threat = true; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1148 const std::vector<SBPrefix>& prefixes) { | 1195 const std::vector<SBPrefix>& prefixes) { |
1149 DCHECK(safe_browsing_task_runner_->RunsTasksOnCurrentThread()); | 1196 DCHECK(safe_browsing_task_runner_->RunsTasksOnCurrentThread()); |
1150 | 1197 |
1151 std::vector<SBPrefix> prefix_hits; | 1198 std::vector<SBPrefix> prefix_hits; |
1152 const bool result = | 1199 const bool result = |
1153 database_->ContainsExtensionPrefixes(prefixes, &prefix_hits); | 1200 database_->ContainsExtensionPrefixes(prefixes, &prefix_hits); |
1154 DCHECK_EQ(result, !prefix_hits.empty()); | 1201 DCHECK_EQ(result, !prefix_hits.empty()); |
1155 return prefix_hits; | 1202 return prefix_hits; |
1156 } | 1203 } |
1157 | 1204 |
1205 std::vector<SBPrefix> | |
1206 LocalSafeBrowsingDatabaseManager::CheckResourceUrlOnSBThread( | |
1207 const std::vector<SBPrefix>& prefixes) { | |
1208 DCHECK(safe_browsing_task_runner_->RunsTasksOnCurrentThread()); | |
1209 | |
1210 std::vector<SBPrefix> prefix_hits; | |
1211 const bool result = | |
1212 database_->ContainsResourceUrlPrefixes(prefixes, &prefix_hits); | |
1213 DCHECK_EQ(result, !prefix_hits.empty()); | |
1214 return prefix_hits; | |
1215 } | |
1216 | |
1158 void LocalSafeBrowsingDatabaseManager::TimeoutCallback( | 1217 void LocalSafeBrowsingDatabaseManager::TimeoutCallback( |
1159 SafeBrowsingCheck* check) { | 1218 SafeBrowsingCheck* check) { |
1160 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1219 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1161 DCHECK(check); | 1220 DCHECK(check); |
1162 | 1221 |
1163 if (!enabled_) | 1222 if (!enabled_) |
1164 return; | 1223 return; |
1165 | 1224 |
1166 DCHECK(checks_.find(check) != checks_.end()); | 1225 DCHECK(checks_.find(check) != checks_.end()); |
1167 if (check->client) { | 1226 if (check->client) { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1202 FROM_HERE, base::Bind(&LocalSafeBrowsingDatabaseManager::TimeoutCallback, | 1261 FROM_HERE, base::Bind(&LocalSafeBrowsingDatabaseManager::TimeoutCallback, |
1203 check->weak_ptr_factory_->GetWeakPtr(), check), | 1262 check->weak_ptr_factory_->GetWeakPtr(), check), |
1204 check_timeout_); | 1263 check_timeout_); |
1205 } | 1264 } |
1206 | 1265 |
1207 bool LocalSafeBrowsingDatabaseManager::download_protection_enabled() const { | 1266 bool LocalSafeBrowsingDatabaseManager::download_protection_enabled() const { |
1208 return enable_download_protection_; | 1267 return enable_download_protection_; |
1209 } | 1268 } |
1210 | 1269 |
1211 } // namespace safe_browsing | 1270 } // namespace safe_browsing |
OLD | NEW |