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.
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_
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
{
27 // A truncated hash's type.
28 typedef uint32 SBPrefix
;
30 // Container for holding a chunk URL and the list it belongs to.
33 std::string list_name
;
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.
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;
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;
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
{
91 // TODO(shess): Refactor to allow ListType here.
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
{
120 std::string list_name
;
122 std::vector
<ChunkRange
> chunk_del
;
125 // Different types of threats that SafeBrowsing protects against.
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];
186 // Obsolete BINHASH = 3,
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.
198 // See above comment. Leave 13 available.
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
{
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_