OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 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 // Utilities for the SafeBrowsing code. | 5 // Utilities for the SafeBrowsing DB code. |
6 | 6 |
7 #ifndef CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_UTIL_H_ | 7 #ifndef COMPONENTS_SAFE_BROWSING_DB_UTIL_H_ |
8 #define CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_UTIL_H_ | 8 #define COMPONENTS_SAFE_BROWSING_DB_UTIL_H_ |
9 | 9 |
10 #include <cstring> | 10 #include <cstring> |
11 #include <string> | 11 #include <string> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
15 #include "base/memory/scoped_ptr.h" | |
16 #include "base/strings/string_piece.h" | 15 #include "base/strings/string_piece.h" |
17 #include "base/time/time.h" | 16 #include "base/time/time.h" |
18 #include "chrome/browser/safe_browsing/chunk_range.h" | |
19 #include "components/safe_browsing_db/safe_browsing_db_util.h" | |
20 | 17 |
21 namespace safe_browsing { | |
22 class ChunkData; | |
23 }; | |
24 | 18 |
25 class GURL; | 19 class GURL; |
26 | 20 |
27 // Container for holding a chunk URL and the list it belongs to. | 21 // Different types of threats that SafeBrowsing protects against. |
28 struct ChunkUrl { | 22 enum SBThreatType { |
29 std::string url; | 23 // No threat at all. |
30 std::string list_name; | 24 SB_THREAT_TYPE_SAFE, |
| 25 |
| 26 // The URL is being used for phishing. |
| 27 SB_THREAT_TYPE_URL_PHISHING, |
| 28 |
| 29 // The URL hosts malware. |
| 30 SB_THREAT_TYPE_URL_MALWARE, |
| 31 |
| 32 // The URL hosts unwanted programs. |
| 33 SB_THREAT_TYPE_URL_UNWANTED, |
| 34 |
| 35 // The download URL is malware. |
| 36 SB_THREAT_TYPE_BINARY_MALWARE_URL, |
| 37 |
| 38 // Url detected by the client-side phishing model. Note that unlike the |
| 39 // above values, this does not correspond to a downloaded list. |
| 40 SB_THREAT_TYPE_CLIENT_SIDE_PHISHING_URL, |
| 41 |
| 42 // The Chrome extension or app (given by its ID) is malware. |
| 43 SB_THREAT_TYPE_EXTENSION, |
| 44 |
| 45 // Url detected by the client-side malware IP list. This IP list is part |
| 46 // of the client side detection model. |
| 47 SB_THREAT_TYPE_CLIENT_SIDE_MALWARE_URL, |
31 }; | 48 }; |
32 | 49 |
33 // Data for an individual chunk sent from the server. | |
34 class SBChunkData { | |
35 public: | |
36 SBChunkData(); | |
37 ~SBChunkData(); | |
38 | 50 |
39 // Create with manufactured data, for testing only. | 51 // TODO(vakh): Move all these declarations under safe_browsing namespace. |
40 // TODO(shess): Right now the test code calling this is in an anonymous | 52 // A truncated hash's type. |
41 // namespace. Figure out how to shift this into private:. | 53 typedef uint32 SBPrefix; |
42 explicit SBChunkData(safe_browsing::ChunkData* chunk_data); | |
43 | 54 |
44 // Read serialized ChunkData, returning true if the parse suceeded. | 55 // A full hash. |
45 bool ParseFrom(const unsigned char* data, size_t length); | 56 union SBFullHash { |
46 | 57 char full_hash[32]; |
47 // Access the chunk data. |AddChunkNumberAt()| can only be called if | 58 SBPrefix prefix; |
48 // |IsSub()| returns true. |Prefix*()| and |FullHash*()| can only be called | |
49 // if the corrosponding |Is*()| returned true. | |
50 int ChunkNumber() const; | |
51 bool IsAdd() const; | |
52 bool IsSub() const; | |
53 int AddChunkNumberAt(size_t i) const; | |
54 bool IsPrefix() const; | |
55 size_t PrefixCount() const; | |
56 SBPrefix PrefixAt(size_t i) const; | |
57 bool IsFullHash() const; | |
58 size_t FullHashCount() const; | |
59 SBFullHash FullHashAt(size_t i) const; | |
60 | |
61 private: | |
62 // Protocol buffer sent from server. | |
63 scoped_ptr<safe_browsing::ChunkData> chunk_data_; | |
64 | |
65 DISALLOW_COPY_AND_ASSIGN(SBChunkData); | |
66 }; | 59 }; |
67 | 60 |
68 // Used when we get a gethash response. | 61 // Used when we get a gethash response. |
69 struct SBFullHashResult { | 62 struct SBFullHashResult { |
70 SBFullHash hash; | 63 SBFullHash hash; |
71 // TODO(shess): Refactor to allow ListType here. | 64 // TODO(shess): Refactor to allow ListType here. |
72 int list_id; | 65 int list_id; |
73 std::string metadata; | 66 std::string metadata; |
74 }; | 67 }; |
75 | 68 |
76 // Caches individual response from GETHASH request. | 69 // Caches individual response from GETHASH request. |
77 struct SBCachedFullHashResult { | 70 struct SBCachedFullHashResult { |
78 SBCachedFullHashResult(); | 71 SBCachedFullHashResult(); |
79 explicit SBCachedFullHashResult(const base::Time& in_expire_after); | 72 explicit SBCachedFullHashResult(const base::Time& in_expire_after); |
80 ~SBCachedFullHashResult(); | 73 ~SBCachedFullHashResult(); |
81 | 74 |
82 base::Time expire_after; | 75 base::Time expire_after; |
83 std::vector<SBFullHashResult> full_hashes; | 76 std::vector<SBFullHashResult> full_hashes; |
84 }; | 77 }; |
85 | 78 |
86 // Contains information about a list in the database. | |
87 struct SBListChunkRanges { | |
88 explicit SBListChunkRanges(const std::string& n); | |
89 | 79 |
90 std::string name; // The list name. | 80 namespace safe_browsing { |
91 std::string adds; // The ranges for add chunks. | |
92 std::string subs; // The ranges for sub chunks. | |
93 }; | |
94 | |
95 // Container for deleting chunks from the database. | |
96 struct SBChunkDelete { | |
97 SBChunkDelete(); | |
98 ~SBChunkDelete(); | |
99 | |
100 std::string list_name; | |
101 bool is_sub_del; | |
102 std::vector<ChunkRange> chunk_del; | |
103 }; | |
104 | |
105 // Utility functions ----------------------------------------------------------- | |
106 | |
107 namespace safe_browsing_util { | |
108 | 81 |
109 // SafeBrowsing list names. | 82 // SafeBrowsing list names. |
110 extern const char kMalwareList[]; | 83 extern const char kMalwareList[]; |
111 extern const char kPhishingList[]; | 84 extern const char kPhishingList[]; |
112 // Binary Download list name. | 85 // Binary Download list name. |
113 extern const char kBinUrlList[]; | 86 extern const char kBinUrlList[]; |
114 // SafeBrowsing client-side detection whitelist list name. | 87 // SafeBrowsing client-side detection whitelist list name. |
115 extern const char kCsdWhiteList[]; | 88 extern const char kCsdWhiteList[]; |
116 // SafeBrowsing download whitelist list name. | 89 // SafeBrowsing download whitelist list name. |
117 extern const char kDownloadWhiteList[]; | 90 extern const char kDownloadWhiteList[]; |
118 // SafeBrowsing extension list name. | 91 // SafeBrowsing extension list name. |
119 extern const char kExtensionBlacklist[]; | 92 extern const char kExtensionBlacklist[]; |
120 // SafeBrowsing csd malware IP blacklist name. | 93 // SafeBrowsing csd malware IP blacklist name. |
121 extern const char kIPBlacklist[]; | 94 extern const char kIPBlacklist[]; |
122 // SafeBrowsing unwanted URL list. | 95 // SafeBrowsing unwanted URL list. |
123 extern const char kUnwantedUrlList[]; | 96 extern const char kUnwantedUrlList[]; |
124 // SafeBrowsing off-domain inclusion whitelist list name. | 97 // SafeBrowsing off-domain inclusion whitelist list name. |
125 extern const char kInclusionWhitelist[]; | 98 extern const char kInclusionWhitelist[]; |
126 | |
127 // This array must contain all Safe Browsing lists. | 99 // This array must contain all Safe Browsing lists. |
128 extern const char* kAllLists[9]; | 100 extern const char* kAllLists[9]; |
129 | 101 |
| 102 |
130 enum ListType { | 103 enum ListType { |
131 INVALID = -1, | 104 INVALID = -1, |
132 MALWARE = 0, | 105 MALWARE = 0, |
133 PHISH = 1, | 106 PHISH = 1, |
134 BINURL = 2, | 107 BINURL = 2, |
135 // Obsolete BINHASH = 3, | 108 // Obsolete BINHASH = 3, |
136 CSDWHITELIST = 4, | 109 CSDWHITELIST = 4, |
137 // SafeBrowsing lists are stored in pairs. Keep ListType 5 | 110 // SafeBrowsing lists are stored in pairs. Keep ListType 5 |
138 // available for a potential second list that we would store in the | 111 // available for a potential second list that we would store in the |
139 // csd-whitelist store file. | 112 // csd-whitelist store file. |
140 DOWNLOADWHITELIST = 6, | 113 DOWNLOADWHITELIST = 6, |
141 // See above comment. Leave 7 available. | 114 // See above comment. Leave 7 available. |
142 EXTENSIONBLACKLIST = 8, | 115 EXTENSIONBLACKLIST = 8, |
143 // See above comment. Leave 9 available. | 116 // See above comment. Leave 9 available. |
144 // Obsolete SIDEEFFECTFREEWHITELIST = 10, | 117 // Obsolete SIDEEFFECTFREEWHITELIST = 10, |
145 // See above comment. Leave 11 available. | 118 // See above comment. Leave 11 available. |
146 IPBLACKLIST = 12, | 119 IPBLACKLIST = 12, |
147 // See above comment. Leave 13 available. | 120 // See above comment. Leave 13 available. |
148 UNWANTEDURL = 14, | 121 UNWANTEDURL = 14, |
149 // See above comment. Leave 15 available. | 122 // See above comment. Leave 15 available. |
150 INCLUSIONWHITELIST = 16, | 123 INCLUSIONWHITELIST = 16, |
151 // See above comment. Leave 17 available. | 124 // See above comment. Leave 17 available. |
152 }; | 125 }; |
153 | 126 |
| 127 |
| 128 inline bool SBFullHashEqual(const SBFullHash& a, const SBFullHash& b) { |
| 129 return !memcmp(a.full_hash, b.full_hash, sizeof(a.full_hash)); |
| 130 } |
| 131 |
| 132 inline bool SBFullHashLess(const SBFullHash& a, const SBFullHash& b) { |
| 133 return memcmp(a.full_hash, b.full_hash, sizeof(a.full_hash)) < 0; |
| 134 } |
| 135 |
| 136 // Generate full hash for the given string. |
| 137 SBFullHash SBFullHashForString(const base::StringPiece& str); |
| 138 SBFullHash StringToSBFullHash(const std::string& hash_in); |
| 139 std::string SBFullHashToString(const SBFullHash& hash_out); |
| 140 |
| 141 |
154 // Maps a list name to ListType. | 142 // Maps a list name to ListType. |
155 ListType GetListId(const base::StringPiece& name); | 143 ListType GetListId(const base::StringPiece& name); |
156 | 144 |
157 // Maps a ListId to list name. Return false if fails. | 145 // Maps a ListId to list name. Return false if fails. |
158 bool GetListName(ListType list_id, std::string* list); | 146 bool GetListName(ListType list_id, std::string* list); |
159 | 147 |
160 // Canonicalizes url as per Google Safe Browsing Specification. | 148 // Canonicalizes url as per Google Safe Browsing Specification. |
161 // See section 6.1 in | 149 // See section 6.1 in |
162 // http://code.google.com/p/google-safe-browsing/wiki/Protocolv2Spec. | 150 // http://code.google.com/p/google-safe-browsing/wiki/Protocolv2Spec. |
163 void CanonicalizeUrl(const GURL& url, std::string* canonicalized_hostname, | 151 void CanonicalizeUrl(const GURL& url, std::string* canonicalized_hostname, |
164 std::string* canonicalized_path, | 152 std::string* canonicalized_path, |
165 std::string* canonicalized_query); | 153 std::string* canonicalized_query); |
166 | 154 |
167 // Given a URL, returns all the hosts we need to check. They are returned | 155 // Given a URL, returns all the hosts we need to check. They are returned |
168 // in order of size (i.e. b.c is first, then a.b.c). | 156 // in order of size (i.e. b.c is first, then a.b.c). |
169 void GenerateHostsToCheck(const GURL& url, std::vector<std::string>* hosts); | 157 void GenerateHostsToCheck(const GURL& url, std::vector<std::string>* hosts); |
170 | 158 |
171 // Given a URL, returns all the paths we need to check. | 159 // Given a URL, returns all the paths we need to check. |
172 void GeneratePathsToCheck(const GURL& url, std::vector<std::string>* paths); | 160 void GeneratePathsToCheck(const GURL& url, std::vector<std::string>* paths); |
173 | 161 |
174 // Given a URL, returns all the patterns we need to check. | 162 // Given a URL, returns all the patterns we need to check. |
175 void GeneratePatternsToCheck(const GURL& url, std::vector<std::string>* urls); | 163 void GeneratePatternsToCheck(const GURL& url, std::vector<std::string>* urls); |
176 | 164 |
177 SBFullHash StringToSBFullHash(const std::string& hash_in); | 165 } // namespace safe_browsing |
178 std::string SBFullHashToString(const SBFullHash& hash_out); | |
179 | 166 |
180 } // namespace safe_browsing_util | 167 #endif // COMPONENTS_SAFE_BROWSING_DB_UTIL_H_ |
181 | |
182 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_UTIL_H_ | |
OLD | NEW |