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

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

Issue 337723004: [Safe browsing] Clean up code to scan hash results for threats. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 6 months 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 | Annotate | Revision Log
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 #include "chrome/browser/safe_browsing/database_manager.h" 5 #include "chrome/browser/safe_browsing/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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 } 61 }
62 62
63 bool IsExpectedThreat( 63 bool IsExpectedThreat(
64 const SBThreatType threat_type, 64 const SBThreatType threat_type,
65 const std::vector<SBThreatType>& expected_threats) { 65 const std::vector<SBThreatType>& expected_threats) {
66 return expected_threats.end() != std::find(expected_threats.begin(), 66 return expected_threats.end() != std::find(expected_threats.begin(),
67 expected_threats.end(), 67 expected_threats.end(),
68 threat_type); 68 threat_type);
69 } 69 }
70 70
71 // |list_id| is from |safe_browsing_util::ListType|. 71 // Return the list id from the first result in |full_hashes| which matches
72 SBThreatType GetThreatTypeFromListId(int list_id) { 72 // |hash|, or INVALID if none match.
73 if (list_id == safe_browsing_util::PHISH) { 73 safe_browsing_util::ListType GetHashThreatListType(
74 return SB_THREAT_TYPE_URL_PHISHING; 74 const SBFullHash& hash,
75 const std::vector<SBFullHashResult>& full_hashes) {
76 for (size_t i = 0; i < full_hashes.size(); ++i) {
77 if (SBFullHashEqual(hash, full_hashes[i].hash))
78 return static_cast<safe_browsing_util::ListType>(full_hashes[i].list_id);
75 } 79 }
80 return safe_browsing_util::INVALID;
81 }
76 82
77 if (list_id == safe_browsing_util::MALWARE) { 83 // Given a URL, compare all the possible host + path full hashes to the set of
78 return SB_THREAT_TYPE_URL_MALWARE; 84 // provided full hashes. Returns the list id of the a matching result from
85 // |full_hashes|, or INVALID if none match.
86 safe_browsing_util::ListType GetUrlThreatListType(
87 const GURL& url,
88 const std::vector<SBFullHashResult>& full_hashes) {
89 if (full_hashes.empty())
90 return safe_browsing_util::INVALID;
91
92 std::vector<std::string> patterns;
93 safe_browsing_util::GeneratePatternsToCheck(url, &patterns);
94
95 for (size_t i = 0; i < patterns.size(); ++i) {
96 safe_browsing_util::ListType threat =
97 GetHashThreatListType(SBFullHashForString(patterns[i]), full_hashes);
98 if (threat != safe_browsing_util::INVALID)
99 return threat;
79 } 100 }
101 return safe_browsing_util::INVALID;
102 }
80 103
81 if (list_id == safe_browsing_util::BINURL) { 104 SBThreatType GetThreatTypeFromListType(safe_browsing_util::ListType list_type) {
82 return SB_THREAT_TYPE_BINARY_MALWARE_URL; 105 switch (list_type) {
106 case safe_browsing_util::PHISH:
107 return SB_THREAT_TYPE_URL_PHISHING;
108 case safe_browsing_util::MALWARE:
109 return SB_THREAT_TYPE_URL_MALWARE;
110 case safe_browsing_util::BINURL:
111 return SB_THREAT_TYPE_BINARY_MALWARE_URL;
112 case safe_browsing_util::EXTENSIONBLACKLIST:
113 return SB_THREAT_TYPE_EXTENSION;
114 default:
115 DVLOG(1) << "Unknown safe browsing list id " << list_type;
116 return SB_THREAT_TYPE_SAFE;
83 } 117 }
84
85 if (list_id == safe_browsing_util::EXTENSIONBLACKLIST) {
86 return SB_THREAT_TYPE_EXTENSION;
87 }
88
89 DVLOG(1) << "Unknown safe browsing list id " << list_id;
90 return SB_THREAT_TYPE_SAFE;
91 } 118 }
92 119
93 } // namespace 120 } // namespace
94 121
122 // static
123 SBThreatType SafeBrowsingDatabaseManager::GetHashThreatType(
124 const SBFullHash& hash,
125 const std::vector<SBFullHashResult>& full_hashes) {
126 return GetThreatTypeFromListType(GetHashThreatListType(hash, full_hashes));
127 }
128
129 // static
130 SBThreatType SafeBrowsingDatabaseManager::GetUrlThreatType(
131 const GURL& url,
132 const std::vector<SBFullHashResult>& full_hashes) {
133 return GetThreatTypeFromListType(GetUrlThreatListType(url, full_hashes));
134 }
135
95 SafeBrowsingDatabaseManager::SafeBrowsingCheck::SafeBrowsingCheck( 136 SafeBrowsingDatabaseManager::SafeBrowsingCheck::SafeBrowsingCheck(
96 const std::vector<GURL>& urls, 137 const std::vector<GURL>& urls,
97 const std::vector<SBFullHash>& full_hashes, 138 const std::vector<SBFullHash>& full_hashes,
98 Client* client, 139 Client* client,
99 safe_browsing_util::ListType check_type, 140 safe_browsing_util::ListType check_type,
100 const std::vector<SBThreatType>& expected_threats) 141 const std::vector<SBThreatType>& expected_threats)
101 : urls(urls), 142 : urls(urls),
102 url_results(urls.size(), SB_THREAT_TYPE_SAFE), 143 url_results(urls.size(), SB_THREAT_TYPE_SAFE),
103 full_hashes(full_hashes), 144 full_hashes(full_hashes),
104 full_hash_results(full_hashes.size(), SB_THREAT_TYPE_SAFE), 145 full_hash_results(full_hashes.size(), SB_THREAT_TYPE_SAFE),
(...skipping 769 matching lines...) Expand 10 before | Expand all | Expand 10 after
874 } 915 }
875 916
876 bool SafeBrowsingDatabaseManager::HandleOneCheck( 917 bool SafeBrowsingDatabaseManager::HandleOneCheck(
877 SafeBrowsingCheck* check, 918 SafeBrowsingCheck* check,
878 const std::vector<SBFullHashResult>& full_hashes) { 919 const std::vector<SBFullHashResult>& full_hashes) {
879 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 920 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
880 DCHECK(check); 921 DCHECK(check);
881 922
882 bool is_threat = false; 923 bool is_threat = false;
883 924
925 // TODO(shess): GetHashThreadListType() contains a loop,
926 // GetUrlThreatListType() a loop around that loop. Having another loop out
927 // here concerns me. It is likely that SAFE is an expected outcome, which
928 // means all of those loops run to completion. Refactoring this to generate a
929 // set of sorted items to compare in sequence would probably improve things.
930 //
931 // Additionally, the set of patterns generated from the urls is very similar
932 // to the patterns generated in ContainsBrowseUrl() and other database checks,
933 // which are called from this code. Refactoring that across the checks could
934 // interact well with batching the checks here.
935
884 for (size_t i = 0; i < check->urls.size(); ++i) { 936 for (size_t i = 0; i < check->urls.size(); ++i) {
885 int index = 937 SBThreatType threat = GetUrlThreatType(check->urls[i], full_hashes);
886 safe_browsing_util::GetUrlHashIndex(check->urls[i], full_hashes);
887 if (index == -1)
888 continue;
889 SBThreatType threat =
890 GetThreatTypeFromListId(full_hashes[index].list_id);
891 if (threat != SB_THREAT_TYPE_SAFE && 938 if (threat != SB_THREAT_TYPE_SAFE &&
892 IsExpectedThreat(threat, check->expected_threats)) { 939 IsExpectedThreat(threat, check->expected_threats)) {
893 check->url_results[i] = threat; 940 check->url_results[i] = threat;
894 is_threat = true; 941 is_threat = true;
895 } 942 }
896 } 943 }
897 944
898 for (size_t i = 0; i < check->full_hashes.size(); ++i) { 945 for (size_t i = 0; i < check->full_hashes.size(); ++i) {
899 int index = 946 SBThreatType threat = GetHashThreatType(check->full_hashes[i], full_hashes);
900 safe_browsing_util::GetHashIndex(check->full_hashes[i], full_hashes);
901 if (index == -1)
902 continue;
903 SBThreatType threat =
904 GetThreatTypeFromListId(full_hashes[index].list_id);
905 if (threat != SB_THREAT_TYPE_SAFE && 947 if (threat != SB_THREAT_TYPE_SAFE &&
906 IsExpectedThreat(threat, check->expected_threats)) { 948 IsExpectedThreat(threat, check->expected_threats)) {
907 check->full_hash_results[i] = threat; 949 check->full_hash_results[i] = threat;
908 is_threat = true; 950 is_threat = true;
909 } 951 }
910 } 952 }
911 953
912 SafeBrowsingCheckDone(check); 954 SafeBrowsingCheckDone(check);
913 return is_threat; 955 return is_threat;
914 } 956 }
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
1011 new base::WeakPtrFactory<SafeBrowsingDatabaseManager>(this)); 1053 new base::WeakPtrFactory<SafeBrowsingDatabaseManager>(this));
1012 checks_.insert(check); 1054 checks_.insert(check);
1013 1055
1014 safe_browsing_thread_->message_loop()->PostTask(FROM_HERE, task); 1056 safe_browsing_thread_->message_loop()->PostTask(FROM_HERE, task);
1015 1057
1016 base::MessageLoop::current()->PostDelayedTask(FROM_HERE, 1058 base::MessageLoop::current()->PostDelayedTask(FROM_HERE,
1017 base::Bind(&SafeBrowsingDatabaseManager::TimeoutCallback, 1059 base::Bind(&SafeBrowsingDatabaseManager::TimeoutCallback,
1018 check->timeout_factory_->GetWeakPtr(), check), 1060 check->timeout_factory_->GetWeakPtr(), check),
1019 check_timeout_); 1061 check_timeout_);
1020 } 1062 }
OLDNEW
« no previous file with comments | « chrome/browser/safe_browsing/database_manager.h ('k') | chrome/browser/safe_browsing/database_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698