1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
//* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef LookupCache_h__
#define LookupCache_h__
#include "Entries.h"
#include "nsString.h"
#include "nsTArray.h"
#include "nsCOMPtr.h"
#include "nsIFile.h"
#include "mozilla/RefPtr.h"
#include "nsUrlClassifierPrefixSet.h"
#include "VariableLengthPrefixSet.h"
#include "mozilla/Logging.h"
#include "mozilla/TypedEnumBits.h"
#include "nsIUrlClassifierInfo.h"
namespace mozilla {
namespace safebrowsing {
#define MAX_HOST_COMPONENTS 5
#define MAX_PATH_COMPONENTS 4
class LookupResult {
public:
LookupResult()
: mNoise(false),
mProtocolConfirmed(false),
mPartialHashLength(0),
mConfirmed(false),
mProtocolV2(true) {}
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(LookupResult);
// The fragment that matched in the LookupCache
union {
Prefix fixedLengthPrefix;
Completion complete;
} hash;
const Completion& CompleteHash() const {
MOZ_ASSERT(!mNoise);
return hash.complete;
}
nsCString PartialHash() const {
MOZ_ASSERT(mPartialHashLength <= COMPLETE_SIZE);
if (mNoise) {
return nsCString(
reinterpret_cast<const char*>(hash.fixedLengthPrefix.buf),
PREFIX_SIZE);
} else {
return nsCString(reinterpret_cast<const char*>(hash.complete.buf),
mPartialHashLength);
}
}
nsAutoCString PartialHashHex() const {
nsAutoCString hex;
for (size_t i = 0; i < mPartialHashLength; i++) {
hex.AppendPrintf("%.2X", hash.complete.buf[i]);
}
return hex;
}
bool Confirmed() const { return mConfirmed || mProtocolConfirmed; }
// True if we have a complete match for this hash in the table.
bool Complete() const { return mPartialHashLength == COMPLETE_SIZE; }
// True if this is a noise entry, i.e. an extra entry
// that is inserted to mask the true URL we are requesting.
// Noise entries will not have a complete 256-bit hash as
// they are fetched from the local 32-bit database and we
// don't know the corresponding full URL.
bool mNoise;
bool mProtocolConfirmed;
nsCString mTableName;
uint32_t mPartialHashLength;
// True as long as this lookup is complete and hasn't expired.
bool mConfirmed;
bool mProtocolV2;
private:
~LookupResult() = default;
};
typedef nsTArray<RefPtr<LookupResult>> LookupResultArray;
class CacheResult {
public:
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CacheResult);
enum { V2, V4 };
virtual int Ver() const = 0;
virtual bool findCompletion(const Completion& aCompletion) const = 0;
template <typename T>
static const T* Cast(const CacheResult* aThat) {
return ((aThat && T::VER == aThat->Ver())
? reinterpret_cast<const T*>(aThat)
: nullptr);
}
nsCString table;
Prefix prefix;
protected:
virtual ~CacheResult() = default;
};
class CacheResultV2 final : public CacheResult {
public:
static const int VER;
// True when 'prefix' in CacheResult indicates a prefix that
// cannot be completed.
bool miss = false;
// 'completion' and 'addChunk' are used when 'miss' field is false.
Completion completion;
uint32_t addChunk;
bool operator==(const CacheResultV2& aOther) const {
if (table != aOther.table || prefix != aOther.prefix ||
miss != aOther.miss) {
return false;
}
if (miss) {
return true;
}
return completion == aOther.completion && addChunk == aOther.addChunk;
}
bool findCompletion(const Completion& aCompletion) const override {
return completion == aCompletion;
}
virtual int Ver() const override { return VER; }
};
class CacheResultV4 final : public CacheResult {
public:
static const int VER;
CachedFullHashResponse response;
bool operator==(const CacheResultV4& aOther) const {
return table == aOther.table && prefix == aOther.prefix &&
response == aOther.response;
}
bool findCompletion(const Completion& aCompletion) const override {
nsDependentCSubstring completion(
reinterpret_cast<const char*>(aCompletion.buf), COMPLETE_SIZE);
return response.fullHashes.Contains(completion);
}
virtual int Ver() const override { return VER; }
};
typedef nsTArray<RefPtr<const CacheResult>> ConstCacheResultArray;
class LookupCache {
public:
// Check for a canonicalized IP address.
static bool IsCanonicalizedIP(const nsACString& aHost);
// take a lookup string (www.hostname.com/path/to/resource.html) and
// expand it into the set of fragments that should be searched for in an
// entry
static nsresult GetLookupFragments(const nsACString& aSpec,
nsTArray<nsCString>* aFragments);
static nsresult GetLookupEntitylistFragments(const nsACString& aSpec,
nsTArray<nsCString>* aFragments);
LookupCache(const nsACString& aTableName, const nsACString& aProvider,
nsCOMPtr<nsIFile>& aStoreFile);
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(LookupCache);
const nsCString& TableName() const { return mTableName; }
// The directory handle where we operate will
// be moved away when a backup is made.
nsresult UpdateRootDirHandle(nsCOMPtr<nsIFile>& aRootStoreDirectory);
// Write data stored in lookup cache to disk.
nsresult WriteFile();
bool IsPrimed() const { return mPrimed; };
// Called when update to clear expired entries.
void InvalidateExpiredCacheEntries();
// Copy fullhash cache from another LookupCache.
void CopyFullHashCache(const LookupCache* aSource);
// Clear fullhash cache from fullhash/gethash response.
void ClearCache();
// Check if completions can be found in cache.
// Currently this is only used by testcase.
bool IsInCache(uint32_t key) const { return mFullHashCache.Get(key); };
uint32_t PrefixLength() const {
return mVLPrefixSet->FixedLengthPrefixLength();
}
#if DEBUG
void DumpCache() const;
#endif
void GetCacheInfo(nsIUrlClassifierCacheInfo** aCache) const;
nsresult VerifyCRC32(nsCOMPtr<nsIInputStream>& aIn);
virtual nsresult Open();
virtual nsresult Init();
;
virtual nsresult ClearPrefixes();
virtual nsresult Has(const Completion& aCompletion, bool* aHas,
uint32_t* aMatchLength, bool* aConfirmed) = 0;
// Prefix files file header
struct Header {
uint32_t magic;
uint32_t version;
};
virtual nsresult StoreToFile(nsCOMPtr<nsIFile>& aFile);
virtual nsresult LoadFromFile(nsCOMPtr<nsIFile>& aFile);
virtual bool IsEmpty() const;
virtual void ClearAll();
virtual nsresult LoadMozEntries() = 0;
template <typename T>
static T* Cast(LookupCache* aThat) {
return ((aThat && T::VER == aThat->Ver()) ? reinterpret_cast<T*>(aThat)
: nullptr);
}
template <typename T>
static const T* Cast(const LookupCache* aThat) {
return ((aThat && T::VER == aThat->Ver())
? reinterpret_cast<const T*>(aThat)
: nullptr);
}
private:
nsresult LoadPrefixSet();
virtual size_t SizeOfPrefixSet() const;
virtual nsCString GetPrefixSetSuffix() const = 0;
virtual int Ver() const = 0;
virtual void GetHeader(Header& aHeader) = 0;
virtual nsresult SanityCheck(const Header& aHeader) = 0;
virtual nsresult LoadLegacyFile() = 0;
virtual nsresult ClearLegacyFile() = 0;
protected:
virtual ~LookupCache() = default;
// Buffer size for file read/write
static const uint32_t MAX_BUFFER_SIZE;
// Check completions in positive cache and prefix in negative cache.
// 'aHas' and 'aConfirmed' are output parameters.
nsresult CheckCache(const Completion& aCompletion, bool* aHas,
bool* aConfirmed);
bool mPrimed; // true when the PrefixSet has been loaded (or constructed)
const nsCString mTableName;
const nsCString mProvider;
nsCOMPtr<nsIFile> mRootStoreDirectory;
nsCOMPtr<nsIFile> mStoreDirectory;
// For gtest to inspect private members.
friend class PerProviderDirectoryTestUtils;
// Cache stores fullhash response(V4)/gethash response(V2)
FullHashResponseMap mFullHashCache;
RefPtr<VariableLengthPrefixSet> mVLPrefixSet;
template <typename T>
static nsresult WriteValue(nsIOutputStream* aOutputStream, const T& aValue);
template <typename T>
static nsresult ReadValue(nsIInputStream* aInputStream, T& aValue);
};
typedef nsTArray<RefPtr<LookupCache>> LookupCacheArray;
class LookupCacheV2 final : public LookupCache {
public:
explicit LookupCacheV2(const nsACString& aTableName,
const nsACString& aProvider,
nsCOMPtr<nsIFile>& aStoreFile)
: LookupCache(aTableName, aProvider, aStoreFile) {}
virtual nsresult Has(const Completion& aCompletion, bool* aHas,
uint32_t* aMatchLength, bool* aConfirmed) override;
nsresult Build(AddPrefixArray& aAddPrefixes, AddCompleteArray& aAddCompletes);
nsresult GetPrefixes(FallibleTArray<uint32_t>& aAddPrefixes);
nsresult GetPrefixes(FallibleTArray<uint32_t>& aAddPrefixes,
FallibleTArray<nsCString>& aAddCompletes);
nsresult GetPrefixByIndex(uint32_t aIndex, uint32_t* aOutPrefix) const;
// This will Clear() the passed arrays when done.
// 'aExpirySec' is used by testcase to config an expired time.
void AddGethashResultToCache(const AddCompleteArray& aAddCompletes,
const MissPrefixArray& aMissPrefixes,
int64_t aExpirySec = 0);
virtual nsresult LoadMozEntries() override;
static const int VER;
static const uint32_t VLPSET_MAGIC;
static const uint32_t VLPSET_VERSION;
protected:
virtual nsCString GetPrefixSetSuffix() const override;
private:
~LookupCacheV2() = default;
virtual int Ver() const override { return VER; }
virtual void GetHeader(Header& aHeader) override;
virtual nsresult SanityCheck(const Header& aHeader) override;
virtual nsresult LoadLegacyFile() override;
virtual nsresult ClearLegacyFile() override;
};
} // namespace safebrowsing
} // namespace mozilla
#endif
|