Backed out changeset fc8099c8f98a (bug 1311935)

This commit is contained in:
Sebastian Hengst
2017-04-07 19:05:42 +02:00
parent 95f06a1763
commit ac6f4701fe
14 changed files with 106 additions and 457 deletions

View File

@@ -6,7 +6,6 @@
#include "nsAutoPtr.h"
#include "nsCOMPtr.h"
#include "nsAppDirectoryServiceDefs.h"
#include "nsArrayUtils.h"
#include "nsCRT.h"
#include "nsICryptoHash.h"
#include "nsICryptoHMAC.h"
@@ -123,9 +122,6 @@ LazyLogModule gUrlClassifierDbServiceLog("UrlClassifierDbService");
#define CONFIRM_AGE_PREF "urlclassifier.max-complete-age"
#define CONFIRM_AGE_DEFAULT_SEC (45 * 60)
// 30 minutes as the maximum negative cache duration.
#define MAXIMUM_NEGATIVE_CACHE_DURATION_SEC (30 * 60 * 1000)
// TODO: The following two prefs are to be removed after we
// roll out full v4 hash completion. See Bug 1331534.
#define TAKE_V4_COMPLETION_RESULT_PREF "browser.safebrowsing.temporary.take_v4_completion_result"
@@ -842,26 +838,19 @@ nsUrlClassifierDBServiceWorker::CacheCompletions(CacheResultArray *results)
}
LOG(("nsUrlClassifierDBServiceWorker::CacheCompletions [%p]", this));
if (!mClassifier) {
if (!mClassifier)
return NS_OK;
}
// Ownership is transferred in to us
nsAutoPtr<CacheResultArray> resultsPtr(results);
if (resultsPtr->Length() == 0) {
return NS_OK;
}
if (IsSameAsLastResults(*resultsPtr)) {
if (mLastResults == *resultsPtr) {
LOG(("Skipping completions that have just been cached already."));
return NS_OK;
}
nsAutoPtr<ProtocolParser> pParse;
pParse = resultsPtr->ElementAt(0)->Ver() == CacheResult::V2 ?
static_cast<ProtocolParser*>(new ProtocolParserV2()) :
static_cast<ProtocolParser*>(new ProtocolParserProtobuf());
nsAutoPtr<ProtocolParserV2> pParse(new ProtocolParserV2());
nsTArray<TableUpdate*> updates;
// Only cache results for tables that we have, don't take
// in tables we might accidentally have hit during a completion.
@@ -870,28 +859,37 @@ nsUrlClassifierDBServiceWorker::CacheCompletions(CacheResultArray *results)
nsresult rv = mClassifier->ActiveTables(tables);
NS_ENSURE_SUCCESS(rv, rv);
nsTArray<TableUpdate*> updates;
for (uint32_t i = 0; i < resultsPtr->Length(); i++) {
bool activeTable = false;
CacheResult* result = resultsPtr->ElementAt(i).get();
for (uint32_t table = 0; table < tables.Length(); table++) {
if (tables[table].Equals(result->table)) {
if (tables[table].Equals(resultsPtr->ElementAt(i).table)) {
activeTable = true;
break;
}
}
if (activeTable) {
TableUpdate* tu = pParse->GetTableUpdate(result->table);
TableUpdateV2* tuV2 = TableUpdate::Cast<TableUpdateV2>(
pParse->GetTableUpdate(resultsPtr->ElementAt(i).table));
rv = CacheResultToTableUpdate(result, tu);
// Ignore V4 for now.
if (!tuV2) {
continue;
}
LOG(("CacheCompletion Addchunk %d hash %X", resultsPtr->ElementAt(i).entry.addChunk,
resultsPtr->ElementAt(i).entry.ToUint32()));
rv = tuV2->NewAddComplete(resultsPtr->ElementAt(i).entry.addChunk,
resultsPtr->ElementAt(i).entry.complete);
if (NS_FAILED(rv)) {
// We can bail without leaking here because ForgetTableUpdates
// hasn't been called yet.
return rv;
}
updates.AppendElement(tu);
rv = tuV2->NewAddChunk(resultsPtr->ElementAt(i).entry.addChunk);
if (NS_FAILED(rv)) {
return rv;
}
updates.AppendElement(tuV2);
pParse->ForgetTableUpdates();
} else {
LOG(("Completion received, but table is not active, so not caching."));
@@ -899,53 +897,10 @@ nsUrlClassifierDBServiceWorker::CacheCompletions(CacheResultArray *results)
}
mClassifier->ApplyFullHashes(&updates);
mLastResults = Move(resultsPtr);
mLastResults = *resultsPtr;
return NS_OK;
}
nsresult
nsUrlClassifierDBServiceWorker::CacheResultToTableUpdate(CacheResult* aCacheResult,
TableUpdate* aUpdate)
{
auto tuV2 = TableUpdate::Cast<TableUpdateV2>(aUpdate);
if (tuV2) {
auto result = CacheResult::Cast<CacheResultV2>(aCacheResult);
MOZ_ASSERT(result);
LOG(("CacheCompletion hash %X, Addchunk %d", result->completion.ToUint32(),
result->addChunk));
nsresult rv = tuV2->NewAddComplete(result->addChunk, result->completion);
if (NS_FAILED(rv)) {
return rv;
}
rv = tuV2->NewAddChunk(result->addChunk);
return rv;
}
auto tuV4 = TableUpdate::Cast<TableUpdateV4>(aUpdate);
if (tuV4) {
auto result = CacheResult::Cast<CacheResultV4>(aCacheResult);
MOZ_ASSERT(result);
if (LOG_ENABLED()) {
const FullHashExpiryCache& fullHashes = result->response.fullHashes;
for (auto iter = fullHashes.ConstIter(); !iter.Done(); iter.Next()) {
Completion completion;
completion.Assign(iter.Key());
LOG(("CacheCompletion(v4) hash %X, CacheExpireTime %" PRId64, completion.ToUint32(),
iter.Data()));
}
}
tuV4->NewFullHashResponse(result->prefix, result->response);
return NS_OK;
}
// tableUpdate object should be either v2 or v4.
return NS_ERROR_FAILURE;
}
nsresult
nsUrlClassifierDBServiceWorker::CacheMisses(PrefixArray *results)
{
@@ -1007,39 +962,10 @@ NS_IMETHODIMP
nsUrlClassifierDBServiceWorker::ClearLastResults()
{
MOZ_ASSERT(!NS_IsMainThread(), "Must be on the background thread");
if (mLastResults) {
mLastResults->Clear();
}
mLastResults.Clear();
return NS_OK;
}
bool
nsUrlClassifierDBServiceWorker::IsSameAsLastResults(CacheResultArray& aResult)
{
if (!mLastResults || mLastResults->Length() != aResult.Length()) {
return false;
}
bool equal = true;
for (uint32_t i = 0; i < mLastResults->Length() && equal; i++) {
CacheResult* lhs = mLastResults->ElementAt(i).get();
CacheResult* rhs = aResult[i].get();
if (lhs->Ver() != rhs->Ver()) {
return false;
}
if (lhs->Ver() == CacheResult::V2) {
equal = *(CacheResult::Cast<CacheResultV2>(lhs)) ==
*(CacheResult::Cast<CacheResultV2>(rhs));
} else if (lhs->Ver() == CacheResult::V4) {
equal = *(CacheResult::Cast<CacheResultV4>(lhs)) ==
*(CacheResult::Cast<CacheResultV4>(rhs));
}
}
return equal;
}
// -------------------------------------------------------------------------
// nsUrlClassifierLookupCallback
@@ -1068,7 +994,6 @@ private:
~nsUrlClassifierLookupCallback();
nsresult HandleResults();
nsresult ProcessComplete(CacheResult* aCacheResult);
RefPtr<nsUrlClassifierDBService> mDBService;
nsAutoPtr<LookupResultArray> mResults;
@@ -1184,95 +1109,30 @@ nsUrlClassifierLookupCallback::CompletionFinished(nsresult status)
}
NS_IMETHODIMP
nsUrlClassifierLookupCallback::CompletionV2(const nsACString& aCompleteHash,
const nsACString& aTableName,
uint32_t aChunkId)
nsUrlClassifierLookupCallback::Completion(const nsACString& completeHash,
const nsACString& tableName,
uint32_t chunkId)
{
LOG(("nsUrlClassifierLookupCallback::Completion [%p, %s, %d]",
this, PromiseFlatCString(aTableName).get(), aChunkId));
this, PromiseFlatCString(tableName).get(), chunkId));
MOZ_ASSERT(!StringEndsWith(aTableName, NS_LITERAL_CSTRING("-proto")));
mozilla::safebrowsing::Completion hash;
hash.Assign(completeHash);
auto result = new CacheResultV2;
result->table = aTableName;
result->completion.Assign(aCompleteHash);
result->addChunk = aChunkId;
return ProcessComplete(result);
}
NS_IMETHODIMP
nsUrlClassifierLookupCallback::CompletionV4(const nsACString& aPartialHash,
const nsACString& aTableName,
uint32_t aNegativeCacheDuration,
nsIArray* aFullHashes)
{
LOG(("nsUrlClassifierLookupCallback::CompletionV4 [%p, %s, %d]",
this, PromiseFlatCString(aTableName).get(), aNegativeCacheDuration));
MOZ_ASSERT(StringEndsWith(aTableName, NS_LITERAL_CSTRING("-proto")));
if(!aFullHashes) {
return NS_ERROR_INVALID_ARG;
}
if (!Preferences::GetBool(TAKE_V4_COMPLETION_RESULT_PREF,
TAKE_V4_COMPLETION_RESULT_DEFAULT)) {
// Bug 1331534 - We temporarily ignore hash completion result
// for v4 tables.
return NS_OK;
}
if (aNegativeCacheDuration > MAXIMUM_NEGATIVE_CACHE_DURATION_SEC) {
LOG(("Negative cache duration too large, clamping it down to"
"a reasonable value."));
aNegativeCacheDuration = MAXIMUM_NEGATIVE_CACHE_DURATION_SEC;
}
auto result = new CacheResultV4;
int64_t nowSec = PR_Now() / PR_USEC_PER_SEC;
result->table = aTableName;
result->prefix = aPartialHash;
result->response.negativeCacheExpirySec = nowSec + aNegativeCacheDuration;
// Fill in positive cache entries.
uint32_t fullHashCount = 0;
nsresult rv = aFullHashes->GetLength(&fullHashCount);
if (NS_FAILED(rv)) {
return rv;
}
for (uint32_t i = 0; i < fullHashCount; i++) {
nsCOMPtr<nsIFullHashMatch> match = do_QueryElementAt(aFullHashes, i);
nsCString fullHash;
match->GetFullHash(fullHash);
uint32_t duration;
match->GetCacheDuration(&duration);
result->response.fullHashes.Put(fullHash, nowSec + duration);
}
return ProcessComplete(result);
}
nsresult
nsUrlClassifierLookupCallback::ProcessComplete(CacheResult* aCacheResult)
{
// Send this completion to the store for caching.
if (!mCacheResults) {
mCacheResults = new CacheResultArray();
if (!mCacheResults) {
if (!mCacheResults)
return NS_ERROR_OUT_OF_MEMORY;
}
}
CacheResult result;
result.entry.addChunk = chunkId;
result.entry.complete = hash;
result.table = tableName;
// OK if this fails, we just won't cache the item.
mCacheResults->AppendElement(aCacheResult);
mCacheResults->AppendElement(result);
// Check if this matched any of our results.
for (uint32_t i = 0; i < mResults->Length(); i++) {
@@ -1280,8 +1140,8 @@ nsUrlClassifierLookupCallback::ProcessComplete(CacheResult* aCacheResult)
// Now, see if it verifies a lookup
if (!result.mNoise
&& result.mTableName.Equals(aCacheResult->table)
&& aCacheResult->findCompletion(result.CompleteHash())) {
&& result.CompleteHash() == hash
&& result.mTableName.Equals(tableName)) {
result.mProtocolConfirmed = true;
}
}
@@ -1364,6 +1224,14 @@ nsUrlClassifierLookupCallback::HandleResults()
continue;
}
if (StringEndsWith(result.mTableName, NS_LITERAL_CSTRING("-proto")) &&
!Preferences::GetBool(TAKE_V4_COMPLETION_RESULT_PREF,
TAKE_V4_COMPLETION_RESULT_DEFAULT)) {
// Bug 1331534 - We temporarily ignore hash completion result
// for v4 tables.
continue;
}
LOG(("Confirmed result %s from table %s",
result.PartialHashHex().get(), result.mTableName.get()));