OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #ifndef CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_DATABASE_H_ | 5 #ifndef CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_DATABASE_H_ |
6 #define CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_DATABASE_H_ | 6 #define CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_DATABASE_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 } | 86 } |
87 | 87 |
88 virtual ~SafeBrowsingDatabase(); | 88 virtual ~SafeBrowsingDatabase(); |
89 | 89 |
90 // Initializes the database with the given filename. | 90 // Initializes the database with the given filename. |
91 virtual void Init(const base::FilePath& filename) = 0; | 91 virtual void Init(const base::FilePath& filename) = 0; |
92 | 92 |
93 // Deletes the current database and creates a new one. | 93 // Deletes the current database and creates a new one. |
94 virtual bool ResetDatabase() = 0; | 94 virtual bool ResetDatabase() = 0; |
95 | 95 |
96 // Returns false if |url| is not in the browse database. If it | 96 // Returns false if |url| is not in the browse database or already was cached |
97 // returns true, then either |matching_list| is the name of the matching | 97 // as a miss. If it returns true, |prefix_hits| contains matching hash |
98 // list, or |prefix_hits| and |full_hits| contains the matching hash | 98 // prefixes which had no cached results and |cache_hits| contains any matching |
99 // prefixes. This function is safe to call from threads other than | 99 // cached gethash results. This function is safe to call from threads other |
100 // the creation thread. | 100 // than the creation thread. |
101 virtual bool ContainsBrowseUrl(const GURL& url, | 101 virtual bool ContainsBrowseUrl(const GURL& url, |
102 std::string* matching_list, | |
103 std::vector<SBPrefix>* prefix_hits, | 102 std::vector<SBPrefix>* prefix_hits, |
104 std::vector<SBFullHashResult>* full_hits, | 103 std::vector<SBFullHashResult>* cache_hits) = 0; |
105 base::Time last_update) = 0; | |
106 | 104 |
107 // Returns false if none of |urls| are in Download database. If it returns | 105 // Returns false if none of |urls| are in Download database. If it returns |
108 // true, |prefix_hits| should contain the prefixes for the URLs that were in | 106 // true, |prefix_hits| should contain the prefixes for the URLs that were in |
109 // the database. This function could ONLY be accessed from creation thread. | 107 // the database. This function could ONLY be accessed from creation thread. |
110 virtual bool ContainsDownloadUrl(const std::vector<GURL>& urls, | 108 virtual bool ContainsDownloadUrl(const std::vector<GURL>& urls, |
111 std::vector<SBPrefix>* prefix_hits) = 0; | 109 std::vector<SBPrefix>* prefix_hits) = 0; |
112 | 110 |
113 // Returns false if |url| is not on the client-side phishing detection | 111 // Returns false if |url| is not on the client-side phishing detection |
114 // whitelist. Otherwise, this function returns true. Note: the whitelist | 112 // whitelist. Otherwise, this function returns true. Note: the whitelist |
115 // only contains full-length hashes so we don't return any prefix hit. | 113 // only contains full-length hashes so we don't return any prefix hit. |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 virtual bool UpdateStarted(std::vector<SBListChunkRanges>* lists) = 0; | 163 virtual bool UpdateStarted(std::vector<SBListChunkRanges>* lists) = 0; |
166 virtual void InsertChunks(const std::string& list_name, | 164 virtual void InsertChunks(const std::string& list_name, |
167 const SBChunkList& chunks) = 0; | 165 const SBChunkList& chunks) = 0; |
168 virtual void DeleteChunks( | 166 virtual void DeleteChunks( |
169 const std::vector<SBChunkDelete>& chunk_deletes) = 0; | 167 const std::vector<SBChunkDelete>& chunk_deletes) = 0; |
170 virtual void UpdateFinished(bool update_succeeded) = 0; | 168 virtual void UpdateFinished(bool update_succeeded) = 0; |
171 | 169 |
172 // Store the results of a GetHash response. In the case of empty results, we | 170 // Store the results of a GetHash response. In the case of empty results, we |
173 // cache the prefixes until the next update so that we don't have to issue | 171 // cache the prefixes until the next update so that we don't have to issue |
174 // further GetHash requests we know will be empty. | 172 // further GetHash requests we know will be empty. |
175 virtual void CacheHashResults( | 173 virtual void CacheHashResults(const std::vector<SBPrefix>& prefixes, |
176 const std::vector<SBPrefix>& prefixes, | 174 const std::vector<SBFullHashResult>& full_hits, |
177 const std::vector<SBFullHashResult>& full_hits) = 0; | 175 const base::TimeDelta& cache_lifetime) = 0; |
178 | 176 |
179 // Returns true if the malware IP blacklisting killswitch URL is present | 177 // Returns true if the malware IP blacklisting killswitch URL is present |
180 // in the csd whitelist. | 178 // in the csd whitelist. |
181 virtual bool IsMalwareIPMatchKillSwitchOn() = 0; | 179 virtual bool IsMalwareIPMatchKillSwitchOn() = 0; |
182 | 180 |
183 // The name of the bloom-filter file for the given database file. | 181 // The name of the bloom-filter file for the given database file. |
184 // NOTE(shess): OBSOLETE. Present for deleting stale files. | 182 // NOTE(shess): OBSOLETE. Present for deleting stale files. |
185 static base::FilePath BloomFilterForFilename( | 183 static base::FilePath BloomFilterForFilename( |
186 const base::FilePath& db_filename); | 184 const base::FilePath& db_filename); |
187 | 185 |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 // Create a database with a browse store. This is a legacy interface that | 282 // Create a database with a browse store. This is a legacy interface that |
285 // useds Sqlite. | 283 // useds Sqlite. |
286 SafeBrowsingDatabaseNew(); | 284 SafeBrowsingDatabaseNew(); |
287 | 285 |
288 virtual ~SafeBrowsingDatabaseNew(); | 286 virtual ~SafeBrowsingDatabaseNew(); |
289 | 287 |
290 // Implement SafeBrowsingDatabase interface. | 288 // Implement SafeBrowsingDatabase interface. |
291 virtual void Init(const base::FilePath& filename) OVERRIDE; | 289 virtual void Init(const base::FilePath& filename) OVERRIDE; |
292 virtual bool ResetDatabase() OVERRIDE; | 290 virtual bool ResetDatabase() OVERRIDE; |
293 virtual bool ContainsBrowseUrl(const GURL& url, | 291 virtual bool ContainsBrowseUrl(const GURL& url, |
294 std::string* matching_list, | |
295 std::vector<SBPrefix>* prefix_hits, | 292 std::vector<SBPrefix>* prefix_hits, |
296 std::vector<SBFullHashResult>* full_hits, | 293 std::vector<SBFullHashResult>* cache_hits) |
297 base::Time last_update) OVERRIDE; | 294 OVERRIDE; |
298 virtual bool ContainsDownloadUrl(const std::vector<GURL>& urls, | 295 virtual bool ContainsDownloadUrl(const std::vector<GURL>& urls, |
299 std::vector<SBPrefix>* prefix_hits) OVERRIDE; | 296 std::vector<SBPrefix>* prefix_hits) OVERRIDE; |
300 virtual bool ContainsCsdWhitelistedUrl(const GURL& url) OVERRIDE; | 297 virtual bool ContainsCsdWhitelistedUrl(const GURL& url) OVERRIDE; |
301 virtual bool ContainsDownloadWhitelistedUrl(const GURL& url) OVERRIDE; | 298 virtual bool ContainsDownloadWhitelistedUrl(const GURL& url) OVERRIDE; |
302 virtual bool ContainsDownloadWhitelistedString( | 299 virtual bool ContainsDownloadWhitelistedString( |
303 const std::string& str) OVERRIDE; | 300 const std::string& str) OVERRIDE; |
304 virtual bool ContainsExtensionPrefixes( | 301 virtual bool ContainsExtensionPrefixes( |
305 const std::vector<SBPrefix>& prefixes, | 302 const std::vector<SBPrefix>& prefixes, |
306 std::vector<SBPrefix>* prefix_hits) OVERRIDE; | 303 std::vector<SBPrefix>* prefix_hits) OVERRIDE; |
307 virtual bool ContainsSideEffectFreeWhitelistUrl(const GURL& url) OVERRIDE; | 304 virtual bool ContainsSideEffectFreeWhitelistUrl(const GURL& url) OVERRIDE; |
308 virtual bool ContainsMalwareIP(const std::string& ip_address) OVERRIDE; | 305 virtual bool ContainsMalwareIP(const std::string& ip_address) OVERRIDE; |
309 virtual bool UpdateStarted(std::vector<SBListChunkRanges>* lists) OVERRIDE; | 306 virtual bool UpdateStarted(std::vector<SBListChunkRanges>* lists) OVERRIDE; |
310 virtual void InsertChunks(const std::string& list_name, | 307 virtual void InsertChunks(const std::string& list_name, |
311 const SBChunkList& chunks) OVERRIDE; | 308 const SBChunkList& chunks) OVERRIDE; |
312 virtual void DeleteChunks( | 309 virtual void DeleteChunks( |
313 const std::vector<SBChunkDelete>& chunk_deletes) OVERRIDE; | 310 const std::vector<SBChunkDelete>& chunk_deletes) OVERRIDE; |
314 virtual void UpdateFinished(bool update_succeeded) OVERRIDE; | 311 virtual void UpdateFinished(bool update_succeeded) OVERRIDE; |
315 virtual void CacheHashResults( | 312 virtual void CacheHashResults(const std::vector<SBPrefix>& prefixes, |
316 const std::vector<SBPrefix>& prefixes, | 313 const std::vector<SBFullHashResult>& full_hits, |
317 const std::vector<SBFullHashResult>& full_hits) OVERRIDE; | 314 const base::TimeDelta& cache_lifetime) OVERRIDE; |
318 | 315 |
319 // Returns the value of malware_kill_switch_; | 316 // Returns the value of malware_kill_switch_; |
320 virtual bool IsMalwareIPMatchKillSwitchOn() OVERRIDE; | 317 virtual bool IsMalwareIPMatchKillSwitchOn() OVERRIDE; |
321 | 318 |
322 private: | 319 private: |
323 friend class SafeBrowsingDatabaseTest; | 320 friend class SafeBrowsingDatabaseTest; |
324 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingDatabaseTest, HashCaching); | 321 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingDatabaseTest, HashCaching); |
325 | 322 |
326 // A SafeBrowsing whitelist contains a list of whitelisted full-hashes (stored | 323 // A SafeBrowsing whitelist contains a list of whitelisted full-hashes (stored |
327 // in a sorted vector) as well as a boolean flag indicating whether all | 324 // in a sorted vector) as well as a boolean flag indicating whether all |
328 // lookups in the whitelist should be considered matches for safety. | 325 // lookups in the whitelist should be considered matches for safety. |
329 typedef std::pair<std::vector<SBFullHash>, bool> SBWhitelist; | 326 typedef std::pair<std::vector<SBFullHash>, bool> SBWhitelist; |
330 | 327 |
331 // This map holds a csd malware IP blacklist which maps a prefix mask | 328 // This map holds a csd malware IP blacklist which maps a prefix mask |
332 // to a set of hashed blacklisted IP prefixes. Each IP prefix is a hashed | 329 // to a set of hashed blacklisted IP prefixes. Each IP prefix is a hashed |
333 // IPv6 IP prefix using SHA-1. | 330 // IPv6 IP prefix using SHA-1. |
334 typedef std::map<std::string, base::hash_set<std::string> > IPBlacklist; | 331 typedef std::map<std::string, base::hash_set<std::string> > IPBlacklist; |
335 | 332 |
| 333 // Helper for ContainsBrowseUrl, takes a sorted vector of fullhashes instead |
| 334 // of an URL. |
| 335 bool ContainsBrowseUrlHashes(const std::vector<SBFullHash>& full_hashes, |
| 336 std::vector<SBPrefix>* prefix_hits, |
| 337 std::vector<SBFullHashResult>* cache_hits); |
| 338 |
336 // Returns true if the whitelist is disabled or if any of the given hashes | 339 // Returns true if the whitelist is disabled or if any of the given hashes |
337 // matches the whitelist. | 340 // matches the whitelist. |
338 bool ContainsWhitelistedHashes(const SBWhitelist& whitelist, | 341 bool ContainsWhitelistedHashes(const SBWhitelist& whitelist, |
339 const std::vector<SBFullHash>& hashes); | 342 const std::vector<SBFullHash>& hashes); |
340 | 343 |
341 // Return the browse_store_, download_store_, download_whitelist_store or | 344 // Return the browse_store_, download_store_, download_whitelist_store or |
342 // csd_whitelist_store_ based on list_id. | 345 // csd_whitelist_store_ based on list_id. |
343 SafeBrowsingStore* GetStore(int list_id); | 346 SafeBrowsingStore* GetStore(int list_id); |
344 | 347 |
345 // Deletes the files on disk. | 348 // Deletes the files on disk. |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 SafeBrowsingStore* store, | 395 SafeBrowsingStore* store, |
393 SBWhitelist* whitelist); | 396 SBWhitelist* whitelist); |
394 void UpdateIpBlacklistStore(); | 397 void UpdateIpBlacklistStore(); |
395 | 398 |
396 // Used to verify that various calls are made from the thread the | 399 // Used to verify that various calls are made from the thread the |
397 // object was created on. | 400 // object was created on. |
398 base::MessageLoop* creation_loop_; | 401 base::MessageLoop* creation_loop_; |
399 | 402 |
400 // Lock for protecting access to variables that may be used on the | 403 // Lock for protecting access to variables that may be used on the |
401 // IO thread. This includes |prefix_set_|, |full_browse_hashes_|, | 404 // IO thread. This includes |prefix_set_|, |full_browse_hashes_|, |
402 // |pending_browse_hashes_|, |prefix_miss_cache_|, |csd_whitelist_|. | 405 // |browse_gethash_cache_|, |csd_whitelist_|. |
403 base::Lock lookup_lock_; | 406 base::Lock lookup_lock_; |
404 | 407 |
405 // Underlying persistent store for chunk data. | 408 // Underlying persistent store for chunk data. |
406 // For browsing related (phishing and malware URLs) chunks and prefixes. | 409 // For browsing related (phishing and malware URLs) chunks and prefixes. |
407 base::FilePath browse_filename_; | 410 base::FilePath browse_filename_; |
408 scoped_ptr<SafeBrowsingStore> browse_store_; | 411 scoped_ptr<SafeBrowsingStore> browse_store_; |
409 | 412 |
410 // For download related (download URL and binary hash) chunks and prefixes. | 413 // For download related (download URL and binary hash) chunks and prefixes. |
411 base::FilePath download_filename_; | 414 base::FilePath download_filename_; |
412 scoped_ptr<SafeBrowsingStore> download_store_; | 415 scoped_ptr<SafeBrowsingStore> download_store_; |
(...skipping 20 matching lines...) Expand all Loading... |
433 base::FilePath ip_blacklist_filename_; | 436 base::FilePath ip_blacklist_filename_; |
434 scoped_ptr<SafeBrowsingStore> ip_blacklist_store_; | 437 scoped_ptr<SafeBrowsingStore> ip_blacklist_store_; |
435 | 438 |
436 SBWhitelist csd_whitelist_; | 439 SBWhitelist csd_whitelist_; |
437 SBWhitelist download_whitelist_; | 440 SBWhitelist download_whitelist_; |
438 SBWhitelist extension_blacklist_; | 441 SBWhitelist extension_blacklist_; |
439 | 442 |
440 // The IP blacklist should be small. At most a couple hundred IPs. | 443 // The IP blacklist should be small. At most a couple hundred IPs. |
441 IPBlacklist ip_blacklist_; | 444 IPBlacklist ip_blacklist_; |
442 | 445 |
443 // Cached browse store related full-hash items, ordered by prefix for | 446 // Full-hashes from browse store, ordered by prefix for efficient scanning. |
444 // efficient scanning. | |
445 // |full_browse_hashes_| are items from |browse_store_|, | |
446 // |pending_browse_hashes_| are items from |CacheHashResults()|, which | |
447 // will be pushed to the store on the next update. | |
448 std::vector<SBAddFullHash> full_browse_hashes_; | 447 std::vector<SBAddFullHash> full_browse_hashes_; |
449 std::vector<SBAddFullHash> pending_browse_hashes_; | |
450 | 448 |
451 // Cache of prefixes that returned empty results (no full hash | 449 // Cache of gethash results for browse store. Entries should not be used if |
452 // match) to |CacheHashResults()|. Cached to prevent asking for | 450 // they are older than their expire_after field. Cached misses will have |
453 // them every time. Cleared on next update. | 451 // empty full_hashes field. Cleared on each update. |
454 std::set<SBPrefix> prefix_miss_cache_; | 452 std::map<SBPrefix, SBCachedFullHashResult> browse_gethash_cache_; |
455 | 453 |
456 // Used to schedule resetting the database because of corruption. | 454 // Used to schedule resetting the database because of corruption. |
457 base::WeakPtrFactory<SafeBrowsingDatabaseNew> reset_factory_; | 455 base::WeakPtrFactory<SafeBrowsingDatabaseNew> reset_factory_; |
458 | 456 |
459 // Set if corruption is detected during the course of an update. | 457 // Set if corruption is detected during the course of an update. |
460 // Causes the update functions to fail with no side effects, until | 458 // Causes the update functions to fail with no side effects, until |
461 // the next call to |UpdateStarted()|. | 459 // the next call to |UpdateStarted()|. |
462 bool corruption_detected_; | 460 bool corruption_detected_; |
463 | 461 |
464 // Set to true if any chunks are added or deleted during an update. | 462 // Set to true if any chunks are added or deleted during an update. |
465 // Used to optimize away database update. | 463 // Used to optimize away database update. |
466 bool change_detected_; | 464 bool change_detected_; |
467 | 465 |
468 // Used to check if a prefix was in the browse database. | 466 // Used to check if a prefix was in the browse database. |
469 base::FilePath browse_prefix_set_filename_; | 467 base::FilePath browse_prefix_set_filename_; |
470 scoped_ptr<safe_browsing::PrefixSet> browse_prefix_set_; | 468 scoped_ptr<safe_browsing::PrefixSet> browse_prefix_set_; |
471 | 469 |
472 // Used to check if a prefix was in the browse database. | 470 // Used to check if a prefix was in the browse database. |
473 base::FilePath side_effect_free_whitelist_prefix_set_filename_; | 471 base::FilePath side_effect_free_whitelist_prefix_set_filename_; |
474 scoped_ptr<safe_browsing::PrefixSet> side_effect_free_whitelist_prefix_set_; | 472 scoped_ptr<safe_browsing::PrefixSet> side_effect_free_whitelist_prefix_set_; |
475 }; | 473 }; |
476 | 474 |
477 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_DATABASE_H_ | 475 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_DATABASE_H_ |
OLD | NEW |