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 24 matching lines...) Expand all Loading... | |
35 public: | 35 public: |
36 SafeBrowsingDatabaseFactory() { } | 36 SafeBrowsingDatabaseFactory() { } |
37 virtual ~SafeBrowsingDatabaseFactory() { } | 37 virtual ~SafeBrowsingDatabaseFactory() { } |
38 virtual SafeBrowsingDatabase* CreateSafeBrowsingDatabase( | 38 virtual SafeBrowsingDatabase* CreateSafeBrowsingDatabase( |
39 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 39 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
40 bool enable_download_protection, | 40 bool enable_download_protection, |
41 bool enable_client_side_whitelist, | 41 bool enable_client_side_whitelist, |
42 bool enable_download_whitelist, | 42 bool enable_download_whitelist, |
43 bool enable_extension_blacklist, | 43 bool enable_extension_blacklist, |
44 bool enable_ip_blacklist, | 44 bool enable_ip_blacklist, |
45 bool enable_unwanted_software_list) = 0; | 45 bool enable_unwanted_software_list, |
46 bool enable_resource_blacklists_list) = 0; | |
46 | 47 |
47 private: | 48 private: |
48 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingDatabaseFactory); | 49 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingDatabaseFactory); |
49 }; | 50 }; |
50 | 51 |
51 // Encapsulates on-disk databases that for safebrowsing. There are | 52 // Encapsulates on-disk databases that for safebrowsing. There are |
52 // four databases: browse, download, download whitelist and | 53 // four databases: browse, download, download whitelist and |
53 // client-side detection (csd) whitelist databases. The browse database contains | 54 // client-side detection (csd) whitelist databases. The browse database contains |
54 // information about phishing and malware urls. The download database contains | 55 // information about phishing and malware urls. The download database contains |
55 // URLs for bad binaries (e.g: those containing virus) and hash of | 56 // URLs for bad binaries (e.g: those containing virus) and hash of |
(...skipping 10 matching lines...) Expand all Loading... | |
66 // It is not thread safe. | 67 // It is not thread safe. |
67 // The browse list and off-domain inclusion whitelist are always on; | 68 // The browse list and off-domain inclusion whitelist are always on; |
68 // availability of other lists is controlled by the flags on this method. | 69 // availability of other lists is controlled by the flags on this method. |
69 static SafeBrowsingDatabase* Create( | 70 static SafeBrowsingDatabase* Create( |
70 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 71 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
71 bool enable_download_protection, | 72 bool enable_download_protection, |
72 bool enable_client_side_whitelist, | 73 bool enable_client_side_whitelist, |
73 bool enable_download_whitelist, | 74 bool enable_download_whitelist, |
74 bool enable_extension_blacklist, | 75 bool enable_extension_blacklist, |
75 bool enable_ip_blacklist, | 76 bool enable_ip_blacklist, |
76 bool enable_unwanted_software_list); | 77 bool enable_unwanted_software_list, |
78 bool enable_resource_blacklist); | |
Nathan Parker
2016/02/16 21:42:00
Do you need this?
veranika
2016/02/17 15:37:54
Probably I don't. I added it here mostly to be uni
| |
77 | 79 |
78 // Makes the passed |factory| the factory used to instantiate | 80 // Makes the passed |factory| the factory used to instantiate |
79 // a SafeBrowsingDatabase. This is used for tests. | 81 // a SafeBrowsingDatabase. This is used for tests. |
80 static void RegisterFactory(SafeBrowsingDatabaseFactory* factory) { | 82 static void RegisterFactory(SafeBrowsingDatabaseFactory* factory) { |
81 factory_ = factory; | 83 factory_ = factory; |
82 } | 84 } |
83 | 85 |
84 virtual ~SafeBrowsingDatabase(); | 86 virtual ~SafeBrowsingDatabase(); |
85 | 87 |
86 // Initializes the database with the given filename. | 88 // Initializes the database with the given filename. |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
163 // | 165 // |
164 // This function can ONLY be accessed from the creation thread. | 166 // This function can ONLY be accessed from the creation thread. |
165 virtual bool ContainsExtensionPrefixes( | 167 virtual bool ContainsExtensionPrefixes( |
166 const std::vector<SBPrefix>& prefixes, | 168 const std::vector<SBPrefix>& prefixes, |
167 std::vector<SBPrefix>* prefix_hits) = 0; | 169 std::vector<SBPrefix>* prefix_hits) = 0; |
168 | 170 |
169 // Returns true iff the given IP is currently on the csd malware IP blacklist. | 171 // Returns true iff the given IP is currently on the csd malware IP blacklist. |
170 // This function is safe to call from any thread. | 172 // This function is safe to call from any thread. |
171 virtual bool ContainsMalwareIP(const std::string& ip_address) = 0; | 173 virtual bool ContainsMalwareIP(const std::string& ip_address) = 0; |
172 | 174 |
175 // Populates |prefix_hits| with any prefixes in |prefixes| that have matches | |
176 // in the database. Returns true iff there were any matches. | |
177 // | |
178 // This function can ONLY by accessed from the creation thread. | |
179 virtual bool ContainsResourceUrlPrefixes( | |
180 const std::vector<SBPrefix>& prefixes, | |
181 std::vector<SBPrefix>* prefix_hits) = 0; | |
182 | |
173 // A database transaction should look like: | 183 // A database transaction should look like: |
174 // | 184 // |
175 // std::vector<SBListChunkRanges> lists; | 185 // std::vector<SBListChunkRanges> lists; |
176 // if (db.UpdateStarted(&lists)) { | 186 // if (db.UpdateStarted(&lists)) { |
177 // // Do something with |lists|. | 187 // // Do something with |lists|. |
178 // | 188 // |
179 // // Process add/sub commands. | 189 // // Process add/sub commands. |
180 // db.InsertChunks(list_name, chunks); | 190 // db.InsertChunks(list_name, chunks); |
181 // | 191 // |
182 // // Process adddel/subdel commands. | 192 // // Process adddel/subdel commands. |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
254 const base::FilePath& side_effect_free_whitelist_base_filename); | 264 const base::FilePath& side_effect_free_whitelist_base_filename); |
255 | 265 |
256 // Filename for the csd malware IP blacklist database. | 266 // Filename for the csd malware IP blacklist database. |
257 static base::FilePath IpBlacklistDBFilename( | 267 static base::FilePath IpBlacklistDBFilename( |
258 const base::FilePath& ip_blacklist_base_filename); | 268 const base::FilePath& ip_blacklist_base_filename); |
259 | 269 |
260 // Filename for the unwanted software blacklist database. | 270 // Filename for the unwanted software blacklist database. |
261 static base::FilePath UnwantedSoftwareDBFilename( | 271 static base::FilePath UnwantedSoftwareDBFilename( |
262 const base::FilePath& db_filename); | 272 const base::FilePath& db_filename); |
263 | 273 |
274 static base::FilePath ResourceBlacklistDBFilename( | |
275 const base::FilePath& db_filename); | |
276 | |
264 // Get the prefixes matching the download |urls|. | 277 // Get the prefixes matching the download |urls|. |
265 static void GetDownloadUrlPrefixes(const std::vector<GURL>& urls, | 278 static void GetDownloadUrlPrefixes(const std::vector<GURL>& urls, |
266 std::vector<SBPrefix>* prefixes); | 279 std::vector<SBPrefix>* prefixes); |
267 | 280 |
268 // SafeBrowsing Database failure types for histogramming purposes. Explicitly | 281 // SafeBrowsing Database failure types for histogramming purposes. Explicitly |
269 // label new values and do not re-use old values. Also make sure to reflect | 282 // label new values and do not re-use old values. Also make sure to reflect |
270 // modifications made below in the SB2DatabaseFailure histogram enum. | 283 // modifications made below in the SB2DatabaseFailure histogram enum. |
271 enum FailureType { | 284 enum FailureType { |
272 FAILURE_DATABASE_CORRUPT = 0, | 285 FAILURE_DATABASE_CORRUPT = 0, |
273 FAILURE_DATABASE_CORRUPT_HANDLER = 1, | 286 FAILURE_DATABASE_CORRUPT_HANDLER = 1, |
(...skipping 23 matching lines...) Expand all Loading... | |
297 // Obsolete: FAILURE_SIDE_EFFECT_FREE_WHITELIST_PREFIX_SET_DELETE = 25, | 310 // Obsolete: FAILURE_SIDE_EFFECT_FREE_WHITELIST_PREFIX_SET_DELETE = 25, |
298 FAILURE_IP_BLACKLIST_UPDATE_BEGIN = 26, | 311 FAILURE_IP_BLACKLIST_UPDATE_BEGIN = 26, |
299 FAILURE_IP_BLACKLIST_UPDATE_FINISH = 27, | 312 FAILURE_IP_BLACKLIST_UPDATE_FINISH = 27, |
300 FAILURE_IP_BLACKLIST_UPDATE_INVALID = 28, | 313 FAILURE_IP_BLACKLIST_UPDATE_INVALID = 28, |
301 FAILURE_IP_BLACKLIST_DELETE = 29, | 314 FAILURE_IP_BLACKLIST_DELETE = 29, |
302 FAILURE_UNWANTED_SOFTWARE_DATABASE_UPDATE_BEGIN = 30, | 315 FAILURE_UNWANTED_SOFTWARE_DATABASE_UPDATE_BEGIN = 30, |
303 FAILURE_UNWANTED_SOFTWARE_DATABASE_UPDATE_FINISH = 31, | 316 FAILURE_UNWANTED_SOFTWARE_DATABASE_UPDATE_FINISH = 31, |
304 FAILURE_UNWANTED_SOFTWARE_PREFIX_SET_READ = 32, | 317 FAILURE_UNWANTED_SOFTWARE_PREFIX_SET_READ = 32, |
305 FAILURE_UNWANTED_SOFTWARE_PREFIX_SET_WRITE = 33, | 318 FAILURE_UNWANTED_SOFTWARE_PREFIX_SET_WRITE = 33, |
306 FAILURE_UNWANTED_SOFTWARE_PREFIX_SET_DELETE = 34, | 319 FAILURE_UNWANTED_SOFTWARE_PREFIX_SET_DELETE = 34, |
320 FAILURE_RESOURCE_BLACKLIST_UPDATE_BEGIN = 35, | |
321 FAILURE_RESOURCE_BLACKLIST_UPDATE_FINISH = 36, | |
322 FAILURE_RESOURCE_BLACKLIST_DELETE = 37, | |
307 | 323 |
308 // Memory space for histograms is determined by the max. ALWAYS | 324 // Memory space for histograms is determined by the max. ALWAYS |
309 // ADD NEW VALUES BEFORE THIS ONE. | 325 // ADD NEW VALUES BEFORE THIS ONE. |
310 FAILURE_DATABASE_MAX | 326 FAILURE_DATABASE_MAX |
311 }; | 327 }; |
312 | 328 |
313 static void RecordFailure(FailureType failure_type); | 329 static void RecordFailure(FailureType failure_type); |
314 | 330 |
315 private: | 331 private: |
316 // The factory used to instantiate a SafeBrowsingDatabase object. | 332 // The factory used to instantiate a SafeBrowsingDatabase object. |
317 // Useful for tests, so they can provide their own implementation of | 333 // Useful for tests, so they can provide their own implementation of |
318 // SafeBrowsingDatabase. | 334 // SafeBrowsingDatabase. |
319 static SafeBrowsingDatabaseFactory* factory_; | 335 static SafeBrowsingDatabaseFactory* factory_; |
320 }; | 336 }; |
321 | 337 |
322 class SafeBrowsingDatabaseNew : public SafeBrowsingDatabase { | 338 class SafeBrowsingDatabaseNew : public SafeBrowsingDatabase { |
323 public: | 339 public: |
324 // Create a database with the stores below. Takes ownership of all store | 340 // Create a database with the stores below. Takes ownership of all store |
325 // objects handed to this constructor. Ignores all future operations on lists | 341 // objects handed to this constructor. Ignores all future operations on lists |
326 // for which the store is initialized to NULL. | 342 // for which the store is initialized to NULL. |
327 SafeBrowsingDatabaseNew( | 343 SafeBrowsingDatabaseNew( |
328 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 344 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
329 SafeBrowsingStore* browse_store, | 345 SafeBrowsingStore* browse_store, |
330 SafeBrowsingStore* download_store, | 346 SafeBrowsingStore* download_store, |
331 SafeBrowsingStore* csd_whitelist_store, | 347 SafeBrowsingStore* csd_whitelist_store, |
332 SafeBrowsingStore* download_whitelist_store, | 348 SafeBrowsingStore* download_whitelist_store, |
333 SafeBrowsingStore* inclusion_whitelist_store, | 349 SafeBrowsingStore* inclusion_whitelist_store, |
334 SafeBrowsingStore* extension_blacklist_store, | 350 SafeBrowsingStore* extension_blacklist_store, |
335 SafeBrowsingStore* ip_blacklist_store, | 351 SafeBrowsingStore* ip_blacklist_store, |
336 SafeBrowsingStore* unwanted_software_store); | 352 SafeBrowsingStore* unwanted_software_store, |
353 SafeBrowsingStore* resource_blacklist_store); | |
337 | 354 |
338 ~SafeBrowsingDatabaseNew() override; | 355 ~SafeBrowsingDatabaseNew() override; |
339 | 356 |
340 // Implement SafeBrowsingDatabase interface. | 357 // Implement SafeBrowsingDatabase interface. |
341 void Init(const base::FilePath& filename) override; | 358 void Init(const base::FilePath& filename) override; |
342 bool ResetDatabase() override; | 359 bool ResetDatabase() override; |
343 bool ContainsBrowseUrl(const GURL& url, | 360 bool ContainsBrowseUrl(const GURL& url, |
344 std::vector<SBPrefix>* prefix_hits, | 361 std::vector<SBPrefix>* prefix_hits, |
345 std::vector<SBFullHashResult>* cache_hits) override; | 362 std::vector<SBFullHashResult>* cache_hits) override; |
346 bool ContainsBrowseHashes(const std::vector<SBFullHash>& full_hashes, | 363 bool ContainsBrowseHashes(const std::vector<SBFullHash>& full_hashes, |
347 std::vector<SBPrefix>* prefix_hits, | 364 std::vector<SBPrefix>* prefix_hits, |
348 std::vector<SBFullHashResult>* cache_hits) override; | 365 std::vector<SBFullHashResult>* cache_hits) override; |
349 bool ContainsUnwantedSoftwareUrl( | 366 bool ContainsUnwantedSoftwareUrl( |
350 const GURL& url, | 367 const GURL& url, |
351 std::vector<SBPrefix>* prefix_hits, | 368 std::vector<SBPrefix>* prefix_hits, |
352 std::vector<SBFullHashResult>* cache_hits) override; | 369 std::vector<SBFullHashResult>* cache_hits) override; |
353 bool ContainsUnwantedSoftwareHashes( | 370 bool ContainsUnwantedSoftwareHashes( |
354 const std::vector<SBFullHash>& full_hashes, | 371 const std::vector<SBFullHash>& full_hashes, |
355 std::vector<SBPrefix>* prefix_hits, | 372 std::vector<SBPrefix>* prefix_hits, |
356 std::vector<SBFullHashResult>* cache_hits) override; | 373 std::vector<SBFullHashResult>* cache_hits) override; |
357 bool ContainsDownloadUrlPrefixes(const std::vector<SBPrefix>& prefixes, | 374 bool ContainsDownloadUrlPrefixes(const std::vector<SBPrefix>& prefixes, |
358 std::vector<SBPrefix>* prefix_hits) override; | 375 std::vector<SBPrefix>* prefix_hits) override; |
359 bool ContainsCsdWhitelistedUrl(const GURL& url) override; | 376 bool ContainsCsdWhitelistedUrl(const GURL& url) override; |
360 bool ContainsDownloadWhitelistedUrl(const GURL& url) override; | 377 bool ContainsDownloadWhitelistedUrl(const GURL& url) override; |
361 bool ContainsDownloadWhitelistedString(const std::string& str) override; | 378 bool ContainsDownloadWhitelistedString(const std::string& str) override; |
362 bool ContainsInclusionWhitelistedUrl(const GURL& url) override; | 379 bool ContainsInclusionWhitelistedUrl(const GURL& url) override; |
363 bool ContainsExtensionPrefixes(const std::vector<SBPrefix>& prefixes, | 380 bool ContainsExtensionPrefixes(const std::vector<SBPrefix>& prefixes, |
364 std::vector<SBPrefix>* prefix_hits) override; | 381 std::vector<SBPrefix>* prefix_hits) override; |
365 bool ContainsMalwareIP(const std::string& ip_address) override; | 382 bool ContainsMalwareIP(const std::string& ip_address) override; |
383 bool ContainsResourceUrlPrefixes(const std::vector<SBPrefix>& prefixes, | |
384 std::vector<SBPrefix>* prefix_hits) override; | |
385 | |
366 bool UpdateStarted(std::vector<SBListChunkRanges>* lists) override; | 386 bool UpdateStarted(std::vector<SBListChunkRanges>* lists) override; |
367 void InsertChunks( | 387 void InsertChunks( |
368 const std::string& list_name, | 388 const std::string& list_name, |
369 const std::vector<scoped_ptr<SBChunkData>>& chunks) override; | 389 const std::vector<scoped_ptr<SBChunkData>>& chunks) override; |
370 void DeleteChunks(const std::vector<SBChunkDelete>& chunk_deletes) override; | 390 void DeleteChunks(const std::vector<SBChunkDelete>& chunk_deletes) override; |
371 void UpdateFinished(bool update_succeeded) override; | 391 void UpdateFinished(bool update_succeeded) override; |
372 void CacheHashResults(const std::vector<SBPrefix>& prefixes, | 392 void CacheHashResults(const std::vector<SBPrefix>& prefixes, |
373 const std::vector<SBFullHashResult>& full_hits, | 393 const std::vector<SBFullHashResult>& full_hits, |
374 const base::TimeDelta& cache_lifetime) override; | 394 const base::TimeDelta& cache_lifetime) override; |
375 | 395 |
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
674 // whitelist chunks and full-length hashes. This list only contains 256 | 694 // whitelist chunks and full-length hashes. This list only contains 256 |
675 // bit hashes. | 695 // bit hashes. |
676 // - |download_whitelist_store_|: For the download whitelist chunks and | 696 // - |download_whitelist_store_|: For the download whitelist chunks and |
677 // full-length hashes. This list only contains 256 bit hashes. | 697 // full-length hashes. This list only contains 256 bit hashes. |
678 // - |inclusion_whitelist_store_|: For the inclusion whitelist. Same format | 698 // - |inclusion_whitelist_store_|: For the inclusion whitelist. Same format |
679 // as |download_whitelist_store_|. | 699 // as |download_whitelist_store_|. |
680 // - |extension_blacklist_store_|: For extension IDs. | 700 // - |extension_blacklist_store_|: For extension IDs. |
681 // - |ip_blacklist_store_|: For IP blacklist. | 701 // - |ip_blacklist_store_|: For IP blacklist. |
682 // - |unwanted_software_store_|: For unwanted software list (format | 702 // - |unwanted_software_store_|: For unwanted software list (format |
683 // identical to browsing lists). | 703 // identical to browsing lists). |
704 // - |resource_blacklist_store_|: For script resource list (format identical | |
705 // to browsing lists). | |
684 // | 706 // |
685 // The stores themselves will be modified throughout the existence of this | 707 // The stores themselves will be modified throughout the existence of this |
686 // database, but shouldn't ever be swapped out (hence the const scoped_ptr -- | 708 // database, but shouldn't ever be swapped out (hence the const scoped_ptr -- |
687 // which could be swapped for C++11's std::optional when that's available). | 709 // which could be swapped for C++11's std::optional when that's available). |
688 // They are NonThreadSafe and should thus only be accessed on the database's | 710 // They are NonThreadSafe and should thus only be accessed on the database's |
689 // main thread as enforced by SafeBrowsingStoreFile's implementation. | 711 // main thread as enforced by SafeBrowsingStoreFile's implementation. |
690 const scoped_ptr<SafeBrowsingStore> browse_store_; | 712 const scoped_ptr<SafeBrowsingStore> browse_store_; |
691 const scoped_ptr<SafeBrowsingStore> download_store_; | 713 const scoped_ptr<SafeBrowsingStore> download_store_; |
692 const scoped_ptr<SafeBrowsingStore> csd_whitelist_store_; | 714 const scoped_ptr<SafeBrowsingStore> csd_whitelist_store_; |
693 const scoped_ptr<SafeBrowsingStore> download_whitelist_store_; | 715 const scoped_ptr<SafeBrowsingStore> download_whitelist_store_; |
694 const scoped_ptr<SafeBrowsingStore> inclusion_whitelist_store_; | 716 const scoped_ptr<SafeBrowsingStore> inclusion_whitelist_store_; |
695 const scoped_ptr<SafeBrowsingStore> extension_blacklist_store_; | 717 const scoped_ptr<SafeBrowsingStore> extension_blacklist_store_; |
696 const scoped_ptr<SafeBrowsingStore> ip_blacklist_store_; | 718 const scoped_ptr<SafeBrowsingStore> ip_blacklist_store_; |
697 const scoped_ptr<SafeBrowsingStore> unwanted_software_store_; | 719 const scoped_ptr<SafeBrowsingStore> unwanted_software_store_; |
720 const scoped_ptr<SafeBrowsingStore> resource_blacklist_store_; | |
698 | 721 |
699 // Used to schedule resetting the database because of corruption. This factory | 722 // Used to schedule resetting the database because of corruption. This factory |
700 // and the WeakPtrs it issues should only be used on the database's main | 723 // and the WeakPtrs it issues should only be used on the database's main |
701 // thread. | 724 // thread. |
702 base::WeakPtrFactory<SafeBrowsingDatabaseNew> reset_factory_; | 725 base::WeakPtrFactory<SafeBrowsingDatabaseNew> reset_factory_; |
703 }; | 726 }; |
704 | 727 |
705 } // namespace safe_browsing | 728 } // namespace safe_browsing |
706 | 729 |
707 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_DATABASE_H_ | 730 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_DATABASE_H_ |
OLD | NEW |