OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 // | |
5 // Utilities for the SafeBrowsing DB code. | |
6 | |
7 #ifndef COMPONENTS_SAFE_BROWSING_DB_UTIL_H_ | |
8 #define COMPONENTS_SAFE_BROWSING_DB_UTIL_H_ | |
9 | |
10 #include <cstring> | |
11 #include <string> | |
12 #include <vector> | |
13 | |
14 #include "base/basictypes.h" | |
15 #include "base/strings/string_piece.h" | |
16 #include "base/time/time.h" | |
17 | |
18 | |
19 class GURL; | |
20 | |
21 // Different types of threats that SafeBrowsing protects against. | |
22 enum SBThreatType { | |
23 // No threat at all. | |
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, | |
48 }; | |
49 | |
50 | |
51 // TODO(vakh): Move all these declarations under safe_browsing namespace. | |
52 // A truncated hash's type. | |
53 typedef uint32 SBPrefix; | |
54 | |
55 // A full hash. | |
56 union SBFullHash { | |
57 char full_hash[32]; | |
58 SBPrefix prefix; | |
59 }; | |
60 | |
61 // Used when we get a gethash response. | |
62 struct SBFullHashResult { | |
63 SBFullHash hash; | |
64 // TODO(shess): Refactor to allow ListType here. | |
65 int list_id; | |
66 std::string metadata; | |
67 }; | |
68 | |
69 // Caches individual response from GETHASH request. | |
70 struct SBCachedFullHashResult { | |
71 SBCachedFullHashResult(); | |
72 explicit SBCachedFullHashResult(const base::Time& in_expire_after); | |
73 ~SBCachedFullHashResult(); | |
74 | |
75 base::Time expire_after; | |
76 std::vector<SBFullHashResult> full_hashes; | |
77 }; | |
78 | |
79 | |
80 namespace safe_browsing { | |
81 | |
82 // SafeBrowsing list names. | |
83 extern const char kMalwareList[]; | |
84 extern const char kPhishingList[]; | |
85 // Binary Download list name. | |
86 extern const char kBinUrlList[]; | |
87 // SafeBrowsing client-side detection whitelist list name. | |
88 extern const char kCsdWhiteList[]; | |
89 // SafeBrowsing download whitelist list name. | |
90 extern const char kDownloadWhiteList[]; | |
91 // SafeBrowsing extension list name. | |
92 extern const char kExtensionBlacklist[]; | |
93 // SafeBrowsing csd malware IP blacklist name. | |
94 extern const char kIPBlacklist[]; | |
95 // SafeBrowsing unwanted URL list. | |
96 extern const char kUnwantedUrlList[]; | |
97 // SafeBrowsing off-domain inclusion whitelist list name. | |
98 extern const char kInclusionWhitelist[]; | |
99 // This array must contain all Safe Browsing lists. | |
100 extern const char* kAllLists[9]; | |
101 | |
102 | |
103 enum ListType { | |
104 INVALID = -1, | |
105 MALWARE = 0, | |
106 PHISH = 1, | |
107 BINURL = 2, | |
108 // Obsolete BINHASH = 3, | |
109 CSDWHITELIST = 4, | |
110 // SafeBrowsing lists are stored in pairs. Keep ListType 5 | |
111 // available for a potential second list that we would store in the | |
112 // csd-whitelist store file. | |
113 DOWNLOADWHITELIST = 6, | |
114 // See above comment. Leave 7 available. | |
115 EXTENSIONBLACKLIST = 8, | |
116 // See above comment. Leave 9 available. | |
117 // Obsolete SIDEEFFECTFREEWHITELIST = 10, | |
118 // See above comment. Leave 11 available. | |
119 IPBLACKLIST = 12, | |
120 // See above comment. Leave 13 available. | |
121 UNWANTEDURL = 14, | |
122 // See above comment. Leave 15 available. | |
123 INCLUSIONWHITELIST = 16, | |
124 // See above comment. Leave 17 available. | |
125 }; | |
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 | |
142 // Maps a list name to ListType. | |
143 ListType GetListId(const base::StringPiece& name); | |
144 | |
145 // Maps a ListId to list name. Return false if fails. | |
146 bool GetListName(ListType list_id, std::string* list); | |
147 | |
148 // Canonicalizes url as per Google Safe Browsing Specification. | |
149 // See section 6.1 in | |
150 // http://code.google.com/p/google-safe-browsing/wiki/Protocolv2Spec. | |
151 void CanonicalizeUrl(const GURL& url, std::string* canonicalized_hostname, | |
152 std::string* canonicalized_path, | |
153 std::string* canonicalized_query); | |
154 | |
155 // Given a URL, returns all the hosts we need to check. They are returned | |
156 // in order of size (i.e. b.c is first, then a.b.c). | |
157 void GenerateHostsToCheck(const GURL& url, std::vector<std::string>* hosts); | |
158 | |
159 // Given a URL, returns all the paths we need to check. | |
160 void GeneratePathsToCheck(const GURL& url, std::vector<std::string>* paths); | |
161 | |
162 // Given a URL, returns all the patterns we need to check. | |
163 void GeneratePatternsToCheck(const GURL& url, std::vector<std::string>* urls); | |
164 | |
165 } // namespace safe_browsing | |
166 | |
167 #endif // COMPONENTS_SAFE_BROWSING_DB_UTIL_H_ | |
OLD | NEW |