2012-08-15 11:04:19 +04:00
|
|
|
//* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-08-15 11:10:56 +04:00
|
|
|
/* 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/. */
|
2012-08-15 11:04:19 +04:00
|
|
|
|
|
|
|
#include "ProtocolParser.h"
|
|
|
|
#include "LookupCache.h"
|
|
|
|
#include "nsNetCID.h"
|
2018-12-17 03:03:00 +03:00
|
|
|
#include "mozilla/Components.h"
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2012-08-15 11:04:19 +04:00
|
|
|
#include "prnetdb.h"
|
|
|
|
#include "prprf.h"
|
|
|
|
|
2016-11-25 11:02:37 +03:00
|
|
|
#include "nsUrlClassifierDBService.h"
|
2012-08-15 11:04:19 +04:00
|
|
|
#include "nsUrlClassifierUtils.h"
|
2016-08-04 13:10:12 +03:00
|
|
|
#include "nsPrintfCString.h"
|
2016-08-12 12:29:42 +03:00
|
|
|
#include "mozilla/Base64.h"
|
2016-10-05 09:59:53 +03:00
|
|
|
#include "RiceDeltaDecoder.h"
|
|
|
|
#include "mozilla/EndianUtils.h"
|
2018-02-21 00:54:30 +03:00
|
|
|
#include "mozilla/ErrorNames.h"
|
2016-12-16 06:16:31 +03:00
|
|
|
#include "mozilla/IntegerPrintfMacros.h"
|
2012-08-15 11:04:19 +04:00
|
|
|
|
2016-05-26 21:14:47 +03:00
|
|
|
// MOZ_LOG=UrlClassifierProtocolParser:5
|
2016-01-28 21:37:00 +03:00
|
|
|
mozilla::LazyLogModule gUrlClassifierProtocolParserLog(
|
|
|
|
"UrlClassifierProtocolParser");
|
2015-09-15 04:03:48 +03:00
|
|
|
#define PARSER_LOG(args) \
|
|
|
|
MOZ_LOG(gUrlClassifierProtocolParserLog, mozilla::LogLevel::Debug, args)
|
2012-08-15 11:04:19 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace safebrowsing {
|
|
|
|
|
|
|
|
// Updates will fail if fed chunks larger than this
|
2019-12-28 00:11:45 +03:00
|
|
|
const uint32_t MAX_CHUNK_SIZE = (4 * 1024 * 1024);
|
2015-02-25 16:05:17 +03:00
|
|
|
// Updates will fail if the total number of tocuhed chunks is larger than this
|
|
|
|
const uint32_t MAX_CHUNK_RANGE = 1000000;
|
2012-08-15 11:04:19 +04:00
|
|
|
|
2012-10-12 03:38:04 +04:00
|
|
|
const uint32_t DOMAIN_SIZE = 4;
|
2012-08-15 11:04:19 +04:00
|
|
|
|
|
|
|
// Parse one stringified range of chunks of the form "n" or "n-m" from a
|
|
|
|
// comma-separated list of chunks. Upon return, 'begin' will point to the
|
|
|
|
// next range of chunks in the list of chunks.
|
|
|
|
static bool ParseChunkRange(nsACString::const_iterator& aBegin,
|
|
|
|
const nsACString::const_iterator& aEnd,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t* aFirst, uint32_t* aLast) {
|
2012-08-15 11:04:19 +04:00
|
|
|
nsACString::const_iterator iter = aBegin;
|
|
|
|
FindCharInReadable(',', iter, aEnd);
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString element(Substring(aBegin, iter));
|
2012-08-15 11:04:19 +04:00
|
|
|
aBegin = iter;
|
|
|
|
if (aBegin != aEnd) aBegin++;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t numRead = PR_sscanf(element.get(), "%u-%u", aFirst, aLast);
|
2012-08-15 11:04:19 +04:00
|
|
|
if (numRead == 2) {
|
|
|
|
if (*aFirst > *aLast) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t tmp = *aFirst;
|
2012-08-15 11:04:19 +04:00
|
|
|
*aFirst = *aLast;
|
|
|
|
*aLast = tmp;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (numRead == 1) {
|
|
|
|
*aLast = *aFirst;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-12 06:55:48 +03:00
|
|
|
///////////////////////////////////////////////////////////////
|
|
|
|
// ProtocolParser implementation
|
|
|
|
|
2013-01-08 19:43:33 +04:00
|
|
|
ProtocolParser::ProtocolParser() : mUpdateStatus(NS_OK), mUpdateWaitSec(0) {}
|
2012-08-15 11:04:19 +04:00
|
|
|
|
2020-03-10 11:48:24 +03:00
|
|
|
ProtocolParser::~ProtocolParser() = default;
|
2016-08-12 06:55:48 +03:00
|
|
|
|
2018-04-24 11:08:56 +03:00
|
|
|
nsresult ProtocolParser::Begin(const nsACString& aTable,
|
|
|
|
const nsTArray<nsCString>& aUpdateTables) {
|
|
|
|
// ProtocolParser objects should never be reused.
|
|
|
|
MOZ_ASSERT(mPending.IsEmpty());
|
|
|
|
MOZ_ASSERT(mTableUpdates.IsEmpty());
|
|
|
|
MOZ_ASSERT(mForwards.IsEmpty());
|
|
|
|
MOZ_ASSERT(mRequestedTables.IsEmpty());
|
|
|
|
MOZ_ASSERT(mTablesToReset.IsEmpty());
|
|
|
|
|
|
|
|
if (!aTable.IsEmpty()) {
|
|
|
|
SetCurrentTable(aTable);
|
|
|
|
}
|
|
|
|
SetRequestedTables(aUpdateTables);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-08-12 06:55:48 +03:00
|
|
|
RefPtr<TableUpdate> ProtocolParser::GetTableUpdate(const nsACString& aTable) {
|
|
|
|
for (uint32_t i = 0; i < mTableUpdates.Length(); i++) {
|
|
|
|
if (aTable.Equals(mTableUpdates[i]->TableName())) {
|
|
|
|
return mTableUpdates[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We free automatically on destruction, ownership of these
|
|
|
|
// updates can be transferred to DBServiceWorker, which passes
|
|
|
|
// them back to Classifier when doing the updates, and that
|
|
|
|
// will free them.
|
2018-06-02 01:48:48 +03:00
|
|
|
RefPtr<TableUpdate> update = CreateTableUpdate(aTable);
|
2016-08-12 06:55:48 +03:00
|
|
|
mTableUpdates.AppendElement(update);
|
|
|
|
return update;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
// ProtocolParserV2
|
|
|
|
|
|
|
|
ProtocolParserV2::ProtocolParserV2()
|
|
|
|
: mState(PROTOCOL_STATE_CONTROL), mTableUpdate(nullptr) {}
|
|
|
|
|
2020-03-10 11:48:24 +03:00
|
|
|
ProtocolParserV2::~ProtocolParserV2() = default;
|
2016-08-12 06:55:48 +03:00
|
|
|
|
|
|
|
void ProtocolParserV2::SetCurrentTable(const nsACString& aTable) {
|
2018-06-02 01:48:48 +03:00
|
|
|
RefPtr<TableUpdate> update = GetTableUpdate(aTable);
|
2016-08-12 06:55:48 +03:00
|
|
|
mTableUpdate = TableUpdate::Cast<TableUpdateV2>(update);
|
2012-08-15 11:04:19 +04:00
|
|
|
}
|
|
|
|
|
2016-08-12 06:55:48 +03:00
|
|
|
nsresult ProtocolParserV2::AppendStream(const nsACString& aData) {
|
2012-08-15 11:04:19 +04:00
|
|
|
if (NS_FAILED(mUpdateStatus)) return mUpdateStatus;
|
|
|
|
|
|
|
|
nsresult rv;
|
2019-10-07 18:02:39 +03:00
|
|
|
if (!mPending.Append(aData, mozilla::fallible)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2017-01-19 12:16:13 +03:00
|
|
|
#ifdef MOZ_SAFEBROWSING_DUMP_FAILED_UPDATES
|
|
|
|
mRawUpdate.Append(aData);
|
|
|
|
#endif
|
2012-08-15 11:04:19 +04:00
|
|
|
|
|
|
|
bool done = false;
|
|
|
|
while (!done) {
|
2016-11-25 11:02:37 +03:00
|
|
|
if (nsUrlClassifierDBService::ShutdownHasStarted()) {
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
2012-08-15 11:04:19 +04:00
|
|
|
if (mState == PROTOCOL_STATE_CONTROL) {
|
|
|
|
rv = ProcessControl(&done);
|
|
|
|
} else if (mState == PROTOCOL_STATE_CHUNK) {
|
|
|
|
rv = ProcessChunk(&done);
|
|
|
|
} else {
|
|
|
|
NS_ERROR("Unexpected protocol state");
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mUpdateStatus = rv;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-08-12 06:55:48 +03:00
|
|
|
void ProtocolParserV2::End() {
|
2016-08-04 13:10:12 +03:00
|
|
|
// Inbound data has already been processed in every AppendStream() call.
|
2018-05-11 03:58:01 +03:00
|
|
|
mTableUpdate = nullptr;
|
2016-08-04 13:10:12 +03:00
|
|
|
}
|
|
|
|
|
2016-08-12 06:55:48 +03:00
|
|
|
nsresult ProtocolParserV2::ProcessControl(bool* aDone) {
|
2012-08-15 11:04:19 +04:00
|
|
|
nsresult rv;
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString line;
|
2012-08-15 11:04:19 +04:00
|
|
|
*aDone = true;
|
|
|
|
while (NextLine(line)) {
|
2015-09-15 04:03:48 +03:00
|
|
|
PARSER_LOG(("Processing %s\n", line.get()));
|
2012-08-15 11:04:19 +04:00
|
|
|
|
2020-07-01 11:29:29 +03:00
|
|
|
if (StringBeginsWith(line, "i:"_ns)) {
|
2013-09-07 04:12:33 +04:00
|
|
|
// Set the table name from the table header line.
|
2012-08-15 11:04:19 +04:00
|
|
|
SetCurrentTable(Substring(line, 2));
|
2020-07-01 11:29:29 +03:00
|
|
|
} else if (StringBeginsWith(line, "n:"_ns)) {
|
2016-10-18 09:45:21 +03:00
|
|
|
if (PR_sscanf(line.get(), "n:%d", &mUpdateWaitSec) != 1) {
|
|
|
|
PARSER_LOG(("Error parsing n: '%s' (%d)", line.get(), mUpdateWaitSec));
|
2016-03-25 04:50:44 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2012-08-15 11:04:19 +04:00
|
|
|
}
|
|
|
|
} else if (line.EqualsLiteral("r:pleasereset")) {
|
2018-04-12 20:11:30 +03:00
|
|
|
PARSER_LOG(("All tables will be reset."));
|
2020-04-30 12:40:24 +03:00
|
|
|
mTablesToReset = mRequestedTables.Clone();
|
2020-07-01 11:29:29 +03:00
|
|
|
} else if (StringBeginsWith(line, "u:"_ns)) {
|
2012-08-15 11:04:19 +04:00
|
|
|
rv = ProcessForward(line);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2020-07-01 11:29:29 +03:00
|
|
|
} else if (StringBeginsWith(line, "a:"_ns) ||
|
|
|
|
StringBeginsWith(line, "s:"_ns)) {
|
2012-08-15 11:04:19 +04:00
|
|
|
rv = ProcessChunkControl(line);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
*aDone = false;
|
|
|
|
return NS_OK;
|
2020-07-01 11:29:29 +03:00
|
|
|
} else if (StringBeginsWith(line, "ad:"_ns) ||
|
|
|
|
StringBeginsWith(line, "sd:"_ns)) {
|
2012-08-15 11:04:19 +04:00
|
|
|
rv = ProcessExpirations(line);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*aDone = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-08-12 06:55:48 +03:00
|
|
|
nsresult ProtocolParserV2::ProcessExpirations(const nsCString& aLine) {
|
2012-08-15 11:04:19 +04:00
|
|
|
if (!mTableUpdate) {
|
|
|
|
NS_WARNING("Got an expiration without a table.");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2017-06-20 12:19:52 +03:00
|
|
|
const nsACString& list = Substring(aLine, 3);
|
2012-08-15 11:04:19 +04:00
|
|
|
nsACString::const_iterator begin, end;
|
|
|
|
list.BeginReading(begin);
|
|
|
|
list.EndReading(end);
|
|
|
|
while (begin != end) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t first, last;
|
2012-08-15 11:04:19 +04:00
|
|
|
if (ParseChunkRange(begin, end, &first, &last)) {
|
2015-02-25 16:05:17 +03:00
|
|
|
if (last < first) return NS_ERROR_FAILURE;
|
|
|
|
if (last - first > MAX_CHUNK_RANGE) return NS_ERROR_FAILURE;
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t num = first; num <= last; num++) {
|
2015-02-25 16:05:17 +03:00
|
|
|
if (aLine[0] == 'a') {
|
|
|
|
nsresult rv = mTableUpdate->NewAddExpiration(num);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nsresult rv = mTableUpdate->NewSubExpiration(num);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
2012-08-15 11:04:19 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-08-12 06:55:48 +03:00
|
|
|
nsresult ProtocolParserV2::ProcessChunkControl(const nsCString& aLine) {
|
2012-08-15 11:04:19 +04:00
|
|
|
if (!mTableUpdate) {
|
|
|
|
NS_WARNING("Got a chunk before getting a table.");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mState = PROTOCOL_STATE_CHUNK;
|
|
|
|
char command;
|
|
|
|
|
|
|
|
mChunkState.Clear();
|
|
|
|
|
|
|
|
if (PR_sscanf(aLine.get(), "%c:%d:%d:%d", &command, &mChunkState.num,
|
|
|
|
&mChunkState.hashSize, &mChunkState.length) != 4) {
|
2015-09-15 04:03:48 +03:00
|
|
|
NS_WARNING(("PR_sscanf failed"));
|
2012-08-15 11:04:19 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mChunkState.length > MAX_CHUNK_SIZE) {
|
2015-09-15 04:03:48 +03:00
|
|
|
NS_WARNING("Invalid length specified in update.");
|
2012-08-15 11:04:19 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(mChunkState.hashSize == PREFIX_SIZE ||
|
|
|
|
mChunkState.hashSize == COMPLETE_SIZE)) {
|
|
|
|
NS_WARNING("Invalid hash size specified in update.");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2020-07-01 11:29:29 +03:00
|
|
|
if (StringEndsWith(mTableUpdate->TableName(), "-shavar"_ns) ||
|
|
|
|
StringEndsWith(mTableUpdate->TableName(), "-simple"_ns)) {
|
2013-09-07 04:12:33 +04:00
|
|
|
// Accommodate test tables ending in -simple for now.
|
|
|
|
mChunkState.type = (command == 'a') ? CHUNK_ADD : CHUNK_SUB;
|
2020-07-01 11:29:29 +03:00
|
|
|
} else if (StringEndsWith(mTableUpdate->TableName(), "-digest256"_ns)) {
|
2013-09-07 04:12:33 +04:00
|
|
|
mChunkState.type = (command == 'a') ? CHUNK_ADD_DIGEST : CHUNK_SUB_DIGEST;
|
|
|
|
}
|
2015-02-25 16:05:17 +03:00
|
|
|
nsresult rv;
|
2013-09-07 04:12:33 +04:00
|
|
|
switch (mChunkState.type) {
|
|
|
|
case CHUNK_ADD:
|
2015-02-25 16:05:17 +03:00
|
|
|
rv = mTableUpdate->NewAddChunk(mChunkState.num);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2013-09-07 04:12:33 +04:00
|
|
|
break;
|
|
|
|
case CHUNK_SUB:
|
2015-02-25 16:05:17 +03:00
|
|
|
rv = mTableUpdate->NewSubChunk(mChunkState.num);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2013-09-07 04:12:33 +04:00
|
|
|
break;
|
|
|
|
case CHUNK_ADD_DIGEST:
|
2015-02-25 16:05:17 +03:00
|
|
|
rv = mTableUpdate->NewAddChunk(mChunkState.num);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2013-09-07 04:12:33 +04:00
|
|
|
break;
|
|
|
|
case CHUNK_SUB_DIGEST:
|
2015-02-25 16:05:17 +03:00
|
|
|
rv = mTableUpdate->NewSubChunk(mChunkState.num);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2013-09-07 04:12:33 +04:00
|
|
|
break;
|
2012-08-15 11:04:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-08-12 06:55:48 +03:00
|
|
|
nsresult ProtocolParserV2::ProcessForward(const nsCString& aLine) {
|
2017-06-20 12:19:52 +03:00
|
|
|
const nsACString& forward = Substring(aLine, 2);
|
2014-01-16 12:27:58 +04:00
|
|
|
return AddForward(forward);
|
2012-08-15 11:04:19 +04:00
|
|
|
}
|
|
|
|
|
2016-08-12 06:55:48 +03:00
|
|
|
nsresult ProtocolParserV2::AddForward(const nsACString& aUrl) {
|
2012-08-15 11:04:19 +04:00
|
|
|
if (!mTableUpdate) {
|
|
|
|
NS_WARNING("Forward without a table name.");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
ForwardedUpdate* forward = mForwards.AppendElement();
|
|
|
|
forward->table = mTableUpdate->TableName();
|
|
|
|
forward->url.Assign(aUrl);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-08-12 06:55:48 +03:00
|
|
|
nsresult ProtocolParserV2::ProcessChunk(bool* aDone) {
|
2012-08-15 11:04:19 +04:00
|
|
|
if (!mTableUpdate) {
|
|
|
|
NS_WARNING("Processing chunk without an active table.");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(mChunkState.num != 0, "Must have a chunk number.");
|
|
|
|
|
|
|
|
if (mPending.Length() < mChunkState.length) {
|
|
|
|
*aDone = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pull the chunk out of the pending stream data.
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString chunk;
|
2012-08-15 11:04:19 +04:00
|
|
|
chunk.Assign(Substring(mPending, 0, mChunkState.length));
|
2014-11-19 05:42:43 +03:00
|
|
|
mPending.Cut(0, mChunkState.length);
|
2012-08-15 11:04:19 +04:00
|
|
|
|
|
|
|
*aDone = false;
|
|
|
|
mState = PROTOCOL_STATE_CONTROL;
|
|
|
|
|
2020-07-01 11:29:29 +03:00
|
|
|
if (StringEndsWith(mTableUpdate->TableName(), "-shavar"_ns)) {
|
2012-08-15 11:04:19 +04:00
|
|
|
return ProcessShaChunk(chunk);
|
|
|
|
}
|
2020-07-01 11:29:29 +03:00
|
|
|
if (StringEndsWith(mTableUpdate->TableName(), "-digest256"_ns)) {
|
2013-09-07 04:12:33 +04:00
|
|
|
return ProcessDigestChunk(chunk);
|
|
|
|
}
|
|
|
|
return ProcessPlaintextChunk(chunk);
|
2012-08-15 11:04:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process a plaintext chunk (currently only used in unit tests).
|
|
|
|
*/
|
2016-08-12 06:55:48 +03:00
|
|
|
nsresult ProtocolParserV2::ProcessPlaintextChunk(const nsACString& aChunk) {
|
2012-08-15 11:04:19 +04:00
|
|
|
if (!mTableUpdate) {
|
|
|
|
NS_WARNING("Chunk received with no table.");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-10-20 00:13:49 +03:00
|
|
|
PARSER_LOG(("Handling a %d-byte simple chunk", aChunk.Length()));
|
|
|
|
|
2012-08-15 11:04:19 +04:00
|
|
|
nsTArray<nsCString> lines;
|
|
|
|
ParseString(PromiseFlatCString(aChunk), '\n', lines);
|
|
|
|
|
|
|
|
// non-hashed tables need to be hashed
|
2012-10-12 03:38:04 +04:00
|
|
|
for (uint32_t i = 0; i < lines.Length(); i++) {
|
2012-08-15 11:04:19 +04:00
|
|
|
nsCString& line = lines[i];
|
|
|
|
|
|
|
|
if (mChunkState.type == CHUNK_ADD) {
|
|
|
|
if (mChunkState.hashSize == COMPLETE_SIZE) {
|
|
|
|
Completion hash;
|
2017-09-05 12:14:54 +03:00
|
|
|
hash.FromPlaintext(line);
|
2015-02-25 16:05:17 +03:00
|
|
|
nsresult rv = mTableUpdate->NewAddComplete(mChunkState.num, hash);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2012-08-15 11:04:19 +04:00
|
|
|
} else {
|
|
|
|
NS_ASSERTION(mChunkState.hashSize == 4,
|
|
|
|
"Only 32- or 4-byte hashes can be used for add chunks.");
|
2013-01-08 19:43:33 +04:00
|
|
|
Prefix hash;
|
2017-09-05 12:14:54 +03:00
|
|
|
hash.FromPlaintext(line);
|
2015-02-25 16:05:17 +03:00
|
|
|
nsresult rv = mTableUpdate->NewAddPrefix(mChunkState.num, hash);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2012-08-15 11:04:19 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nsCString::const_iterator begin, iter, end;
|
|
|
|
line.BeginReading(begin);
|
|
|
|
line.EndReading(end);
|
|
|
|
iter = begin;
|
2012-10-12 03:38:04 +04:00
|
|
|
uint32_t addChunk;
|
2012-08-15 11:04:19 +04:00
|
|
|
if (!FindCharInReadable(':', iter, end) ||
|
|
|
|
PR_sscanf(lines[i].get(), "%d:", &addChunk) != 1) {
|
|
|
|
NS_WARNING("Received sub chunk without associated add chunk.");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
iter++;
|
|
|
|
|
|
|
|
if (mChunkState.hashSize == COMPLETE_SIZE) {
|
|
|
|
Completion hash;
|
2017-09-05 12:14:54 +03:00
|
|
|
hash.FromPlaintext(Substring(iter, end));
|
2015-02-25 16:05:17 +03:00
|
|
|
nsresult rv =
|
|
|
|
mTableUpdate->NewSubComplete(addChunk, hash, mChunkState.num);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2012-08-15 11:04:19 +04:00
|
|
|
} else {
|
|
|
|
NS_ASSERTION(mChunkState.hashSize == 4,
|
|
|
|
"Only 32- or 4-byte hashes can be used for add chunks.");
|
|
|
|
Prefix hash;
|
2017-09-05 12:14:54 +03:00
|
|
|
hash.FromPlaintext(Substring(iter, end));
|
2015-02-25 16:05:17 +03:00
|
|
|
nsresult rv =
|
|
|
|
mTableUpdate->NewSubPrefix(addChunk, hash, mChunkState.num);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2012-08-15 11:04:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-08-12 06:55:48 +03:00
|
|
|
nsresult ProtocolParserV2::ProcessShaChunk(const nsACString& aChunk) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t start = 0;
|
2012-08-15 11:04:19 +04:00
|
|
|
while (start < aChunk.Length()) {
|
|
|
|
// First four bytes are the domain key.
|
|
|
|
Prefix domain;
|
|
|
|
domain.Assign(Substring(aChunk, start, DOMAIN_SIZE));
|
|
|
|
start += DOMAIN_SIZE;
|
|
|
|
|
|
|
|
// Then a count of entries.
|
2012-10-12 03:38:04 +04:00
|
|
|
uint8_t numEntries = static_cast<uint8_t>(aChunk[start]);
|
2012-08-15 11:04:19 +04:00
|
|
|
start++;
|
|
|
|
|
2015-10-20 00:13:49 +03:00
|
|
|
PARSER_LOG(
|
|
|
|
("Handling a %d-byte shavar chunk containing %u entries"
|
|
|
|
" for domain %X",
|
|
|
|
aChunk.Length(), numEntries, domain.ToUint32()));
|
|
|
|
|
2012-08-15 11:04:19 +04:00
|
|
|
nsresult rv;
|
|
|
|
if (mChunkState.type == CHUNK_ADD && mChunkState.hashSize == PREFIX_SIZE) {
|
|
|
|
rv = ProcessHostAdd(domain, numEntries, aChunk, &start);
|
|
|
|
} else if (mChunkState.type == CHUNK_ADD &&
|
|
|
|
mChunkState.hashSize == COMPLETE_SIZE) {
|
|
|
|
rv = ProcessHostAddComplete(numEntries, aChunk, &start);
|
|
|
|
} else if (mChunkState.type == CHUNK_SUB &&
|
|
|
|
mChunkState.hashSize == PREFIX_SIZE) {
|
|
|
|
rv = ProcessHostSub(domain, numEntries, aChunk, &start);
|
|
|
|
} else if (mChunkState.type == CHUNK_SUB &&
|
|
|
|
mChunkState.hashSize == COMPLETE_SIZE) {
|
|
|
|
rv = ProcessHostSubComplete(numEntries, aChunk, &start);
|
|
|
|
} else {
|
|
|
|
NS_WARNING("Unexpected chunk type/hash size!");
|
2015-09-15 04:03:48 +03:00
|
|
|
PARSER_LOG(("Got an unexpected chunk type/hash size: %s:%d",
|
2012-08-15 11:04:19 +04:00
|
|
|
mChunkState.type == CHUNK_ADD ? "add" : "sub",
|
|
|
|
mChunkState.hashSize));
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-08-12 06:55:48 +03:00
|
|
|
nsresult ProtocolParserV2::ProcessDigestChunk(const nsACString& aChunk) {
|
2015-10-20 00:13:49 +03:00
|
|
|
PARSER_LOG(("Handling a %d-byte digest256 chunk", aChunk.Length()));
|
|
|
|
|
2013-09-07 04:12:33 +04:00
|
|
|
if (mChunkState.type == CHUNK_ADD_DIGEST) {
|
|
|
|
return ProcessDigestAdd(aChunk);
|
|
|
|
}
|
|
|
|
if (mChunkState.type == CHUNK_SUB_DIGEST) {
|
|
|
|
return ProcessDigestSub(aChunk);
|
|
|
|
}
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2016-08-12 06:55:48 +03:00
|
|
|
nsresult ProtocolParserV2::ProcessDigestAdd(const nsACString& aChunk) {
|
2018-05-11 03:58:01 +03:00
|
|
|
MOZ_ASSERT(mTableUpdate);
|
2013-09-07 04:12:33 +04:00
|
|
|
// The ABNF format for add chunks is (HASH)+, where HASH is 32 bytes.
|
|
|
|
MOZ_ASSERT(aChunk.Length() % 32 == 0,
|
|
|
|
"Chunk length in bytes must be divisible by 4");
|
|
|
|
uint32_t start = 0;
|
|
|
|
while (start < aChunk.Length()) {
|
|
|
|
Completion hash;
|
|
|
|
hash.Assign(Substring(aChunk, start, COMPLETE_SIZE));
|
|
|
|
start += COMPLETE_SIZE;
|
2015-02-25 16:05:17 +03:00
|
|
|
nsresult rv = mTableUpdate->NewAddComplete(mChunkState.num, hash);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2013-09-07 04:12:33 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-08-12 06:55:48 +03:00
|
|
|
nsresult ProtocolParserV2::ProcessDigestSub(const nsACString& aChunk) {
|
2018-05-11 03:58:01 +03:00
|
|
|
MOZ_ASSERT(mTableUpdate);
|
2013-09-07 04:12:33 +04:00
|
|
|
// The ABNF format for sub chunks is (ADDCHUNKNUM HASH)+, where ADDCHUNKNUM
|
|
|
|
// is a 4 byte chunk number, and HASH is 32 bytes.
|
|
|
|
MOZ_ASSERT(aChunk.Length() % 36 == 0,
|
|
|
|
"Chunk length in bytes must be divisible by 36");
|
|
|
|
uint32_t start = 0;
|
|
|
|
while (start < aChunk.Length()) {
|
|
|
|
// Read ADDCHUNKNUM
|
2017-06-20 12:19:52 +03:00
|
|
|
const nsACString& addChunkStr = Substring(aChunk, start, 4);
|
2013-09-07 04:12:33 +04:00
|
|
|
start += 4;
|
|
|
|
|
|
|
|
uint32_t addChunk;
|
|
|
|
memcpy(&addChunk, addChunkStr.BeginReading(), 4);
|
|
|
|
addChunk = PR_ntohl(addChunk);
|
|
|
|
|
|
|
|
// Read the hash
|
|
|
|
Completion hash;
|
|
|
|
hash.Assign(Substring(aChunk, start, COMPLETE_SIZE));
|
|
|
|
start += COMPLETE_SIZE;
|
|
|
|
|
2015-02-25 16:05:17 +03:00
|
|
|
nsresult rv = mTableUpdate->NewSubComplete(addChunk, hash, mChunkState.num);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2013-09-07 04:12:33 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-08-12 06:55:48 +03:00
|
|
|
nsresult ProtocolParserV2::ProcessHostAdd(const Prefix& aDomain,
|
|
|
|
uint8_t aNumEntries,
|
2012-08-22 19:56:38 +04:00
|
|
|
const nsACString& aChunk,
|
|
|
|
uint32_t* aStart) {
|
2018-05-11 03:58:01 +03:00
|
|
|
MOZ_ASSERT(mTableUpdate);
|
2012-08-15 11:04:19 +04:00
|
|
|
NS_ASSERTION(mChunkState.hashSize == PREFIX_SIZE,
|
|
|
|
"ProcessHostAdd should only be called for prefix hashes.");
|
|
|
|
|
|
|
|
if (aNumEntries == 0) {
|
2015-02-25 16:05:17 +03:00
|
|
|
nsresult rv = mTableUpdate->NewAddPrefix(mChunkState.num, aDomain);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2012-08-15 11:04:19 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*aStart + (PREFIX_SIZE * aNumEntries) > aChunk.Length()) {
|
|
|
|
NS_WARNING("Chunk is not long enough to contain the expected entries.");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-10-12 03:38:04 +04:00
|
|
|
for (uint8_t i = 0; i < aNumEntries; i++) {
|
2012-08-15 11:04:19 +04:00
|
|
|
Prefix hash;
|
|
|
|
hash.Assign(Substring(aChunk, *aStart, PREFIX_SIZE));
|
2015-10-20 00:13:49 +03:00
|
|
|
PARSER_LOG(("Add prefix %X", hash.ToUint32()));
|
2015-02-25 16:05:17 +03:00
|
|
|
nsresult rv = mTableUpdate->NewAddPrefix(mChunkState.num, hash);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2012-08-15 11:04:19 +04:00
|
|
|
*aStart += PREFIX_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-08-12 06:55:48 +03:00
|
|
|
nsresult ProtocolParserV2::ProcessHostSub(const Prefix& aDomain,
|
|
|
|
uint8_t aNumEntries,
|
2012-08-22 19:56:38 +04:00
|
|
|
const nsACString& aChunk,
|
|
|
|
uint32_t* aStart) {
|
2018-05-11 03:58:01 +03:00
|
|
|
MOZ_ASSERT(mTableUpdate);
|
2012-08-15 11:04:19 +04:00
|
|
|
NS_ASSERTION(mChunkState.hashSize == PREFIX_SIZE,
|
|
|
|
"ProcessHostSub should only be called for prefix hashes.");
|
|
|
|
|
|
|
|
if (aNumEntries == 0) {
|
|
|
|
if ((*aStart) + 4 > aChunk.Length()) {
|
|
|
|
NS_WARNING("Received a zero-entry sub chunk without an associated add.");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2017-06-20 12:19:52 +03:00
|
|
|
const nsACString& addChunkStr = Substring(aChunk, *aStart, 4);
|
2012-08-15 11:04:19 +04:00
|
|
|
*aStart += 4;
|
|
|
|
|
2012-10-12 03:38:04 +04:00
|
|
|
uint32_t addChunk;
|
2012-08-15 11:04:19 +04:00
|
|
|
memcpy(&addChunk, addChunkStr.BeginReading(), 4);
|
|
|
|
addChunk = PR_ntohl(addChunk);
|
|
|
|
|
2015-10-20 00:13:49 +03:00
|
|
|
PARSER_LOG(("Sub prefix (addchunk=%u)", addChunk));
|
2015-02-25 16:05:17 +03:00
|
|
|
nsresult rv =
|
|
|
|
mTableUpdate->NewSubPrefix(addChunk, aDomain, mChunkState.num);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2012-08-15 11:04:19 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*aStart + ((PREFIX_SIZE + 4) * aNumEntries) > aChunk.Length()) {
|
|
|
|
NS_WARNING("Chunk is not long enough to contain the expected entries.");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-10-12 03:38:04 +04:00
|
|
|
for (uint8_t i = 0; i < aNumEntries; i++) {
|
2017-06-20 12:19:52 +03:00
|
|
|
const nsACString& addChunkStr = Substring(aChunk, *aStart, 4);
|
2012-08-15 11:04:19 +04:00
|
|
|
*aStart += 4;
|
|
|
|
|
2012-10-12 03:38:04 +04:00
|
|
|
uint32_t addChunk;
|
2012-08-15 11:04:19 +04:00
|
|
|
memcpy(&addChunk, addChunkStr.BeginReading(), 4);
|
|
|
|
addChunk = PR_ntohl(addChunk);
|
|
|
|
|
|
|
|
Prefix prefix;
|
|
|
|
prefix.Assign(Substring(aChunk, *aStart, PREFIX_SIZE));
|
|
|
|
*aStart += PREFIX_SIZE;
|
|
|
|
|
2015-10-20 00:13:49 +03:00
|
|
|
PARSER_LOG(("Sub prefix %X (addchunk=%u)", prefix.ToUint32(), addChunk));
|
2015-02-25 16:05:17 +03:00
|
|
|
nsresult rv = mTableUpdate->NewSubPrefix(addChunk, prefix, mChunkState.num);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2012-08-15 11:04:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-08-12 06:55:48 +03:00
|
|
|
nsresult ProtocolParserV2::ProcessHostAddComplete(uint8_t aNumEntries,
|
2012-08-22 19:56:38 +04:00
|
|
|
const nsACString& aChunk,
|
|
|
|
uint32_t* aStart) {
|
2018-05-11 03:58:01 +03:00
|
|
|
MOZ_ASSERT(mTableUpdate);
|
2012-08-15 11:04:19 +04:00
|
|
|
NS_ASSERTION(
|
|
|
|
mChunkState.hashSize == COMPLETE_SIZE,
|
|
|
|
"ProcessHostAddComplete should only be called for complete hashes.");
|
|
|
|
|
|
|
|
if (aNumEntries == 0) {
|
|
|
|
// this is totally comprehensible.
|
2013-09-07 04:12:33 +04:00
|
|
|
// My sarcasm detector is going off!
|
2012-08-15 11:04:19 +04:00
|
|
|
NS_WARNING("Expected > 0 entries for a 32-byte hash add.");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*aStart + (COMPLETE_SIZE * aNumEntries) > aChunk.Length()) {
|
|
|
|
NS_WARNING("Chunk is not long enough to contain the expected entries.");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-10-12 03:38:04 +04:00
|
|
|
for (uint8_t i = 0; i < aNumEntries; i++) {
|
2012-08-15 11:04:19 +04:00
|
|
|
Completion hash;
|
|
|
|
hash.Assign(Substring(aChunk, *aStart, COMPLETE_SIZE));
|
2015-02-25 16:05:17 +03:00
|
|
|
nsresult rv = mTableUpdate->NewAddComplete(mChunkState.num, hash);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2012-08-15 11:04:19 +04:00
|
|
|
*aStart += COMPLETE_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-08-12 06:55:48 +03:00
|
|
|
nsresult ProtocolParserV2::ProcessHostSubComplete(uint8_t aNumEntries,
|
2018-06-02 01:48:48 +03:00
|
|
|
const nsACString& aChunk,
|
|
|
|
uint32_t* aStart) {
|
2018-05-11 03:58:01 +03:00
|
|
|
MOZ_ASSERT(mTableUpdate);
|
2012-08-15 11:05:32 +04:00
|
|
|
NS_ASSERTION(
|
|
|
|
mChunkState.hashSize == COMPLETE_SIZE,
|
|
|
|
"ProcessHostSubComplete should only be called for complete hashes.");
|
2012-08-15 11:04:19 +04:00
|
|
|
|
|
|
|
if (aNumEntries == 0) {
|
|
|
|
// this is totally comprehensible.
|
2012-08-15 11:05:32 +04:00
|
|
|
NS_WARNING("Expected > 0 entries for a 32-byte hash sub.");
|
2012-08-15 11:04:19 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*aStart + ((COMPLETE_SIZE + 4) * aNumEntries) > aChunk.Length()) {
|
|
|
|
NS_WARNING("Chunk is not long enough to contain the expected entries.");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint8_t i = 0; i < aNumEntries; i++) {
|
2012-08-15 11:04:19 +04:00
|
|
|
Completion hash;
|
|
|
|
hash.Assign(Substring(aChunk, *aStart, COMPLETE_SIZE));
|
|
|
|
*aStart += COMPLETE_SIZE;
|
|
|
|
|
2017-06-20 12:19:52 +03:00
|
|
|
const nsACString& addChunkStr = Substring(aChunk, *aStart, 4);
|
2012-08-15 11:04:19 +04:00
|
|
|
*aStart += 4;
|
|
|
|
|
2012-10-12 03:38:04 +04:00
|
|
|
uint32_t addChunk;
|
2012-08-15 11:04:19 +04:00
|
|
|
memcpy(&addChunk, addChunkStr.BeginReading(), 4);
|
|
|
|
addChunk = PR_ntohl(addChunk);
|
|
|
|
|
2015-02-25 16:05:17 +03:00
|
|
|
nsresult rv = mTableUpdate->NewSubComplete(addChunk, hash, mChunkState.num);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2012-08-15 11:04:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-08-12 06:55:48 +03:00
|
|
|
bool ProtocolParserV2::NextLine(nsACString& aLine) {
|
2012-10-12 03:38:04 +04:00
|
|
|
int32_t newline = mPending.FindChar('\n');
|
2012-08-15 11:04:19 +04:00
|
|
|
if (newline == kNotFound) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-11-19 05:42:43 +03:00
|
|
|
aLine.Assign(Substring(mPending, 0, newline));
|
|
|
|
mPending.Cut(0, newline + 1);
|
2012-08-15 11:04:19 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-12 06:55:48 +03:00
|
|
|
RefPtr<TableUpdate> ProtocolParserV2::CreateTableUpdate(
|
|
|
|
const nsACString& aTableName) const {
|
|
|
|
return new TableUpdateV2(aTableName);
|
2012-08-15 11:04:19 +04:00
|
|
|
}
|
|
|
|
|
2016-08-04 13:10:12 +03:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
// ProtocolParserProtobuf
|
|
|
|
|
2020-03-10 11:48:24 +03:00
|
|
|
ProtocolParserProtobuf::ProtocolParserProtobuf() = default;
|
2016-08-04 13:10:12 +03:00
|
|
|
|
2020-03-10 11:48:24 +03:00
|
|
|
ProtocolParserProtobuf::~ProtocolParserProtobuf() = default;
|
2016-08-04 13:10:12 +03:00
|
|
|
|
2016-08-12 06:55:48 +03:00
|
|
|
void ProtocolParserProtobuf::SetCurrentTable(const nsACString& aTable) {
|
|
|
|
// Should never occur.
|
|
|
|
MOZ_ASSERT_UNREACHABLE("SetCurrentTable shouldn't be called");
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<TableUpdate> ProtocolParserProtobuf::CreateTableUpdate(
|
|
|
|
const nsACString& aTableName) const {
|
|
|
|
return new TableUpdateV4(aTableName);
|
|
|
|
}
|
|
|
|
|
2016-08-04 13:10:12 +03:00
|
|
|
nsresult ProtocolParserProtobuf::AppendStream(const nsACString& aData) {
|
|
|
|
// Protobuf data cannot be parsed progressively. Just save the incoming data.
|
2019-10-07 18:02:39 +03:00
|
|
|
if (!mPending.Append(aData, mozilla::fallible)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2016-08-04 13:10:12 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtocolParserProtobuf::End() {
|
|
|
|
// mUpdateStatus will be updated to success as long as not all
|
|
|
|
// the responses are invalid.
|
|
|
|
mUpdateStatus = NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
FetchThreatListUpdatesResponse response;
|
|
|
|
if (!response.ParseFromArray(mPending.get(), mPending.Length())) {
|
|
|
|
NS_WARNING("ProtocolParserProtobuf failed parsing data.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-18 09:45:21 +03:00
|
|
|
auto minWaitDuration = response.minimum_wait_duration();
|
|
|
|
mUpdateWaitSec =
|
|
|
|
minWaitDuration.seconds() + minWaitDuration.nanos() / 1000000000;
|
|
|
|
|
2016-08-04 13:10:12 +03:00
|
|
|
for (int i = 0; i < response.list_update_responses_size(); i++) {
|
|
|
|
auto r = response.list_update_responses(i);
|
2018-04-12 20:11:30 +03:00
|
|
|
nsAutoCString listName;
|
|
|
|
nsresult rv = ProcessOneResponse(r, listName);
|
2016-08-04 13:10:12 +03:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
mUpdateStatus = rv;
|
|
|
|
} else {
|
2018-02-21 00:54:30 +03:00
|
|
|
nsAutoCString errorName;
|
|
|
|
mozilla::GetErrorName(rv, errorName);
|
2018-04-12 20:11:30 +03:00
|
|
|
NS_WARNING(nsPrintfCString("Failed to process one response for '%s': %s",
|
|
|
|
listName.get(), errorName.get())
|
|
|
|
.get());
|
|
|
|
if (!listName.IsEmpty()) {
|
|
|
|
PARSER_LOG(("Table %s will be reset.", listName.get()));
|
|
|
|
mTablesToReset.AppendElement(listName);
|
|
|
|
}
|
2016-08-04 13:10:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-12 20:11:30 +03:00
|
|
|
nsresult ProtocolParserProtobuf::ProcessOneResponse(
|
|
|
|
const ListUpdateResponse& aResponse, nsACString& aListName) {
|
|
|
|
MOZ_ASSERT(aListName.IsEmpty());
|
|
|
|
|
2016-08-04 13:10:12 +03:00
|
|
|
// A response must have a threat type.
|
|
|
|
if (!aResponse.has_threat_type()) {
|
|
|
|
NS_WARNING(
|
|
|
|
"Threat type not initialized. This seems to be an invalid response.");
|
2018-02-21 00:54:30 +03:00
|
|
|
return NS_ERROR_UC_PARSER_MISSING_PARAM;
|
2016-08-04 13:10:12 +03:00
|
|
|
}
|
|
|
|
|
2019-02-19 17:35:57 +03:00
|
|
|
nsUrlClassifierUtils* urlUtil = nsUrlClassifierUtils::GetInstance();
|
|
|
|
if (NS_WARN_IF(!urlUtil)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2016-08-04 13:10:12 +03:00
|
|
|
// Convert threat type to list name.
|
2016-08-16 12:30:11 +03:00
|
|
|
nsCString possibleListNames;
|
|
|
|
nsresult rv = urlUtil->ConvertThreatTypeToListNames(aResponse.threat_type(),
|
|
|
|
possibleListNames);
|
2016-08-04 13:10:12 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2016-12-19 23:15:14 +03:00
|
|
|
PARSER_LOG(("Threat type to list name conversion error: %d",
|
|
|
|
aResponse.threat_type()));
|
2018-02-21 00:54:30 +03:00
|
|
|
return NS_ERROR_UC_PARSER_UNKNOWN_THREAT;
|
2016-08-04 13:10:12 +03:00
|
|
|
}
|
|
|
|
|
2016-08-16 12:30:11 +03:00
|
|
|
// Match the table name we received with one of the ones we requested.
|
|
|
|
// We ignore the case where a threat type matches more than one list
|
|
|
|
// per provider and return the first one. See bug 1287059."
|
|
|
|
nsTArray<nsCString> possibleListNameArray;
|
|
|
|
Classifier::SplitTables(possibleListNames, possibleListNameArray);
|
|
|
|
for (auto possibleName : possibleListNameArray) {
|
|
|
|
if (mRequestedTables.Contains(possibleName)) {
|
2018-04-12 20:11:30 +03:00
|
|
|
aListName = possibleName;
|
2016-08-16 12:30:11 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-12 20:11:30 +03:00
|
|
|
if (aListName.IsEmpty()) {
|
2016-08-16 12:30:11 +03:00
|
|
|
PARSER_LOG(
|
|
|
|
("We received an update for a list we didn't ask for. Ignoring it."));
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2016-08-04 13:10:12 +03:00
|
|
|
// Test if this is a full update.
|
|
|
|
bool isFullUpdate = false;
|
|
|
|
if (aResponse.has_response_type()) {
|
|
|
|
isFullUpdate = aResponse.response_type() == ListUpdateResponse::FULL_UPDATE;
|
|
|
|
} else {
|
|
|
|
NS_WARNING("Response type not initialized.");
|
2018-02-21 00:54:30 +03:00
|
|
|
return NS_ERROR_UC_PARSER_MISSING_PARAM;
|
2016-08-04 13:10:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Warn if there's no new state.
|
|
|
|
if (!aResponse.has_new_client_state()) {
|
|
|
|
NS_WARNING("New state not initialized.");
|
2018-02-21 00:54:30 +03:00
|
|
|
return NS_ERROR_UC_PARSER_MISSING_PARAM;
|
2016-08-04 13:10:12 +03:00
|
|
|
}
|
|
|
|
|
2018-04-12 20:11:30 +03:00
|
|
|
auto tu = GetTableUpdate(aListName);
|
2016-08-12 06:55:48 +03:00
|
|
|
auto tuV4 = TableUpdate::Cast<TableUpdateV4>(tu);
|
|
|
|
NS_ENSURE_TRUE(tuV4, NS_ERROR_FAILURE);
|
|
|
|
|
2016-08-12 12:29:42 +03:00
|
|
|
nsCString state(aResponse.new_client_state().c_str(),
|
|
|
|
aResponse.new_client_state().size());
|
2016-10-13 10:22:08 +03:00
|
|
|
tuV4->SetNewClientState(state);
|
|
|
|
|
|
|
|
if (aResponse.has_checksum()) {
|
2019-03-07 17:40:14 +03:00
|
|
|
tuV4->SetSHA256(aResponse.checksum().sha256());
|
2016-10-13 10:22:08 +03:00
|
|
|
}
|
2016-08-12 12:29:42 +03:00
|
|
|
|
2016-08-04 13:10:12 +03:00
|
|
|
PARSER_LOG(
|
|
|
|
("==== Update for threat type '%d' ====", aResponse.threat_type()));
|
2018-04-12 20:11:30 +03:00
|
|
|
PARSER_LOG(("* aListName: %s\n", PromiseFlatCString(aListName).get()));
|
2016-08-04 13:10:12 +03:00
|
|
|
PARSER_LOG(("* newState: %s\n", aResponse.new_client_state().c_str()));
|
|
|
|
PARSER_LOG(("* isFullUpdate: %s\n", (isFullUpdate ? "yes" : "no")));
|
2016-10-13 10:22:08 +03:00
|
|
|
PARSER_LOG(
|
|
|
|
("* hasChecksum: %s\n", (aResponse.has_checksum() ? "yes" : "no")));
|
2018-02-23 04:37:53 +03:00
|
|
|
PARSER_LOG(("* additions: %d\n", aResponse.additions().size()));
|
|
|
|
PARSER_LOG(("* removals: %d\n", aResponse.removals().size()));
|
2016-09-19 06:51:01 +03:00
|
|
|
|
|
|
|
tuV4->SetFullUpdate(isFullUpdate);
|
2016-10-26 05:03:49 +03:00
|
|
|
|
|
|
|
rv = ProcessAdditionOrRemoval(*tuV4, aResponse.additions(),
|
|
|
|
true /*aIsAddition*/);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = ProcessAdditionOrRemoval(*tuV4, aResponse.removals(), false);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2016-08-04 13:10:12 +03:00
|
|
|
PARSER_LOG(("\n\n"));
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-08-12 06:55:48 +03:00
|
|
|
nsresult ProtocolParserProtobuf::ProcessAdditionOrRemoval(
|
|
|
|
TableUpdateV4& aTableUpdate, const ThreatEntrySetList& aUpdate,
|
2016-08-04 13:10:12 +03:00
|
|
|
bool aIsAddition) {
|
|
|
|
nsresult ret = NS_OK;
|
|
|
|
|
|
|
|
for (int i = 0; i < aUpdate.size(); i++) {
|
|
|
|
auto update = aUpdate.Get(i);
|
|
|
|
if (!update.has_compression_type()) {
|
|
|
|
NS_WARNING(nsPrintfCString("%s with no compression type.",
|
|
|
|
aIsAddition ? "Addition" : "Removal")
|
|
|
|
.get());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (update.compression_type()) {
|
|
|
|
case COMPRESSION_TYPE_UNSPECIFIED:
|
|
|
|
NS_WARNING("Unspecified compression type.");
|
|
|
|
break;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-08-04 13:10:12 +03:00
|
|
|
case RAW:
|
2016-08-12 06:55:48 +03:00
|
|
|
ret = (aIsAddition ? ProcessRawAddition(aTableUpdate, update)
|
|
|
|
: ProcessRawRemoval(aTableUpdate, update));
|
2016-08-04 13:10:12 +03:00
|
|
|
break;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-08-04 13:10:12 +03:00
|
|
|
case RICE:
|
2016-10-05 09:59:53 +03:00
|
|
|
ret = (aIsAddition ? ProcessEncodedAddition(aTableUpdate, update)
|
|
|
|
: ProcessEncodedRemoval(aTableUpdate, update));
|
2016-08-04 13:10:12 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-08-12 06:55:48 +03:00
|
|
|
nsresult ProtocolParserProtobuf::ProcessRawAddition(
|
|
|
|
TableUpdateV4& aTableUpdate, const ThreatEntrySet& aAddition) {
|
2016-08-04 13:10:12 +03:00
|
|
|
if (!aAddition.has_raw_hashes()) {
|
|
|
|
PARSER_LOG(("* No raw addition."));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto rawHashes = aAddition.raw_hashes();
|
|
|
|
if (!rawHashes.has_prefix_size()) {
|
|
|
|
NS_WARNING("Raw hash has no prefix size");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-04-04 03:11:30 +03:00
|
|
|
uint32_t prefixSize = rawHashes.prefix_size();
|
|
|
|
MOZ_ASSERT(prefixSize >= PREFIX_SIZE && prefixSize <= COMPLETE_SIZE);
|
2016-08-04 13:10:12 +03:00
|
|
|
|
2018-04-04 03:11:30 +03:00
|
|
|
nsCString prefixes;
|
|
|
|
if (!prefixes.Assign(rawHashes.raw_hashes().c_str(),
|
|
|
|
rawHashes.raw_hashes().size(), mozilla::fallible)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2016-08-12 06:55:48 +03:00
|
|
|
}
|
2018-04-04 03:11:30 +03:00
|
|
|
MOZ_ASSERT(prefixes.Length() % prefixSize == 0,
|
|
|
|
"PrefixString length must be a multiple of the prefix size.");
|
2016-08-12 06:55:48 +03:00
|
|
|
|
2018-04-04 03:11:30 +03:00
|
|
|
if (LOG_ENABLED()) {
|
|
|
|
PARSER_LOG((" Raw addition (%d-byte prefixes)", prefixSize));
|
|
|
|
PARSER_LOG((" - # of prefixes: %u", prefixes.Length() / prefixSize));
|
|
|
|
if (4 == prefixSize) {
|
|
|
|
uint32_t* fixedLengthPrefixes = (uint32_t*)prefixes.get();
|
|
|
|
PARSER_LOG((" - Memory address: 0x%p", fixedLengthPrefixes));
|
|
|
|
}
|
|
|
|
}
|
2016-08-12 06:55:48 +03:00
|
|
|
|
2018-04-04 03:11:30 +03:00
|
|
|
aTableUpdate.NewPrefixes(prefixSize, prefixes);
|
2016-08-04 13:10:12 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-08-12 06:55:48 +03:00
|
|
|
nsresult ProtocolParserProtobuf::ProcessRawRemoval(
|
|
|
|
TableUpdateV4& aTableUpdate, const ThreatEntrySet& aRemoval) {
|
2016-08-04 13:10:12 +03:00
|
|
|
if (!aRemoval.has_raw_indices()) {
|
|
|
|
NS_WARNING("A removal has no indices.");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// indices is an array of int32.
|
|
|
|
auto indices = aRemoval.raw_indices().indices();
|
|
|
|
PARSER_LOG(("* Raw removal"));
|
|
|
|
PARSER_LOG((" - # of removal: %d", indices.size()));
|
|
|
|
|
2017-07-20 11:20:27 +03:00
|
|
|
nsresult rv = aTableUpdate.NewRemovalIndices((const uint32_t*)indices.data(),
|
|
|
|
indices.size());
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
PARSER_LOG(("Failed to create new removal indices."));
|
|
|
|
return rv;
|
|
|
|
}
|
2016-08-12 06:55:48 +03:00
|
|
|
|
2016-08-04 13:10:12 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-10-05 09:59:53 +03:00
|
|
|
static nsresult DoRiceDeltaDecode(const RiceDeltaEncoding& aEncoding,
|
|
|
|
nsTArray<uint32_t>& aDecoded) {
|
2016-10-26 05:03:49 +03:00
|
|
|
if (aEncoding.num_entries() > 0 &&
|
|
|
|
(!aEncoding.has_rice_parameter() || !aEncoding.has_encoded_data())) {
|
|
|
|
PARSER_LOG(("Rice parameter or encoded data is missing."));
|
2018-02-21 00:54:30 +03:00
|
|
|
return NS_ERROR_UC_PARSER_MISSING_PARAM;
|
2018-10-16 13:36:01 +03:00
|
|
|
} else if (aEncoding.num_entries() == 0 && !aEncoding.has_first_value()) {
|
|
|
|
PARSER_LOG(("Missing first_value for an single-integer Rice encoding."));
|
|
|
|
return NS_ERROR_UC_PARSER_MISSING_VALUE;
|
2016-10-26 05:03:49 +03:00
|
|
|
}
|
2016-10-05 09:59:53 +03:00
|
|
|
|
2018-10-16 13:36:01 +03:00
|
|
|
auto first_value = aEncoding.has_first_value() ? aEncoding.first_value() : 0;
|
|
|
|
|
2016-10-05 09:59:53 +03:00
|
|
|
PARSER_LOG(("* Encoding info:"));
|
2018-10-16 13:36:01 +03:00
|
|
|
PARSER_LOG((" - First value: %" PRId64, first_value));
|
2016-10-05 09:59:53 +03:00
|
|
|
PARSER_LOG((" - Num of entries: %d", aEncoding.num_entries()));
|
|
|
|
PARSER_LOG((" - Rice parameter: %d", aEncoding.rice_parameter()));
|
|
|
|
|
|
|
|
// Set up the input buffer. Note that the bits should be read
|
|
|
|
// from LSB to MSB so that we in-place reverse the bits before
|
|
|
|
// feeding to the decoder.
|
|
|
|
auto encoded =
|
|
|
|
const_cast<RiceDeltaEncoding&>(aEncoding).mutable_encoded_data();
|
|
|
|
RiceDeltaDecoder decoder((uint8_t*)encoded->c_str(), encoded->size());
|
|
|
|
|
|
|
|
// Setup the output buffer. The "first value" is included in
|
|
|
|
// the output buffer.
|
2018-01-31 00:26:43 +03:00
|
|
|
if (!aDecoded.SetLength(aEncoding.num_entries() + 1, mozilla::fallible)) {
|
|
|
|
NS_WARNING("Not enough memory to decode the RiceDelta input.");
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2016-10-05 09:59:53 +03:00
|
|
|
|
|
|
|
// Decode!
|
|
|
|
bool rv = decoder.Decode(
|
|
|
|
aEncoding.rice_parameter(), first_value,
|
|
|
|
aEncoding.num_entries(), // # of entries (first value not included).
|
2016-10-26 05:03:49 +03:00
|
|
|
&aDecoded[0]);
|
2016-10-05 09:59:53 +03:00
|
|
|
|
2018-02-21 00:54:30 +03:00
|
|
|
NS_ENSURE_TRUE(rv, NS_ERROR_UC_PARSER_DECODE_FAILURE);
|
2016-10-05 09:59:53 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult ProtocolParserProtobuf::ProcessEncodedAddition(
|
|
|
|
TableUpdateV4& aTableUpdate, const ThreatEntrySet& aAddition) {
|
|
|
|
if (!aAddition.has_rice_hashes()) {
|
|
|
|
PARSER_LOG(("* No rice encoded addition."));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<uint32_t> decoded;
|
|
|
|
nsresult rv = DoRiceDeltaDecode(aAddition.rice_hashes(), decoded);
|
2016-10-26 05:03:49 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
PARSER_LOG(("Failed to parse encoded prefixes."));
|
|
|
|
return rv;
|
|
|
|
}
|
2016-10-05 09:59:53 +03:00
|
|
|
|
|
|
|
// Say we have the following raw prefixes
|
|
|
|
// BE LE
|
|
|
|
// 00 00 00 01 1 16777216
|
|
|
|
// 00 00 02 00 512 131072
|
|
|
|
// 00 03 00 00 196608 768
|
|
|
|
// 04 00 00 00 67108864 4
|
|
|
|
//
|
|
|
|
// which can be treated as uint32 (big-endian) sorted in increasing order:
|
|
|
|
//
|
|
|
|
// [1, 512, 196608, 67108864]
|
|
|
|
//
|
|
|
|
// According to https://developers.google.com/safe-browsing/v4/compression,
|
|
|
|
// the following should be done prior to compression:
|
|
|
|
//
|
|
|
|
// 1) re-interpret in little-endian ==> [16777216, 131072, 768, 4]
|
|
|
|
// 2) sort in increasing order ==> [4, 768, 131072, 16777216]
|
|
|
|
//
|
|
|
|
// In order to get the original byte stream from |decoded|
|
|
|
|
// ([4, 768, 131072, 16777216] in this case), we have to:
|
|
|
|
//
|
|
|
|
// 1) sort in big-endian order ==> [16777216, 131072, 768, 4]
|
|
|
|
// 2) copy each uint32 in little-endian to the result string
|
|
|
|
//
|
|
|
|
|
|
|
|
// The 4-byte prefixes have to be re-sorted in Big-endian increasing order.
|
|
|
|
struct CompareBigEndian {
|
|
|
|
bool Equals(const uint32_t& aA, const uint32_t& aB) const {
|
|
|
|
return aA == aB;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LessThan(const uint32_t& aA, const uint32_t& aB) const {
|
|
|
|
return NativeEndian::swapToBigEndian(aA) <
|
|
|
|
NativeEndian::swapToBigEndian(aB);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
decoded.Sort(CompareBigEndian());
|
|
|
|
|
|
|
|
// The encoded prefixes are always 4 bytes.
|
2018-04-04 03:11:30 +03:00
|
|
|
nsCString prefixes;
|
|
|
|
if (!prefixes.SetCapacity(decoded.Length() * 4, mozilla::fallible)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2016-10-05 09:59:53 +03:00
|
|
|
for (size_t i = 0; i < decoded.Length(); i++) {
|
|
|
|
// Note that the third argument is the number of elements we want
|
|
|
|
// to copy (and swap) but not the number of bytes we want to copy.
|
|
|
|
char p[4];
|
|
|
|
NativeEndian::copyAndSwapToLittleEndian(p, &decoded[i], 1);
|
2018-04-04 03:11:30 +03:00
|
|
|
prefixes.Append(p, 4);
|
2016-10-05 09:59:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
aTableUpdate.NewPrefixes(4, prefixes);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult ProtocolParserProtobuf::ProcessEncodedRemoval(
|
|
|
|
TableUpdateV4& aTableUpdate, const ThreatEntrySet& aRemoval) {
|
|
|
|
if (!aRemoval.has_rice_indices()) {
|
|
|
|
PARSER_LOG(("* No rice encoded removal."));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<uint32_t> decoded;
|
|
|
|
nsresult rv = DoRiceDeltaDecode(aRemoval.rice_indices(), decoded);
|
2016-10-26 05:03:49 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
PARSER_LOG(("Failed to decode encoded removal indices."));
|
|
|
|
return rv;
|
|
|
|
}
|
2016-10-05 09:59:53 +03:00
|
|
|
|
|
|
|
// The encoded prefixes are always 4 bytes.
|
2017-07-20 11:20:27 +03:00
|
|
|
rv = aTableUpdate.NewRemovalIndices(&decoded[0], decoded.Length());
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
PARSER_LOG(("Failed to create new removal indices."));
|
|
|
|
return rv;
|
|
|
|
}
|
2016-10-05 09:59:53 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2016-08-04 13:10:12 +03:00
|
|
|
|
2013-09-07 04:12:33 +04:00
|
|
|
} // namespace safebrowsing
|
|
|
|
} // namespace mozilla
|