NaCl docs: add sanitizers to GSoC ideas
[chromium-blink-merge.git] / chrome / browser / safe_browsing / safe_browsing_util.h
blob13d43dddd4ba1f36c3f7654b07a26dfb828c02f8
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
3 // found in the LICENSE file.
4 //
5 // Utilities for the SafeBrowsing code.
7 #ifndef CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_UTIL_H_
8 #define CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_UTIL_H_
10 #include <cstring>
11 #include <set>
12 #include <string>
13 #include <vector>
15 #include "base/basictypes.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/strings/string_piece.h"
18 #include "base/time/time.h"
19 #include "chrome/browser/safe_browsing/chunk_range.h"
21 namespace safe_browsing {
22 class ChunkData;
25 class GURL;
27 // A truncated hash's type.
28 typedef uint32 SBPrefix;
30 // Container for holding a chunk URL and the list it belongs to.
31 struct ChunkUrl {
32 std::string url;
33 std::string list_name;
36 // A full hash.
37 union SBFullHash {
38 char full_hash[32];
39 SBPrefix prefix;
42 inline bool SBFullHashEqual(const SBFullHash& a, const SBFullHash& b) {
43 return !memcmp(a.full_hash, b.full_hash, sizeof(a.full_hash));
46 inline bool SBFullHashLess(const SBFullHash& a, const SBFullHash& b) {
47 return memcmp(a.full_hash, b.full_hash, sizeof(a.full_hash)) < 0;
50 // Generate full hash for the given string.
51 SBFullHash SBFullHashForString(const base::StringPiece& str);
53 // Data for an individual chunk sent from the server.
54 class SBChunkData {
55 public:
56 SBChunkData();
57 ~SBChunkData();
59 // Create with manufactured data, for testing only.
60 // TODO(shess): Right now the test code calling this is in an anonymous
61 // namespace. Figure out how to shift this into private:.
62 explicit SBChunkData(safe_browsing::ChunkData* chunk_data);
64 // Read serialized ChunkData, returning true if the parse suceeded.
65 bool ParseFrom(const unsigned char* data, size_t length);
67 // Access the chunk data. |AddChunkNumberAt()| can only be called if
68 // |IsSub()| returns true. |Prefix*()| and |FullHash*()| can only be called
69 // if the corrosponding |Is*()| returned true.
70 int ChunkNumber() const;
71 bool IsAdd() const;
72 bool IsSub() const;
73 int AddChunkNumberAt(size_t i) const;
74 bool IsPrefix() const;
75 size_t PrefixCount() const;
76 SBPrefix PrefixAt(size_t i) const;
77 bool IsFullHash() const;
78 size_t FullHashCount() const;
79 SBFullHash FullHashAt(size_t i) const;
81 private:
82 // Protocol buffer sent from server.
83 scoped_ptr<safe_browsing::ChunkData> chunk_data_;
85 DISALLOW_COPY_AND_ASSIGN(SBChunkData);
88 // Used when we get a gethash response.
89 struct SBFullHashResult {
90 SBFullHash hash;
91 // TODO(shess): Refactor to allow ListType here.
92 int list_id;
93 std::string metadata;
96 // Caches individual response from GETHASH request.
97 struct SBCachedFullHashResult {
98 SBCachedFullHashResult();
99 explicit SBCachedFullHashResult(const base::Time& in_expire_after);
100 ~SBCachedFullHashResult();
102 base::Time expire_after;
103 std::vector<SBFullHashResult> full_hashes;
106 // Contains information about a list in the database.
107 struct SBListChunkRanges {
108 explicit SBListChunkRanges(const std::string& n);
110 std::string name; // The list name.
111 std::string adds; // The ranges for add chunks.
112 std::string subs; // The ranges for sub chunks.
115 // Container for deleting chunks from the database.
116 struct SBChunkDelete {
117 SBChunkDelete();
118 ~SBChunkDelete();
120 std::string list_name;
121 bool is_sub_del;
122 std::vector<ChunkRange> chunk_del;
125 // Different types of threats that SafeBrowsing protects against.
126 enum SBThreatType {
127 // No threat at all.
128 SB_THREAT_TYPE_SAFE,
130 // The URL is being used for phishing.
131 SB_THREAT_TYPE_URL_PHISHING,
133 // The URL hosts malware.
134 SB_THREAT_TYPE_URL_MALWARE,
136 // The URL hosts unwanted programs.
137 SB_THREAT_TYPE_URL_UNWANTED,
139 // The download URL is malware.
140 SB_THREAT_TYPE_BINARY_MALWARE_URL,
142 // Url detected by the client-side phishing model. Note that unlike the
143 // above values, this does not correspond to a downloaded list.
144 SB_THREAT_TYPE_CLIENT_SIDE_PHISHING_URL,
146 // The Chrome extension or app (given by its ID) is malware.
147 SB_THREAT_TYPE_EXTENSION,
149 // Url detected by the client-side malware IP list. This IP list is part
150 // of the client side detection model.
151 SB_THREAT_TYPE_CLIENT_SIDE_MALWARE_URL,
154 // Utility functions -----------------------------------------------------------
156 namespace safe_browsing_util {
158 // SafeBrowsing list names.
159 extern const char kMalwareList[];
160 extern const char kPhishingList[];
161 // Binary Download list name.
162 extern const char kBinUrlList[];
163 // SafeBrowsing client-side detection whitelist list name.
164 extern const char kCsdWhiteList[];
165 // SafeBrowsing download whitelist list name.
166 extern const char kDownloadWhiteList[];
167 // SafeBrowsing extension list name.
168 extern const char kExtensionBlacklist[];
169 // SafeBrowsing side-effect free whitelist name.
170 extern const char kSideEffectFreeWhitelist[];
171 // SafeBrowsing csd malware IP blacklist name.
172 extern const char kIPBlacklist[];
173 // SafeBrowsing unwanted URL list.
174 extern const char kUnwantedUrlList[];
175 // SafeBrowsing off-domain inclusion whitelist list name.
176 extern const char kInclusionWhitelist[];
178 // This array must contain all Safe Browsing lists.
179 extern const char* kAllLists[10];
181 enum ListType {
182 INVALID = -1,
183 MALWARE = 0,
184 PHISH = 1,
185 BINURL = 2,
186 // Obsolete BINHASH = 3,
187 CSDWHITELIST = 4,
188 // SafeBrowsing lists are stored in pairs. Keep ListType 5
189 // available for a potential second list that we would store in the
190 // csd-whitelist store file.
191 DOWNLOADWHITELIST = 6,
192 // See above comment. Leave 7 available.
193 EXTENSIONBLACKLIST = 8,
194 // See above comment. Leave 9 available.
195 SIDEEFFECTFREEWHITELIST = 10,
196 // See above comment. Leave 11 available.
197 IPBLACKLIST = 12,
198 // See above comment. Leave 13 available.
199 UNWANTEDURL = 14,
200 // See above comment. Leave 15 available.
201 INCLUSIONWHITELIST = 16,
202 // See above comment. Leave 17 available.
205 // M40 experimental flag controls rollout of the UwS warning.
206 enum UnwantedStatus {
207 UWS_OFF,
208 UWS_ON_INVISIBLE,
209 UWS_ON
212 // Maps a list name to ListType.
213 ListType GetListId(const base::StringPiece& name);
215 // Maps a ListId to list name. Return false if fails.
216 bool GetListName(ListType list_id, std::string* list);
218 // Canonicalizes url as per Google Safe Browsing Specification.
219 // See section 6.1 in
220 // http://code.google.com/p/google-safe-browsing/wiki/Protocolv2Spec.
221 void CanonicalizeUrl(const GURL& url, std::string* canonicalized_hostname,
222 std::string* canonicalized_path,
223 std::string* canonicalized_query);
225 // Given a URL, returns all the hosts we need to check. They are returned
226 // in order of size (i.e. b.c is first, then a.b.c).
227 void GenerateHostsToCheck(const GURL& url, std::vector<std::string>* hosts);
229 // Given a URL, returns all the paths we need to check.
230 void GeneratePathsToCheck(const GURL& url, std::vector<std::string>* paths);
232 // Given a URL, returns all the patterns we need to check.
233 void GeneratePatternsToCheck(const GURL& url, std::vector<std::string>* urls);
235 GURL GeneratePhishingReportUrl(const std::string& report_page,
236 const std::string& url_to_report,
237 bool is_client_side_detection);
239 SBFullHash StringToSBFullHash(const std::string& hash_in);
240 std::string SBFullHashToString(const SBFullHash& hash_out);
242 // Look up the status of the UwS warning. The default is off.
243 UnwantedStatus GetUnwantedTrialGroup();
245 } // namespace safe_browsing_util
247 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_UTIL_H_